diff options
author | Aria Shrimpton <me@aria.rip> | 2024-02-07 15:42:03 +0000 |
---|---|---|
committer | Aria Shrimpton <me@aria.rip> | 2024-02-07 15:42:03 +0000 |
commit | 93c63b75a1095f7ad578d583ebda4d230fa9fce6 (patch) | |
tree | 855b6beeaa36b26e7db7460f427cf15b0cb6afc0 /src | |
parent | 2e726757bbfbcb7f050fbbf7382477e6a6304981 (diff) |
make indexable trait immutable
Diffstat (limited to 'src')
-rw-r--r-- | src/crates/library/src/eager_sorted_vector.rs | 12 | ||||
-rw-r--r-- | src/crates/library/src/eager_unique_vector.rs | 12 | ||||
-rw-r--r-- | src/crates/library/src/list.rs | 6 | ||||
-rw-r--r-- | src/crates/library/src/profiler.rs | 68 | ||||
-rw-r--r-- | src/crates/library/src/traits.rs | 6 | ||||
-rw-r--r-- | src/crates/library/src/treeset.rs | 6 | ||||
-rw-r--r-- | src/crates/library/src/vector.rs | 6 |
7 files changed, 61 insertions, 55 deletions
diff --git a/src/crates/library/src/eager_sorted_vector.rs b/src/crates/library/src/eager_sorted_vector.rs index 64b5711..2ea6bde 100644 --- a/src/crates/library/src/eager_sorted_vector.rs +++ b/src/crates/library/src/eager_sorted_vector.rs @@ -52,15 +52,15 @@ impl<T: Ord> EagerSortedVec<T> { self.v.clear() } - pub fn first(&mut self) -> Option<&T> { + pub fn first(&self) -> Option<&T> { self.v.first() } - pub fn last(&mut self) -> Option<&T> { + pub fn last(&self) -> Option<&T> { self.v.last() } - pub fn iter(&mut self) -> Iter<'_, T> { + pub fn iter(&self) -> Iter<'_, T> { self.v.iter() } @@ -169,7 +169,7 @@ impl<T: Ord> Indexable<T> for EagerSortedVec<T> { (define (pre-first xs) (equal? xs (sort xs <))) (define (post-first xs r) (equal? r (op-first xs))) *ENDLIBSPEC*/ - fn first(&mut self) -> Option<&T> { + fn first(&self) -> Option<&T> { EagerSortedVec::first(self) } @@ -184,7 +184,7 @@ impl<T: Ord> Indexable<T> for EagerSortedVec<T> { (define (pre-last xs) (equal? xs (sort xs <))) (define (post-last xs r) (equal? r (op-last xs))) *ENDLIBSPEC*/ - fn last(&mut self) -> Option<&T> { + fn last(&self) -> Option<&T> { EagerSortedVec::last(self) } @@ -200,7 +200,7 @@ impl<T: Ord> Indexable<T> for EagerSortedVec<T> { (define (pre-nth xs) (equal? xs (sort xs <))) (define (post-nth xs n r) (equal? r (op-nth xs n))) *ENDLIBSPEC*/ - fn nth(&mut self, n: usize) -> Option<&T> { + fn nth(&self, n: usize) -> Option<&T> { EagerSortedVec::iter(self).nth(n) } } diff --git a/src/crates/library/src/eager_unique_vector.rs b/src/crates/library/src/eager_unique_vector.rs index 8dfebbc..2e2d00d 100644 --- a/src/crates/library/src/eager_unique_vector.rs +++ b/src/crates/library/src/eager_unique_vector.rs @@ -59,15 +59,15 @@ impl<T: PartialEq> EagerUniqueVec<T> { self.v.clear() } - pub fn first(&mut self) -> Option<&T> { + pub fn first(&self) -> Option<&T> { self.v.first() } - pub fn last(&mut self) -> Option<&T> { + pub fn last(&self) -> Option<&T> { self.v.last() } - pub fn iter(&mut self) -> Iter<'_, T> { + pub fn iter(&self) -> Iter<'_, T> { self.v.iter() } @@ -176,7 +176,7 @@ impl<T: PartialEq> Indexable<T> for EagerUniqueVec<T> { (define (pre-first xs) #t) (define (post-first xs r) (equal? r (op-first xs))) *ENDLIBSPEC*/ - fn first(&mut self) -> Option<&T> { + fn first(&self) -> Option<&T> { EagerUniqueVec::first(self) } @@ -191,7 +191,7 @@ impl<T: PartialEq> Indexable<T> for EagerUniqueVec<T> { (define (pre-last xs) #t) (define (post-last xs r) (equal? r (op-last xs))) *ENDLIBSPEC*/ - fn last(&mut self) -> Option<&T> { + fn last(&self) -> Option<&T> { EagerUniqueVec::last(self) } @@ -207,7 +207,7 @@ impl<T: PartialEq> Indexable<T> for EagerUniqueVec<T> { (define (pre-nth xs) #t) (define (post-nth xs n r) (equal? r (op-nth xs n))) *ENDLIBSPEC*/ - fn nth(&mut self, n: usize) -> Option<&T> { + fn nth(&self, n: usize) -> Option<&T> { EagerUniqueVec::iter(self).nth(n) } } diff --git a/src/crates/library/src/list.rs b/src/crates/library/src/list.rs index 4ee045c..ae72b54 100644 --- a/src/crates/library/src/list.rs +++ b/src/crates/library/src/list.rs @@ -149,7 +149,7 @@ impl<T> Indexable<T> for LinkedList<T> { (define (pre-first xs) #t) (define (post-first xs r) (equal? r (op-first xs))) *ENDLIBSPEC*/ - fn first(&mut self) -> Option<&T> { + fn first(&self) -> Option<&T> { LinkedList::front(self) } @@ -164,7 +164,7 @@ impl<T> Indexable<T> for LinkedList<T> { (define (pre-last xs) #t) (define (post-last xs r) (equal? r (op-last xs))) *ENDLIBSPEC*/ - fn last(&mut self) -> Option<&T> { + fn last(&self) -> Option<&T> { LinkedList::back(self) } @@ -180,7 +180,7 @@ impl<T> Indexable<T> for LinkedList<T> { (define (pre-nth xs) #t) (define (post-nth xs n r) (equal? r (op-nth xs n))) *ENDLIBSPEC*/ - fn nth(&mut self, n: usize) -> Option<&T> { + fn nth(&self, n: usize) -> Option<&T> { LinkedList::iter(self).nth(n) } } diff --git a/src/crates/library/src/profiler.rs b/src/crates/library/src/profiler.rs index 6a72916..2f1af37 100644 --- a/src/crates/library/src/profiler.rs +++ b/src/crates/library/src/profiler.rs @@ -1,4 +1,5 @@ use std::{ + cell::Cell, env::var, fs::{create_dir, metadata, File}, io::Write, @@ -10,14 +11,14 @@ use crate::traits::{Container, Indexable, Mapping, Stack}; pub struct ProfilerWrapper<const ID: usize, T, E> { inner: Option<T>, - max_n: usize, + max_n: Cell<usize>, n_contains: usize, n_insert: usize, n_clear: usize, n_remove: usize, - n_first: usize, - n_last: usize, - n_nth: usize, + n_first: Cell<usize>, + n_last: Cell<usize>, + n_nth: Cell<usize>, n_push: usize, n_pop: usize, n_get: usize, @@ -28,14 +29,14 @@ impl<const ID: usize, T: Default, E> Default for ProfilerWrapper<ID, T, E> { fn default() -> Self { Self { inner: Some(T::default()), - max_n: 0, + max_n: Cell::new(0), n_contains: 0, n_insert: 0, n_clear: 0, n_remove: 0, - n_first: 0, - n_last: 0, - n_nth: 0, + n_first: Cell::new(0), + n_last: Cell::new(0), + n_nth: Cell::new(0), n_push: 0, n_pop: 0, n_get: 0, @@ -45,14 +46,16 @@ impl<const ID: usize, T: Default, E> Default for ProfilerWrapper<ID, T, E> { } impl<const ID: usize, T: Container<E>, E> ProfilerWrapper<ID, T, E> { - fn add_n(&mut self) { - self.max_n = self.max_n.max(self.inner.as_ref().unwrap().len()); + fn add_n(&self) { + self.max_n + .set(self.max_n.get().max(self.inner.as_ref().unwrap().len())); } } impl<const ID: usize, T: Mapping<K, V>, K, V> ProfilerWrapper<ID, T, (K, V)> { - fn add_n_map(&mut self) { - self.max_n = self.max_n.max(self.inner.as_ref().unwrap().len()); + fn add_n_map(&self) { + self.max_n + .set(self.max_n.get().max(self.inner.as_ref().unwrap().len())); } } @@ -93,22 +96,22 @@ impl<const ID: usize, T: Container<E>, E> Container<E> for ProfilerWrapper<ID, T impl<const ID: usize, T: Indexable<E> + Container<E>, E> Indexable<E> for ProfilerWrapper<ID, T, E> { - fn first(&mut self) -> Option<&E> { + fn first(&self) -> Option<&E> { self.add_n(); - self.n_first += 1; - self.inner.as_mut().unwrap().first() + self.n_first.set(self.n_first.get() + 1); + self.inner.as_ref().unwrap().first() } - fn last(&mut self) -> Option<&E> { + fn last(&self) -> Option<&E> { self.add_n(); - self.n_last += 1; - self.inner.as_mut().unwrap().last() + self.n_last.set(self.n_last.get() + 1); + self.inner.as_ref().unwrap().last() } - fn nth(&mut self, n: usize) -> Option<&E> { + fn nth(&self, n: usize) -> Option<&E> { self.add_n(); - self.n_nth += 1; - self.inner.as_mut().unwrap().nth(n) + self.n_nth.set(self.n_nth.get() + 1); + self.inner.as_ref().unwrap().nth(n) } } @@ -175,14 +178,14 @@ impl<const ID: usize, T, E> Drop for ProfilerWrapper<ID, T, E> { } let mut f = File::create(format!("{}/{}", dir, unix_time.as_nanos())).unwrap(); - writeln!(f, "{}", self.max_n).unwrap(); + writeln!(f, "{}", self.max_n.get()).unwrap(); writeln!(f, "{}", self.n_contains).unwrap(); writeln!(f, "{}", self.n_insert).unwrap(); writeln!(f, "{}", self.n_clear).unwrap(); writeln!(f, "{}", self.n_remove).unwrap(); - writeln!(f, "{}", self.n_first).unwrap(); - writeln!(f, "{}", self.n_last).unwrap(); - writeln!(f, "{}", self.n_nth).unwrap(); + writeln!(f, "{}", self.n_first.get()).unwrap(); + writeln!(f, "{}", self.n_last.get()).unwrap(); + writeln!(f, "{}", self.n_nth.get()).unwrap(); writeln!(f, "{}", self.n_push).unwrap(); writeln!(f, "{}", self.n_pop).unwrap(); writeln!(f, "{}", self.n_get).unwrap(); @@ -199,18 +202,21 @@ impl<const ID: usize, T: IntoIterator, E> IntoIterator for ProfilerWrapper<ID, T } } -impl<const ID: usize, C: FromIterator<E>, E> FromIterator<E> for ProfilerWrapper<ID, C, E> { +impl<const ID: usize, C: FromIterator<E> + Container<E>, E> FromIterator<E> + for ProfilerWrapper<ID, C, E> +{ fn from_iter<T: IntoIterator<Item = E>>(iter: T) -> Self { + let c = C::from_iter(iter); Self { - inner: Some(C::from_iter(iter)), - max_n: 0, + max_n: Cell::new(c.len()), + inner: Some(c), n_contains: 0, n_insert: 0, n_clear: 0, n_remove: 0, - n_first: 0, - n_last: 0, - n_nth: 0, + n_first: Cell::new(0), + n_last: Cell::new(0), + n_nth: Cell::new(0), n_push: 0, n_pop: 0, n_get: 0, diff --git a/src/crates/library/src/traits.rs b/src/crates/library/src/traits.rs index e08ef91..7d75f13 100644 --- a/src/crates/library/src/traits.rs +++ b/src/crates/library/src/traits.rs @@ -26,9 +26,9 @@ pub trait Stack<T> { } pub trait Indexable<T> { - fn first(&mut self) -> Option<&T>; - fn last(&mut self) -> Option<&T>; - fn nth(&mut self, n: usize) -> Option<&T>; + fn first(&self) -> Option<&T>; + fn last(&self) -> Option<&T>; + fn nth(&self, n: usize) -> Option<&T>; } pub trait Mapping<K, V>: Default + IntoIterator<Item = (K, V)> { diff --git a/src/crates/library/src/treeset.rs b/src/crates/library/src/treeset.rs index f4f6923..8845d33 100644 --- a/src/crates/library/src/treeset.rs +++ b/src/crates/library/src/treeset.rs @@ -106,7 +106,7 @@ impl<T: Ord> Indexable<T> for BTreeSet<T> { (define (pre-first xs) (equal? xs (remove-duplicates (sort xs <)))) (define (post-first xs r) (equal? r (op-first xs))) *ENDLIBSPEC*/ - fn first(&mut self) -> Option<&T> { + fn first(&self) -> Option<&T> { BTreeSet::first(self) } @@ -121,7 +121,7 @@ impl<T: Ord> Indexable<T> for BTreeSet<T> { (define (pre-last xs) (equal? xs (remove-duplicates (sort xs <)))) (define (post-last xs r) (equal? r (op-last xs))) *ENDLIBSPEC*/ - fn last(&mut self) -> Option<&T> { + fn last(&self) -> Option<&T> { BTreeSet::last(self) } @@ -137,7 +137,7 @@ impl<T: Ord> Indexable<T> for BTreeSet<T> { (define (pre-nth xs) (equal? xs (remove-duplicates (sort xs <)))) (define (post-nth n xs r) (equal? r (op-nth xs n))) *ENDLIBSPEC*/ - fn nth(&mut self, n: usize) -> Option<&T> { + fn nth(&self, n: usize) -> Option<&T> { BTreeSet::iter(self).nth(n) } } diff --git a/src/crates/library/src/vector.rs b/src/crates/library/src/vector.rs index 496f4f0..f508f65 100644 --- a/src/crates/library/src/vector.rs +++ b/src/crates/library/src/vector.rs @@ -137,7 +137,7 @@ impl<T> Indexable<T> for Vec<T> { (define (pre-first xs) #t) (define (post-first xs r) (equal? r (op-first xs))) *ENDLIBSPEC*/ - fn first(&mut self) -> Option<&T> { + fn first(&self) -> Option<&T> { <[T]>::first(self) } @@ -152,7 +152,7 @@ impl<T> Indexable<T> for Vec<T> { (define (pre-last xs) #t) (define (post-last xs r) (equal? r (op-last xs))) *ENDLIBSPEC*/ - fn last(&mut self) -> Option<&T> { + fn last(&self) -> Option<&T> { <[T]>::last(self) } @@ -168,7 +168,7 @@ impl<T> Indexable<T> for Vec<T> { (define (pre-nth xs) #t) (define (post-nth xs n r) (equal? r (op-nth xs n))) *ENDLIBSPEC*/ - fn nth(&mut self, n: usize) -> Option<&T> { + fn nth(&self, n: usize) -> Option<&T> { <[T]>::iter(self).nth(n) } } |