Advertisement
Guest User

Untitled

a guest
Mar 17th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.98 KB | None | 0 0
  1. pub struct FlatSet<Key> {
  2.     values: Vec<Key>
  3. }
  4.  
  5. impl<Key> FlatSet<Key> where Key: std::cmp::PartialOrd {
  6.     pub fn new() -> FlatSet<Key> {
  7.         FlatSet { values: Vec::new() }
  8.     }
  9.  
  10.     pub fn with_capacity(capacity: usize) -> FlatSet<Key> {
  11.         FlatSet { values: Vec::with_capacity(capacity) }
  12.     }
  13.  
  14.     pub fn capacity(&self) -> usize {
  15.         self.values.capacity()
  16.     }
  17.  
  18.     pub fn reserve(&mut self, additional: usize) {
  19.         self.values.reserve(additional)
  20.     }
  21.  
  22.     pub fn reserve_exact(&mut self, additional: usize) {
  23.         self.values.reserve_exact(additional)
  24.     }
  25.  
  26.     pub fn into_boxed_slice(self) -> Box<[Key]> {
  27.         self.values.into_boxed_slice()
  28.     }
  29.  
  30.     pub fn as_slice(&self) -> &[Key] {
  31.         self.values.as_slice()
  32.     }
  33.  
  34.     pub fn add(&mut self, k: Key) -> bool {
  35.         if self.values.is_empty() {
  36.             self.values.push(k);
  37.             return true
  38.         } else {
  39.             let mut a = 0;
  40.             let mut b = self.values.len();
  41.             let mut idx;
  42.             while a < b {
  43.                 idx = (a + b) / 2;
  44.                 if k < self.values[idx] {
  45.                     b = idx;
  46.                 } else if k > self.values[idx] {
  47.                     a = idx + 1;
  48.                 } else {
  49.                     return false
  50.                 }
  51.             }
  52.             self.values.insert(a, k);
  53.             return true
  54.         }
  55.     }
  56.  
  57. //    pub fn remove(&mut self, index: usize) -> Key {
  58. //    }
  59.  
  60.     pub fn clear(&mut self) {
  61.         self.clear()
  62.     }
  63.  
  64.     pub fn len(&self) -> usize {
  65.         self.len()
  66.     }
  67.  
  68.     pub fn is_empty(&self) -> bool {
  69.         self.is_empty()
  70.     }
  71. }
  72.  
  73. #[cfg(test)]
  74. mod tests {
  75.     #[test]
  76.     fn it_works() {
  77.         assert_eq!(2 + 2, 4);
  78.     }
  79. }
  80.  
  81. $ cargo build
  82.    Compiling akarui v0.1.0 (/home/phdp/Dropbox/work/akarui)
  83. warning: function cannot return without recursing
  84.   --> src/lib.rs:60:5
  85.    |
  86. 60 |     pub fn clear(&mut self) {
  87.    |     ^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
  88. 61 |         self.clear()
  89.    |         ------------ recursive call site
  90.    |
  91.    = note: #[warn(unconditional_recursion)] on by default
  92.    = help: a `loop` may express intention better if this is on purpose
  93.  
  94. warning: function cannot return without recursing
  95.   --> src/lib.rs:64:5
  96.    |
  97. 64 |     pub fn len(&self) -> usize {
  98.    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
  99. 65 |         self.len()
  100.    |         ---------- recursive call site
  101.    |
  102.    = help: a `loop` may express intention better if this is on purpose
  103.  
  104. warning: function cannot return without recursing
  105.   --> src/lib.rs:68:5
  106.    |
  107. 68 |     pub fn is_empty(&self) -> bool {
  108.    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
  109. 69 |         self.is_empty()
  110.    |         --------------- recursive call site
  111.    |
  112.    = help: a `loop` may express intention better if this is on purpose
  113.  
  114.     Finished dev [unoptimized + debuginfo] target(s) in 0.11s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement