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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#![doc(html_root_url="https://docs.rs/fera-unionfind/0.1.0/")]

//! Union-find ([disjoint-set]) data structure implementation.
//!
//! This implementation use path compression and rank heuristic. With default type parameters the
//! parents and ranks are stored in a [`std::collections::HashMap`]. If the keys are in range
//! `0..n`, use [`UnionFindRange`].
//!
//! The keys should implement `Copy`. If the keys does not implement `Copy`, references to the keys
//! stored elsewhere should be used.
//!
//! This crate can be used through [`fera`] crate.
//!
//!
//! # Examples
//!
//! ```
//! use fera_unionfind::UnionFind;
//!
//! // Explicit type to make it clear
//! let mut s: UnionFind<&'static str> = UnionFind::new();
//!
//! s.make_set("red");
//! s.make_set("green");
//! s.make_set("blue");
//!
//! assert_eq!(3, s.num_sets());
//! assert!(!s.in_same_set("red", "green"));
//! assert!(!s.in_same_set("red", "blue"));
//!
//! s.union("red", "blue");
//!
//! assert_eq!(2, s.num_sets());
//! assert!(!s.in_same_set("red", "green"));
//! assert!(s.in_same_set("red", "blue"));
//! ```
//!
//! Using non `Copy` keys.
//!
//! ```
//! use fera_unionfind::UnionFind;
//!
//! // This is invalid. String does not implement copy.
//! // let mut x: UnionFind<String> = UnionFind::new();
//! // Lets store the keys in a vector and use references (references are Copy).
//! let v = vec!["red".to_string(), "green".to_string(), "blue".to_string()];
//!
//! // The type of s is Union<&'a String> where 'a is the lifetime of v.
//! let mut s = UnionFind::new();
//!
//! s.make_set(&v[0]);
//! s.make_set(&v[1]);
//! s.make_set(&v[2]);
//!
//! assert_eq!(3, s.num_sets());
//! assert!(!s.in_same_set(&v[0], &v[1]));
//! assert!(!s.in_same_set(&v[0], &v[2]));
//!
//! s.union(&v[0], &v[2]);
//!
//! assert_eq!(2, s.num_sets());
//! assert!(!s.in_same_set(&v[0], &v[1]));
//! assert!(s.in_same_set(&v[0], &v[2]));
//! ```
//!
//! Using keys in the range `0..n`.
//!
//! ```
//! use fera_unionfind::UnionFindRange;
//!
//! let mut s = UnionFindRange::with_keys_in_range(..5);
//!
//! // It is not necessary to call UnionFind::make_set
//!
//! assert_eq!(5, s.num_sets());
//! assert!(!s.in_same_set(0, 1));
//! assert!(!s.in_same_set(0, 2));
//!
//! s.union(0, 2);
//!
//! assert_eq!(4, s.num_sets());
//! assert!(!s.in_same_set(0, 1));
//! assert!(s.in_same_set(0, 2));
//!
//! s.reset();
//! assert_eq!(5, s.num_sets());
//! ```
//!
//!
//! [disjoint-set]: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
//! [`fera`]: https://docs.rs/fera
//! [`std::collections::HashMap`]: https://doc.rust-lang.org/stable/std/collections/struct.HashMap.html
//! [`UnionFindRange`]: type.UnionFindRange.html

#![cfg_attr(feature = "cargo-clippy", allow(inline_always))]

use std::collections::HashMap;
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash};
use std::marker::PhantomData;
use std::ops::{Index, IndexMut, RangeTo};

/// [`UnionFind`] with keys in range `0..n`.
///
/// [`UnionFind`]: struct.UnionFind.html
pub type UnionFindRange = UnionFind<usize, Vec<usize>, Vec<usize>>;

/// A union-find ([disjoint-set]) struct.
///
/// [disjoint-set]: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
#[derive(Clone)]
pub struct UnionFind<Key, Parent = IndexedHashMap<Key, Key>, Rank = IndexedHashMap<Key, usize>>
    where Key: Copy + PartialEq,
          Parent: IndexMut<Key, Output = Key>,
          Rank: IndexMut<Key, Output = usize>
{
    parent: Parent,
    rank: Rank,
    num_sets: usize,
    _marker: PhantomData<Key>,
}

impl<Key, Parent, Rank> UnionFind<Key, Parent, Rank>
    where Key: Copy + PartialEq,
          Parent: IndexMut<Key, Output = Key>,
          Rank: IndexMut<Key, Output = usize>
{
    /// Creates a new `UnionFind`.
    #[doc(hidden)]
    pub fn with_parent_rank_num_sets(parent: Parent, rank: Rank, num_sets: usize) -> Self {
        UnionFind {
            parent: parent,
            rank: rank,
            num_sets: num_sets,
            _marker: PhantomData,
        }
    }

    /// Adds the key in it's own set. The number of sets is increased by 1.
    ///
    /// It's undefined behavior to call this method with a key that is already in a set.
    pub fn make_set(&mut self, x: Key) {
        // TODO: if x has a parent?
        self.set_parent(x, x);
        self.set_rank(x, 0);
        self.num_sets += 1;
    }

    /// Joins the sets with the keys `x` and `y`. The number of sets is decreased by 1.
    ///
    /// # Panics
    ///
    /// If `x` or `y` is not in any set or if both are in the same set.
    pub fn union(&mut self, x: Key, y: Key) {
        let a = self.find_set(x);
        let b = self.find_set(y);
        assert!(a != b);
        self.link(a, b);
    }

    /// Returns `true` if `x` and `y` is in the same set, otherwise `false`.
    ///
    /// # Panics
    ///
    /// If `x` or `y` is not in any set.
    pub fn in_same_set(&mut self, x: Key, y: Key) -> bool {
        self.find_set(x) == self.find_set(y)
    }

    /// Returns the representative of the set that contains `x`.
    ///
    /// # Panics
    ///
    /// If `x` is not in any set.
    pub fn find_set(&mut self, mut x: Key) -> Key {
        while self.parent(x) != x {
            let p = self.parent(self.parent(x));
            self.set_parent(x, p);
            x = p;
        }
        self.parent(x)
    }

    /// Returns the number of distinct sets.
    pub fn num_sets(&self) -> usize {
        self.num_sets
    }

    fn link(&mut self, x: Key, y: Key) {
        self.num_sets -= 1;
        if self.rank(x) > self.rank(y) {
            self.set_parent(y, x);
        } else {
            self.set_parent(x, y);
            if self.rank(x) == self.rank(y) {
                self.inc_rank(y);
            }
        }
    }

    #[inline(always)]
    fn inc_rank(&mut self, k: Key) {
        self.rank[k] += 1;
    }

    #[inline(always)]
    fn rank(&self, k: Key) -> usize {
        self.rank[k]
    }

    #[inline(always)]
    fn set_rank(&mut self, k: Key, rank: usize) {
        self.rank[k] = rank;
    }

    #[inline(always)]
    fn parent(&self, k: Key) -> Key {
        self.parent[k]
    }

    #[inline(always)]
    fn set_parent(&mut self, k: Key, p: Key) {
        self.parent[k] = p;
    }
}

