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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright (c) 2024 <Wei Li>.
//
// This source code is licensed under the GNU license found in the
// LICENSE file in the root directory of this source tree.

use std::fmt;
use std::slice;

use crate::util::bit_vec::{BitIter, BitVec, Idx};

const SMALL_SET_CAPACITY: usize = 32;

pub trait PointsToSet<T> {
    type Iter<'a>: Iterator<Item = T>
    where
        Self: 'a;

    fn new() -> Self;
    fn clear(&mut self);
    fn count(&self) -> usize;
    fn contains(&self, elem: T) -> bool;
    fn is_empty(&self) -> bool;
    fn superset(&self, other: &Self) -> bool;
    fn insert(&mut self, elem: T) -> bool;
    fn remove(&mut self, elem: T) -> bool;
    fn union(&mut self, other: &Self) -> bool;
    fn subtract(&mut self, other: &Self) -> bool;
    fn intersect(&mut self, other: &Self) -> bool;
    fn iter<'a>(&'a self) -> Self::Iter<'a>;
}

/// Hybrid implementation of points to set,
/// which uses an explicit array for small sets, and a bit vector for large sets.
#[derive(Clone)]
pub struct HybridPointsToSet<T> {
    points_to: HybridSet<T>,
}

impl<T: Idx> fmt::Debug for HybridPointsToSet<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.points_to.fmt(f)
    }
}

/// IntoIterator
impl<'a, T: Idx> IntoIterator for &'a HybridPointsToSet<T> {
    type Item = T;
    type IntoIter = HybridIter<'a, T>;

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

impl<T: Idx> PointsToSet<T> for HybridPointsToSet<T> {
    fn new() -> Self {
        HybridPointsToSet {
            points_to: HybridSet::new(),
        }
    }

    /// Clear all elements.
    fn clear(&mut self) {
        self.points_to.clear();
    }

    /// Count the number of elements in the set.
    fn count(&self) -> usize {
        self.points_to.count()
    }

    /// Returns `true` if `self` contains `elem`.
    fn contains(&self, elem: T) -> bool {
        self.points_to.contains(elem)
    }

    fn is_empty(&self) -> bool {
        self.points_to.is_empty()
    }

    /// Is `self` is a superset of `other`?
    fn superset(&self, other: &HybridPointsToSet<T>) -> bool {
        self.points_to.superset(&other.points_to)
    }

    /// Adds `elem` to this set, returns true if n was not already in this set.
    fn insert(&mut self, elem: T) -> bool {
        self.points_to.insert(elem)
    }

    fn remove(&mut self, elem: T) -> bool {
        self.points_to.remove(elem)
    }

    fn union(&mut self, other: &HybridPointsToSet<T>) -> bool {
        self.points_to.union(&other.points_to)
    }

    fn subtract(&mut self, other: &HybridPointsToSet<T>) -> bool {
        self.points_to.subtract(&other.points_to)
    }

    fn intersect(&mut self, other: &HybridPointsToSet<T>) -> bool {
        self.points_to.intersect(&other.points_to)
    }

    type Iter<'a> = HybridIter<'a, T>;
    fn iter(&self) -> HybridIter<'_, T> {
        self.points_to.iter()
    }
}

#[derive(Clone)]
pub enum HybridSet<T> {
    SmallSet(Vec<T>),
    LargeSet(BitVec<T>),
}

impl<T: Idx> fmt::Debug for HybridSet<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SmallSet(s) => s.fmt(f),
            Self::LargeSet(s) => s.fmt(f),
        }
    }
}

impl<T: Idx> HybridSet<T> {
    pub fn new() -> Self {
        HybridSet::SmallSet(Vec::with_capacity(SMALL_SET_CAPACITY))
    }

    /// Clear all elements.
    pub fn clear(&mut self) {
        match self {
            HybridSet::SmallSet(small) => small.clear(),
            HybridSet::LargeSet(_) => {
                *self = HybridSet::SmallSet(Vec::with_capacity(SMALL_SET_CAPACITY));
            }
        }
    }

    /// Count the number of elements in the set.
    pub fn count(&self) -> usize {
        match self {
            HybridSet::SmallSet(small) => small.len(),
            HybridSet::LargeSet(large) => large.count(),
        }
    }

    /// Returns `true` if `self` contains `elem`.
    pub fn contains(&self, elem: T) -> bool {
        match self {
            HybridSet::SmallSet(small) => small.contains(&elem),
            HybridSet::LargeSet(large) => large.contains(elem),
        }
    }

    /// Is `self` is a superset of `other`?
    pub fn superset(&self, other: &HybridSet<T>) -> bool {
        match (self, other) {
            (HybridSet::LargeSet(self_large), HybridSet::LargeSet(other_large)) => {
                self_large.superset(&other_large)
            }
            _ => other.iter().all(|elem| self.contains(elem)),
        }
    }

