#[macro_export]
macro_rules! new {
( @internal $t:ident, $(#[$cmt:meta])* btree set: $set:ident $($tail:tt)* ) => (
$(#[$cmt])*
pub type $set = std::collections::BTreeSet<$t> ;
$crate::new!{ @internal $t $($tail)* }
) ;
( @internal $t:ident, $(#[$cmt:meta])* btree map: $map:ident $($tail:tt)* ) => (
$(#[$cmt])*
pub type $map<T> = std::collections::BTreeMap<$t, T> ;
$crate::new!{ @internal $t $($tail)* }
) ;
( @internal $t:ident, $(#[$cmt:meta])* range: $range:ident $($tail:tt)* ) => (
impl $t {
pub fn up_to(self, end : $t) -> $range {
$range { start: self, end }
}
}
$(#[$cmt])*
#[derive(Debug)]
pub struct $range {
start: $t,
end: $t,
}
impl $range {
pub fn new<
T1: std::convert::Into<$t>,
T2: std::convert::Into<$t>,
>(start: T1, end: T2) -> Self {
$range { start: start.into(), end: end.into() }
}
pub fn zero_to<T: std::convert::Into<$t>>(end: T) -> Self {
$range { start: 0.into(), end: end.into() }
}
}
impl std::iter::Iterator for $range {
type Item = $t ;
fn next(&mut self) -> Option<$t> {
if self.start >= self.end { None } else {
let res = Some(self.start) ;
self.start.val += 1 ;
res
}
}
}
$crate::new!{ @internal $t $($tail)* }
) ;
(
@internal $t:ident, $(#[$cmt:meta])*
map: $map:ident with iter: $iter:ident
$($tail:tt)*
) => (
$(#[$cmt])*
#[derive(Debug, PartialOrd, Ord)]
pub struct $map<T> {
vec: Vec<T>
}
impl<T> Default for $map<T> {
fn default() -> Self { Self::new() }
}
impl<T: Clone> Clone for $map<T> {
fn clone(& self) -> Self {
$map { vec: self.vec.clone() }
}
}
impl<T> $map<T> {
#[inline]
pub const fn of(vec: Vec<T>) -> Self {
$map { vec: vec }
}
#[inline]
pub const fn new() -> Self {
$map { vec: Vec::new() }
}
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
$map { vec: Vec::with_capacity(capacity) }
}
#[inline]
pub fn reserve(&mut self, capa: usize) {
self.vec.reserve(capa)
}
#[inline]
pub fn get(&self, uid: $t) -> Option<&T> {
self.vec.get(uid.get())
}
#[inline]
pub fn get_mut(&mut self, uid: $t) -> Option<&mut T> {
self.vec.get_mut(uid.get())
}
#[inline]
pub fn last(&self) -> Option<&T> {
self.vec.last()
}
#[inline]
pub fn last_mut(&self) -> Option<&T> {
self.vec.last()
}
#[inline]
pub fn len(& self) -> usize {
self.vec.len()
}
#[inline]
pub fn capacity(& self) -> usize {
self.vec.capacity()
}
#[inline]
pub fn next_index(& self) -> $t {
self.len().into()
}
#[inline]
pub fn last_index(& self) -> Option<$t> {
let len = self.len();
if len > 0 { Some((len - 1).into()) } else { None }
}
#[inline]
pub fn push(&mut self, elem: T) -> $t {
let idx = self.next_index();
self.vec.push(elem);
idx
}
#[inline]
pub fn pop(&mut self) -> Option<T> {
self.vec.pop()
}
#[inline]
pub fn clear(&mut self) {
self.vec.clear()
}
#[inline]
pub fn iter(& self) -> std::slice::Iter<T> {
self.vec.iter()
}
#[inline]
pub fn index_iter<'a>(&'a self) -> $iter<&'a $map<T>>
where T: 'a {
$iter::mk_ref(self)
}
#[inline]
pub fn index_iter_mut<'a>(&'a mut self) -> $iter<
std::slice::IterMut<'a, T>
>
where T: 'a {
$iter::mk_ref_mut(self)
}
#[inline]
pub fn into_index_iter(self) -> $iter<$map<T>> {
$iter::new(self)
}
#[inline]
pub fn iter_mut(&mut self) -> std::slice::IterMut<T> {
self.vec.iter_mut()
}
#[inline]
pub fn shrink_to_fit(&mut self) {
self.vec.shrink_to_fit()
}
#[inline]
pub fn swap(&mut self, a: $t, b: $t) {
self.vec.swap(* a, *b)
}
#[inline]
pub fn swap_remove(&mut self, idx: $t) -> T {
self.vec.swap_remove(* idx)
}
#[inline]
pub fn split(&self, idx: $t) -> (impl Iterator<Item = &T>, &T, impl Iterator<Item = &T>) {
let before = self.vec[0..idx.val].iter();
let after = if idx.val < self.vec.len() {
self.vec[idx.val + 1 ..].iter()
} else {
self.vec[0..0].iter()
};
(before, &self.vec[idx.val], after)
}
}
impl<T: Clone> $map<T> {
#[inline]
pub fn of_elems(elem: T, size: usize) -> Self {
$map { vec: vec![ elem ; size ] }
}
}
impl<T: PartialEq> PartialEq for $map<T> {
fn eq(& self, other: & Self) -> bool {
self.vec.eq( & other.vec )
}
}
impl<T: Eq> Eq for $map<T> {}
impl<T> std::convert::From< Vec<T> > for $map<T> {
fn from(vec: Vec<T>) -> Self {
$map { vec }
}
}
impl<T> std::iter::IntoIterator for $map<T> {
type Item = T ;
type IntoIter = std::vec::IntoIter<T> ;
fn into_iter(self) -> std::vec::IntoIter<T> {
self.vec.into_iter()
}
}
impl<'a, T> std::iter::IntoIterator for &'a $map<T> {
type Item = &'a T ;
type IntoIter = std::slice::Iter<'a, T> ;
fn into_iter(self) -> std::slice::Iter<'a, T> {
self.iter()
}
}
impl<'a, T> std::iter::IntoIterator for &'a mut $map<T> {
type Item = &'a mut T ;
type IntoIter = std::slice::IterMut<'a, T> ;
fn into_iter(self) -> std::slice::IterMut<'a, T> {
self.iter_mut()
}
}
impl<T> std::iter::FromIterator<T> for $map<T> {
fn from_iter<
I: std::iter::IntoIterator<Item = T>
>(iter: I) -> Self {
$map { vec: iter.into_iter().collect() }
}
}
impl<T> std::ops::Index<$t> for $map<T> {
type Output = T ;
fn index(& self, index: $t) -> & T {
& self.vec[ index.get() ]
}
}
impl<T> std::ops::Index<std::ops::RangeFrom<$t>> for $map<T> {
type Output = [T];
fn index(& self, std::ops::RangeFrom { start }: std::ops::RangeFrom<$t>) -> &[T] {
& self.vec[ start.get() .. ]
}
}
impl<T> std::ops::Index<std::ops::Range<$t>> for $map<T> {
type Output = [T];
fn index(& self, std::ops::Range { start, end }: std::ops::Range<$t>) -> &[T] {
& self.vec[ start.get() .. end.get() ]
}
}
impl<T> std::ops::Index<std::ops::RangeInclusive<$t>> for $map<T> {
type Output = [T];
fn index(& self, range: std::ops::RangeInclusive<$t>) -> &[T] {
& self.vec[ range.start().get() ..= range.end().get() ]
}
}
impl<T> std::ops::Index<std::ops::RangeFull> for $map<T> {
type Output = [T];
fn index(& self, _: std::ops::RangeFull) -> &[T] {
& self.vec[..]
}
}
impl<T> std::ops::Index<std::ops::RangeTo<$t>> for $map<T> {
type Output = [T];
fn index(& self, std::ops::RangeTo { end }: std::ops::RangeTo<$t>) -> &[T] {
& self.vec[..end.get()]
}
}
impl<T> std::ops::Index<std::ops::RangeToInclusive<$t>> for $map<T> {
type Output = [T];
fn index(
& self,
std::ops::RangeToInclusive { end }: std::ops::RangeToInclusive<$t>
) -> &[T] {
& self.vec[..=end.get()]
}
}
impl<T> std::ops::IndexMut<$t> for $map<T> {
fn index_mut(&mut self, index: $t) -> &mut T {
&mut self.vec[ index.get() ]
}
}
impl<T> std::ops::Index<
std::ops::Range<usize>
> for $map<T> {
type Output = [T] ;
fn index(& self, index: std::ops::Range<usize>) -> & [T] {
self.vec.index(index)
}
}
impl<T> std::ops::Index<
std::ops::RangeInclusive<usize>
> for $map<T> {
type Output = [T] ;
fn index(& self, index: std::ops::RangeInclusive<usize>) -> & [T] {
self.vec.index(index)
}
}
impl<T> std::ops::Index<
std::ops::RangeFrom<usize>
> for $map<T> {
type Output = [T] ;
fn index(& self, index: std::ops::RangeFrom<usize>) -> & [T] {
self.vec.index(index)
}
}
impl<T> std::ops::Index<
std::ops::RangeTo<usize>
> for $map<T> {
type Output = [T] ;
fn index(& self, index: std::ops::RangeTo<usize>) -> & [T] {
self.vec.index(index)
}
}
impl<T> std::ops::Deref for $map<T> {
type Target = Vec<T> ;
fn deref(& self) -> & Vec<T> {
& self.vec
}
}
#[derive(Clone)]
pub struct $iter<T> {
cursor: $t,
map: T,
}
impl<'a, T> $iter<&'a $map<T>> {
const fn mk_ref(map: &'a $map<T>) -> Self {
$iter { cursor: $t::zero(), map: map }
}
}
impl<'a, T: 'a> std::iter::Iterator for $iter<&'a $map<T>> {
type Item = ($t, &'a T) ;
fn next(&mut self) -> Option< ($t, &'a T) > {
if self.cursor >= self.map.len() {
None
} else {
let res = (self.cursor, & self.map[self.cursor]) ;
self.cursor.inc() ;
Some(res)
}
}
}
impl<'a, T: 'a> $iter<std::slice::IterMut<'a, T>> {
fn mk_ref_mut(map: &'a mut $map<T>) -> Self {
$iter { cursor: $t::zero(), map: map.vec.iter_mut() }
}
}
impl<'a, T: 'a> std::iter::Iterator for $iter<
std::slice::IterMut<'a, T>
> {
type Item = ($t, &'a mut T) ;
fn next(&mut self) -> Option< ($t, &'a mut T) > {
self.map.next().map(
|res| {
let index = self.cursor ;
self.cursor.inc() ;
(index, res)
}
)
}
}
impl<T> $iter<$map<T>> {
fn new(mut map: $map<T>) -> Self {
map.vec.reverse() ;
$iter { cursor: $t::zero(), map: map }
}
}
impl<T> std::iter::Iterator for $iter<$map<T>> {
type Item = ($t, T) ;
fn next(&mut self) -> Option< ($t, T) > {
if let Some(elem) = self.map.pop() {
let res = (self.cursor, elem) ;
self.cursor.inc() ;
Some(res)
} else {
None
}
}
}
$crate::new!{ @internal $t $($tail)* }
) ;
( @internal $t:ident $(,)? ) => () ;
(
$(#[$cmt:meta])* $t:ident
$($tail:tt)*
) => (
$(#[$cmt])*
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct $t {
val: usize
}
impl $t {
#[inline]
pub const fn new(val: usize) -> Self {
$t { val: val }
}
#[inline]
pub const fn zero() -> Self {
$t { val: 0 }
}
#[inline]
pub const fn one() -> Self {
$t { val: 1 }
}
#[inline]
pub const fn get(& self) -> usize {
self.val
}
#[inline]
pub fn inc(&mut self) {
self.val += 1
}
#[inline]
pub fn dec(&mut self) {
self.val -= 1
}
}
impl std::convert::From<usize> for $t {
#[inline]
fn from(val: usize) -> Self {
$t::new(val)
}
}
impl<'a> std::convert::From<&'a usize> for $t {
#[inline]
fn from(val: &'a usize) -> Self {
$t::new(* val)
}
}
impl std::convert::Into<usize> for $t {
#[inline]
fn into(self) -> usize {
self.val
}
}
impl<'a> std::convert::Into<usize> for &'a $t {
#[inline]
fn into(self) -> usize {
self.val
}
}
impl<T: std::convert::Into<usize>> std::ops::AddAssign<T> for $t {
#[inline]
fn add_assign(&mut self, rhs: T) {
self.val += rhs.into()
}
}
impl<T: std::convert::Into<usize>> std::ops::Add<T> for $t {
type Output = $t ;
#[inline]
fn add(mut self, rhs: T) -> $t {
self.val += rhs.into() ;
self
}
}
impl Default for $t {
#[inline]
fn default() -> Self {
Self::zero()
}
}
impl std::ops::Deref for $t {
type Target = usize ;
#[inline]
fn deref(& self) -> & usize {
& self.val
}
}
impl std::fmt::Display for $t {
#[inline]
fn fmt(& self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "{}", self.val)
}
}
impl std::cmp::PartialEq<usize> for $t {
#[inline]
fn eq(& self, int: & usize) -> bool {
self.val.eq(int)
}
}
impl std::cmp::PartialOrd<usize> for $t {
#[inline]
fn partial_cmp(& self, int: & usize) -> Option<
std::cmp::Ordering
> {
self.val.partial_cmp(int)
}
}
$crate::new!{ @internal $t $($tail)* }
) ;
}
pub mod examples;