Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub struct FlatSet<Key> {
- values: Vec<Key>
- }
- impl<Key> FlatSet<Key> where Key: std::cmp::PartialOrd {
- pub fn new() -> FlatSet<Key> {
- FlatSet { values: Vec::new() }
- }
- pub fn with_capacity(capacity: usize) -> FlatSet<Key> {
- FlatSet { values: Vec::with_capacity(capacity) }
- }
- pub fn capacity(&self) -> usize {
- self.values.capacity()
- }
- pub fn reserve(&mut self, additional: usize) {
- self.values.reserve(additional)
- }
- pub fn reserve_exact(&mut self, additional: usize) {
- self.values.reserve_exact(additional)
- }
- pub fn into_boxed_slice(self) -> Box<[Key]> {
- self.values.into_boxed_slice()
- }
- pub fn as_slice(&self) -> &[Key] {
- self.values.as_slice()
- }
- pub fn add(&mut self, k: Key) -> bool {
- if self.values.is_empty() {
- self.values.push(k);
- return true
- } else {
- let mut a = 0;
- let mut b = self.values.len();
- let mut idx;
- while a < b {
- idx = (a + b) / 2;
- if k < self.values[idx] {
- b = idx;
- } else if k > self.values[idx] {
- a = idx + 1;
- } else {
- return false
- }
- }
- self.values.insert(a, k);
- return true
- }
- }
- // pub fn remove(&mut self, index: usize) -> Key {
- // }
- pub fn clear(&mut self) {
- self.clear()
- }
- pub fn len(&self) -> usize {
- self.len()
- }
- pub fn is_empty(&self) -> bool {
- self.is_empty()
- }
- }
- #[cfg(test)]
- mod tests {
- #[test]
- fn it_works() {
- assert_eq!(2 + 2, 4);
- }
- }
- $ cargo build
- Compiling akarui v0.1.0 (/home/phdp/Dropbox/work/akarui)
- warning: function cannot return without recursing
- --> src/lib.rs:60:5
- |
- 60 | pub fn clear(&mut self) {
- | ^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
- 61 | self.clear()
- | ------------ recursive call site
- |
- = note: #[warn(unconditional_recursion)] on by default
- = help: a `loop` may express intention better if this is on purpose
- warning: function cannot return without recursing
- --> src/lib.rs:64:5
- |
- 64 | pub fn len(&self) -> usize {
- | ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
- 65 | self.len()
- | ---------- recursive call site
- |
- = help: a `loop` may express intention better if this is on purpose
- warning: function cannot return without recursing
- --> src/lib.rs:68:5
- |
- 68 | pub fn is_empty(&self) -> bool {
- | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
- 69 | self.is_empty()
- | --------------- recursive call site
- |
- = help: a `loop` may express intention better if this is on purpose
- Finished dev [unoptimized + debuginfo] target(s) in 0.11s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement