1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.Collections;
24 using System.Collections.Generic;
30 /// インデックスでのアクセスが可能なソートされた重複のないコレクション
32 public class IndexedSortedSet<T> : ISet<T>, IReadOnlyList<T>
34 private readonly List<T> innerList;
35 private readonly IComparer<T> comparer;
38 => this.innerList.Count;
40 public bool IsReadOnly
43 public T this[int index]
44 => this.innerList[index];
46 public IndexedSortedSet()
47 : this(Enumerable.Empty<T>(), Comparer<T>.Default)
51 public IndexedSortedSet(IComparer<T> comparer)
52 : this(Enumerable.Empty<T>(), comparer)
56 public IndexedSortedSet(IEnumerable<T> items)
57 : this(items, Comparer<T>.Default)
61 public IndexedSortedSet(IEnumerable<T> items, IComparer<T> comparer)
63 this.innerList = new List<T>(items.OrderBy(x => x, comparer));
64 this.comparer = comparer;
67 public bool Add(T item)
69 if (this.innerList.Count == 0)
71 this.innerList.Add(item);
75 var index = this.innerList.BinarySearch(item, comparer);
79 // ~index → item の次に大きい要素のインデックス
80 this.innerList.Insert(~index, item);
85 void ICollection<T>.Add(T item)
88 public bool Remove(T item)
90 var index = this.IndexOf(item);
94 this.innerList.RemoveAt(index);
100 => this.innerList.Clear();
102 public bool Contains(T item)
103 => this.IndexOf(item) != -1;
105 public int IndexOf(T item)
107 var index = this.innerList.BinarySearch(item, this.comparer);
109 return index < 0 ? -1 : index;
112 public void RemoveAt(int index)
113 => this.innerList.RemoveAt(index);
115 public void CopyTo(T[] array, int arrayIndex)
118 throw new ArgumentNullException(nameof(array));
120 throw new ArgumentOutOfRangeException(nameof(arrayIndex));
121 if (arrayIndex >= array.Length)
122 throw new ArgumentException($"{nameof(arrayIndex)} is equal to or greater than {nameof(array)}.Length.", nameof(arrayIndex));
123 if (array.Length - arrayIndex < this.Count)
124 throw new ArgumentException("The destination array is too small.", nameof(array));
126 foreach (var pair in this)
127 array[arrayIndex++] = pair;
130 public IEnumerator<T> GetEnumerator()
131 => this.innerList.GetEnumerator();
133 IEnumerator IEnumerable.GetEnumerator()
134 => this.GetEnumerator();
136 public void UnionWith(IEnumerable<T> other)
138 foreach (var item in other)
142 public void IntersectWith(IEnumerable<T> other)
144 var containsMark = new bool[this.Count];
146 foreach (var item in other)
148 var index = this.IndexOf(item);
150 containsMark[index] = true;
153 foreach (var index in MyCommon.CountDown(this.Count - 1, 0))
155 if (!containsMark[index])
156 this.RemoveAt(index);
160 public void ExceptWith(IEnumerable<T> other)
162 foreach (var item in other)
166 public void SymmetricExceptWith(IEnumerable<T> other)
168 foreach (var item in other)
170 if (this.Contains(item))
177 public bool IsSubsetOf(IEnumerable<T> other)
179 var containsMark = new bool[this.Count];
181 foreach (var item in other)
183 var index = this.IndexOf(item);
185 containsMark[index] = true;
188 return containsMark.All(x => x == true);
191 public bool IsSupersetOf(IEnumerable<T> other)
193 foreach (var item in other)
195 if (!this.Contains(item))
202 public bool IsProperSupersetOf(IEnumerable<T> other)
203 => !this.SetEquals(other) && this.IsSupersetOf(other);
205 public bool IsProperSubsetOf(IEnumerable<T> other)
206 => !this.SetEquals(other) && this.IsSubsetOf(other);
208 public bool Overlaps(IEnumerable<T> other)
210 foreach (var item in other)
212 if (this.Contains(item))
219 public bool SetEquals(IEnumerable<T> other)
221 var containsMark = new bool[this.Count];
223 foreach (var item in other)
225 var index = this.IndexOf(item);
229 containsMark[index] = true;
232 return containsMark.All(x => x == true);