    pub fn is_empty(&self) -> bool {
        match self {
            HybridSet::SmallSet(small) => small.is_empty(),
            HybridSet::LargeSet(large) => large.is_empty(),
        }
    }

    /// Adds `elem` to this set, returns true if n was not already in this set.
    pub fn insert(&mut self, elem: T) -> bool {
        match self {
            HybridSet::SmallSet(small) if small.contains(&elem) => {
                // The set is small and `elem` is not present.
                false
            }
            HybridSet::SmallSet(small) if small.len() < SMALL_SET_CAPACITY => {
                // The set is small and has space for `elem`.
                small.push(elem);
                true
            }
            HybridSet::SmallSet(small) => {
                // The set is small and full. Convert to a large set.
                let mut large = BitVec::new_empty();
                for elem in small {
                    large.insert(*elem);
                }
                let changed = large.insert(elem);
                *self = HybridSet::LargeSet(large);
                changed
            }
            HybridSet::LargeSet(large) => large.insert(elem),
        }
    }

    pub fn remove(&mut self, elem: T) -> bool {
        // Note: we currently don't bother going from Large back to Small.
        match self {
            HybridSet::SmallSet(small) => {
                if let Some(pos) = small.iter().position(|x| *x == elem) {
                    small.swap_remove(pos);
                    true
                } else {
                    false
                }
            }
            HybridSet::LargeSet(large) => large.remove(elem),
        }
    }

    pub fn iter(&self) -> HybridIter<'_, T> {
        match self {
            HybridSet::SmallSet(small) => HybridIter::SmallIter(small.iter()),
            HybridSet::LargeSet(large) => HybridIter::LargeIter(large.iter()),
        }
    }

    pub fn union(&mut self, other: &HybridSet<T>) -> bool {
        match self {
            HybridSet::LargeSet(self_large) => match other {
                HybridSet::LargeSet(other_large) => self_large.union(&other_large),
                HybridSet::SmallSet(other_small) => {
                    let mut changed = false;
                    for elem in other_small.iter() {
                        changed |= self_large.insert(*elem);
                    }
                    changed
                }
            },
            HybridSet::SmallSet(self_small) => {
                match other {
                    HybridSet::LargeSet(other_large) => {
                        // convert self set to a large set
                        let mut self_large = BitVec::new_empty();
                        for elem in self_small.iter() {
                            self_large.insert(*elem);
                        }
                        let changed = self_large.union(&other_large);
                        *self = HybridSet::LargeSet(self_large);
                        changed
                    }
                    HybridSet::SmallSet(other_small) => {
                        let mut changed = false;
                        for &elem in other_small.iter() {
                            changed |= self.insert(elem);
                        }
                        changed
                    }
                }
            }
        }
    }

    pub fn subtract(&mut self, other: &HybridSet<T>) -> bool {
        match self {
            HybridSet::LargeSet(self_large) => match other {
                HybridSet::LargeSet(other_large) => self_large.subtract(&other_large),
                HybridSet::SmallSet(other_small) => {
                    let mut changed = false;
                    for &elem in other_small.iter() {
                        changed |= self_large.remove(elem);
                    }
                    changed
                }
            },
            HybridSet::SmallSet(self_small) => {
                let mut changed = false;
                self_small.retain(|&elem| {
                    let contains = other.contains(elem);
                    if contains {
                        changed = true;
                    }
                    !contains
                });
                changed
            }
        }
    }

    pub fn intersect(&mut self, other: &HybridSet<T>) -> bool {
        match self {
            HybridSet::LargeSet(self_large) => {
                match other {
                    HybridSet::LargeSet(other_large) => self_large.intersect(&other_large),
                    HybridSet::SmallSet(other_small) => {
                        // convert self set to a small set
                        let mut self_small = other_small.clone();
                        let mut changed = false;
                        self_small.retain(|&elem| {
                            let contains = self_large.contains(elem);
                            if !contains {
                                changed = true;
                            }
                            contains
                        });
                        *self = HybridSet::SmallSet(self_small);
                        changed
                    }
                }
            }
            HybridSet::SmallSet(self_small) => {
                let mut changed = false;
                self_small.retain(|&elem| {
                    let contains = other.contains(elem);
                    if !contains {
                        changed = true;
                    }
                    contains
                });
                changed
            }
        }
    }
}

