1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2021 Xavier Gillard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

//! This crate provides a series of allocation free set of integers capable of 
//! holding small integer values. These sets are all implemented as bitsets 
//! and are therefore only capable of storing integers with large values.
//!
//! # Warning
//! These collections willingly only target the case of small integer values.
//! Hence they will not try to grow so as to accomodate more values depending
//! on your need. 

/// This macro generates an implementation for the desired types. It simply 
/// avoid code duplication for structs that cannot otherwise be generic.
macro_rules! small_set {
    ($name:ident, $iter:ident, $inner:ty, $capa: literal, $tests:ident) => {
        /// This structure encapsulates a small allocation free set of integers.
        /// Because it is implemented as a fixed size bitset, it can only
        /// accomodate values in the range 0..$capa. 
        ///
        /// Because of this restriction on the range of allowed values, all the
        /// items that can be stored in this set are of type 'u8'. 
        #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
        pub struct $name($inner);

        impl $name {
            /// This method creates an empty set 
            pub fn empty() -> Self { Self(0) }
            /// This method returns the complete set
            pub fn full() -> Self { Self(!0) }
            /// This method creates a singleton set holding the single value 'x'.
            pub fn singleton(x: u8) -> Self { Self(1 << x) }
            /// This method returns the union of two sets
            pub fn union(self, other: Self) -> Self { Self(self.0 | other.0) }
            /// This method returns the intersection of two sets
            pub fn inter(self, other: Self) -> Self { Self(self.0 & other.0) }
            /// This method returns the difference of two sets.
            pub fn diff (self, other: Self) -> Self { Self(self.0 &!other.0) }
            /// This method retuns the complement of the current set
            pub fn complement(self) -> Self { Self(!self.0) }
            /// This method returns the set obtained by adding the singleton x
            /// to the current set
            pub fn insert(&mut self, x: u8) -> Self { 
               self.union(Self::singleton(x))
            }
            /// This method returns the set obtained by removing the singleton 
            /// x from the current set
            pub fn remove(self, x: u8)-> Self {
               self.diff(Self::singleton(x))
            }
            /// Returns true iff the set is empty 
            pub fn is_empty(self) -> bool { 
                self.0 == 0 
            }
            /// Returns the number of items in this set
            pub fn len(self) -> usize { 
                self.0.count_ones() as usize 
            }
            /// Returns true iff the current set contains the given item x
            pub fn contains(self, x: u8) -> bool {
                !self.inter(Self::singleton(x)).is_empty()
            }
            /// Returns the capcity of this set
            pub const fn capacity(self) -> usize {
                $capa as usize
            }
            /// Returns an iterator over the elements of this set
            pub fn iter(self) -> $iter {
                $iter::new(self)
            }
        }
        /// This structure provides a convenient iterator over the items of 
        /// a $name .
        #[derive(Copy, Clone, Debug)]
        pub struct $iter {
            /// The set iterated upon
            set: $name,
            /// The current position in the iteration (aka a cursor)
            pos: u8
        }
        impl $iter {
            /// Creates a new $iter to iterate over a given $name.
            pub fn new(set: $name) -> Self { Self {set, pos: 0 } }
        }
        impl ::std::iter::Iterator for $iter {
            type Item = u8;

            fn next(&mut self) -> Option<Self::Item> {
                while self.pos < $capa {
                    if self.set.contains(self.pos) {
                        let result = Some(self.pos);
                        self.pos += 1;
                        return result;
                    } else {
                        self.pos += 1;
                    }
                }
                None
            }
        }

        impl ::std::iter::IntoIterator for $name {
            type Item = u8;
            type IntoIter = $iter;

            fn into_iter(self) -> Self::IntoIter {
                self.iter()
            }
        }

        impl ::std::fmt::Display for $name {
            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                ::std::write!(f, "{{")?;
                let mut it   = self.iter();
                let mut next = it.next();
                while let Some(v) = next {
                    next = it.next();
                    if next.is_some() {
                        ::std::write!(f, "{}, ", v)?;
                    } else {
                        ::std::write!(f, "{}", v)?;
                    }
                }
                ::std::write!(f, "}}")
            }
        }

        impl From<$name> for $inner {
            fn from(x: $name) -> $inner {
                x.0
            }
        }
        impl From<$inner> for $name {
            fn from(x: $inner) -> $name {
                $name(x)
            }
        }

        #[cfg(test)]
        mod $tests {
            use super::*;

            // --- EMPTY SET -------------------------------------------------
            #[test]
            fn emptyset_is_empty() {
                let x = $name::empty();
                assert!(x.is_empty());
            }
            #[test]
            fn emptyset_len() {
                let x = $name::empty();
                assert_eq!(0, x.len());
            }
            #[test]
            fn emptyset_iter_count(){
                let x = $name::empty();
                assert_eq!(0, x.iter().count());
            }
            #[test]
            fn emptyset_contains_no_item() {
                let x = $name::empty();
                for i in 0..$capa {
                    assert!(!x.contains(i));
                }
            }
            #[test]
            fn emptyset_complement() {
                assert_eq!($name::empty().complement(), $name::full());
                assert_eq!($name::empty().complement().complement(), $name::empty());
            }

            // --- FULL SET --------------------------------------------------
            #[test]
            fn fullset_is_not_empty() {
                let x = $name::full();
                assert!(!x.is_empty());
            }
            #[test]
            fn fullset_len() {
                let x = $name::full();
                assert_eq!($capa, x.len());
            }
            #[test]
            fn fullset_iter_count(){
                let x = $name::full();
                assert_eq!($capa, x.iter().count());
            }
            #[test]
            fn fullset_contains_all_items() {
                let x = $name::full();
                for i in 0..$capa {
                    assert!(x.contains(i));
                }
            }
            #[test]
            fn fullset_complement() {
                assert_eq!($name::full().complement(), $name::empty());
                assert_eq!($name::full().complement().complement(), $name::full());
            }
            
            // --- SINGLETON --------------------------------------------------
            #[test]
            fn singleton_is_not_empty() {
                let x = $name::singleton(0);
                assert!(!x.is_empty());
            }
            #[test]
            fn singleton_len() {
                let x = $name::singleton(1);
                assert_eq!(1, x.len());
            }
            #[test]
            fn singleton_iter_count(){
                let x = $name::singleton(2);
                assert_eq!(1, x.iter().count());
            }
            #[test]
            fn singleton_contains_one_single_item() {
                let x = $name::singleton(3);
                for i in 0..$capa {
                    if i == 3 {
                        assert!(x.contains(i));
                    } else {
                        assert!(!x.contains(i));
                    }
                }
            }
            #[test]
            fn singleton_complement() {
                let x = $name::singleton(4);
                assert_eq!(x.complement().complement(), x);
            }

            // --- OTHER METHODS ---------------------------------------------
            #[test]
            fn test_union() {
                let _124   = $name::empty().insert(1).insert(2).insert(4);
                let _035   = $name::empty().insert(0).insert(3).insert(5);

                let _01235 = $name::empty()
                    .insert(0).insert(1).insert(2)
                    .insert(3).insert(4).insert(5);

                assert_eq!(_01235, _124.union(_035));
            }
            #[test]
            fn union_with_is_idempotent() {
                let x = $name::singleton(4);
                assert_eq!(x, x.union(x));
            }
            #[test]
            fn test_inter() {
                let _124   = $name::empty().insert(1).insert(2).insert(4);
                let _025   = $name::empty().insert(0).insert(2).insert(5);

                assert_eq!($name::singleton(2), _124.inter(_025));
            }
            #[test]
            fn diff_removes_existing_item() {
                let _02 = $name::singleton(0).insert(2);
                
                assert_eq!($name::singleton(0), _02.diff($name::singleton(2)));
            }
            #[test]
            fn diff_removes_all_existing_item() {
                let _02  = $name::singleton(0).insert(2);
                let delta= $name::singleton(0).insert(2);
                
                assert_eq!($name::empty(), _02.diff(delta));
            }
            #[test]
            fn diff_leaves_non_existing_items() {
                let _02  = $name::singleton(0).insert(2);
                let delta= $name::singleton(5).insert(6);
                
                assert_eq!(_02, _02.diff(delta));
            }
            #[test]
            fn insert_contains() {
                let mut x = $name::empty();
                assert!(!x.contains(1));
                assert!(!x.contains(2));
                x = x.insert(1);
                assert!(x.contains(1));
                assert!(!x.contains(2));
                // duplicate add has no effect
                x = x.insert(1);
                assert!(x.contains(1));
                assert!(!x.contains(2));
            }
            #[test]
            fn remove_contains() {
                let mut x = $name::singleton(4);
                assert!(!x.contains(1));
                assert!(!x.contains(2));
                assert!(x.contains(4));
                // removing non existing has no effect
                x = x.remove(1);
                assert!(!x.contains(1));
                assert!(!x.contains(2));
                assert!(x.contains(4));
                // removing existing
                x = x.remove(4);
                assert!(!x.contains(1));
                assert!(!x.contains(2));
                assert!(!x.contains(4));
                // duplicate remove has no effect
                x = x.remove(4);
                assert!(!x.contains(1));
                assert!(!x.contains(2));
                assert!(!x.contains(4));
            }
            #[test]
            fn test_iter() {
                let x = $name::singleton(1).insert(2).insert(3);
                let v = x.iter().collect::<Vec<u8>>();
                assert_eq!(vec![1, 2, 3], v);
            }
            #[test]
            fn test_into_iter() {
                let x = $name::singleton(1).insert(2).insert(3);
                let mut v = vec![];
                for i in x {
                    v.push(i);
                }
                assert_eq!(vec![1, 2, 3], v);
            }
            #[test]
            fn test_capacity() {
                assert_eq!($name::empty().capacity(), $capa);
            }


            #[test]
            fn test_display_empty(){
                let set = $name::empty();
                assert_eq!("{}", format!("{}", set));
            }
            #[test]
            fn test_display_singleton(){
                let set = $name::singleton(4);
                assert_eq!("{4}", format!("{}", set));
            }
            #[test]
            fn test_display_multi(){
                let set = $name::singleton(1).insert(4).insert(6);
                assert_eq!("{1, 4, 6}", format!("{}", set));
            }
        }
    }
}

// This is where we declare and define the structures for the various types of
// sets.
small_set!(Set8,    Set8Iter,    u8,      8, test_8);
small_set!(Set16,   Set16Iter,   u16,    16, test_16);
small_set!(Set32,   Set32Iter,   u32,    32, test_32);
small_set!(Set64,   Set64Iter,   u64,    64, test_64);
small_set!(Set128,  Set128Iter,  u128,  128, test128);
// This may be un commented once usize::BITS stabilizes
//small_set!(SetSize, SetSizeIter, usize, usize::BITS, testsize);