impl<K: Copy + Hash + Eq> UnionFind<K> {
    /// Creates a new [`UnionFind`].
    ///
    /// [`UnionFind`]: struct.UnionFind.html
    pub fn new() -> Self {
        UnionFind::with_parent_rank_num_sets(
            IndexedHashMap::new_parent_with_hasher(RandomState::new()),
            IndexedHashMap::new_rank_with_hasher(RandomState::new()),
            0,
        )
    }
}

impl UnionFindRange {
    /// Creates a new `UnionFindRange` with keys in `range`.
    pub fn with_keys_in_range(range: RangeTo<usize>) -> Self {
        UnionFind::with_parent_rank_num_sets((0..range.end).collect(),
                                             vec![0; range.end],
                                             range.end)
    }

    /// Reset the struct putting each key in it's own set.
    // TODO: how to implement this method for any UnionFind?
    pub fn reset(&mut self) {
        let n = self.parent.len();
        for i in 0..n {
            self.parent[i] = i;
            self.rank[i] = 0;
        }
        self.num_sets = n;
    }
}

/// This implements a map that can be used with [`UnionFind`].
///
/// [`UnionFind`]: struct.UnionFind.html
///
/// TODO: add docs of how to use a different hasher
pub struct IndexedHashMap<K, V, S = RandomState>
    where K: Copy + Hash + Eq,
          S: BuildHasher
{
    map: HashMap<K, V, S>,
    default: fn(&K) -> V,
}

impl<K, S> IndexedHashMap<K, K, S>
    where K: Copy + Hash + Eq,
          S: BuildHasher
{
    pub fn new_parent_with_hasher(hasher: S) -> Self {
        IndexedHashMap {
            map: HashMap::with_hasher(hasher),
            default: K::clone,
        }
    }
}

impl<K, S> IndexedHashMap<K, usize, S>
    where K: Copy + Hash + Eq,
          S: BuildHasher
{
    pub fn new_rank_with_hasher(hasher: S) -> Self {
        fn zero<K: Clone>(_: &K) -> usize {
            0
        }

        IndexedHashMap {
            map: HashMap::with_hasher(hasher),
            default: zero,
        }
    }
}

impl<K, V> Index<K> for IndexedHashMap<K, V>
    where K: Copy + Hash + Eq
{
    type Output = V;
    fn index(&self, index: K) -> &Self::Output {
        &self.map[&index]
    }
}

impl<K, V> IndexMut<K> for IndexedHashMap<K, V>
    where K: Copy + Hash + Eq
{
    fn index_mut(&mut self, index: K) -> &mut Self::Output {
        let f = self.default;
        self.map.entry(index).or_insert_with(|| f(&index))
    }
}


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

    type UF = UnionFind<usize, Vec<usize>, Vec<usize>>;

    fn check(ds: &mut UF, num_sets: usize, groups: &[&[usize]]) {
        assert_eq!(num_sets, ds.num_sets());
        for group in groups {
            for &a in *group {
                assert!(ds.in_same_set(group[0], a));
            }
        }
    }

    #[test]
    fn unionfind1() {
        let mut ds = UnionFind::with_keys_in_range(..5);
        check(&mut ds, 5, &[&[]]);
        ds.union(0, 2);
        check(&mut ds, 4, &[&[0, 2]]);
        ds.union(1, 3);
        check(&mut ds, 3, &[&[0, 2], &[1, 3]]);
        ds.union(2, 4);
        check(&mut ds, 2, &[&[0, 2, 4], &[1, 3]]);
        ds.union(3, 4);
        check(&mut ds, 1, &[&[0, 2, 4, 1, 3]]);
    }

    #[test]
    fn unionfind2() {
        let mut ds = UnionFind::with_keys_in_range(..16);
        ds.union(0, 1);
        ds.union(2, 3);
        ds.union(4, 5);
        ds.union(6, 7);

        ds.union(1, 2);
        ds.union(5, 6);
        ds.union(3, 7);

        ds.union(8, 9);
        ds.union(10, 11);
        ds.union(12, 13);
        ds.union(14, 15);

        ds.union(9, 10);
        ds.union(13, 14);
        ds.union(11, 15);

        ds.union(7, 15);

        check(&mut ds,
              1,
              &[&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]);
    }
}