Satsuma
a delicious .NET graph library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Pages
DisjointSet.cs
Go to the documentation of this file.
1 #region License
2 /*This file is part of Satsuma Graph Library
3 Copyright © 2013 Balázs Szalkai
4 
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.*/
23 #endregion
24 
25 using System;
26 using System.Collections.Generic;
27 
28 namespace Satsuma
29 {
32  public struct DisjointSetSet<T> : IEquatable<DisjointSetSet<T>>
33  {
34  public T Representative { get; private set; }
35 
36  public DisjointSetSet(T representative)
37  : this()
38  {
39  Representative = representative;
40  }
41 
42  public bool Equals(DisjointSetSet<T> other)
43  {
44  return Representative.Equals(other.Representative);
45  }
46 
47  public override bool Equals(object obj)
48  {
49  if (obj is DisjointSetSet<T>) return Equals((DisjointSetSet<T>)obj);
50  return false;
51  }
52 
53  public static bool operator==(DisjointSetSet<T> a, DisjointSetSet<T> b)
54  {
55  return a.Equals(b);
56  }
57 
58  public static bool operator !=(DisjointSetSet<T> a, DisjointSetSet<T> b)
59  {
60  return !(a == b);
61  }
62 
63  public override int GetHashCode()
64  {
65  return Representative.GetHashCode();
66  }
67 
68  public override string ToString()
69  {
70  return "[DisjointSetSet:" + Representative + "]";
71  }
72  }
73 
75  public interface IReadOnlyDisjointSet<T>
76  {
78  DisjointSetSet<T> WhereIs(T element);
80  IEnumerable<T> Elements(DisjointSetSet<T> aSet);
81  }
82 
86  public interface IDisjointSet<T> : IReadOnlyDisjointSet<T>, IClearable
87  {
90  }
91 
93  public sealed class DisjointSet<T> : IDisjointSet<T>
94  {
95  private readonly Dictionary<T, T> parent;
96  // The first child of a representative, or the next sibling of a child.
97  private readonly Dictionary<T, T> next;
98  // The last child of a representative.
99  private readonly Dictionary<T, T> last;
100  private readonly List<T> tmpList;
101 
102  public DisjointSet()
103  {
104  parent = new Dictionary<T, T>();
105  next = new Dictionary<T, T>();
106  last = new Dictionary<T, T>();
107  tmpList = new List<T>();
108  }
109 
110  public void Clear()
111  {
112  parent.Clear();
113  next.Clear();
114  last.Clear();
115  }
116 
117  public DisjointSetSet<T> WhereIs(T element)
118  {
119  T p;
120  while (true)
121  {
122  if (!parent.TryGetValue(element, out p))
123  {
124  foreach (var a in tmpList) parent[a] = element;
125  tmpList.Clear();
126  return new DisjointSetSet<T>(element);
127  }
128  else
129  {
130  tmpList.Add(element);
131  element = p;
132  }
133  }
134  }
135 
136  private T GetLast(T x)
137  {
138  T y;
139  if (last.TryGetValue(x, out y)) return y;
140  return x;
141  }
142 
144  {
145  T x = a.Representative;
146  T y = b.Representative;
147 
148  if (!x.Equals(y))
149  {
150  parent[x] = y;
151  next[GetLast(y)] = x;
152  last[y] = GetLast(x);
153  }
154 
155  return b;
156  }
157 
158  public IEnumerable<T> Elements(DisjointSetSet<T> aSet)
159  {
160  T element = aSet.Representative;
161  while (true)
162  {
163  yield return element;
164  if (!next.TryGetValue(element, out element)) break;
165  }
166  }
167  }
168 }