1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std::collections::range::RangeArgument;
13 use std::iter::{self, FromIterator};
15 use std::marker::PhantomData;
16 use std::ops::{Index, IndexMut, Range};
21 use rustc_serialize as serialize;
23 /// Represents some newtyped `usize` wrapper.
25 /// (purpose: avoid mixing indexes for different bitvector domains.)
26 pub trait Idx: Copy + 'static + Eq + Debug {
27 fn new(idx: usize) -> Self;
28 fn index(self) -> usize;
32 fn new(idx: usize) -> Self { idx }
33 fn index(self) -> usize { self }
37 fn new(idx: usize) -> Self { assert!(idx <= u32::MAX as usize); idx as u32 }
38 fn index(self) -> usize { self as usize }
42 macro_rules! newtype_index {
43 // ---- public rules ----
45 // Use default constants
50 @debug_name[unsafe {::std::intrinsics::type_name::<$name>() }]);
53 // Define any constants
54 ($name:ident { $($tokens:tt)+ }) => (
58 @debug_name[unsafe {::std::intrinsics::type_name::<$name>() }]
62 // ---- private rules ----
64 // Base case, user-defined constants (if any) have already been defined
65 (@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr]) => (
66 #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
67 RustcEncodable, RustcDecodable)]
68 pub struct $type(pub u32);
71 fn new(value: usize) -> Self {
72 assert!(value < ($max) as usize);
75 fn index(self) -> usize {
80 impl ::std::fmt::Debug for $type {
81 fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
82 write!(fmt, "{}{}", $debug_name, self.0)
87 // Rewrite final without comma to one that includes comma
88 (@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr]
89 $name:ident = $constant:expr) => (
90 newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $name = $constant,);
93 // Rewrite final const without comma to one that includes comma
94 (@type[$type:ident] @max[$_max:expr] @debug_name[$debug_name:expr]
95 const $name:ident = $constant:expr) => (
96 newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] const $name = $constant,);
99 // Replace existing default for max
100 (@type[$type:ident] @max[$_max:expr] @debug_name[$debug_name:expr]
101 MAX = $max:expr, $($tokens:tt)*) => (
102 newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $($tokens)*);
105 // Replace existing default for debug_name
106 (@type[$type:ident] @max[$max:expr] @debug_name[$_debug_name:expr]
107 DEBUG_NAME = $debug_name:expr, $($tokens:tt)*) => (
108 newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $($tokens)*);
111 // Assign a user-defined constant (as final param)
112 (@type[$type:ident] @max[$max:expr] @debug_name[$debug_name:expr]
113 const $name:ident = $constant:expr, $($tokens:tt)*) => (
114 pub const $name: $type = $type($constant);
115 newtype_index!(@type[$type] @max[$max] @debug_name[$debug_name] $($tokens)*);
119 #[derive(Clone, PartialEq, Eq)]
120 pub struct IndexVec<I: Idx, T> {
122 _marker: PhantomData<Fn(&I)>
125 impl<I: Idx, T: serialize::Encodable> serialize::Encodable for IndexVec<I, T> {
126 fn encode<S: serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
127 serialize::Encodable::encode(&self.raw, s)
131 impl<I: Idx, T: serialize::Decodable> serialize::Decodable for IndexVec<I, T> {
132 fn decode<D: serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
133 serialize::Decodable::decode(d).map(|v| {
134 IndexVec { raw: v, _marker: PhantomData }
139 impl<I: Idx, T: fmt::Debug> fmt::Debug for IndexVec<I, T> {
140 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
141 fmt::Debug::fmt(&self.raw, fmt)
145 pub type Enumerated<I, J> = iter::Map<iter::Enumerate<J>, IntoIdx<I>>;
147 impl<I: Idx, T> IndexVec<I, T> {
149 pub fn new() -> Self {
150 IndexVec { raw: Vec::new(), _marker: PhantomData }
154 pub fn with_capacity(capacity: usize) -> Self {
155 IndexVec { raw: Vec::with_capacity(capacity), _marker: PhantomData }
159 pub fn from_elem<S>(elem: T, universe: &IndexVec<I, S>) -> Self
162 IndexVec { raw: vec![elem; universe.len()], _marker: PhantomData }
166 pub fn from_elem_n(elem: T, n: usize) -> Self
169 IndexVec { raw: vec![elem; n], _marker: PhantomData }
173 pub fn push(&mut self, d: T) -> I {
174 let idx = I::new(self.len());
180 pub fn len(&self) -> usize {
185 pub fn is_empty(&self) -> bool {
190 pub fn into_iter(self) -> vec::IntoIter<T> {
195 pub fn into_iter_enumerated(self) -> Enumerated<I, vec::IntoIter<T>>
197 self.raw.into_iter().enumerate().map(IntoIdx { _marker: PhantomData })
201 pub fn iter(&self) -> slice::Iter<T> {
206 pub fn iter_enumerated(&self) -> Enumerated<I, slice::Iter<T>>
208 self.raw.iter().enumerate().map(IntoIdx { _marker: PhantomData })
212 pub fn indices(&self) -> iter::Map<Range<usize>, IntoIdx<I>> {
213 (0..self.len()).map(IntoIdx { _marker: PhantomData })
217 pub fn iter_mut(&mut self) -> slice::IterMut<T> {
222 pub fn iter_enumerated_mut(&mut self) -> Enumerated<I, slice::IterMut<T>>
224 self.raw.iter_mut().enumerate().map(IntoIdx { _marker: PhantomData })
228 pub fn drain<'a, R: RangeArgument<usize>>(
229 &'a mut self, range: R) -> impl Iterator<Item=T> + 'a {
230 self.raw.drain(range)
234 pub fn drain_enumerated<'a, R: RangeArgument<usize>>(
235 &'a mut self, range: R) -> impl Iterator<Item=(I, T)> + 'a {
236 self.raw.drain(range).enumerate().map(IntoIdx { _marker: PhantomData })
240 pub fn last(&self) -> Option<I> {
241 self.len().checked_sub(1).map(I::new)
245 pub fn shrink_to_fit(&mut self) {
246 self.raw.shrink_to_fit()
250 pub fn swap(&mut self, a: usize, b: usize) {
255 pub fn truncate(&mut self, a: usize) {
260 pub fn get(&self, index: I) -> Option<&T> {
261 self.raw.get(index.index())
265 pub fn get_mut(&mut self, index: I) -> Option<&mut T> {
266 self.raw.get_mut(index.index())
270 impl<I: Idx, T: Clone> IndexVec<I, T> {
272 pub fn resize(&mut self, new_len: usize, value: T) {
273 self.raw.resize(new_len, value)
277 impl<I: Idx, T: Ord> IndexVec<I, T> {
279 pub fn binary_search(&self, value: &T) -> Result<I, I> {
280 match self.raw.binary_search(value) {
281 Ok(i) => Ok(Idx::new(i)),
282 Err(i) => Err(Idx::new(i)),
287 impl<I: Idx, T> Index<I> for IndexVec<I, T> {
291 fn index(&self, index: I) -> &T {
292 &self.raw[index.index()]
296 impl<I: Idx, T> IndexMut<I> for IndexVec<I, T> {
298 fn index_mut(&mut self, index: I) -> &mut T {
299 &mut self.raw[index.index()]
303 impl<I: Idx, T> Default for IndexVec<I, T> {
305 fn default() -> Self {
310 impl<I: Idx, T> Extend<T> for IndexVec<I, T> {
312 fn extend<J: IntoIterator<Item = T>>(&mut self, iter: J) {
313 self.raw.extend(iter);
317 impl<I: Idx, T> FromIterator<T> for IndexVec<I, T> {
319 fn from_iter<J>(iter: J) -> Self where J: IntoIterator<Item=T> {
320 IndexVec { raw: FromIterator::from_iter(iter), _marker: PhantomData }
324 impl<I: Idx, T> IntoIterator for IndexVec<I, T> {
326 type IntoIter = vec::IntoIter<T>;
329 fn into_iter(self) -> vec::IntoIter<T> {
335 impl<'a, I: Idx, T> IntoIterator for &'a IndexVec<I, T> {
337 type IntoIter = slice::Iter<'a, T>;
340 fn into_iter(self) -> slice::Iter<'a, T> {
345 impl<'a, I: Idx, T> IntoIterator for &'a mut IndexVec<I, T> {
346 type Item = &'a mut T;
347 type IntoIter = slice::IterMut<'a, T>;
350 fn into_iter(self) -> slice::IterMut<'a, T> {
355 pub struct IntoIdx<I: Idx> { _marker: PhantomData<fn(&I)> }
356 impl<I: Idx, T> FnOnce<((usize, T),)> for IntoIdx<I> {
357 type Output = (I, T);
359 extern "rust-call" fn call_once(self, ((n, t),): ((usize, T),)) -> Self::Output {
364 impl<I: Idx, T> FnMut<((usize, T),)> for IntoIdx<I> {
365 extern "rust-call" fn call_mut(&mut self, ((n, t),): ((usize, T),)) -> Self::Output {
370 impl<I: Idx> FnOnce<(usize,)> for IntoIdx<I> {
373 extern "rust-call" fn call_once(self, (n,): (usize,)) -> Self::Output {
378 impl<I: Idx> FnMut<(usize,)> for IntoIdx<I> {
379 extern "rust-call" fn call_mut(&mut self, (n,): (usize,)) -> Self::Output {