pub enum HybridIter<'a, T: Idx> {
    SmallIter(slice::Iter<'a, T>),
    LargeIter(BitIter<'a, T>),
}

impl<'a, T: Idx> Iterator for HybridIter<'a, T> {
    type Item = T;

    fn next(&mut self) -> Option<T> {
        match self {
            HybridIter::SmallIter(small) => small.next().copied(),
            HybridIter::LargeIter(large) => large.next(),
        }
    }
}

#[test]
fn hybrid_set_tests() {
    // small set test
    let mut a = HybridPointsToSet::<u32>::new();
    a.insert(1);
    a.insert(3);
    a.insert(5);
    a.insert(3);
    a.insert(11);

    assert_eq!(a.count(), 4);
    assert_eq!(a.contains(3), true);
    assert_eq!(a.contains(7), false);
    assert_eq!(a.iter().collect::<Vec<_>>(), [1, 3, 5, 11]);
    assert!(matches!(a.points_to, HybridSet::SmallSet(_)));

    // large set test
    let mut b = HybridPointsToSet::<u32>::new();
    b.insert(1);
    b.insert(10);
    b.insert(19);
    b.insert(62);
    b.insert(63);
    b.insert(64);
    b.insert(65);
    b.insert(66);
    b.insert(99);
    b.insert(2);
    b.insert(20);
    b.insert(38);
    b.insert(124);
    b.insert(126);
    b.insert(128);
    b.insert(130);
    b.insert(132);
    b.insert(99);
    assert_eq!(b.count(), 17);
    assert_eq!(b.contains(38), true);
    assert_eq!(b.contains(7), false);
    assert_eq!(
        b.iter().collect::<Vec<_>>(),
        [1, 2, 10, 19, 20, 38, 62, 63, 64, 65, 66, 99, 124, 126, 128, 130, 132]
    );
    assert_eq!(b.superset(&a), false);
    assert!(matches!(b.points_to, HybridSet::LargeSet(_)));

    // remove test
    assert_eq!(a.remove(3), true);
    assert_eq!(a.count(), 3);
    assert_eq!(a.contains(3), false);
    assert_eq!(a.remove(17), false);
    a.insert(3);

    assert_eq!(b.remove(10), true);
    assert_eq!(b.count(), 16);
    assert_eq!(b.contains(10), false);
    assert_eq!(b.remove(17), false);
    b.insert(10);

    // union test
    // small set union large set
    let mut c = a.clone();
    c.union(&b);
    assert_eq!(c.count(), 20);
    assert_eq!(c.superset(&a), true);
    assert_eq!(c.superset(&b), true);
    assert_eq!(
        c.iter().collect::<Vec<_>>(),
        [1, 2, 3, 5, 10, 11, 19, 20, 38, 62, 63, 64, 65, 66, 99, 124, 126, 128, 130, 132]
    );
    assert!(matches!(c.points_to, HybridSet::LargeSet(_)));

    // small set union small set
    c = a.clone();
    let mut d = HybridPointsToSet::<u32>::new();
    d.insert(3);
    d.insert(17);
    d.insert(25);
    d.insert(37);
    d.insert(46);
    d.insert(55);
    d.insert(63);
    d.insert(77);
    d.insert(89);
    d.insert(90);
    d.insert(102);
    d.insert(111);
    d.insert(123);
    d.insert(134);
    c.union(&d);
    assert_eq!(c.count(), 17);
    assert_eq!(c.superset(&a), true);
    assert_eq!(
        c.iter().collect::<Vec<_>>(),
        [1, 3, 5, 11, 17, 25, 37, 46, 55, 63, 77, 89, 90, 102, 111, 123, 134]
    );
    assert!(matches!(d.points_to, HybridSet::SmallSet(_)));
    assert!(matches!(c.points_to, HybridSet::LargeSet(_)));

    // large set union small set
    let mut e = b.clone();
    e.union(&a);
    assert_eq!(e.count(), 20);
    assert_eq!(e.superset(&a), true);
    assert_eq!(e.superset(&b), true);
    assert_eq!(
        e.iter().collect::<Vec<_>>(),
        [1, 2, 3, 5, 10, 11, 19, 20, 38, 62, 63, 64, 65, 66, 99, 124, 126, 128, 130, 132]
    );
    assert!(matches!(e.points_to, HybridSet::LargeSet(_)));

    // large set union large set
    let mut f = b.clone();
    f.insert(156);
    f.insert(10001);
    e.union(&f);
    assert_eq!(e.count(), 22);
    assert_eq!(
        e.iter().collect::<Vec<_>>(),
        [1, 2, 3, 5, 10, 11, 19, 20, 38, 62, 63, 64, 65, 66, 99, 124, 126, 128, 130, 132, 156, 10001]
    );

    // subtract test
    c = a.clone();
    assert_eq!(c.subtract(&b), true);
    assert_eq!(c.count(), 3);
    assert_eq!(c.contains(1), false);

    e = b.clone();
    assert_eq!(e.subtract(&a), true);
    assert_eq!(e.count(), 16);
    assert_eq!(e.contains(1), false);

    // intersect test
    c = a.clone();
    assert_eq!(c.intersect(&b), true);
    assert_eq!(c.count(), 1);
    assert_eq!(c.contains(1), true);
    assert!(matches!(c.points_to, HybridSet::SmallSet(_)));

    e = b.clone();
    assert_eq!(e.intersect(&a), true);
    assert_eq!(e.count(), 1);
    assert_eq!(e.contains(1), true);
    assert!(matches!(e.points_to, HybridSet::SmallSet(_)));
}