diff options
-rw-r--r-- | src/crates/library/src/lib.rs | 8 | ||||
-rw-r--r-- | src/crates/library/src/sorted_unique_vector.rs (renamed from src/crates/library/src/eager_unique_vector.rs) | 45 | ||||
-rw-r--r-- | src/crates/library/src/sorted_vector.rs (renamed from src/crates/library/src/eager_sorted_vector.rs) | 42 |
3 files changed, 48 insertions, 47 deletions
diff --git a/src/crates/library/src/lib.rs b/src/crates/library/src/lib.rs index 825ffad..44df8db 100644 --- a/src/crates/library/src/lib.rs +++ b/src/crates/library/src/lib.rs @@ -10,8 +10,8 @@ pub use profiler::{MappingProfilerWrapper, ProfilerWrapper}; mod adaptive; pub use adaptive::AdaptiveContainer; -mod eager_sorted_vector; -mod eager_unique_vector; +mod sorted_unique_vector; +mod sorted_vector; mod btreemap; mod hashmap; @@ -20,8 +20,8 @@ mod list; mod treeset; mod vector; -pub use eager_sorted_vector::EagerSortedVec; -pub use eager_unique_vector::EagerUniqueVec; +pub use sorted_unique_vector::SortedUniqueVec; +pub use sorted_vector::SortedVec; #[cfg(test)] pub mod proptest; diff --git a/src/crates/library/src/eager_unique_vector.rs b/src/crates/library/src/sorted_unique_vector.rs index da98421..c5a36d8 100644 --- a/src/crates/library/src/eager_unique_vector.rs +++ b/src/crates/library/src/sorted_unique_vector.rs @@ -1,5 +1,5 @@ /*LIBSPEC-NAME* -rust-eager-unique-vec-spec primrose_library::EagerUniqueVec +rust-eager-unique-vec-spec primrose_library::SortedUniqueVec *ENDLIBSPEC-NAME*/ use crate::traits::{Container, Indexable}; @@ -8,23 +8,23 @@ use std::vec::Vec; /// A Unique Vector #[derive(Debug, Clone)] -pub struct EagerUniqueVec<T> { +pub struct SortedUniqueVec<T> { v: Vec<T>, } -impl<T: PartialEq> EagerUniqueVec<T> { - pub fn from_vec(v: Vec<T>) -> EagerUniqueVec<T> { +impl<T: PartialEq + std::cmp::Ord> SortedUniqueVec<T> { + pub fn from_vec(v: Vec<T>) -> SortedUniqueVec<T> { let mut vec = Vec::<T>::new(); for i in v { if !vec.contains(&i) { vec.push(i); } } - EagerUniqueVec { v: vec } + SortedUniqueVec { v: vec } } - pub fn new() -> EagerUniqueVec<T> { - EagerUniqueVec { v: Vec::new() } + pub fn new() -> SortedUniqueVec<T> { + SortedUniqueVec { v: Vec::new() } } pub fn len(&self) -> usize { @@ -41,8 +41,9 @@ impl<T: PartialEq> EagerUniqueVec<T> { // Duplicated elements will be discarded pub fn push(&mut self, value: T) { - if !self.contains(&value) { - self.v.push(value); + // Ok indicates already in list + if let Err(idx) = self.v.binary_search(&value) { + self.v.insert(idx, value); } } @@ -74,7 +75,7 @@ impl<T: PartialEq> EagerUniqueVec<T> { /*IMPL* Container *ENDIMPL*/ -impl<T: Ord + PartialEq> Container<T> for EagerUniqueVec<T> { +impl<T: Ord + PartialEq> Container<T> for SortedUniqueVec<T> { /*LIBSPEC* /*OPNAME* len op-len pre-len post-len @@ -84,7 +85,7 @@ impl<T: Ord + PartialEq> Container<T> for EagerUniqueVec<T> { (define (post-len xs r) (equal? r (op-len xs))) *ENDLIBSPEC*/ fn len(&self) -> usize { - EagerUniqueVec::len(self) + SortedUniqueVec::len(self) } /*LIBSPEC* @@ -99,7 +100,7 @@ impl<T: Ord + PartialEq> Container<T> for EagerUniqueVec<T> { (define (post-contains xs x r) (equal? r (op-contains xs x))) *ENDLIBSPEC*/ fn contains(&self, x: &T) -> bool { - EagerUniqueVec::contains(self, x) // use fully qualified syntax to avoid function name collision + SortedUniqueVec::contains(self, x) // use fully qualified syntax to avoid function name collision } /*LIBSPEC* @@ -111,7 +112,7 @@ impl<T: Ord + PartialEq> Container<T> for EagerUniqueVec<T> { (define (post-is-empty xs r) (equal? r (op-is-empty xs))) *ENDLIBSPEC*/ fn is_empty(&self) -> bool { - EagerUniqueVec::is_empty(self) + SortedUniqueVec::is_empty(self) } /*LIBSPEC* @@ -123,7 +124,7 @@ impl<T: Ord + PartialEq> Container<T> for EagerUniqueVec<T> { (define (post-clear xs r) (equal? r (op-clear xs))) *ENDLIBSPEC*/ fn clear(&mut self) { - EagerUniqueVec::clear(self); + SortedUniqueVec::clear(self); } /*LIBSPEC* @@ -135,7 +136,7 @@ impl<T: Ord + PartialEq> Container<T> for EagerUniqueVec<T> { (define (post-insert xs x ys) (equal? ys (op-insert xs x))) *ENDLIBSPEC*/ fn insert(&mut self, elt: T) { - EagerUniqueVec::push(self, elt); + SortedUniqueVec::push(self, elt); } /*LIBSPEC* @@ -165,7 +166,7 @@ impl<T: Ord + PartialEq> Container<T> for EagerUniqueVec<T> { /*IMPL* Indexable *ENDIMPL*/ -impl<T: PartialEq> Indexable<T> for EagerUniqueVec<T> { +impl<T: PartialEq + Ord> Indexable<T> for SortedUniqueVec<T> { /*LIBSPEC* /*OPNAME* first op-first pre-first post-first @@ -178,7 +179,7 @@ impl<T: PartialEq> Indexable<T> for EagerUniqueVec<T> { (define (post-first xs r) (equal? r (op-first xs))) *ENDLIBSPEC*/ fn first(&self) -> Option<&T> { - EagerUniqueVec::first(self) + SortedUniqueVec::first(self) } /*LIBSPEC* @@ -193,7 +194,7 @@ impl<T: PartialEq> Indexable<T> for EagerUniqueVec<T> { (define (post-last xs r) (equal? r (op-last xs))) *ENDLIBSPEC*/ fn last(&self) -> Option<&T> { - EagerUniqueVec::last(self) + SortedUniqueVec::last(self) } /*LIBSPEC* @@ -213,13 +214,13 @@ impl<T: PartialEq> Indexable<T> for EagerUniqueVec<T> { } } -impl<T: Ord> Default for EagerUniqueVec<T> { +impl<T: Ord> Default for SortedUniqueVec<T> { fn default() -> Self { Self::new() } } -impl<T> IntoIterator for EagerUniqueVec<T> { +impl<T> IntoIterator for SortedUniqueVec<T> { type Item = T; type IntoIter = std::vec::IntoIter<T>; @@ -228,7 +229,7 @@ impl<T> IntoIterator for EagerUniqueVec<T> { } } -impl<E: Ord> FromIterator<E> for EagerUniqueVec<E> { +impl<E: Ord> FromIterator<E> for SortedUniqueVec<E> { fn from_iter<T: IntoIterator<Item = E>>(iter: T) -> Self { let mut v = Vec::from_iter(iter); v.sort(); @@ -244,7 +245,7 @@ mod tests { use im::conslist::ConsList; use proptest::prelude::*; - fn abstraction<T>(v: EagerUniqueVec<T>) -> ConsList<T> + fn abstraction<T>(v: SortedUniqueVec<T>) -> ConsList<T> where T: PartialEq, { diff --git a/src/crates/library/src/eager_sorted_vector.rs b/src/crates/library/src/sorted_vector.rs index 27d454b..3d4ae1f 100644 --- a/src/crates/library/src/eager_sorted_vector.rs +++ b/src/crates/library/src/sorted_vector.rs @@ -1,5 +1,5 @@ /*LIBSPEC-NAME* -rust-eager-sorted-vec-spec primrose_library::EagerSortedVec +rust-eager-sorted-vec-spec primrose_library::SortedVec *ENDLIBSPEC-NAME*/ use crate::traits::{Container, Indexable}; @@ -8,18 +8,18 @@ use std::vec::Vec; /// A Sorted Vector #[derive(Debug, Clone)] -pub struct EagerSortedVec<T> { +pub struct SortedVec<T> { v: Vec<T>, } -impl<T: Ord> EagerSortedVec<T> { - pub fn from_vec(mut v: Vec<T>) -> EagerSortedVec<T> { +impl<T: Ord> SortedVec<T> { + pub fn from_vec(mut v: Vec<T>) -> SortedVec<T> { v.sort(); - EagerSortedVec { v } + SortedVec { v } } - pub fn new() -> EagerSortedVec<T> { - EagerSortedVec { v: Vec::new() } + pub fn new() -> SortedVec<T> { + SortedVec { v: Vec::new() } } pub fn len(&self) -> usize { @@ -67,7 +67,7 @@ impl<T: Ord> EagerSortedVec<T> { /*IMPL* Container *ENDIMPL*/ -impl<T: Ord> Container<T> for EagerSortedVec<T> { +impl<T: Ord> Container<T> for SortedVec<T> { /*LIBSPEC* /*OPNAME* len op-len pre-len post-len @@ -77,7 +77,7 @@ impl<T: Ord> Container<T> for EagerSortedVec<T> { (define (post-len xs r) (equal? r (op-len xs))) *ENDLIBSPEC*/ fn len(&self) -> usize { - EagerSortedVec::len(self) + SortedVec::len(self) } /*LIBSPEC* @@ -92,7 +92,7 @@ impl<T: Ord> Container<T> for EagerSortedVec<T> { (define (post-contains xs x r) (equal? r (op-contains xs x))) *ENDLIBSPEC*/ fn contains(&self, x: &T) -> bool { - EagerSortedVec::contains(self, x) + SortedVec::contains(self, x) } /*LIBSPEC* @@ -104,7 +104,7 @@ impl<T: Ord> Container<T> for EagerSortedVec<T> { (define (post-is-empty xs r) (equal? r (op-is-empty xs))) *ENDLIBSPEC*/ fn is_empty(&self) -> bool { - EagerSortedVec::is_empty(self) + SortedVec::is_empty(self) } /*LIBSPEC* @@ -116,7 +116,7 @@ impl<T: Ord> Container<T> for EagerSortedVec<T> { (define (post-clear xs r) (equal? r (op-clear xs))) *ENDLIBSPEC*/ fn clear(&mut self) { - EagerSortedVec::clear(self); + SortedVec::clear(self); } /*LIBSPEC* @@ -128,7 +128,7 @@ impl<T: Ord> Container<T> for EagerSortedVec<T> { (define (post-insert xs x ys) (equal? ys (op-insert xs x))) *ENDLIBSPEC*/ fn insert(&mut self, elt: T) { - EagerSortedVec::push(self, elt); + SortedVec::push(self, elt); } /*LIBSPEC* @@ -158,7 +158,7 @@ impl<T: Ord> Container<T> for EagerSortedVec<T> { /*IMPL* Indexable *ENDIMPL*/ -impl<T: Ord> Indexable<T> for EagerSortedVec<T> { +impl<T: Ord> Indexable<T> for SortedVec<T> { /*LIBSPEC* /*OPNAME* first op-first pre-first post-first @@ -171,7 +171,7 @@ impl<T: Ord> Indexable<T> for EagerSortedVec<T> { (define (post-first xs r) (equal? r (op-first xs))) *ENDLIBSPEC*/ fn first(&self) -> Option<&T> { - EagerSortedVec::first(self) + SortedVec::first(self) } /*LIBSPEC* @@ -186,7 +186,7 @@ impl<T: Ord> Indexable<T> for EagerSortedVec<T> { (define (post-last xs r) (equal? r (op-last xs))) *ENDLIBSPEC*/ fn last(&self) -> Option<&T> { - EagerSortedVec::last(self) + SortedVec::last(self) } /*LIBSPEC* @@ -202,17 +202,17 @@ impl<T: Ord> Indexable<T> for EagerSortedVec<T> { (define (post-nth xs n r) (equal? r (op-nth xs n))) *ENDLIBSPEC*/ fn nth(&self, n: usize) -> Option<&T> { - EagerSortedVec::iter(self).nth(n) + SortedVec::iter(self).nth(n) } } -impl<T: Ord> Default for EagerSortedVec<T> { +impl<T: Ord> Default for SortedVec<T> { fn default() -> Self { Self::new() } } -impl<T> IntoIterator for EagerSortedVec<T> { +impl<T> IntoIterator for SortedVec<T> { type Item = T; type IntoIter = std::vec::IntoIter<T>; @@ -221,7 +221,7 @@ impl<T> IntoIterator for EagerSortedVec<T> { } } -impl<E: Ord> FromIterator<E> for EagerSortedVec<E> { +impl<E: Ord> FromIterator<E> for SortedVec<E> { fn from_iter<T: IntoIterator<Item = E>>(iter: T) -> Self { let mut v = Vec::from_iter(iter); v.sort(); @@ -237,7 +237,7 @@ mod tests { use im::conslist::ConsList; use proptest::prelude::*; - fn abstraction<T>(v: EagerSortedVec<T>) -> ConsList<T> + fn abstraction<T>(v: SortedVec<T>) -> ConsList<T> where T: Ord, { |