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
// Copyright 2016 union-find-rs Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use std::cmp::Ordering;
use {Union, UnionResult};

/// Operates the `union` with using the size of the sets as weight.
///
/// A smaller sized set will be the children of a larger sized set.
#[derive(Copy, Clone, Debug)]
pub struct UnionBySize(usize);

impl Union for UnionBySize {
    #[inline]
    fn union(left: UnionBySize, right: UnionBySize) -> UnionResult<UnionBySize> {
        let lsize = left.size();
        let rsize = right.size();
        let result = UnionBySize(lsize + rsize);
        if lsize >= rsize {
            UnionResult::Left(result)
        } else {
            UnionResult::Right(result)
        }
    }
}

impl Default for UnionBySize {
    #[inline]
    fn default() -> UnionBySize {
        UnionBySize(1)
    }
}

impl UnionBySize {
    /// Returns the size of the set.
    #[inline]
    pub fn size(&self) -> usize {
        let UnionBySize(size) = *self;
        size
    }
}


/// Operates the `union` with using the rank of the sets as weight.
///
/// A smaller ranked set will be the children of a larger ranked set.
/// If both sets have the same rank, the size of the set is used.
#[derive(Copy, Clone, Debug)]
pub struct UnionByRank(u8);

impl Union for UnionByRank {
    #[inline]
    fn union(left: UnionByRank, right: UnionByRank) -> UnionResult<UnionByRank> {
        let lrank = left.rank();
        let rrank = right.rank();
        match lrank.cmp(&rrank) {
            Ordering::Less => UnionResult::Right(right),
            Ordering::Greater => UnionResult::Left(left),
            Ordering::Equal => UnionResult::Left(UnionByRank(lrank + 1)),
        }
    }
}

impl Default for UnionByRank {
    #[inline]
    fn default() -> UnionByRank {
        UnionByRank(0)
    }
}

impl UnionByRank {
    /// Returns the rankq of the set.
    #[inline]
    pub fn rank(&self) -> u8 {
        let UnionByRank(rank) = *self;
        rank
    }
}

/// Operates the `union` with using the size and the rank of the sets as weight.
///
/// A smaller sized set will be the children of a larger sized set.
/// If both sets have the same size, compared by the rank.
#[derive(Copy, Clone, Debug)]
pub struct UnionBySizeRank(usize, u8);

impl Union for UnionBySizeRank {
    #[inline]
    fn union(left: UnionBySizeRank, right: UnionBySizeRank) -> UnionResult<UnionBySizeRank> {
        let lsize = left.size();
        let lrank = left.rank();
        let rsize = right.size();
        let rrank = right.rank();

        let rank_cmp = lrank.cmp(&rrank);
        let new_size = lsize + rsize;
        let new_rank = match rank_cmp {
            Ordering::Less => rrank,
            Ordering::Greater => lrank,
            Ordering::Equal => lrank + 1,
        };

        let result = UnionBySizeRank(new_size, new_rank);
        match lsize.cmp(&rsize) {
            Ordering::Less => UnionResult::Right(result),
            Ordering::Greater => UnionResult::Left(result),
            Ordering::Equal => match rank_cmp {
                Ordering::Less => UnionResult::Right(result),
                _ => UnionResult::Left(result),
            },
        }
    }
}

impl Default for UnionBySizeRank {
    #[inline]
    fn default() -> UnionBySizeRank {
        UnionBySizeRank(1, 0)
    }
}

impl UnionBySizeRank {
    /// Returns the size of the set.
    #[inline]
    pub fn size(&self) -> usize {
        let UnionBySizeRank(size, _) = *self;
        size
    }

    /// Returns the rank of the set.
    #[inline]
    pub fn rank(&self) -> u8 {
        let UnionBySizeRank(_, rank) = *self;
        rank
    }
}

/// Operates the `union` with using the ramk and the size of the sets as weight.
///
/// A smaller ranked set will be the children of a larger ranked set.
/// If both sets have the same rank, compared by the size.
#[derive(Copy, Clone, Debug)]
pub struct UnionByRankSize(u8, usize);

impl Union for UnionByRankSize {
    #[inline]
    fn union(left: UnionByRankSize, right: UnionByRankSize) -> UnionResult<UnionByRankSize> {
        let lrank = left.rank();
        let lsize = left.size();
        let rrank = right.rank();
        let rsize = right.size();

        let rank_cmp = lrank.cmp(&rrank);
        let new_size = lsize + rsize;
        let new_rank = match rank_cmp {
            Ordering::Less => rrank,
            Ordering::Greater => lrank,
            Ordering::Equal => lrank + 1,
        };

        let result = UnionByRankSize(new_rank, new_size);
        match rank_cmp {
            Ordering::Less => UnionResult::Right(result),
            Ordering::Greater => UnionResult::Left(result),
            Ordering::Equal => match lsize.cmp(&rsize) {
                Ordering::Less => UnionResult::Right(result),
                _ => UnionResult::Left(result),
            },
        }
    }
}

impl Default for UnionByRankSize {
    #[inline]
    fn default() -> UnionByRankSize {
        UnionByRankSize(1, 0)
    }
}

impl UnionByRankSize {
    /// Returns the rank of the set.
    #[inline]
    pub fn rank(&self) -> u8 {
        let UnionByRankSize(rank, _) = *self;
        rank
    }

    /// Returns the size of the set.
    #[inline]
    pub fn size(&self) -> usize {
        let UnionByRankSize(_, size) = *self;
        size
    }
}