|
13,25 |
13,22 |
|
*HashSetとSortedSet [#HashSetSortedSet]
|
*HashSetとSortedSet [#HashSetSortedSet]
|
|
&msdn(netfx,type,System.Collections.Generic.HashSet`1){System.Collections.Generic.HashSetクラス};と&msdn(netfx,type,System.Collections.Generic.SortedSet`1){System.Collections.Generic.SortedSetクラス};は、数学における集合の概念を表すコレクションクラスです。 HashSetは.NET Framework 3.5 SP1、SortedSetは.NET Framework 4より提供されています。
|
&msdn(netfx,type,System.Collections.Generic.HashSet`1){System.Collections.Generic.HashSetクラス};と&msdn(netfx,type,System.Collections.Generic.SortedSet`1){System.Collections.Generic.SortedSetクラス};は、数学における集合の概念を表すコレクションクラスです。 HashSetは.NET Framework 3.5 SP1、SortedSetは.NET Framework 4より提供されています。
|
|
|
|
~ |
HashSetクラス・SortedSetクラスを使うことで、一方のコレクションが他方のコレクションと重複する部分を持つか、あるいは完全に異なるか、他方を内包するか、といったコレクション同士の包含関係を調べることができます。 ([[#HashSetSortedSet_algebra_of_sets]]) また、コレクション同士の和(OR)を求めて二つのコレクションをマージしたり、積(AND)を求めて共通する要素だけを残したりすることができます。 ([[#HashSetSortedSet_calculating_sets]])
|
HashSetクラス・SortedSetクラスを使うことで、一方のコレクションが他方のコレクションと重複する部分を持つか、あるいは完全に異なるか、他方を内包するか、といったコレクション同士の包含関係を調べることができます。 また、コレクション同士の和(OR)を求めて二つのコレクションをマージしたり、積(AND)を求めて共通する要素だけを残したりすることができます。
|
|
|
|
~ |
HashSet・SortedSetでは、要素は重複することなく格納されます。 [[List>programming/netfx/collections/2_generic_1_list]]などとは異なり既に追加されている要素を追加しようとしても重複して格納されることなく、常に単一の要素として扱われます。 そのため、HashSet・SortedSetはキーだけを扱う一種の[[Dictionary>programming/netfx/collections/2_generic_2_dictionary]]のようなコレクションと見ることもできます。 一方、Add/Remove/Clearなど要素を動的に追加・削除するメソッドや、foreachによる列挙などもサポートされているため、その点では[[List>programming/netfx/collections/2_generic_1_list]]等の他のコレクションと同様に扱うこともできます。 ([[#HashSetSortedSet_basic_operations]])
|
HashSet・SortedSetでは、要素は重複することなく格納されます。 Listなどとは異なり既に追加されている要素を追加しようとしても重複して格納されることなく、常に単一の要素として扱われます。 そのため、HashSet・SortedSetはキーだけを扱う一種のDictionaryのようなコレクションと見ることもできます。
|
|
|
|
~ |
SortedSetクラスは、名前のとおりHashSetクラスに並べ替えの機能を追加したクラスで、要素を追加したり列挙したりする際には自動的にソートされます。 ほかにも、集合内における[[最大値・最小値の取得>#SortedSet_MinMax]]ができるなど、[[SortedSetでのみ提供される機能>#SortedSet_specific_operations]]も存在します。
|
またSortedSetクラスは、名前のとおりHashSetクラスに並べ替えの機能を追加したクラスで、要素を追加したり列挙したりする際には自動的にソートされます。
|
|
|
|
~ |
.NET Framework 4以降ではHashSet・SortedSetはともに&msdn(netfx,type,System.Collections.Generic.ISet`1){ISet<T>インターフェイス};を実装しています。 .NET Framework 3.5ではISet<T>は存在せず、そのためHashSetもISet<T>を実装していないので注意してください。 また.NET 5以降では、ISet<T>に対応する読み取り専用インターフェイス&msdn(netfx,type,System.Collections.Generic.IReadOnlySet`1){IReadOnlySet<T>インターフェイス};が新たに導入されるため、HashSet・SortedSetを含むISetを読み取り専用で扱うことができるようになります。
|
.NET Framework 4以降ではHashSet・SortedSetはともに&msdn(netfx,type,System.Collections.Generic.ISet`1){ISet<T>インターフェイス};を実装しています。 .NET Framework 3.5ではISet<T>は存在せず、そのためHashSetもISet<T>を実装していないので注意してください。
|
|
|
|
~ |
System.Collections名前空間ではHashSetクラス・SortedSetクラスに相当するクラス、集合の概念を表す非ジェネリックなコレクションクラスは提供されていません。
|
System.Collections名前空間ではHashSetクラス・SortedSetクラスに相当するクラス、集合の概念を表すコレクションクラスは提供されていません。
|
|
|
|
|
|
|
~ |
|
*基本操作
|
~ |
*基本操作 [#HashSetSortedSet_basic_operations]
|
HashSetクラス・SortedSetクラスともに、Listや他のコレクションクラスと共通するAdd, Remove, Contains, Clearなどのメソッドが用意されていて、単なるコレクションとして使う場合は他のコレクションクラスと何ら違いはありません。 また、SortedSetでは自動的に並べ替えが行われる点を除けば、動作と結果も同じです。
|
+ |
HashSetクラス・SortedSetクラスともに、Listなどの他のコレクションクラスと共通する&msdn(netfx,member,System.Collections.Generic.HashSet`1.Add){Add};, &msdn(netfx,member,System.Collections.Generic.HashSet`1.Remove){Remove};, &msdn(netfx,member,System.Collections.Generic.HashSet`1.Contains){Contains};, &msdn(netfx,member,System.Collections.Generic.HashSet`1.Clear){Clear};などのメソッドが用意されています。 [[IEnumerable<T>インターフェイス>programming/netfx/enumerator/0_enumerable_enumerator]]を実装しているため、[[foreach/For Eachステートメントで要素を列挙>#HashSetSortedSet_basic_operations_enumeration]]することもできます。
|
|
+ |
|
|
+ |
そのため、[[重複する値が単一の要素として扱われる>#HashSetSortedSet_basic_operations_duplicate_value]]以外は、他のコレクションクラスと同様に扱うことができます。 また、SortedSetでは自動的に並べ替えが行われる点を除けば、動作と結果もHashSetと同じです。
|
|
|
|
|
|
#column(layout=fixed,HashSet)
|
#column(layout=fixed,HashSet)
|
~ |
#tabpage(codelang=cs,container-title=HashSetでの要素の追加・削除・検索)
|
#tabpage(codelang=cs,container-title=HashSetへの要素の追加・削除・Containsによる要素の検索)
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
|
39,22 |
36,31 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new HashSet<int>() {3, 1, 6, 4, 0};
|
HashSet<int> s = new HashSet<int>(new int[] {0, 1, 3, 4, 6});
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s));
|
Print(s);
|
|
|
|
|
// 要素を追加
|
// 要素を追加
|
|
s.Add(2);
|
s.Add(2);
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s));
|
Print(s);
|
|
|
|
|
// 要素を削除
|
// 要素を削除
|
|
s.Remove(6);
|
s.Remove(6);
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s));
|
Print(s);
|
|
|
|
|
// 値5が含まれているか
|
// 値5が含まれているか
|
~ |
Console.WriteLine("Contains 5? {0}", s.Contains(5));
|
Console.WriteLine("Contains 5: {0}", s.Contains(5));
|
- |
|
}
|
- |
|
|
- |
|
static void Print(HashSet<int> s)
|
- |
|
{
|
- |
|
foreach (int e in s) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
- |
|
|
- |
|
Console.WriteLine();
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
65,35 |
71,43 |
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
Dim s As New HashSet(Of Integer) From {3, 1, 6, 4, 0}
|
Dim s As New HashSet(Of Integer)(New Integer() {0, 1, 3, 4, 6})
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s))
|
Print(s)
|
|
|
|
|
' 要素を追加
|
' 要素を追加
|
|
s.Add(2)
|
s.Add(2)
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s))
|
Print(s)
|
|
|
|
|
' 要素を削除
|
' 要素を削除
|
|
s.Remove(6)
|
s.Remove(6)
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s))
|
Print(s)
|
|
|
|
|
' 値5が含まれているか
|
' 値5が含まれているか
|
~ |
Console.WriteLine("Contains 5? {0}", s.Contains(5))
|
Console.WriteLine("Contains 5: {0}", s.Contains(5))
|
- |
|
End Sub
|
- |
|
|
- |
|
Shared Sub Print(ByVal s As HashSet(Of Integer))
|
- |
|
For Each e As Integer In s
|
- |
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
- |
|
|
- |
|
Console.WriteLine()
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
3, 1, 6, 4, 0
|
0, 1, 3, 4, 6,
|
~ |
3, 1, 6, 4, 0, 2
|
0, 1, 3, 4, 6, 2,
|
~ |
3, 1, 4, 0, 2
|
0, 1, 3, 4, 2,
|
~ |
Contains 5? False
|
Contains 5: False
|
|
}}
|
}}
|
|
#column(SortedSet)
|
#column(SortedSet)
|
~ |
#tabpage(codelang=cs,container-title=SortedSetでの要素の追加・削除・検索)
|
#tabpage(codelang=cs,container-title=SortedSetへの要素の追加・削除・Containsによる要素の検索)
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
|
101,89 |
115,27 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new SortedSet<int>() {3, 1, 6, 4, 0};
|
SortedSet<int> s = new SortedSet<int>(new int[] {0, 1, 3, 4, 6});
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s));
|
Print(s);
|
|
|
|
|
// 要素を追加
|
// 要素を追加
|
|
s.Add(2);
|
s.Add(2);
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s));
|
Print(s);
|
|
|
|
|
// 要素を削除
|
// 要素を削除
|
|
s.Remove(6);
|
s.Remove(6);
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s));
|
Print(s);
|
|
|
|
|
// 値5が含まれているか
|
// 値5が含まれているか
|
~ |
Console.WriteLine("Contains 5? {0}", s.Contains(5));
|
Console.WriteLine("Contains 5: {0}", s.Contains(5));
|
|
}
|
}
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
+ |
|
|
+ |
Class Sample
|
|
+ |
Shared Sub Main()
|
|
+ |
Dim s As New SortedSet(Of Integer) From {3, 1, 6, 4, 0}
|
|
+ |
|
|
+ |
Console.WriteLine(String.Join(", ", s))
|
|
+ |
|
|
+ |
' 要素を追加
|
|
+ |
s.Add(2)
|
|
+ |
|
|
+ |
Console.WriteLine(String.Join(", ", s))
|
|
|
|
|
~ |
' 要素を削除
|
static void Print(SortedSet<int> s)
|
+ |
s.Remove(6)
|
|
+ |
|
|
+ |
Console.WriteLine(String.Join(", ", s))
|
|
+ |
|
|
+ |
' 値5が含まれているか
|
|
+ |
Console.WriteLine("Contains 5? {0}", s.Contains(5))
|
|
+ |
End Sub
|
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
+ |
|
|
+ |
#prompt(実行結果){{
|
|
+ |
0, 1, 3, 4, 6
|
|
+ |
0, 1, 2, 3, 4, 6
|
|
+ |
0, 1, 2, 3, 4
|
|
+ |
Contains 5? False
|
|
+ |
}}
|
|
+ |
#column-end
|
|
+ |
|
|
+ |
|
|
+ |
**列挙操作 [#HashSetSortedSet_basic_operations_enumeration]
|
|
+ |
HashSet・SortedSetともに、foreach/For Eachステートメントで要素を列挙することができます。 HashSetでは、[[List>programming/netfx/collections/2_generic_1_list]]と同様に要素を追加した順に列挙されますが、SortedSetでは[[値の大小関係>#Comparison]]に従って小さい順に列挙されます。
|
|
+ |
|
|
+ |
|
|
+ |
#column(layout=fixed,HashSet)
|
|
+ |
#tabpage(codelang=cs,container-title=HashSetでの列挙操作と列挙順)
|
|
+ |
#code{{
|
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
+ |
|
|
+ |
class Sample {
|
|
+ |
static void Main()
|
|
|
{
|
{
|
~ |
var s = new HashSet<int>() {3, 1, 6, 4, 0};
|
foreach (int e in s) {
|
+ |
|
|
+ |
// 列挙して要素を表示
|
|
+ |
foreach (var e in s) {
|
|
+ |
Console.Write("{0}, ", e);
|
|
+ |
}
|
|
+ |
|
|
+ |
Console.WriteLine();
|
|
+ |
|
|
+ |
// 要素を追加
|
|
+ |
s.Add(2);
|
|
+ |
|
|
+ |
// 列挙して要素を表示
|
|
+ |
foreach (var e in s) {
|
|
|
Console.Write("{0}, ", e);
|
Console.Write("{0}, ", e);
|
|
}
|
}
|
|
|
|
|
198,83 |
150,25 |
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
Dim s As New HashSet(Of Integer) From {3, 1, 6, 4, 0}
|
Dim s As New SortedSet(Of Integer)(New Integer() {0, 1, 3, 4, 6})
|
+ |
|
|
+ |
' 列挙して要素を表示
|
|
+ |
For Each e As Integer In s
|
|
+ |
Console.Write("{0}, ", e)
|
|
+ |
Next
|
|
|
|
|
~ |
Console.WriteLine()
|
Print(s)
|
|
|
|
|
' 要素を追加
|
' 要素を追加
|
|
s.Add(2)
|
s.Add(2)
|
|
|
|
~ |
' 列挙して要素を表示
|
Print(s)
|
+ |
For Each e As Integer In s
|
|
+ |
Console.Write("{0}, ", e)
|
|
+ |
Next
|
|
|
|
|
~ |
Console.WriteLine()
|
' 要素を削除
|
~ |
End Sub
|
s.Remove(6)
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
|
|
|
~ |
#prompt(実行結果){{
|
Print(s)
|
+ |
3, 1, 6, 4, 0,
|
|
+ |
3, 1, 6, 4, 0, 2,
|
|
+ |
}}
|
|
+ |
#column(SortedSet)
|
|
+ |
#tabpage(codelang=cs,container-title=SortedSetでの列挙操作と列挙順)
|
|
+ |
#code{{
|
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
|
|
|
~ |
class Sample {
|
' 値5が含まれているか
|
~ |
static void Main()
|
Console.WriteLine("Contains 5: {0}", s.Contains(5))
|
~ |
{
|
End Sub
|
+ |
var s = new SortedSet<int>() {3, 1, 6, 4, 0};
|
|
+ |
|
|
+ |
// 列挙して要素を表示
|
|
+ |
foreach (var e in s) {
|
|
+ |
Console.Write("{0}, ", e);
|
|
+ |
}
|
|
+ |
|
|
+ |
Console.WriteLine();
|
|
+ |
|
|
+ |
// 要素を追加
|
|
+ |
s.Add(2);
|
|
+ |
|
|
+ |
// 列挙して要素を表示
|
|
+ |
foreach (var e in s) {
|
|
+ |
Console.Write("{0}, ", e);
|
|
+ |
}
|
|
+ |
|
|
+ |
Console.WriteLine();
|
|
+ |
}
|
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
|
|
|
~ |
Class Sample
|
Shared Sub Print(ByVal s As SortedSet(Of Integer))
|
+ |
Shared Sub Main()
|
|
+ |
Dim s As New SortedSet(Of Integer) From {3, 1, 6, 4, 0}
|
|
+ |
|
|
+ |
' 列挙して要素を表示
|
|
+ |
For Each e As Integer In s
|
|
+ |
Console.Write("{0}, ", e)
|
|
+ |
Next
|
|
+ |
|
|
+ |
Console.WriteLine()
|
|
+ |
|
|
+ |
' 要素を追加
|
|
+ |
s.Add(2)
|
|
+ |
|
|
+ |
' 列挙して要素を表示
|
|
|
For Each e As Integer In s
|
For Each e As Integer In s
|
|
Console.Write("{0}, ", e)
|
Console.Write("{0}, ", e)
|
|
Next
|
Next
|
|
288,19 |
182,14 |
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
|
0, 1, 3, 4, 6,
|
0, 1, 3, 4, 6,
|
|
0, 1, 2, 3, 4, 6,
|
0, 1, 2, 3, 4, 6,
|
- |
|
0, 1, 2, 3, 4,
|
- |
|
Contains 5: False
|
|
}}
|
}}
|
|
#column-end
|
#column-end
|
|
|
|
+ |
#remarks
|
|
+ |
|
|
+ |
[[Reverseメソッド>#SortedSet.Reverse]]を使うと、逆順(値の大きい順)で列挙することができます。
|
|
+ |
|
|
+ |
#remarks-end
|
|
+ |
|
|
|
|
|
|
|
|
~ |
**重複する要素の追加 [#HashSetSortedSet_basic_operations_duplicate_value]
|
一方、HashSet・SortedSetでは同じ要素を複数回格納しようとしても重複して格納されることはなく、常に1つだけが格納されます。 この点はDictionaryのキーと似た動作と言えますが、重複していても例外がスローされることはありません。
|
+ |
HashSet・SortedSetでは同じ値の要素を複数回格納しようとしても重複して格納されることはなく、常に1つだけが格納されます。 この点はDictionaryのキーと似た動作と言えますが、すでに存在する状態で追加しようとしても例外がスローされることはありません。
|
|
|
|
|
|
HashSet・SortedSetともに同じ要素が重複した状態になることはありませんが、HashSetでは格納した時の順序は維持されます(最初に格納された順になります)。
|
HashSet・SortedSetともに同じ要素が重複した状態になることはありませんが、HashSetでは格納した時の順序は維持されます(最初に格納された順になります)。
|
|
|
|
|
313,10 |
202,9 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new HashSet<int>();
|
HashSet<int> s = new HashSet<int>();
|
|
|
|
|
// 同じ要素を複数回追加
|
// 同じ要素を複数回追加
|
+ |
// (同じ値がすでに存在する場合は、追加されない)
|
|
|
s.Add(3);
|
s.Add(3);
|
|
s.Add(3);
|
s.Add(3);
|
|
s.Add(1);
|
s.Add(1);
|
|
324,7 |
212,16 |
|
s.Add(2);
|
s.Add(2);
|
|
s.Add(3);
|
s.Add(3);
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s));
|
Print(s);
|
- |
|
}
|
- |
|
|
- |
|
static void Print(HashSet<int> s)
|
- |
|
{
|
- |
|
foreach (int e in s) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
- |
|
|
- |
|
Console.WriteLine();
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
338,7 |
235,6 |
|
Dim s As New HashSet(Of Integer)()
|
Dim s As New HashSet(Of Integer)()
|
|
|
|
|
' 同じ要素を複数回追加
|
' 同じ要素を複数回追加
|
+ |
' (同じ値がすでに存在する場合は、追加されない)
|
|
|
s.Add(3)
|
s.Add(3)
|
|
s.Add(3)
|
s.Add(3)
|
|
s.Add(1)
|
s.Add(1)
|
|
346,14 |
242,22 |
|
s.Add(2)
|
s.Add(2)
|
|
s.Add(3)
|
s.Add(3)
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s))
|
Print(s)
|
- |
|
End Sub
|
- |
|
|
- |
|
Shared Sub Print(ByVal s As HashSet(Of Integer))
|
- |
|
For Each e As Integer In s
|
- |
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
- |
|
|
- |
|
Console.WriteLine()
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
3, 1, 2
|
3, 1, 2,
|
|
}}
|
}}
|
|
#column(SortedSet)
|
#column(SortedSet)
|
|
#tabpage(codelang=cs,container-title=SortedSetに重複する要素を追加する)
|
#tabpage(codelang=cs,container-title=SortedSetに重複する要素を追加する)
|
|
364,10 |
268,9 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new SortedSet<int>();
|
SortedSet<int> s = new SortedSet<int>();
|
|
|
|
|
// 同じ要素を複数回追加
|
// 同じ要素を複数回追加
|
+ |
// (同じ値がすでに存在する場合は、追加されない)
|
|
|
s.Add(3);
|
s.Add(3);
|
|
s.Add(3);
|
s.Add(3);
|
|
s.Add(1);
|
s.Add(1);
|
|
375,7 |
278,16 |
|
s.Add(2);
|
s.Add(2);
|
|
s.Add(3);
|
s.Add(3);
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s));
|
Print(s);
|
- |
|
}
|
- |
|
|
- |
|
static void Print(SortedSet<int> s)
|
- |
|
{
|
- |
|
foreach (int e in s) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
- |
|
|
- |
|
Console.WriteLine();
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
389,7 |
301,6 |
|
Dim s As New SortedSet(Of Integer)()
|
Dim s As New SortedSet(Of Integer)()
|
|
|
|
|
' 同じ要素を複数回追加
|
' 同じ要素を複数回追加
|
+ |
' (同じ値がすでに存在する場合は、追加されない)
|
|
|
s.Add(3)
|
s.Add(3)
|
|
s.Add(3)
|
s.Add(3)
|
|
s.Add(1)
|
s.Add(1)
|
|
397,26 |
308,40 |
|
s.Add(2)
|
s.Add(2)
|
|
s.Add(3)
|
s.Add(3)
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s))
|
Print(s)
|
- |
|
End Sub
|
- |
|
|
- |
|
Shared Sub Print(ByVal s As SortedSet(Of Integer))
|
- |
|
For Each e As Integer In s
|
- |
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
- |
|
|
- |
|
Console.WriteLine()
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
1, 2, 3
|
1, 2, 3,
|
|
}}
|
}}
|
|
#column-end
|
#column-end
|
|
|
|
|
|
|
- |
|
*集合演算
|
- |
|
HashSetクラス・SortedSetクラスには集合演算を行うメソッドとして次のメソッドが用意されています。 いずれも引数としてIEnumerable<T>を取り、戻り値はありません(void)。 したがって、これらのメソッドでは引数に指定された配列やコレクションなどとの演算結果をインスタンス自身に反映します。
|
|
|
|
~ |
**要素取得の試行・実際の値の取得 (TryGetValue) [#HashSetSortedSet_TryGetValue]
|
|*集合演算を行うメソッド
|
~ |
HashSet・SortedSetに特定の要素が含まれているかどうかを調べるには[[Containsメソッド>#HashSetSortedSet_basic_operations]]を使うことができます。 このほかに、.NET Standard 2.1/.NET Framework 4.7.2/.NET Core 2.0以降では、&msdn(netfx,member,System.Collections.Generic.HashSet`1.TryGetValue){TryGetValue};メソッドを使うこともできます。
|
|~メソッド|~動作|h
|
- |
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.IntersectWith){HashSet.IntersectWith};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.IntersectWith){SortedSet.IntersectWith};|現在の集合と引数で指定されたIEnumerable<T>との積集合を求める&br;(引数の集合と''共通する要素のみを残す'')|
|
- |
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.UnionWith){HashSet.UnionWith};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.UnionWith){SortedSet.UnionWith};|現在の集合と引数で指定されたIEnumerable<T>との和集合を求める&br;(引数の集合の要素を''マージする'')|
|
- |
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.ExceptWith){HashSet.ExceptWith};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.ExceptWith){SortedSet.ExceptWith};|現在の集合と引数で指定されたIEnumerable<T>との差集合を求める&br;(引数の集合の要素を''除外する'')|
|
- |
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.SymmetricExceptWith){HashSet.SymmetricExceptWith};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.SymmetricExceptWith){SortedSet.SymmetricExceptWith};|現在の集合と引数で指定されたIEnumerable<T>との対称差を求める&br;(引数の集合と''共通する要素のみを除外する'')|
|
|
|
|
~ |
TryGetValueメソッドでは、HashSet・SortedSetに指定した値の要素があるかどうかを調べ、ある場合はその値を取得することができます。
|
以下は、上記の集合演算を使った例です。
|
|
|
|
|
#column(layout=fixed,HashSet)
|
#column(layout=fixed,HashSet)
|
~ |
#tabpage(codelang=cs,container-title=Contains・TryGetValueメソッドでHashSetに指定した値の要素があるかどうか調べる)
|
#tabpage(codelang=cs,container-title=HashSetで集合演算を行う)
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
|
424,148 |
349,45 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new HashSet<int>() {3, 1, 6, 4, 0};
|
int[] set1 = new int[] {6, 2, 0, 4, 8};
|
~ |
|
int[] set2 = new int[] {3, 1, 2, 0, 4};
|
+ |
// 値4が含まれているかContainsメソッドで調べる
|
|
+ |
Console.WriteLine(
|
|
+ |
"Contains 4? {0}",
|
|
+ |
s.Contains(4)
|
|
+ |
);
|
|
+ |
|
|
+ |
// 値4が含まれているかTryGetValueメソッドで調べる
|
|
+ |
// (含まれている場合、その値をoutパラメータで取得することもできる)
|
|
+ |
Console.WriteLine(
|
|
+ |
"TryGetValue 4? {0}",
|
|
+ |
s.TryGetValue(4, out var val)
|
|
+ |
);
|
|
+ |
}
|
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
|
|
|
~ |
Class Sample
|
// 積集合を求める
|
~ |
Shared Sub Main()
|
HashSet<int> s1 = new HashSet<int>(set1);
|
+ |
Dim s As New HashSet(Of Integer) From {3, 1, 6, 4, 0}
|
|
+ |
|
|
+ |
' 値4が含まれているかContainsメソッドで調べる
|
|
+ |
Console.WriteLine(
|
|
+ |
"Contains 4? {0}",
|
|
+ |
s.Contains(4)
|
|
+ |
)
|
|
+ |
|
|
+ |
' 値4が含まれているかTryGetValueメソッドで調べる
|
|
+ |
' (含まれている場合、その値をoutパラメータで取得することもできる)
|
|
+ |
Dim val As Integer
|
|
+ |
|
|
+ |
Console.WriteLine(
|
|
+ |
"TryGetValue 4? {0}",
|
|
+ |
s.TryGetValue(4, val)
|
|
+ |
)
|
|
+ |
End Sub
|
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
|
|
|
~ |
#prompt(実行結果){{
|
s1.IntersectWith(set2);
|
+ |
Contains 4? True
|
|
+ |
TryGetValue 4? True
|
|
+ |
}}
|
|
+ |
#column(SortedSet)
|
|
+ |
#tabpage(codelang=cs,container-title=Contains・TryGetValueメソッドでSortedSetに指定した値の要素があるかどうか調べる)
|
|
+ |
#code{{
|
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
|
|
|
~ |
class Sample {
|
Print(s1);
|
+ |
static void Main()
|
|
+ |
{
|
|
+ |
var s = new SortedSet<int>() {3, 1, 6, 4, 0};
|
|
+ |
|
|
+ |
// 値4が含まれているかContainsメソッドで調べる
|
|
+ |
Console.WriteLine(
|
|
+ |
"Contains 4? {0}",
|
|
+ |
s.Contains(4)
|
|
+ |
);
|
|
+ |
|
|
+ |
// 値4が含まれているかTryGetValueメソッドで調べる
|
|
+ |
// (含まれている場合、その値をoutパラメータで取得することもできる)
|
|
+ |
Console.WriteLine(
|
|
+ |
"TryGetValue 4? {0}",
|
|
+ |
s.TryGetValue(4, out var val)
|
|
+ |
);
|
|
+ |
}
|
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
|
|
|
~ |
Class Sample
|
// 和集合を求める
|
~ |
Shared Sub Main()
|
HashSet<int> s2 = new HashSet<int>(set1);
|
+ |
Dim s As New SortedSet(Of Integer) From {3, 1, 6, 4, 0}
|
|
+ |
|
|
+ |
' 値4が含まれているかContainsメソッドで調べる
|
|
+ |
Console.WriteLine(
|
|
+ |
"Contains 4? {0}",
|
|
+ |
s.Contains(4)
|
|
+ |
)
|
|
+ |
|
|
+ |
' 値4が含まれているかTryGetValueメソッドで調べる
|
|
+ |
' (含まれている場合、その値をoutパラメータで取得することもできる)
|
|
+ |
Dim val As Integer
|
|
+ |
|
|
+ |
Console.WriteLine(
|
|
+ |
"TryGetValue 4? {0}",
|
|
+ |
s.TryGetValue(4, val)
|
|
+ |
)
|
|
+ |
End Sub
|
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
|
|
|
~ |
#prompt(実行結果){{
|
s2.UnionWith(set2);
|
+ |
Contains 4? True
|
|
+ |
TryGetValue 4? True
|
|
+ |
}}
|
|
+ |
#column-end
|
|
|
|
|
~ |
ContainsメソッドとTryGetValueメソッドは、どちらもHashSet・SortedSet内に指定した値の要素があるかどうかを調べるという点では同じです。 一方TryGetValueメソッドは、要素がある場合には実際にHashSet・SortedSet内に格納されている値を取得できる点が異なります。
|
Print(s2);
|
|
|
|
~ |
TryGetValueメソッドは、[[IEqualtyComparer<T>を指定してHashSetを構築した場合>#EqualityComparison]]、または[[IComparer<T>を指定してSortedSetを構築した場合>#Comparison]]などにおいて、単に値が含まれているかどうかだけでなく、''実際に格納されている値''を知りたい場合に有効に機能します。
|
// 差集合を求める
|
- |
|
HashSet<int> s3 = new HashSet<int>(set1);
|
|
|
|
~ |
例えば、大文字小文字の違いを無視するHashSet・SortedSetを構築したときに、HashSet・SortedSetに``ALICE``という値が含まれているか調べつつ、実際に格納されている値が何なのか(``Alice``または``alice``、もしくはそれ以外なのか)を知りたい、といった場合には、ContainsメソッドではなくTryGetValueメソッドを使うことができます。
|
s3.ExceptWith(set2);
|
|
|
|
~ |
#column(layout=fixed,HashSet)
|
Print(s3);
|
+ |
#tabpage(codelang=cs,container-title=TryGetValueメソッドでHashSetに指定した値の要素があるかどうか調べ、実際に格納されている値を取得する)
|
|
+ |
#code{{
|
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
|
|
|
~ |
class Sample {
|
// 対象差を求める
|
~ |
static void Main()
|
HashSet<int> s4 = new HashSet<int>(set1);
|
+ |
{
|
|
+ |
// 大文字小文字の違いを無視するHashSet
|
|
+ |
var caseInsensitiveSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
|
|
+ |
"Alice", "Bob", "Charlie", "Dave", "Eve"
|
|
+ |
};
|
|
|
|
|
~ |
// 文字列"ALICE"が含まれているかどうか調べる
|
s4.SymmetricExceptWith(set2);
|
+ |
// 同時にHashSetに格納されている実際の綴りも取得する
|
|
+ |
var alice = "ALICE";
|
|
|
|
|
~ |
if (caseInsensitiveSet.TryGetValue(alice, out alice))
|
Print(s4);
|
~ |
Console.WriteLine(alice);
|
}
|
|
|
|
~ |
// 文字列"cHArLIe"が含まれているかどうか調べる
|
static void Print(HashSet<int> s)
|
~ |
// 同時にHashSetに格納されている実際の綴りも取得する
|
{
|
~ |
var charlie = "cHArLIe";
|
foreach (int e in s) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
|
|
|
~ |
if (caseInsensitiveSet.TryGetValue(charlie, out charlie))
|
Console.WriteLine();
|
+ |
Console.WriteLine(charlie);
|
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
576,125 |
398,57 |
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
' 大文字小文字の違いを無視するHashSet
|
Dim set1() As Integer = New Integer() {6, 2, 0, 4, 8}
|
~ |
Dim caseInsensitiveSet As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase) From {
|
Dim set2() As Integer = New Integer() {3, 1, 2, 0, 4}
|
~ |
"Alice", "Bob", "Charlie", "Dave", "Eve"
|
|
~ |
}
|
' 積集合を求める
|
- |
|
Dim s1 As New HashSet(Of Integer)(set1)
|
|
|
|
~ |
' 文字列"ALICE"が含まれているかどうか調べる
|
s1.IntersectWith(set2)
|
+ |
' 同時にHashSetに格納されている実際の綴りも取得する
|
|
+ |
Dim alice As String = "ALICE"
|
|
|
|
|
~ |
If caseInsensitiveSet.TryGetValue(alice, alice) Then
|
Print(s1)
|
+ |
Console.WriteLine(alice)
|
|
+ |
End If
|
|
|
|
|
~ |
' 文字列"cHArLIe"が含まれているかどうか調べる
|
' 和集合を求める
|
~ |
' 同時にHashSetに格納されている実際の綴りも取得する
|
Dim s2 As New HashSet(Of Integer)(set1)
|
+ |
Dim charlie As String = "cHArLIe"
|
|
|
|
|
~ |
If caseInsensitiveSet.TryGetValue(charlie, charlie) Then
|
s2.UnionWith(set2)
|
+ |
Console.WriteLine(charlie)
|
|
+ |
End If
|
|
+ |
End Sub
|
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
|
|
|
~ |
#prompt(実行結果){{
|
Print(s2)
|
+ |
Alice
|
|
+ |
Charlie
|
|
+ |
}}
|
|
+ |
#column(SortedSet)
|
|
+ |
#tabpage(codelang=cs,container-title=TryGetValueメソッドでSortedSetに指定した値の要素があるかどうか調べ、実際に格納されている値を取得する)
|
|
+ |
#code{{
|
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
|
|
|
~ |
class Sample {
|
' 差集合を求める
|
~ |
static void Main()
|
Dim s3 As New HashSet(Of Integer)(set1)
|
+ |
{
|
|
+ |
// 大文字小文字の違いを無視するSortedSet
|
|
+ |
var caseInsensitiveSet = new SortedSet<string>(StringComparer.OrdinalIgnoreCase) {
|
|
+ |
"Alice", "Bob", "Charlie", "Dave", "Eve"
|
|
+ |
};
|
|
|
|
|
~ |
// 文字列"ALICE"が含まれているかどうか調べる
|
s3.ExceptWith(set2)
|
+ |
// 同時にSortedSetに格納されている実際の綴りも取得する
|
|
+ |
var alice = "ALICE";
|
|
|
|
|
~ |
if (caseInsensitiveSet.TryGetValue(alice, out alice))
|
Print(s3)
|
+ |
Console.WriteLine(alice);
|
|
|
|
|
~ |
// 文字列"cHArLIe"が含まれているかどうか調べる
|
' 対象差を求める
|
~ |
// 同時にSortedSetに格納されている実際の綴りも取得する
|
Dim s4 As New HashSet(Of Integer)(set1)
|
+ |
var charlie = "cHArLIe";
|
|
|
|
|
~ |
if (caseInsensitiveSet.TryGetValue(charlie, out charlie))
|
s4.SymmetricExceptWith(set2)
|
+ |
Console.WriteLine(charlie);
|
|
+ |
}
|
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
|
|
|
~ |
Class Sample
|
Print(s4)
|
~ |
Shared Sub Main()
|
End Sub
|
+ |
' 大文字小文字の違いを無視するSortedSet
|
|
+ |
Dim caseInsensitiveSet As New SortedSet(Of String)(StringComparer.OrdinalIgnoreCase) From {
|
|
+ |
"Alice", "Bob", "Charlie", "Dave", "Eve"
|
|
+ |
}
|
|
|
|
|
~ |
' 文字列"ALICE"が含まれているかどうか調べる
|
Shared Sub Print(ByVal s As HashSet(Of Integer))
|
~ |
' 同時にSortedSetに格納されている実際の綴りも取得する
|
For Each e As Integer In s
|
~ |
Dim alice As String = "ALICE"
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
|
|
|
~ |
If caseInsensitiveSet.TryGetValue(alice, alice) Then
|
Console.WriteLine()
|
+ |
Console.WriteLine(alice)
|
|
+ |
End If
|
|
+ |
|
|
+ |
' 文字列"cHArLIe"が含まれているかどうか調べる
|
|
+ |
' 同時にSortedSetに格納されている実際の綴りも取得する
|
|
+ |
Dim charlie As String = "cHArLIe"
|
|
+ |
|
|
+ |
If caseInsensitiveSet.TryGetValue(charlie, charlie) Then
|
|
+ |
Console.WriteLine(charlie)
|
|
+ |
End If
|
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
Alice
|
2, 0, 4,
|
~ |
Charlie
|
6, 2, 0, 4, 8, 3, 1,
|
- |
|
6, 8,
|
- |
|
6, 8, 3, 1,
|
|
}}
|
}}
|
~ |
#column-end
|
#column(SortedSet)
|
~ |
|
#tabpage(codelang=cs,container-title=SortedSetで集合演算を行う)
|
+ |
#remarks
|
|
+ |
|
|
+ |
HashSetにIEqualtyComparer<T>を指定して構築する例については[[#EqualityComparison]]、SortedSetにIComparer<T>を指定して構築する例については[[#Comparison]]を参照してください。
|
|
+ |
|
|
+ |
#remarks-end
|
|
+ |
|
|
+ |
|
|
+ |
|
|
+ |
*集合演算 [#HashSetSortedSet_calculating_sets]
|
|
+ |
HashSetクラス・SortedSetクラスには集合演算を行うメソッドとして次のメソッドが用意されています。 いずれも引数としてIEnumerable<T>を取り、戻り値はありません(void)。 したがって、これらのメソッドでは引数に指定された配列やコレクションなどとの演算結果をインスタンス自身に反映します。
|
|
+ |
|
|
+ |
|*集合演算を行うメソッド
|
|
+ |
|~メソッド|~動作|h
|
|
+ |
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.IntersectWith){HashSet.IntersectWith};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.IntersectWith){SortedSet.IntersectWith};|現在の集合と引数で指定されたIEnumerable<T>との積集合を求める&br;(引数の集合と''共通する要素のみを残す'')|
|
|
+ |
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.UnionWith){HashSet.UnionWith};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.UnionWith){SortedSet.UnionWith};|現在の集合と引数で指定されたIEnumerable<T>との和集合を求める&br;(引数の集合の要素を''マージする'')|
|
|
+ |
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.ExceptWith){HashSet.ExceptWith};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.ExceptWith){SortedSet.ExceptWith};|現在の集合と引数で指定されたIEnumerable<T>との差集合を求める&br;(引数の集合の要素を''除外する'')|
|
|
+ |
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.SymmetricExceptWith){HashSet.SymmetricExceptWith};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.SymmetricExceptWith){SortedSet.SymmetricExceptWith};|現在の集合と引数で指定されたIEnumerable<T>との対称差を求める&br;(引数の集合と''共通する要素のみを除外する'')|
|
|
+ |
|
|
+ |
以下は、上記の集合演算を使った例です。
|
|
+ |
|
|
+ |
#column(layout=fixed,HashSet)
|
|
+ |
#tabpage(codelang=cs,container-title=HashSetで積集合・和集合・差集合・対象差を求める)
|
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
|
702,126 |
456,45 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var set1 = new int[] {6, 2, 0, 4, 8};
|
int[] set1 = new int[] {6, 2, 0, 4, 8};
|
~ |
var set2 = new int[] {3, 1, 2, 0, 4};
|
int[] set2 = new int[] {3, 1, 2, 0, 4};
|
|
|
|
~ |
// 積集合を求める(IntersectWith)
|
// 積集合を求める
|
~ |
var s1 = new HashSet<int>(set1);
|
SortedSet<int> s1 = new SortedSet<int>(set1);
|
|
|
|
|
s1.IntersectWith(set2);
|
s1.IntersectWith(set2);
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s1));
|
Print(s1);
|
|
|
|
~ |
// 和集合を求める(UnionWith)
|
// 和集合を求める
|
~ |
var s2 = new HashSet<int>(set1);
|
SortedSet<int> s2 = new SortedSet<int>(set1);
|
|
|
|
|
s2.UnionWith(set2);
|
s2.UnionWith(set2);
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s2));
|
Print(s2);
|
|
|
|
~ |
// 差集合を求める(ExceptWith)
|
// 差集合を求める
|
~ |
var s3 = new HashSet<int>(set1);
|
SortedSet<int> s3 = new SortedSet<int>(set1);
|
|
|
|
|
s3.ExceptWith(set2);
|
s3.ExceptWith(set2);
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s3));
|
Print(s3);
|
|
|
|
~ |
// 対象差を求める(SymmetricExceptWith)
|
// 対象差を求める
|
~ |
var s4 = new HashSet<int>(set1);
|
SortedSet<int> s4 = new SortedSet<int>(set1);
|
|
|
|
|
s4.SymmetricExceptWith(set2);
|
s4.SymmetricExceptWith(set2);
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s4));
|
Print(s4);
|
|
}
|
}
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
|
|
|
~ |
Class Sample
|
static void Print(SortedSet<int> s)
|
+ |
Shared Sub Main()
|
|
+ |
Dim set1() As Integer = New Integer() {6, 2, 0, 4, 8}
|
|
+ |
Dim set2() As Integer = New Integer() {3, 1, 2, 0, 4}
|
|
+ |
|
|
+ |
' 積集合を求める(IntersectWith)
|
|
+ |
Dim s1 As New HashSet(Of Integer)(set1)
|
|
+ |
|
|
+ |
s1.IntersectWith(set2)
|
|
+ |
|
|
+ |
Console.WriteLine(String.Join(", ", s1))
|
|
+ |
|
|
+ |
' 和集合を求める(UnionWith)
|
|
+ |
Dim s2 As New HashSet(Of Integer)(set1)
|
|
+ |
|
|
+ |
s2.UnionWith(set2)
|
|
+ |
|
|
+ |
Console.WriteLine(String.Join(", ", s2))
|
|
+ |
|
|
+ |
' 差集合を求める(ExceptWith)
|
|
+ |
Dim s3 As New HashSet(Of Integer)(set1)
|
|
+ |
|
|
+ |
s3.ExceptWith(set2)
|
|
+ |
|
|
+ |
Console.WriteLine(String.Join(", ", s3))
|
|
+ |
|
|
+ |
' 対象差を求める(SymmetricExceptWith)
|
|
+ |
Dim s4 As New HashSet(Of Integer)(set1)
|
|
+ |
|
|
+ |
s4.SymmetricExceptWith(set2)
|
|
+ |
|
|
+ |
Console.WriteLine(String.Join(", ", s4))
|
|
+ |
End Sub
|
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
+ |
|
|
+ |
#prompt(実行結果){{
|
|
+ |
2, 0, 4
|
|
+ |
6, 2, 0, 4, 8, 3, 1
|
|
+ |
6, 8
|
|
+ |
6, 8, 3, 1
|
|
+ |
}}
|
|
+ |
#column(SortedSet)
|
|
+ |
#tabpage(codelang=cs,container-title=SortedSetで積集合・和集合・差集合・対象差を求める)
|
|
+ |
#code{{
|
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
+ |
|
|
+ |
class Sample {
|
|
+ |
static void Main()
|
|
|
{
|
{
|
~ |
var set1 = new int[] {6, 2, 0, 4, 8};
|
foreach (int e in s) {
|
~ |
var set2 = new int[] {3, 1, 2, 0, 4};
|
Console.Write("{0}, ", e);
|
- |
|
}
|
|
|
|
~ |
// 積集合を求める(IntersectWith)
|
Console.WriteLine();
|
+ |
var s1 = new SortedSet<int>(set1);
|
|
+ |
|
|
+ |
s1.IntersectWith(set2);
|
|
+ |
|
|
+ |
Console.WriteLine(string.Join(", ", s1));
|
|
+ |
|
|
+ |
// 和集合を求める(UnionWith)
|
|
+ |
var s2 = new SortedSet<int>(set1);
|
|
+ |
|
|
+ |
s2.UnionWith(set2);
|
|
+ |
|
|
+ |
Console.WriteLine(string.Join(", ", s2));
|
|
+ |
|
|
+ |
// 差集合を求める(ExceptWith)
|
|
+ |
var s3 = new SortedSet<int>(set1);
|
|
+ |
|
|
+ |
s3.ExceptWith(set2);
|
|
+ |
|
|
+ |
Console.WriteLine(string.Join(", ", s3));
|
|
+ |
|
|
+ |
// 対象差を求める(SymmetricExceptWith)
|
|
+ |
var s4 = new SortedSet<int>(set1);
|
|
+ |
|
|
+ |
s4.SymmetricExceptWith(set2);
|
|
+ |
|
|
+ |
Console.WriteLine(string.Join(", ", s4));
|
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
835,72 |
508,71 |
|
Dim set1() As Integer = New Integer() {6, 2, 0, 4, 8}
|
Dim set1() As Integer = New Integer() {6, 2, 0, 4, 8}
|
|
Dim set2() As Integer = New Integer() {3, 1, 2, 0, 4}
|
Dim set2() As Integer = New Integer() {3, 1, 2, 0, 4}
|
|
|
|
~ |
' 積集合を求める(IntersectWith)
|
' 積集合を求める
|
|
Dim s1 As New SortedSet(Of Integer)(set1)
|
Dim s1 As New SortedSet(Of Integer)(set1)
|
|
|
|
|
s1.IntersectWith(set2)
|
s1.IntersectWith(set2)
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s1))
|
Print(s1)
|
|
|
|
~ |
' 和集合を求める(UnionWith)
|
' 和集合を求める
|
|
Dim s2 As New SortedSet(Of Integer)(set1)
|
Dim s2 As New SortedSet(Of Integer)(set1)
|
|
|
|
|
s2.UnionWith(set2)
|
s2.UnionWith(set2)
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s2))
|
Print(s2)
|
|
|
|
~ |
' 差集合を求める(ExceptWith)
|
' 差集合を求める
|
|
Dim s3 As New SortedSet(Of Integer)(set1)
|
Dim s3 As New SortedSet(Of Integer)(set1)
|
|
|
|
|
s3.ExceptWith(set2)
|
s3.ExceptWith(set2)
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s3))
|
Print(s3)
|
|
|
|
~ |
' 対象差を求める(SymmetricExceptWith)
|
' 対象差を求める
|
|
Dim s4 As New SortedSet(Of Integer)(set1)
|
Dim s4 As New SortedSet(Of Integer)(set1)
|
|
|
|
|
s4.SymmetricExceptWith(set2)
|
s4.SymmetricExceptWith(set2)
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s4))
|
Print(s4)
|
- |
|
End Sub
|
- |
|
|
- |
|
Shared Sub Print(ByVal s As SortedSet(Of Integer))
|
- |
|
For Each e As Integer In s
|
- |
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
- |
|
|
- |
|
Console.WriteLine()
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
0, 2, 4
|
0, 2, 4,
|
~ |
0, 1, 2, 3, 4, 6, 8
|
0, 1, 2, 3, 4, 6, 8,
|
~ |
6, 8
|
6, 8,
|
~ |
1, 3, 6, 8
|
1, 3, 6, 8,
|
|
}}
|
}}
|
|
#column-end
|
#column-end
|
|
|
|
~ |
SortedSetで並べ替えが行われる以外は、結果は同じです。
|
SortedSetで並べ替えが行われる以外は、結果は同じです。 以下の図は、実行結果を図式化したものです。
|
+ |
|
|
+ |
#remarks
|
|
+ |
|
|
+ |
以下の図は、上記のHashSet・SortedSetの集合演算メソッドの実行結果を図式化したものです。
|
|
|
|
|
~ |
#svg(type=flow,shrink,title=IntersectWithの結果,source=set_calculation_intersect.svg,fallback-image=set_calculation_intersect.png)
|
#image(title=HashSet・SortedSetの集合演算メソッドと結果,source1=set_operation_intersectwith.png,title1=IntersectWithの結果,source2=set_operation_unionwith.png,title2=UnionWithの結果,source3=set_operation_exceptwith.png,title3=ExceptWithの結果,source4=set_operation_symmetricexceptwith.png,title4=SymmetricExceptWithの結果)
|
+ |
#svg(type=flow,shrink,title=UnionWithの結果,source=set_calculation_union.svg,fallback-image=set_calculation_union.png)
|
|
+ |
#svg(type=flow,shrink,title=ExceptWithの結果,source=set_calculation_except.svg,fallback-image=set_calculation_except.png)
|
|
+ |
#svg(type=flow,shrink,title=SymmetricExceptWithの結果,source=set_calculation_symmetric_except.svg,fallback-image=set_calculation_symmetric_except.png)
|
|
+ |
|
|
+ |
#remarks-end
|
|
|
|
|
|
**LINQの拡張メソッドを使った集合演算
|
**LINQの拡張メソッドを使った集合演算
|
~ |
LINQの拡張メソッドを使うことでも、[[HashSet・SortedSetと同様の集合演算>#HashSetSortedSet_calculating_sets]]を行うことができます。 それぞれ対応するメソッドは次のとおりです。
|
LINQの拡張メソッドを使うことでも集合演算を行うことはできます。 それぞれ対応するメソッドは次のとおりです。
|
|
|
|
~ |
|*HashSet/SortedSetの集合演算メソッドと、対応するLINQの拡張メソッド
|
|*対応するLINQの拡張メソッド
|
|
|~演算|~HashSet/SortedSetのメソッド|~LINQの拡張メソッド|h
|
|~演算|~HashSet/SortedSetのメソッド|~LINQの拡張メソッド|h
|
~ |
|~積集合|&msdn(netfx,member,System.Collections.Generic.HashSet`1.IntersectWith){IntersectWith};|&msdn(netfx,member,System.Linq.Enumerable.Intersect){Intersect};|
|
|~積集合|IntersectWith|&msdn(netfx,member,System.Linq.Enumerable.Intersect){Intersect};|
|
~ |
|~和集合|&msdn(netfx,member,System.Collections.Generic.HashSet`1.UnionWith){UnionWith};|&msdn(netfx,member,System.Linq.Enumerable.Union){Union};|
|
|~和集合|UnionWith|&msdn(netfx,member,System.Linq.Enumerable.Union){Union};|
|
~ |
|~差集合|&msdn(netfx,member,System.Collections.Generic.HashSet`1.ExceptWith){ExceptWith};|&msdn(netfx,member,System.Linq.Enumerable.Except){Except};|
|
|~差集合|ExceptWith|&msdn(netfx,member,System.Linq.Enumerable.Except){Except};|
|
~ |
|~対象差|&msdn(netfx,member,System.Collections.Generic.HashSet`1.SymmetricExceptWith){SymmetricExceptWith};|-|
|
|~対象差|SymmetricExceptWith|-|
|
|
|
|
|
以下の例はこれらのメソッドを使ってHashSet・SortedSetと同様の集合演算を行う例です。 LINQでは、SymmetricExceptWithに相当するような対象差を求めるメソッドは直接提供されませんが、以下の例のように差集合同士の和集合を求めることで対象差を求めることができます。
|
以下の例はこれらのメソッドを使ってHashSet・SortedSetと同様の集合演算を行う例です。 LINQでは、SymmetricExceptWithに相当するような対象差を求めるメソッドは直接提供されませんが、以下の例のように差集合同士の和集合を求めることで対象差を求めることができます。
|
|
|
|
~ |
#tabpage(codelang=cs,container-title=LINQの拡張メソッドを使って配列同士の積集合・和集合・差集合・対象差を求める)
|
#tabpage(codelang=cs,container-title=LINQの拡張メソッドを使って集合演算を行う)
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
|
909,23 |
581,32 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var set1 = new int[] {6, 2, 0, 4, 8};
|
int[] set1 = new int[] {6, 2, 0, 4, 8};
|
~ |
var set2 = new int[] {3, 1, 2, 0, 4};
|
int[] set2 = new int[] {3, 1, 2, 0, 4};
|
|
|
|
|
// 積集合を求める
|
// 積集合を求める
|
~ |
Console.WriteLine(string.Join(", ", set1.Intersect(set2)));
|
Print(set1.Intersect(set2));
|
|
|
|
|
// 和集合を求める
|
// 和集合を求める
|
~ |
Console.WriteLine(string.Join(", ", set1.Union(set2)));
|
Print(set1.Union(set2));
|
|
|
|
|
// 差集合を求める
|
// 差集合を求める
|
~ |
Console.WriteLine(string.Join(", ", set1.Except(set2)));
|
Print(set1.Except(set2));
|
|
|
|
|
// 対象差を求める
|
// 対象差を求める
|
|
var diffset1 = set1.Except(set2);
|
var diffset1 = set1.Except(set2);
|
|
var diffset2 = set2.Except(set1);
|
var diffset2 = set2.Except(set1);
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", diffset1.Union(diffset2)));
|
Print(diffset1.Union(diffset2));
|
- |
|
}
|
- |
|
|
- |
|
static void Print(IEnumerable<int> s)
|
- |
|
{
|
- |
|
foreach (int e in s) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
- |
|
|
- |
|
Console.WriteLine();
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
941,19 |
622,27 |
|
Dim set2() As Integer = New Integer() {3, 1, 2, 0, 4}
|
Dim set2() As Integer = New Integer() {3, 1, 2, 0, 4}
|
|
|
|
|
' 積集合を求める
|
' 積集合を求める
|
~ |
Console.WriteLine(String.Join(", ", set1.Intersect(set2)))
|
Print(set1.Intersect(set2))
|
|
|
|
|
' 和集合を求める
|
' 和集合を求める
|
~ |
Console.WriteLine(String.Join(", ", set1.Union(set2)))
|
Print(set1.Union(set2))
|
|
|
|
|
' 差集合を求める
|
' 差集合を求める
|
~ |
Console.WriteLine(String.Join(", ", set1.Except(set2)))
|
Print(set1.Except(set2))
|
|
|
|
|
' 対象差を求める
|
' 対象差を求める
|
|
Dim diffset1 As IEnumerable(Of Integer) = set1.Except(set2)
|
Dim diffset1 As IEnumerable(Of Integer) = set1.Except(set2)
|
|
Dim diffset2 As IEnumerable(Of Integer) = set2.Except(set1)
|
Dim diffset2 As IEnumerable(Of Integer) = set2.Except(set1)
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", diffset1.Union(diffset2)))
|
Print(diffset1.Union(diffset2))
|
- |
|
End Sub
|
- |
|
|
- |
|
Shared Sub Print(ByVal s As IEnumerable(Of Integer))
|
- |
|
For Each e As Integer In s
|
- |
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
- |
|
|
- |
|
Console.WriteLine()
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
967,22 |
656,22 |
|
}}
|
}}
|
|
|
|
|
|
|
~ |
*包含関係の検証 [#HashSetSortedSet_algebra_of_sets]
|
*包含関係の検証
|
|
HashSetクラス・SortedSetクラスには包含関係の検証を行うメソッドとして次のメソッドが用意されています。 いずれも引数としてIEnumerable<T>を取り、戻り値は真偽値(boolean)です。 なお、要素の並びが異なっていても包含関係には影響しません。 これらのメソッドでは順序にかかわらずどのような要素が含まれているかどうかのみが検証されます。
|
HashSetクラス・SortedSetクラスには包含関係の検証を行うメソッドとして次のメソッドが用意されています。 いずれも引数としてIEnumerable<T>を取り、戻り値は真偽値(boolean)です。 なお、要素の並びが異なっていても包含関係には影響しません。 これらのメソッドでは順序にかかわらずどのような要素が含まれているかどうかのみが検証されます。
|
|
|
|
|
|*包含関係の検証を行うメソッド
|
|*包含関係の検証を行うメソッド
|
|
|~メソッド|~動作|h
|
|~メソッド|~動作|h
|
~ |
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.SetEquals){HashSet.SetEquals};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.SetEquals){SortedSet.SetEquals};|現在の集合と引数で指定されたIEnumerable<T>が等しいかどうか調べる&br;(現在の集合と引数の集合が''すべて同じ要素で構成されている''場合はtrue)|
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.SetEquals){HashSet.SetEquals};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.SetEquals){SortedSet.SetEquals};|現在の集合と引数で指定されたIEnumerable<T>が等しいかどうか調べる&br;(現在の集合と引数の集合が重複を無視して''同じ要素で構成されている''場合はtrue)|
|
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.Overlaps){HashSet.Overlaps};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.Overlaps){SortedSet.Overlaps};|現在の集合と引数で指定されたIEnumerable<T>が共通する要素を持つかどうか調べる&br;(現在の集合と引数の集合が''一つでも同じ要素を持つ''場合はtrue)|
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.Overlaps){HashSet.Overlaps};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.Overlaps){SortedSet.Overlaps};|現在の集合と引数で指定されたIEnumerable<T>が共通する要素を持つかどうか調べる&br;(現在の集合と引数の集合が''一つでも同じ要素を持つ''場合はtrue)|
|
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.IsSubsetOf){HashSet.IsSubsetOf};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.IsSubsetOf){SortedSet.IsSubsetOf};|現在の集合が引数で指定されたIEnumerable<T>の部分集合かどうか調べる&br;(現在の集合が引数の集合''に含まれる''場合はtrue)|
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.IsSubsetOf){HashSet.IsSubsetOf};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.IsSubsetOf){SortedSet.IsSubsetOf};|現在の集合が引数で指定されたIEnumerable<T>の部分集合かどうか調べる&br;(現在の集合が引数の集合''に含まれる''場合はtrue)|
|
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.IsProperSubsetOf){HashSet.IsProperSubsetOf};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.IsProperSubsetOf){SortedSet.IsProperSubsetOf};|現在の集合が引数で指定されたIEnumerable<T>の真部分集合かどうか調べる&br;(現在の集合が引数の集合''に含まれ、かつ両者が等しくない''場合はtrue)|
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.IsProperSubsetOf){HashSet.IsProperSubsetOf};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.IsProperSubsetOf){SortedSet.IsProperSubsetOf};|現在の集合が引数で指定されたIEnumerable<T>の真部分集合かどうか調べる&br;(現在の集合が引数の集合''に含まれ、かつ両者が等しくない''場合はtrue)|
|
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.IsSupersetOf){HashSet.IsSupersetOf};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.IsSupersetOf){SortedSet.IsSupersetOf};|現在の集合が引数で指定されたIEnumerable<T>の上位集合かどうか調べる&br;(現在の集合が引数の集合''を含む''場合はtrue)|
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.IsSupersetOf){HashSet.IsSupersetOf};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.IsSupersetOf){SortedSet.IsSupersetOf};|現在の集合が引数で指定されたIEnumerable<T>の上位集合かどうか調べる&br;(現在の集合が引数の集合''を含む''場合はtrue)|
|
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.IsProperSupersetOf){HashSet.IsProperSupersetOf};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.IsProperSupersetOf){SortedSet.IsProperSupersetOf};|現在の集合が引数で指定されたIEnumerable<T>の真上位集合かどうか調べる&br;(現在の集合が引数の集合''を含み、かつ両者が等しくない''場合はtrue)|
|
|&msdn(netfx,member,System.Collections.Generic.HashSet`1.IsProperSupersetOf){HashSet.IsProperSupersetOf};&br;&msdn(netfx,member,System.Collections.Generic.SortedSet`1.IsProperSupersetOf){SortedSet.IsProperSupersetOf};|現在の集合が引数で指定されたIEnumerable<T>の真上位集合かどうか調べる&br;(現在の集合が引数の集合''を含み、かつ両者が等しくない''場合はtrue)|
|
|
|
|
~ |
以下は、上記のメソッドを使った例です。 SortedSetでは並べ替えが行われる以外は、結果は同じです。
|
以下は、上記のメソッドを使った例です。
|
|
|
|
|
#column(layout=fixed,HashSet)
|
#column(layout=fixed,HashSet)
|
~ |
#tabpage(codelang=cs,container-title=SetEquals・Overrapsメソッドを使ってHashSetが別の集合と完全一致・部分一致するか調べる)
|
#tabpage(codelang=cs,container-title=HashSetを使って他の集合との包含関係の検証を行う)
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
|
990,111 |
679,53 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new HashSet<int>() {3, 4, 1, 5, 2};
|
HashSet<int> s = new HashSet<int>(new int[] {3, 4, 1, 5, 2});
|
|
|
|
~ |
Console.WriteLine("Set: {{{0}}}", string.Join(", ", s));
|
Console.Write("Set: ");
|
+ |
Console.WriteLine();
|
|
|
|
|
~ |
// 2つの集合が完全に一致するか調べる (SetEquals)
|
Print(s);
|
+ |
Console.WriteLine(
|
|
+ |
"SetEquals {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.SetEquals(new[] {1, 2, 3, 4, 5})
|
|
+ |
);
|
|
+ |
Console.WriteLine(
|
|
+ |
"SetEquals {{1, 2, 3, 4, 6}}: {0}",
|
|
+ |
s.SetEquals(new[] {1, 2, 3, 4, 6})
|
|
+ |
);
|
|
+ |
|
|
+ |
//s 2つの集合が部分一致するか調べる (Overraps)
|
|
+ |
Console.WriteLine(
|
|
+ |
"Overraps {{2, 3, 5, 7}}: {0}",
|
|
+ |
s.Overlaps(new[] {2, 3, 5, 7})
|
|
+ |
);
|
|
+ |
Console.WriteLine(
|
|
+ |
"Overraps {{0, 6}}: {0}",
|
|
+ |
s.Overlaps(new[] {0, 6})
|
|
+ |
);
|
|
+ |
}
|
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
|
|
|
~ |
Class Sample
|
Console.WriteLine();
|
+ |
Shared Sub Main()
|
|
+ |
Dim s As New HashSet(Of Integer)() From {3, 4, 1, 5, 2}
|
|
|
|
|
~ |
Console.WriteLine("Set: {{{0}}}", String.Join(", ", s))
|
Console.WriteLine("SetEquals {{1, 2, 3, 4, 5}}: {0}",
|
~ |
Console.WriteLine()
|
s.SetEquals(new int[] {1, 2, 3, 4, 5}));
|
- |
|
Console.WriteLine("SetEquals {{1, 2, 3, 4, 6}}: {0}",
|
- |
|
s.SetEquals(new int[] {1, 2, 3, 4, 6}));
|
- |
|
Console.WriteLine("Overraps {{2, 3, 5, 7}}: {0}",
|
- |
|
s.Overlaps(new int[] {2, 3, 5, 7}));
|
- |
|
Console.WriteLine("Overraps {{0, 6}}: {0}",
|
- |
|
s.Overlaps(new int[] {0, 6}));
|
|
|
|
~ |
' 2つの集合が完全に一致するか調べる (SetEquals)
|
Console.WriteLine();
|
+ |
Console.WriteLine(
|
|
+ |
"SetEquals {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.SetEquals(New Integer() {1, 2, 3, 4, 5})
|
|
+ |
)
|
|
+ |
Console.WriteLine(
|
|
+ |
"SetEquals {{1, 2, 3, 4, 6}}: {0}",
|
|
+ |
s.SetEquals(New Integer() {1, 2, 3, 4, 6})
|
|
+ |
)
|
|
+ |
|
|
+ |
' 2つの集合が部分一致するか調べる (Overraps)
|
|
+ |
Console.WriteLine(
|
|
+ |
"Overraps {{2, 3, 5, 7}}: {0}",
|
|
+ |
s.Overlaps(New Integer() {2, 3, 5, 7})
|
|
+ |
)
|
|
+ |
Console.WriteLine(
|
|
+ |
"Overraps {{0, 6}}: {0}",
|
|
+ |
s.Overlaps(New Integer() {0, 6})
|
|
+ |
)
|
|
+ |
End Sub
|
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
|
|
|
~ |
#prompt(実行結果){{
|
Console.WriteLine("IsSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}",
|
~ |
Set: {3, 4, 1, 5, 2}
|
s.IsSubsetOf(new int[] {0, 1, 2, 3, 4, 5, 6}));
|
- |
|
Console.WriteLine("IsProperSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}",
|
- |
|
s.IsProperSubsetOf(new int[] {0, 1, 2, 3, 4, 5, 6}));
|
- |
|
Console.WriteLine("IsSubsetOf {{1, 2, 3, 4, 5}}: {0}",
|
- |
|
s.IsSubsetOf(new int[] {1, 2, 3, 4, 5}));
|
- |
|
Console.WriteLine("IsProperSubsetOf {{1, 2, 3, 4, 5}}: {0}",
|
- |
|
s.IsProperSubsetOf(new int[] {1, 2, 3, 4, 5}));
|
|
|
|
~ |
SetEquals {1, 2, 3, 4, 5}: True
|
Console.WriteLine();
|
+ |
SetEquals {1, 2, 3, 4, 6}: False
|
|
+ |
Overraps {2, 3, 5, 7}: True
|
|
+ |
Overraps {0, 6}: False
|
|
+ |
}}
|
|
|
|
|
~ |
#column(SortedSet)
|
Console.WriteLine("IsSupersetOf {{2, 3, 4}}: {0}",
|
~ |
#tabpage(codelang=cs,container-title=SetEquals・Overrapsメソッドを使ってSortedSetが別の集合と完全一致・部分一致するか調べる)
|
s.IsSupersetOf(new int[] {2, 3, 4}));
|
~ |
#code{{
|
Console.WriteLine("IsProperSupersetOf {{2, 3, 4}}: {0}",
|
~ |
using System;
|
s.IsProperSupersetOf(new int[] {2, 3, 4}));
|
~ |
using System.Collections.Generic;
|
Console.WriteLine("IsSupersetOf {{1, 2, 3, 4, 5}}: {0}",
|
- |
|
s.IsSupersetOf(new int[] {1, 2, 3, 4, 5}));
|
- |
|
Console.WriteLine("IsProperSupersetOf {{1, 2, 3, 4, 5}}: {0}",
|
- |
|
s.IsProperSupersetOf(new int[] {1, 2, 3, 4, 5}));
|
- |
|
}
|
|
|
|
~ |
class Sample {
|
static void Print(HashSet<int> s)
|
+ |
static void Main()
|
|
|
{
|
{
|
~ |
var s = new SortedSet<int>() {3, 4, 1, 5, 2};
|
foreach (int e in s) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
|
|
|
+ |
Console.WriteLine("Set: {{{0}}}", string.Join(", ", s));
|
|
|
Console.WriteLine();
|
Console.WriteLine();
|
+ |
|
|
+ |
// 2つの集合が完全に一致するか調べる (SetEquals)
|
|
+ |
Console.WriteLine(
|
|
+ |
"SetEquals {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.SetEquals(new[] {1, 2, 3, 4, 5})
|
|
+ |
);
|
|
+ |
Console.WriteLine(
|
|
+ |
"SetEquals {{1, 2, 3, 4, 6}}: {0}",
|
|
+ |
s.SetEquals(new[] {1, 2, 3, 4, 6})
|
|
+ |
);
|
|
+ |
|
|
+ |
//s 2つの集合が部分一致するか調べる (Overraps)
|
|
+ |
Console.WriteLine(
|
|
+ |
"Overraps {{2, 3, 5, 7}}: {0}",
|
|
+ |
s.Overlaps(new[] {2, 3, 5, 7})
|
|
+ |
);
|
|
+ |
Console.WriteLine(
|
|
+ |
"Overraps {{0, 6}}: {0}",
|
|
+ |
s.Overlaps(new[] {0, 6})
|
|
+ |
);
|
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
1105,129 |
736,78 |
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
Dim s As New SortedSet(Of Integer)() From {3, 4, 1, 5, 2}
|
Dim s As New HashSet(Of Integer)(New Integer() {3, 4, 1, 5, 2})
|
|
|
|
~ |
Console.WriteLine("Set: {{{0}}}", String.Join(", ", s))
|
Console.Write("Set: ")
|
+ |
Console.WriteLine()
|
|
|
|
|
~ |
' 2つの集合が完全に一致するか調べる (SetEquals)
|
Print(s)
|
+ |
Console.WriteLine(
|
|
+ |
"SetEquals {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.SetEquals(New Integer() {1, 2, 3, 4, 5})
|
|
+ |
)
|
|
+ |
Console.WriteLine(
|
|
+ |
"SetEquals {{1, 2, 3, 4, 6}}: {0}",
|
|
+ |
s.SetEquals(New Integer() {1, 2, 3, 4, 6})
|
|
+ |
)
|
|
+ |
|
|
+ |
' 2つの集合が部分一致するか調べる (Overraps)
|
|
+ |
Console.WriteLine(
|
|
+ |
"Overraps {{2, 3, 5, 7}}: {0}",
|
|
+ |
s.Overlaps(New Integer() {2, 3, 5, 7})
|
|
+ |
)
|
|
+ |
Console.WriteLine(
|
|
+ |
"Overraps {{0, 6}}: {0}",
|
|
+ |
s.Overlaps(New Integer() {0, 6})
|
|
+ |
)
|
|
+ |
End Sub
|
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
|
|
|
~ |
#prompt(実行結果){{
|
Console.WriteLine()
|
+ |
Set: {1, 2, 3, 4, 5}
|
|
+ |
|
|
+ |
SetEquals {1, 2, 3, 4, 5}: True
|
|
+ |
SetEquals {1, 2, 3, 4, 6}: False
|
|
+ |
Overraps {2, 3, 5, 7}: True
|
|
+ |
Overraps {0, 6}: False
|
|
+ |
}}
|
|
+ |
#column-end
|
|
|
|
|
- |
|
Console.WriteLine("SetEquals {{1, 2, 3, 4, 5}}: {0}", _
|
- |
|
s.SetEquals(New Integer() {1, 2, 3, 4, 5}))
|
- |
|
Console.WriteLine("SetEquals {{1, 2, 3, 4, 6}}: {0}", _
|
- |
|
s.SetEquals(New Integer() {1, 2, 3, 4, 6}))
|
- |
|
Console.WriteLine("Overraps {{2, 3, 5, 7}}: {0}", _
|
- |
|
s.Overlaps(New Integer() {2, 3, 5, 7}))
|
- |
|
Console.WriteLine("Overraps {{0, 6}}: {0}", _
|
- |
|
s.Overlaps(New Integer() {0, 6}))
|
|
|
|
~ |
#column(layout=fixed,HashSet)
|
Console.WriteLine()
|
+ |
#tabpage(codelang=cs,container-title=IsSubsetOf・IsProperSubsetOfメソッドを使ってHashSetが別の集合の部分集合か・真部分集合かどうか調べる)
|
|
+ |
#code{{
|
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
|
|
|
~ |
class Sample {
|
Console.WriteLine("IsSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}", _
|
~ |
static void Main()
|
s.IsSubsetOf(New Integer() {0, 1, 2, 3, 4, 5, 6}))
|
~ |
{
|
Console.WriteLine("IsProperSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}", _
|
~ |
var s = new HashSet<int>() {3, 4, 1, 5, 2};
|
s.IsProperSubsetOf(New Integer() {0, 1, 2, 3, 4, 5, 6}))
|
- |
|
Console.WriteLine("IsSubsetOf {{1, 2, 3, 4, 5}}: {0}", _
|
- |
|
s.IsSubsetOf(New Integer() {1, 2, 3, 4, 5}))
|
- |
|
Console.WriteLine("IsProperSubsetOf {{1, 2, 3, 4, 5}}: {0}", _
|
- |
|
s.IsProperSubsetOf(New Integer() {1, 2, 3, 4, 5}))
|
|
|
|
~ |
Console.WriteLine("Set: {{{0}}}", string.Join(", ", s));
|
Console.WriteLine()
|
+ |
Console.WriteLine();
|
|
|
|
|
~ |
// HashSetが引数の集合の部分集合かどうか調べる (IsSubsetOf)
|
Console.WriteLine("IsSupersetOf {{2, 3, 4}}: {0}", _
|
~ |
Console.WriteLine(
|
s.IsSupersetOf(New Integer() {2, 3, 4}))
|
~ |
"IsSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}",
|
Console.WriteLine("IsProperSupersetOf {{2, 3, 4}}: {0}", _
|
~ |
s.IsSubsetOf(new[] {0, 1, 2, 3, 4, 5, 6})
|
s.IsProperSupersetOf(New Integer() {2, 3, 4}))
|
~ |
);
|
Console.WriteLine("IsSupersetOf {{1, 2, 3, 4, 5}}: {0}", _
|
~ |
// HashSetが引数の集合の真部分集合かどうか調べる (IsProperSubsetOf)
|
s.IsSupersetOf(New Integer() {1, 2, 3, 4, 5}))
|
~ |
Console.WriteLine(
|
Console.WriteLine("IsProperSupersetOf {{1, 2, 3, 4, 5}}: {0}", _
|
~ |
"IsProperSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}",
|
s.IsProperSupersetOf(New Integer() {1, 2, 3, 4, 5}))
|
~ |
s.IsProperSubsetOf(new[] {0, 1, 2, 3, 4, 5, 6})
|
End Sub
|
+ |
);
|
|
+ |
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsSubsetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsSubsetOf(new[] {1, 2, 3, 4, 5})
|
|
+ |
);
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSubsetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsProperSubsetOf(new[] {1, 2, 3, 4, 5})
|
|
+ |
);
|
|
+ |
}
|
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
|
|
|
~ |
Class Sample
|
Shared Sub Print(ByVal s As HashSet(Of Integer))
|
~ |
Shared Sub Main()
|
For Each e As Integer In s
|
~ |
Dim s As New HashSet(Of Integer)() From {3, 4, 1, 5, 2}
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
|
|
|
+ |
Console.WriteLine("Set: {{{0}}}", String.Join(", ", s))
|
|
|
Console.WriteLine()
|
Console.WriteLine()
|
+ |
|
|
+ |
' HashSetが引数の集合の部分集合かどうか調べる (IsSubsetOf)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}",
|
|
+ |
s.IsSubsetOf(New Integer() {0, 1, 2, 3, 4, 5, 6})
|
|
+ |
)
|
|
+ |
' HashSetが引数の集合の真部分集合かどうか調べる (IsProperSubsetOf)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}",
|
|
+ |
s.IsProperSubsetOf(New Integer() {0, 1, 2, 3, 4, 5, 6})
|
|
+ |
)
|
|
+ |
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsSubsetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsSubsetOf(New Integer() {1, 2, 3, 4, 5})
|
|
+ |
)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSubsetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsProperSubsetOf(New Integer() {1, 2, 3, 4, 5})
|
|
+ |
)
|
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
Set: {3, 4, 1, 5, 2}
|
Set: 3, 4, 1, 5, 2,
|
- |
|
|
- |
|
SetEquals {1, 2, 3, 4, 5}: True
|
- |
|
SetEquals {1, 2, 3, 4, 6}: False
|
- |
|
Overraps {2, 3, 5, 7}: True
|
- |
|
Overraps {0, 6}: False
|
|
|
|
|
IsSubsetOf {0, 1, 2, 3, 4, 5, 6}: True
|
IsSubsetOf {0, 1, 2, 3, 4, 5, 6}: True
|
|
IsProperSubsetOf {0, 1, 2, 3, 4, 5, 6}: True
|
IsProperSubsetOf {0, 1, 2, 3, 4, 5, 6}: True
|
|
IsSubsetOf {1, 2, 3, 4, 5}: True
|
IsSubsetOf {1, 2, 3, 4, 5}: True
|
|
IsProperSubsetOf {1, 2, 3, 4, 5}: False
|
IsProperSubsetOf {1, 2, 3, 4, 5}: False
|
- |
|
|
- |
|
IsSupersetOf {2, 3, 4}: True
|
- |
|
IsProperSupersetOf {2, 3, 4}: True
|
- |
|
IsSupersetOf {1, 2, 3, 4, 5}: True
|
- |
|
IsProperSupersetOf {1, 2, 3, 4, 5}: False
|
|
}}
|
}}
|
|
|
|
|
#column(SortedSet)
|
#column(SortedSet)
|
~ |
#tabpage(codelang=cs,container-title=IsSubsetOf・IsProperSubsetOfメソッドを使ってSortedSetが別の集合の部分集合か・真部分集合かどうか調べる)
|
#tabpage(codelang=cs,container-title=SortedSetを使って他の集合との包含関係の検証を行う)
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
|
1235,112 |
815,53 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new SortedSet<int>() {3, 4, 1, 5, 2};
|
SortedSet<int> s = new SortedSet<int>(new int[] {3, 4, 1, 5, 2});
|
|
|
|
~ |
Console.WriteLine("Set: {{{0}}}", string.Join(", ", s));
|
Console.Write("Set: ");
|
+ |
Console.WriteLine();
|
|
|
|
|
~ |
// SortedSetが引数の集合の部分集合かどうか調べる (IsSubsetOf)
|
Print(s);
|
+ |
Console.WriteLine(
|
|
+ |
"IsSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}",
|
|
+ |
s.IsSubsetOf(new[] {0, 1, 2, 3, 4, 5, 6})
|
|
+ |
);
|
|
+ |
// SortedSetが引数の集合の真部分集合かどうか調べる (IsProperSubsetOf)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}",
|
|
+ |
s.IsProperSubsetOf(new[] {0, 1, 2, 3, 4, 5, 6})
|
|
+ |
);
|
|
+ |
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsSubsetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsSubsetOf(new[] {1, 2, 3, 4, 5})
|
|
+ |
);
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSubsetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsProperSubsetOf(new[] {1, 2, 3, 4, 5})
|
|
+ |
);
|
|
+ |
}
|
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
|
|
|
~ |
Class Sample
|
Console.WriteLine();
|
+ |
Shared Sub Main()
|
|
+ |
Dim s As New SortedSet(Of Integer)() From {3, 4, 1, 5, 2}
|
|
|
|
|
~ |
Console.WriteLine("Set: {{{0}}}", String.Join(", ", s))
|
Console.WriteLine("SetEquals {{1, 2, 3, 4, 5}}: {0}",
|
~ |
Console.WriteLine()
|
s.SetEquals(new int[] {1, 2, 3, 4, 5}));
|
- |
|
Console.WriteLine("SetEquals {{1, 2, 3, 4, 6}}: {0}",
|
- |
|
s.SetEquals(new int[] {1, 2, 3, 4, 6}));
|
- |
|
Console.WriteLine("Overraps {{2, 3, 5, 7}}: {0}",
|
- |
|
s.Overlaps(new int[] {2, 3, 5, 7}));
|
- |
|
Console.WriteLine("Overraps {{0, 6}}: {0}",
|
- |
|
s.Overlaps(new int[] {0, 6}));
|
|
|
|
~ |
' SortedSetが引数の集合の部分集合かどうか調べる (IsSubsetOf)
|
Console.WriteLine();
|
+ |
Console.WriteLine(
|
|
+ |
"IsSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}",
|
|
+ |
s.IsSubsetOf(New Integer() {0, 1, 2, 3, 4, 5, 6})
|
|
+ |
)
|
|
+ |
' SortedSetが引数の集合の真部分集合かどうか調べる (IsProperSubsetOf)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}",
|
|
+ |
s.IsProperSubsetOf(New Integer() {0, 1, 2, 3, 4, 5, 6})
|
|
+ |
)
|
|
+ |
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsSubsetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsSubsetOf(New Integer() {1, 2, 3, 4, 5})
|
|
+ |
)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSubsetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsProperSubsetOf(New Integer() {1, 2, 3, 4, 5})
|
|
+ |
)
|
|
+ |
End Sub
|
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
|
|
|
~ |
#prompt(実行結果){{
|
Console.WriteLine("IsSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}",
|
~ |
Set: {1, 2, 3, 4, 5}
|
s.IsSubsetOf(new int[] {0, 1, 2, 3, 4, 5, 6}));
|
- |
|
Console.WriteLine("IsProperSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}",
|
- |
|
s.IsProperSubsetOf(new int[] {0, 1, 2, 3, 4, 5, 6}));
|
- |
|
Console.WriteLine("IsSubsetOf {{1, 2, 3, 4, 5}}: {0}",
|
- |
|
s.IsSubsetOf(new int[] {1, 2, 3, 4, 5}));
|
- |
|
Console.WriteLine("IsProperSubsetOf {{1, 2, 3, 4, 5}}: {0}",
|
- |
|
s.IsProperSubsetOf(new int[] {1, 2, 3, 4, 5}));
|
|
|
|
~ |
IsSubsetOf {0, 1, 2, 3, 4, 5, 6}: True
|
Console.WriteLine();
|
+ |
IsProperSubsetOf {0, 1, 2, 3, 4, 5, 6}: True
|
|
+ |
IsSubsetOf {1, 2, 3, 4, 5}: True
|
|
+ |
IsProperSubsetOf {1, 2, 3, 4, 5}: False
|
|
+ |
}}
|
|
+ |
#column-end
|
|
|
|
|
- |
|
Console.WriteLine("IsSupersetOf {{2, 3, 4}}: {0}",
|
- |
|
s.IsSupersetOf(new int[] {2, 3, 4}));
|
- |
|
Console.WriteLine("IsProperSupersetOf {{2, 3, 4}}: {0}",
|
- |
|
s.IsProperSupersetOf(new int[] {2, 3, 4}));
|
- |
|
Console.WriteLine("IsSupersetOf {{1, 2, 3, 4, 5}}: {0}",
|
- |
|
s.IsSupersetOf(new int[] {1, 2, 3, 4, 5}));
|
- |
|
Console.WriteLine("IsProperSupersetOf {{1, 2, 3, 4, 5}}: {0}",
|
- |
|
s.IsProperSupersetOf(new int[] {1, 2, 3, 4, 5}));
|
- |
|
}
|
|
|
|
~ |
#column(layout=fixed,HashSet)
|
static void Print(SortedSet<int> s)
|
+ |
#tabpage(codelang=cs,container-title=IsSupersetOf・IsProperSupersetOfメソッドを使ってHashSetが別の集合の上位集合か・真上位集合かどうか調べる)
|
|
+ |
#code{{
|
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
+ |
|
|
+ |
class Sample {
|
|
+ |
static void Main()
|
|
|
{
|
{
|
~ |
var s = new HashSet<int>() {3, 4, 1, 5, 2};
|
foreach (int e in s) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
|
|
|
+ |
Console.WriteLine("Set: {{{0}}}", string.Join(", ", s));
|
|
|
Console.WriteLine();
|
Console.WriteLine();
|
+ |
|
|
+ |
// HashSetが引数の集合の上位集合かどうか調べる (IsSupersetOf)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsSupersetOf {{2, 3, 4}}: {0}",
|
|
+ |
s.IsSupersetOf(new[] {2, 3, 4})
|
|
+ |
);
|
|
+ |
// HashSetが引数の集合の真上位集合かどうか調べる (IsProperSupersetOf)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSupersetOf {{2, 3, 4}}: {0}",
|
|
+ |
s.IsProperSupersetOf(new[] {2, 3, 4})
|
|
+ |
);
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsSupersetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsSupersetOf(new[] {1, 2, 3, 4, 5})
|
|
+ |
);
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSupersetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsProperSupersetOf(new[] {1, 2, 3, 4, 5})
|
|
+ |
);
|
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
1351,115 |
872,69 |
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
Dim s As New HashSet(Of Integer)() From {3, 4, 1, 5, 2}
|
Dim s As New SortedSet(Of Integer)(New Integer() {3, 4, 1, 5, 2})
|
|
|
|
~ |
Console.WriteLine("Set: {{{0}}}", String.Join(", ", s))
|
Console.Write("Set: ")
|
+ |
Console.WriteLine()
|
|
|
|
|
~ |
' HashSetが引数の集合の上位集合かどうか調べる (IsSupersetOf)
|
Print(s)
|
+ |
Console.WriteLine(
|
|
+ |
"IsSupersetOf {{2, 3, 4}}: {0}",
|
|
+ |
s.IsSupersetOf(New Integer() {2, 3, 4})
|
|
+ |
)
|
|
+ |
' HashSetが引数の集合の真上位集合かどうか調べる (IsProperSupersetOf)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSupersetOf {{2, 3, 4}}: {0}",
|
|
+ |
s.IsProperSupersetOf(New Integer() {2, 3, 4})
|
|
+ |
)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsSupersetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsSupersetOf(New Integer() {1, 2, 3, 4, 5})
|
|
+ |
)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSupersetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsProperSupersetOf(New Integer() {1, 2, 3, 4, 5})
|
|
+ |
)
|
|
+ |
End Sub
|
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
|
|
|
~ |
#prompt(実行結果){{
|
Console.WriteLine()
|
+ |
Set: {3, 4, 1, 5, 2}
|
|
|
|
|
~ |
IsSupersetOf {2, 3, 4}: True
|
Console.WriteLine("SetEquals {{1, 2, 3, 4, 5}}: {0}", _
|
~ |
IsProperSupersetOf {2, 3, 4}: True
|
s.SetEquals(New Integer() {1, 2, 3, 4, 5}))
|
~ |
IsSupersetOf {1, 2, 3, 4, 5}: True
|
Console.WriteLine("SetEquals {{1, 2, 3, 4, 6}}: {0}", _
|
~ |
IsProperSupersetOf {1, 2, 3, 4, 5}: False
|
s.SetEquals(New Integer() {1, 2, 3, 4, 6}))
|
~ |
}}
|
Console.WriteLine("Overraps {{2, 3, 5, 7}}: {0}", _
|
- |
|
s.Overlaps(New Integer() {2, 3, 5, 7}))
|
- |
|
Console.WriteLine("Overraps {{0, 6}}: {0}", _
|
- |
|
s.Overlaps(New Integer() {0, 6}))
|
|
|
|
~ |
#column(SortedSet)
|
Console.WriteLine()
|
+ |
#tabpage(codelang=cs,container-title=IsSupersetOf・IsProperSupersetOfメソッドを使ってSortedSetが別の集合の上位集合か・真上位集合かどうか調べる)
|
|
+ |
#code{{
|
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
|
|
|
~ |
class Sample {
|
Console.WriteLine("IsSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}", _
|
~ |
static void Main()
|
s.IsSubsetOf(New Integer() {0, 1, 2, 3, 4, 5, 6}))
|
~ |
{
|
Console.WriteLine("IsProperSubsetOf {{0, 1, 2, 3, 4, 5, 6}}: {0}", _
|
~ |
var s = new SortedSet<int>() {3, 4, 1, 5, 2};
|
s.IsProperSubsetOf(New Integer() {0, 1, 2, 3, 4, 5, 6}))
|
- |
|
Console.WriteLine("IsSubsetOf {{1, 2, 3, 4, 5}}: {0}", _
|
- |
|
s.IsSubsetOf(New Integer() {1, 2, 3, 4, 5}))
|
- |
|
Console.WriteLine("IsProperSubsetOf {{1, 2, 3, 4, 5}}: {0}", _
|
- |
|
s.IsProperSubsetOf(New Integer() {1, 2, 3, 4, 5}))
|
|
|
|
~ |
Console.WriteLine("Set: {{{0}}}", string.Join(", ", s));
|
Console.WriteLine()
|
+ |
Console.WriteLine();
|
|
|
|
|
~ |
// SortedSetが引数の集合の上位集合かどうか調べる (IsSupersetOf)
|
Console.WriteLine("IsSupersetOf {{2, 3, 4}}: {0}", _
|
~ |
Console.WriteLine(
|
s.IsSupersetOf(New Integer() {2, 3, 4}))
|
~ |
"IsSupersetOf {{2, 3, 4}}: {0}",
|
Console.WriteLine("IsProperSupersetOf {{2, 3, 4}}: {0}", _
|
~ |
s.IsSupersetOf(new[] {2, 3, 4})
|
s.IsProperSupersetOf(New Integer() {2, 3, 4}))
|
~ |
);
|
Console.WriteLine("IsSupersetOf {{1, 2, 3, 4, 5}}: {0}", _
|
~ |
// SortedSetが引数の集合の真上位集合かどうか調べる (IsProperSupersetOf)
|
s.IsSupersetOf(New Integer() {1, 2, 3, 4, 5}))
|
~ |
Console.WriteLine(
|
Console.WriteLine("IsProperSupersetOf {{1, 2, 3, 4, 5}}: {0}", _
|
~ |
"IsProperSupersetOf {{2, 3, 4}}: {0}",
|
s.IsProperSupersetOf(New Integer() {1, 2, 3, 4, 5}))
|
~ |
s.IsProperSupersetOf(new[] {2, 3, 4})
|
End Sub
|
+ |
);
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsSupersetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsSupersetOf(new[] {1, 2, 3, 4, 5})
|
|
+ |
);
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSupersetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsProperSupersetOf(new[] {1, 2, 3, 4, 5})
|
|
+ |
);
|
|
+ |
}
|
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
|
|
|
~ |
Class Sample
|
Shared Sub Print(ByVal s As SortedSet(Of Integer))
|
~ |
Shared Sub Main()
|
For Each e As Integer In s
|
~ |
Dim s As New SortedSet(Of Integer)() From {3, 4, 1, 5, 2}
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
|
|
|
+ |
Console.WriteLine("Set: {{{0}}}", String.Join(", ", s))
|
|
|
Console.WriteLine()
|
Console.WriteLine()
|
+ |
|
|
+ |
' SortedSetが引数の集合の上位集合かどうか調べる (IsSupersetOf)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsSupersetOf {{2, 3, 4}}: {0}",
|
|
+ |
s.IsSupersetOf(New Integer() {2, 3, 4})
|
|
+ |
)
|
|
+ |
' SortedSetが引数の集合の真上位集合かどうか調べる (IsProperSupersetOf)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSupersetOf {{2, 3, 4}}: {0}",
|
|
+ |
s.IsProperSupersetOf(New Integer() {2, 3, 4})
|
|
+ |
)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsSupersetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsSupersetOf(New Integer() {1, 2, 3, 4, 5})
|
|
+ |
)
|
|
+ |
Console.WriteLine(
|
|
+ |
"IsProperSupersetOf {{1, 2, 3, 4, 5}}: {0}",
|
|
+ |
s.IsProperSupersetOf(New Integer() {1, 2, 3, 4, 5})
|
|
+ |
)
|
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
Set: {1, 2, 3, 4, 5}
|
Set: 1, 2, 3, 4, 5,
|
- |
|
|
- |
|
SetEquals {1, 2, 3, 4, 5}: True
|
- |
|
SetEquals {1, 2, 3, 4, 6}: False
|
- |
|
Overraps {2, 3, 5, 7}: True
|
- |
|
Overraps {0, 6}: False
|
- |
|
|
- |
|
IsSubsetOf {0, 1, 2, 3, 4, 5, 6}: True
|
- |
|
IsProperSubsetOf {0, 1, 2, 3, 4, 5, 6}: True
|
- |
|
IsSubsetOf {1, 2, 3, 4, 5}: True
|
- |
|
IsProperSubsetOf {1, 2, 3, 4, 5}: False
|
|
|
|
|
IsSupersetOf {2, 3, 4}: True
|
IsSupersetOf {2, 3, 4}: True
|
|
IsProperSupersetOf {2, 3, 4}: True
|
IsProperSupersetOf {2, 3, 4}: True
|
|
1468,9 |
943,10 |
|
}}
|
}}
|
|
#column-end
|
#column-end
|
|
|
|
- |
|
SortedSetで並べ替えが行われる以外は、結果は同じです。
|
|
|
|
|
|
|
~ |
*HashSetでの等価性比較のカスタマイズ [#EqualityComparison]
|
*等価性比較のカスタマイズ [#EqualityComparison]
|
|
HashSetのコンストラクタで適切なIEqualityComparer<T>インターフェイスを指定することで、要素の等価性比較時の動作をカスタマイズ出来ます。 例えば、比較の際に大文字小文字の違いを無視するようにするといったことが出来ます。 以下は、StringComparerクラスを使って、大文字小文字の違いを無視するHashSetを作成する例です。
|
HashSetのコンストラクタで適切なIEqualityComparer<T>インターフェイスを指定することで、要素の等価性比較時の動作をカスタマイズ出来ます。 例えば、比較の際に大文字小文字の違いを無視するようにするといったことが出来ます。 以下は、StringComparerクラスを使って、大文字小文字の違いを無視するHashSetを作成する例です。
|
|
|
|
|
#tabpage(codelang=cs,container-title=StringComparerを指定して大文字小文字の違いを意識する/無視するHashSetを作成する)
|
#tabpage(codelang=cs,container-title=StringComparerを指定して大文字小文字の違いを意識する/無視するHashSetを作成する)
|
|
1481,15 |
957,15 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var names = new[] {"Alice", "Bob", "Charlie", "Dave", "Eve"};
|
string[] names = new string[] {"Alice", "Bob", "Charlie", "Dave", "Eve"};
|
|
|
|
|
// 大文字小文字の違いを意識するHashSet
|
// 大文字小文字の違いを意識するHashSet
|
~ |
var caseSensitiveSet = new HashSet<string>(names, StringComparer.Ordinal);
|
HashSet<string> caseSensitiveSet = new HashSet<string>(names, StringComparer.CurrentCulture);
|
|
// 大文字小文字の違いを無視するHashSet
|
// 大文字小文字の違いを無視するHashSet
|
~ |
var caseInsensitiveSet = new HashSet<string>(names, StringComparer.OrdinalIgnoreCase);
|
HashSet<string> caseInsensitiveSet = new HashSet<string>(names, StringComparer.CurrentCultureIgnoreCase);
|
|
|
|
~ |
Console.WriteLine(caseSensitiveSet.IsSupersetOf(new[] {"aLiCe", "BOB", "dave"}));
|
Console.WriteLine(caseSensitiveSet.IsSupersetOf(new string[] {"aLiCe", "BOB", "dave"}));
|
~ |
Console.WriteLine(caseInsensitiveSet.IsSupersetOf(new[] {"aLiCe", "BOB", "dave"}));
|
Console.WriteLine(caseInsensitiveSet.IsSupersetOf(new string[] {"aLiCe", "BOB", "dave"}));
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
1503,9 |
979,9 |
|
Dim names() As String = New String() {"Alice", "Bob", "Charlie", "Dave", "Eve"}
|
Dim names() As String = New String() {"Alice", "Bob", "Charlie", "Dave", "Eve"}
|
|
|
|
|
' 大文字小文字の違いを意識するHashSet
|
' 大文字小文字の違いを意識するHashSet
|
~ |
Dim caseSensitiveSet As New HashSet(Of String)(names, StringComparer.Ordinal)
|
Dim caseSensitiveSet As New HashSet(Of String)(names, StringComparer.CurrentCulture)
|
|
' 大文字小文字の違いを無視するHashSet
|
' 大文字小文字の違いを無視するHashSet
|
~ |
Dim caseInsensitiveSet As New HashSet(Of String)(names, StringComparer.OrdinalIgnoreCase)
|
Dim caseInsensitiveSet As New HashSet(Of String)(names, StringComparer.CurrentCultureIgnoreCase)
|
|
|
|
|
Console.WriteLine(caseSensitiveSet.IsSupersetOf(New String() {"aLiCe", "BOB", "dave"}))
|
Console.WriteLine(caseSensitiveSet.IsSupersetOf(New String() {"aLiCe", "BOB", "dave"}))
|
|
Console.WriteLine(caseInsensitiveSet.IsSupersetOf(New String() {"aLiCe", "BOB", "dave"}))
|
Console.WriteLine(caseInsensitiveSet.IsSupersetOf(New String() {"aLiCe", "BOB", "dave"}))
|
|
1538,41 |
1014,56 |
|
// KeyValuePair<string, int>の等価性比較を行うIEqualityComparer<T>
|
// KeyValuePair<string, int>の等価性比較を行うIEqualityComparer<T>
|
|
class KeyValuePairEqualityComparer : EqualityComparer<KeyValuePair<string, int>> {
|
class KeyValuePairEqualityComparer : EqualityComparer<KeyValuePair<string, int>> {
|
|
public override bool Equals(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
|
public override bool Equals(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
|
~ |
=> string.Equals(x.Key, y.Key); // KeyValuePairのKeyを比較する
|
{
|
- |
|
// KeyValuePairのKeyを比較する
|
- |
|
return string.Equals(x.Key, y.Key);
|
- |
|
}
|
|
|
|
|
public override int GetHashCode(KeyValuePair<string, int> obj)
|
public override int GetHashCode(KeyValuePair<string, int> obj)
|
~ |
=> obj.Key.GetHashCode(); // KeyValuePairのKeyからハッシュ値を取得する
|
{
|
- |
|
// KeyValuePairのKeyからハッシュ値を取得する
|
- |
|
return obj.Key.GetHashCode();
|
- |
|
}
|
|
}
|
}
|
|
|
|
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var comparer = new KeyValuePairEqualityComparer();
|
KeyValuePairEqualityComparer comparer = new KeyValuePairEqualityComparer();
|
~ |
var s1 = new HashSet<KeyValuePair<string, int>>(comparer);
|
HashSet<KeyValuePair<string, int>> s1 = new HashSet<KeyValuePair<string, int>>(comparer);
|
|
|
|
~ |
s1.Add(KeyValuePair.Create("Dave", 1));
|
s1.Add(new KeyValuePair<string, int>("Dave", 1));
|
~ |
s1.Add(KeyValuePair.Create("Alice", 2));
|
s1.Add(new KeyValuePair<string, int>("Alice", 2));
|
~ |
s1.Add(KeyValuePair.Create("Alice", 99999));
|
s1.Add(new KeyValuePair<string, int>("Alice", 99999));
|
~ |
s1.Add(KeyValuePair.Create("Bob", 3));
|
s1.Add(new KeyValuePair<string, int>("Bob", 3));
|
~ |
s1.Add(KeyValuePair.Create("Eve", 4));
|
s1.Add(new KeyValuePair<string, int>("Eve", 4));
|
~ |
s1.Add(KeyValuePair.Create("Charlie", 5));
|
s1.Add(new KeyValuePair<string, int>("Charlie", 5));
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s1));
|
Print(s1);
|
|
|
|
~ |
var s2 = new HashSet<KeyValuePair<string, int>>(comparer);
|
HashSet<KeyValuePair<string, int>> s2 = new HashSet<KeyValuePair<string, int>>(comparer);
|
|
|
|
~ |
s2.Add(KeyValuePair.Create("Alice", 3));
|
s2.Add(new KeyValuePair<string, int>("Alice", 3));
|
~ |
s2.Add(KeyValuePair.Create("Bob", 1));
|
s2.Add(new KeyValuePair<string, int>("Bob", 1));
|
~ |
s2.Add(KeyValuePair.Create("Charlie", 2));
|
s2.Add(new KeyValuePair<string, int>("Charlie", 2));
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s2));
|
Print(s2);
|
|
|
|
|
// 差集合を求める
|
// 差集合を求める
|
|
Console.WriteLine("[ExceptWith]");
|
Console.WriteLine("[ExceptWith]");
|
|
|
|
|
s1.ExceptWith(s2);
|
s1.ExceptWith(s2);
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s1));
|
Print(s1);
|
- |
|
}
|
- |
|
|
- |
|
static void Print(HashSet<KeyValuePair<string, int>> s)
|
- |
|
{
|
- |
|
foreach (KeyValuePair<string, int> pair in s) {
|
- |
|
Console.Write("{0}:{1}, ", pair.Key, pair.Value);
|
- |
|
}
|
- |
|
|
- |
|
Console.WriteLine();
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
1608,7 |
1099,7 |
|
s1.Add(New KeyValuePair(Of String, Integer)("Eve", 4))
|
s1.Add(New KeyValuePair(Of String, Integer)("Eve", 4))
|
|
s1.Add(New KeyValuePair(Of String, Integer)("Charlie", 5))
|
s1.Add(New KeyValuePair(Of String, Integer)("Charlie", 5))
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s1))
|
Print(s1)
|
|
|
|
|
Dim s2 As New HashSet(Of KeyValuePair(Of String, Integer))(comparer)
|
Dim s2 As New HashSet(Of KeyValuePair(Of String, Integer))(comparer)
|
|
|
|
|
1616,213 |
1107,83 |
|
s2.Add(New KeyValuePair(Of String, Integer)("Bob", 1))
|
s2.Add(New KeyValuePair(Of String, Integer)("Bob", 1))
|
|
s2.Add(New KeyValuePair(Of String, Integer)("Charlie", 2))
|
s2.Add(New KeyValuePair(Of String, Integer)("Charlie", 2))
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s2))
|
Print(s2)
|
|
|
|
|
' 差集合を求める
|
' 差集合を求める
|
|
Console.WriteLine("[ExceptWith]")
|
Console.WriteLine("[ExceptWith]")
|
|
|
|
|
s1.ExceptWith(s2)
|
s1.ExceptWith(s2)
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s1))
|
Print(s1)
|
|
End Sub
|
End Sub
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
|
|
|
~ |
#prompt(実行結果){{
|
Shared Sub Print(ByVal s As HashSet(Of KeyValuePair(Of String, Integer)))
|
~ |
[Dave, 1], [Alice, 2], [Bob, 3], [Eve, 4], [Charlie, 5]
|
For Each pair As KeyValuePair(Of String, Integer) In s
|
~ |
[Alice, 3], [Bob, 1], [Charlie, 2]
|
Console.Write("{0}:{1}, ", pair.Key, pair.Value)
|
~ |
[ExceptWith]
|
Next
|
+ |
[Dave, 1], [Eve, 4]
|
|
+ |
}}
|
|
+ |
|
|
+ |
この例のKeyValuePairEqualityComparerではKeyの値のみを比較しているため、Valueにどのような値が格納されているかといったことは一切考慮されません。 Keyの値が同一であれば、Valueの値に関わらず同一の要素とみなされます。
|
|
+ |
|
|
+ |
|
|
+ |
*SortedSetでの大小比較のカスタマイズ [#Comparison]
|
|
+ |
SortedSetのコンストラクタで適切なIComparer<T>インターフェイスを指定することで、要素の大小関係比較時の動作をカスタマイズ出来ます。 例えば、比較の際に大文字小文字の違いを無視するようにするといったことが出来ます。
|
|
+ |
|
|
+ |
以下は、大文字小文字を無視し、アルファベット順とは逆順(Z-Aの順)になるようにソートするIComparer<string>を実装し、SortedSetでの並べ替え順をカスタマイズする例です。
|
|
+ |
|
|
+ |
#tabpage(codelang=cs,container-title=大文字小文字の違いを無視し、アルファベット順とは逆順に並べ替えるSortedSetを作成する)
|
|
+ |
#code{{
|
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
+ |
|
|
+ |
// 大文字小文字の違いを無視し、アルファベット順とは逆順にソートするためのIComparer
|
|
+ |
class CaseInsensitiveReverseStringComparer : IComparer<string> {
|
|
+ |
public int Compare(string x, string y)
|
|
+ |
// StringComparer.OrdinalIgnoreCase.Compareとは逆の結果を返すようにする
|
|
+ |
=> -1 * StringComparer.OrdinalIgnoreCase.Compare(x, y);
|
|
+ |
}
|
|
+ |
|
|
+ |
class Sample {
|
|
+ |
static void Main()
|
|
+ |
{
|
|
+ |
var names = new[] {"Alice", "Eve", "Charlie", "Bob", "Dave"};
|
|
+ |
|
|
+ |
var caseSensitiveSet = new SortedSet<string>(names, StringComparer.Ordinal);
|
|
+ |
var reverseCaseInsensitiveSet = new SortedSet<string>(names, new CaseInsensitiveReverseStringComparer());
|
|
+ |
|
|
+ |
Console.WriteLine("caseSensitiveSet");
|
|
+ |
Console.WriteLine(string.Join(", ", caseSensitiveSet));
|
|
+ |
Console.WriteLine(caseSensitiveSet.IsSupersetOf(new[] {"aLiCe", "BOB", "dave"}));
|
|
+ |
Console.WriteLine();
|
|
+ |
|
|
+ |
Console.WriteLine("reverseCaseInsensitiveSet");
|
|
+ |
Console.WriteLine(string.Join(", ", reverseCaseInsensitiveSet));
|
|
+ |
Console.WriteLine(reverseCaseInsensitiveSet.IsSupersetOf(new[] {"aLiCe", "BOB", "dave"}));
|
|
+ |
}
|
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
+ |
|
|
+ |
' 大文字小文字の違いを無視し、アルファベット順とは逆順にソートするためのIComparer
|
|
+ |
Class CaseInsensitiveReverseStringComparer
|
|
+ |
Implements IComparer(Of String)
|
|
+ |
|
|
+ |
Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements IComparer(Of String).Compare
|
|
+ |
' StringComparer.OrdinalIgnoreCase.Compareとは逆の結果を返すようにする
|
|
+ |
Return -1 * StringComparer.OrdinalIgnoreCase.Compare(x, y)
|
|
+ |
End Function
|
|
+ |
End Class
|
|
+ |
|
|
+ |
Class Sample
|
|
+ |
Shared Sub Main()
|
|
+ |
Dim names() As String = New String() {"Alice", "Bob", "Charlie", "Dave", "Eve"}
|
|
+ |
|
|
+ |
Dim caseSensitiveSet As New SortedSet(Of String)(names, StringComparer.Ordinal)
|
|
+ |
Dim reverseCaseInsensitiveSet As New SortedSet(Of String)(names, New CaseInsensitiveReverseStringComparer())
|
|
|
|
|
+ |
Console.WriteLine("caseSensitiveSet")
|
|
+ |
Console.WriteLine(String.Join(", ", caseSensitiveSet))
|
|
+ |
Console.WriteLine(caseSensitiveSet.IsSupersetOf(New String() {"aLiCe", "BOB", "dave"}))
|
|
|
Console.WriteLine()
|
Console.WriteLine()
|
+ |
|
|
+ |
Console.WriteLine("reverseCaseInsensitiveSet")
|
|
+ |
Console.WriteLine(String.Join(", ", reverseCaseInsensitiveSet))
|
|
+ |
Console.WriteLine(reverseCaseInsensitiveSet.IsSupersetOf(New String() {"aLiCe", "BOB", "dave"}))
|
|
+ |
End Sub
|
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
+ |
|
|
+ |
#prompt(実行結果){{
|
|
+ |
caseSensitiveSet
|
|
+ |
Alice, Bob, Charlie, Dave, Eve
|
|
+ |
False
|
|
+ |
|
|
+ |
reverseCaseInsensitiveSet
|
|
+ |
Eve, Dave, Charlie, Bob, Alice
|
|
+ |
True
|
|
+ |
}}
|
|
+ |
|
|
+ |
#remarks
|
|
+ |
IComparer<T>インターフェイスや実装例については[[programming/netfx/comparison/0_comparison]]を参照してください。
|
|
+ |
#remarks-end
|
|
+ |
|
|
+ |
#remarks
|
|
+ |
|
|
+ |
文字列の大小関係、StringComparerについて詳しくは[[programming/netfx/string/2_2_compareoptions]]を参照してください。
|
|
+ |
|
|
+ |
#remarks-end
|
|
+ |
|
|
+ |
|
|
+ |
|
|
+ |
*SortedSetのみで提供される操作 [#SortedSet_specific_operations]
|
|
+ |
以下の操作はHashSetには用意されておらず、SortedSetのみで提供される操作です。
|
|
+ |
|
|
+ |
**最小値・最大値 [#SortedSet_MinMax]
|
|
+ |
SortedSetクラスでは、&msdn(netfx,member,System.Collections.Generic.SortedSet`1.Min){Minプロパティ};および&msdn(netfx,member,System.Collections.Generic.SortedSet`1.Max){Maxプロパティ};を参照することで、''SortedSetの並べ替え順序での最小・最大の要素''を取得することが出来ます。
|
|
+ |
|
|
+ |
#tabpage(codelang=cs,container-title=Min・Maxプロパティを参照してSortedSet内の最小・最大の要素を取得する)
|
|
+ |
#code{{
|
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
+ |
|
|
+ |
class Sample {
|
|
+ |
static void Main()
|
|
+ |
{
|
|
+ |
var s = new SortedSet<string>() {"Alice", "Eve", "Charlie", "Bob", "Dave"};
|
|
+ |
|
|
+ |
Console.WriteLine(string.Join(", ", s));
|
|
+ |
|
|
+ |
// 最小値と最大値を表示
|
|
+ |
Console.WriteLine($"Mix: {s.Min}");
|
|
+ |
Console.WriteLine($"Max: {s.Max}");
|
|
+ |
}
|
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
+ |
|
|
+ |
Class Sample
|
|
+ |
Shared Sub Main()
|
|
+ |
Dim s As New SortedSet(Of String)() From {"Alice", "Eve", "Charlie", "Bob", "Dave"}
|
|
+ |
|
|
+ |
Console.WriteLine(String.Join(", ", s))
|
|
+ |
|
|
+ |
' 最小値と最大値を表示
|
|
+ |
Console.WriteLine($"Mix: {s.Min}")
|
|
+ |
Console.WriteLine($"Max: {s.Max}")
|
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
Set: Alice, Bob, Charlie, Dave, Eve,
|
Dave:1, Alice:2, Bob:3, Eve:4, Charlie:5,
|
~ |
Mix: Alice
|
Alice:3, Bob:1, Charlie:2,
|
~ |
Max: Eve
|
[ExceptWith]
|
- |
|
Dave:1, Eve:4,
|
|
}}
|
}}
|
|
|
|
- |
|
この例のKeyValuePairEqualityComparerではKeyの値のみを比較しているため、Valueにどのような値が格納されているかといったことは一切考慮されません。 Keyの値が同一であれば、Valueの値に関わらず同一の要素とみなされます。
|
|
|
|
|
|
|
~ |
SortedSetのコンストラクタで[[IComparer<T>を明示的に指定>#Comparison]]しない場合、最小値・最大値は[[デフォルトのソート順(大小関係)>programming/netfx/sorting/0_basictypes_1_defaultsortorder]]に従って求められます。 一方、コンストラクタでIComparer<T>を指定した場合は、そのIComparer<T>で定義される大小関係に従って最小値・最大値が求められることになります。
|
*大小比較のカスタマイズ [#Comparison]
|
- |
|
SortedSetのコンストラクタで適切なIComparer<T>インターフェイスを指定することで、要素の大小関係比較時の動作をカスタマイズ出来ます。 例えば、比較の際に大文字小文字の違いを無視するようにするといったことが出来ます。
|
|
|
|
~ |
つまり、Minプロパティ・Maxプロパティは、IComparer<T>で定義されるソート順でSortedSetをソート(あるいは列挙)したときの、最初または最後の要素を取得するプロパティとなります。 このため、Minプロパティ・Maxプロパティで取得できる値が、常に''値としての最小値・最大値''になるとは限りません。
|
以下は、大文字小文字を無視し、アルファベット順とは逆順(Z-Aの順)になるようにソートするIComparer<string>を実装し、SortedSetでの並べ替え順をカスタマイズする例です。
|
|
|
|
~ |
デフォルトとは逆順に並べ替えるIComparer<T>を指定してSortedSetを作成し、Minプロパティ・Maxプロパティが返す値の違いを見ると次のようになります。
|
#tabpage(codelang=cs,container-title=大文字小文字の違いを無視し、アルファベット順とは逆順に並べ替えるSortedSetを作成する)
|
+ |
|
|
+ |
#tabpage(codelang=cs,container-title=Min・Maxプロパティを参照してSortedSetの並べ替え順での最初・最後の要素を取得する)
|
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
|
|
|
~ |
class ReverseIntComparer : IComparer<int> {
|
// 大文字小文字の違いを無視し、アルファベット順とは逆順にソートするためのIComparer
|
~ |
public int Compare(int x, int y)
|
class CaseInsensitiveReverseStringComparer : IComparer<string> {
|
~ |
// intのデフォルトの大小関係とは逆の結果を返すようにする
|
public int Compare(string x, string y)
|
~ |
=> -1 * Comparer<int>.Default.Compare(x, y);
|
{
|
- |
|
// StringComparer.CurrentCultureIgnoreCase.Compareとは逆の結果を返すようにする
|
- |
|
return -1 * StringComparer.CurrentCultureIgnoreCase.Compare(x, y);
|
- |
|
}
|
|
}
|
}
|
|
|
|
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
// デフォルトの順序で並べ替えを行うSortedSet
|
string[] names = new string[] {"Alice", "Eve", "Charlie", "Bob", "Dave"};
|
~ |
var s1 = new SortedSet<int>() {0, 1, 2, 3, 4};
|
|
- |
|
SortedSet<string> caseSensitiveSet = new SortedSet<string>(names, StringComparer.CurrentCulture);
|
- |
|
SortedSet<string> reverseCaseInsensitiveSet = new SortedSet<string>(names, new CaseInsensitiveReverseStringComparer());
|
- |
|
|
- |
|
Console.WriteLine("caseSensitiveSet");
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s1));
|
foreach (string e in caseSensitiveSet) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
|
|
|
+ |
// 最小値と最大値を表示
|
|
+ |
// (デフォルトのソート順での最初と最後の要素を表示する)
|
|
+ |
Console.WriteLine($"Mix: {s1.Min}");
|
|
+ |
Console.WriteLine($"Max: {s1.Max}");
|
|
|
Console.WriteLine();
|
Console.WriteLine();
|
|
|
|
~ |
// デフォルトとは逆の順序で並べ替えを行うSortedSet
|
Console.WriteLine(caseSensitiveSet.IsSupersetOf(new string[] {"aLiCe", "BOB", "dave"}));
|
~ |
var s2 = new SortedSet<int>(new ReverseIntComparer()) {0, 1, 2, 3, 4};
|
|
- |
|
Console.WriteLine("reverseCaseInsensitiveSet");
|
- |
|
|
- |
|
foreach (string e in reverseCaseInsensitiveSet) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
|
|
|
~ |
Console.WriteLine(string.Join(", ", s2));
|
Console.WriteLine();
|
|
|
|
~ |
// 最小値と最大値を表示
|
Console.WriteLine(reverseCaseInsensitiveSet.IsSupersetOf(new string[] {"aLiCe", "BOB", "dave"}));
|
+ |
// (デフォルトとは逆のソート順での最初と最後の要素を表示する)
|
|
+ |
Console.WriteLine($"Mix: {s2.Min}");
|
|
+ |
Console.WriteLine($"Max: {s2.Max}");
|
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
1831,112 |
1192,68 |
|
Imports System
|
Imports System
|
|
Imports System.Collections.Generic
|
Imports System.Collections.Generic
|
|
|
|
~ |
Class ReverseIntegerComparer
|
' 大文字小文字の違いを無視し、アルファベット順とは逆順にソートするためのIComparer
|
~ |
Implements IComparer(Of Integer)
|
Class CaseInsensitiveReverseStringComparer
|
- |
|
Implements IComparer(Of String)
|
|
|
|
~ |
Public Function Compare(ByVal x As Integer, ByVal y As Integer) As Integer Implements IComparer(Of Integer).Compare
|
Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements IComparer(Of String).Compare
|
~ |
' Integerのデフォルトの大小関係とは逆の結果を返すようにする
|
' StringComparer.CurrentCultureIgnoreCase.Compareとは逆の結果を返すようにする
|
~ |
Return -1 * Comparer(Of Integer).Default.Compare(x, y)
|
Return -1 * StringComparer.CurrentCultureIgnoreCase.Compare(x, y)
|
|
End Function
|
End Function
|
|
End Class
|
End Class
|
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
' デフォルトの順序で並べ替えを行うSortedSet
|
Dim names() As String = New String() {"Alice", "Bob", "Charlie", "Dave", "Eve"}
|
+ |
Dim s1 As New SortedSet(Of Integer)() From {0, 1, 2, 3, 4}
|
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s1))
|
Dim caseSensitiveSet As New SortedSet(Of String)(names, StringComparer.CurrentCulture)
|
- |
|
Dim reverseCaseInsensitiveSet As New SortedSet(Of String)(names, New CaseInsensitiveReverseStringComparer())
|
|
|
|
~ |
' 最小値と最大値を表示
|
Console.WriteLine("caseSensitiveSet")
|
+ |
' (デフォルトのソート順での最初と最後の要素を表示する)
|
|
+ |
Console.WriteLine($"Mix: {s1.Min}")
|
|
+ |
Console.WriteLine($"Max: {s1.Max}")
|
|
+ |
Console.WriteLine()
|
|
|
|
|
~ |
' デフォルトとは逆の順序で並べ替えを行うSortedSet
|
For Each e As String In caseSensitiveSet
|
~ |
Dim s2 As New SortedSet(Of Integer)(New ReverseIntegerComparer()) From {0, 1, 2, 3, 4}
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
|
|
|
~ |
Console.WriteLine(String.Join(", ", s2))
|
Console.WriteLine()
|
|
|
|
~ |
' 最小値と最大値を表示
|
Console.WriteLine(caseSensitiveSet.IsSupersetOf(New String() {"aLiCe", "BOB", "dave"}))
|
+ |
' (デフォルトとは逆のソート順での最初と最後の要素を表示する)
|
|
+ |
Console.WriteLine($"Mix: {s2.Min}")
|
|
+ |
Console.WriteLine($"Max: {s2.Max}")
|
|
+ |
End Sub
|
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
|
|
|
~ |
#prompt(実行結果){{
|
Console.WriteLine("reverseCaseInsensitiveSet")
|
+ |
0, 1, 2, 3, 4
|
|
+ |
Mix: 0
|
|
+ |
Max: 4
|
|
|
|
|
~ |
4, 3, 2, 1, 0
|
For Each e As String In reverseCaseInsensitiveSet
|
~ |
Mix: 4
|
Console.Write("{0}, ", e)
|
~ |
Max: 0
|
Next
|
+ |
}}
|
|
|
|
|
- |
|
Console.WriteLine()
|
|
|
|
~ |
***HashSetでの最小値・最大値の取得
|
Console.WriteLine(reverseCaseInsensitiveSet.IsSupersetOf(New String() {"aLiCe", "BOB", "dave"}))
|
+ |
SortedSetと異なり、HashSetからは直接最小値・最大値を取得することはできませんが、LINQの拡張メソッドである&msdn(netfx,member,System.Linq.Enumerable.Min){Minメソッド};と&msdn(netfx,member,System.Linq.Enumerable.Max){Maxメソッド};を使うことで求めることができます。
|
|
+ |
|
|
+ |
#tabpage(codelang=cs,container-title=LINQのMin・Maxメソッドを使ってHashSet内の最小値・最大値を取得する)
|
|
+ |
#code{{
|
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
+ |
using System.Linq;
|
|
+ |
|
|
+ |
class Sample {
|
|
+ |
static void Main()
|
|
+ |
{
|
|
+ |
var s = new HashSet<string>() {"Alice", "Eve", "Charlie", "Bob", "Dave"};
|
|
+ |
|
|
+ |
// 最小値と最大値を表示
|
|
+ |
Console.WriteLine($"Mix: {s.Min()}");
|
|
+ |
Console.WriteLine($"Max: {s.Max()}");
|
|
+ |
}
|
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
+ |
Imports System.Linq
|
|
+ |
|
|
+ |
Class Sample
|
|
+ |
Shared Sub Main()
|
|
+ |
Dim s As New HashSet(Of String) From {"Alice", "Eve", "Charlie", "Bob", "Dave"}
|
|
+ |
|
|
+ |
' 最小値と最大値を表示
|
|
+ |
Console.WriteLine($"Mix: {s.Min()}")
|
|
+ |
Console.WriteLine($"Max: {s.Max()}")
|
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
Mix: Alice
|
caseSensitiveSet
|
~ |
Max: Eve
|
Alice, Bob, Charlie, Dave, Eve,
|
- |
|
False
|
- |
|
reverseCaseInsensitiveSet
|
- |
|
Eve, Dave, Charlie, Bob, Alice,
|
- |
|
True
|
|
}}
|
}}
|
|
|
|
|
#remarks
|
#remarks
|
~ |
LINQのMinメソッド・Maxメソッドでは全要素を走査した上で最大・最小の要素を返します。 このメソッドをSortedSetに対しても用いることはできますが、無駄が多く、SortedListのMinプロパティ・Maxプロパティの代わりとしてMinメソッド・Maxメソッドを用いる利点はありません。
|
IComparer<T>インターフェイスや実装例については[[programming/netfx/comparison/0_comparison]]を参照してください。
|
|
#remarks-end
|
#remarks-end
|
|
|
|
|
|
|
- |
|
*SortedSetのみで提供される操作
|
- |
|
以下の操作はHashSetには用意されておらず、SortedSetのみで提供される操作です。
|
|
|
|
- |
|
**最大値・最小値
|
- |
|
SortedListクラスでは、&msdn(netfx,member,System.Collections.Generic.SortedSet`1.Max){Maxプロパティ};および&msdn(netfx,member,System.Collections.Generic.SortedSet`1.Min){Minプロパティ};で集合内での最大・最小の要素を取得することが出来ます。
|
|
|
|
~ |
|
#tabpage(codelang=cs,container-title=Maxプロパティ・Minプロパティを参照してSortedList内の最大・最小の要素を取得する)
|
+ |
|
|
+ |
|
|
+ |
**逆順での列挙 [#SortedSet.Reverse]
|
|
+ |
&msdn(netfx,member,System.Collections.Generic.SortedSet`1.Reverse){Reverseメソッド};は、SortedSetを通常とは逆順に列挙する列挙子(IEnumerator<T>)を返します。 つまり、Reverseメソッドを使うと、SortedSet内の要素を逆の順序で列挙することができます。
|
|
+ |
|
|
+ |
Reverseメソッドは、&msdn(netfx,member,System.Array.Reverse){Array.Reverseメソッド};や&msdn(netfx,member,System.Collections.Generic.List`1.Reverse){List.Reverseメソッド};とは異なり、SortedSet内の要素の並びを変更しない、非破壊的なメソッドです。 Reverseメソッドを呼び出してもSortedSet内の要素は逆順にはならず、あくまで''逆順で列挙する列挙子を返すだけ''という点に注意してください。
|
|
+ |
|
|
+ |
#tabpage(codelang=cs,container-title=Reverseメソッドを使ってSortedSet内の要素を逆順で列挙する)
|
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
|
1944,18 |
1261,23 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new SortedSet<int>() {5, 1, 0, 3, 4, 2};
|
SortedSet<string> s = new SortedSet<string>(new string[] {"Alice", "Eve", "Charlie", "Bob", "Dave"});
|
- |
|
|
- |
|
Console.Write("Set: ");
|
- |
|
|
- |
|
Print(s);
|
- |
|
|
- |
|
// 最小値と最大値を表示
|
- |
|
Console.WriteLine("Mix: {0}", s.Min);
|
- |
|
Console.WriteLine("Max: {0}", s.Max);
|
- |
|
}
|
|
|
|
~ |
// デフォルトの順序でSortedSetを列挙
|
static void Print(SortedSet<string> s)
|
~ |
foreach (int e in s) {
|
{
|
- |
|
foreach (string e in s) {
|
|
Console.Write("{0}, ", e);
|
Console.Write("{0}, ", e);
|
|
}
|
}
|
+ |
Console.WriteLine();
|
|
|
|
|
+ |
// デフォルトとは逆順でSortedSetを列挙
|
|
+ |
foreach (int e in s.Reverse()) {
|
|
+ |
Console.Write("{0}, ", e);
|
|
+ |
}
|
|
|
Console.WriteLine();
|
Console.WriteLine();
|
|
}
|
}
|
|
}
|
}
|
|
1967,18 |
1289,22 |
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
Dim s As New SortedSet(Of Integer) From {5, 1, 0, 3, 4, 2}
|
Dim s As New SortedSet(Of String)(New String() {"Alice", "Eve", "Charlie", "Bob", "Dave"})
|
- |
|
|
- |
|
Console.Write("Set: ")
|
|
|
|
~ |
' デフォルトの順序でSortedSetを列挙
|
Print(s)
|
~ |
For Each e As Integer In s
|
|
- |
|
' 最小値と最大値を表示
|
- |
|
Console.WriteLine("Mix: {0}", s.Min)
|
- |
|
Console.WriteLine("Max: {0}", s.Max)
|
- |
|
End Sub
|
- |
|
|
- |
|
Shared Sub Print(ByVal s As SortedSet(Of String))
|
- |
|
For Each e As String In s
|
|
Console.Write("{0}, ", e)
|
Console.Write("{0}, ", e)
|
|
Next
|
Next
|
+ |
Console.WriteLine()
|
|
|
|
|
+ |
' デフォルトとは逆順でSortedSetを列挙
|
|
+ |
For Each e As Integer In s.Reverse()
|
|
+ |
Console.Write("{0}, ", e)
|
|
+ |
Next
|
|
|
Console.WriteLine()
|
Console.WriteLine()
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
1986,29 |
1312,29 |
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
5, 4, 3, 2, 1, 0,
|
Set: Alice, Bob, Charlie, Dave, Eve,
|
~ |
0, 1, 2, 3, 4, 5,
|
Mix: Alice
|
- |
|
Max: Eve
|
|
}}
|
}}
|
|
|
|
|
|
|
|
|
|
~ |
Reverseメソッドは列挙だけでなく、IEnumerator<T>を引数にとるメソッドやLINQのメソッドに渡して使うこともできます。
|
HashSetで最大・最小の要素を取得したい場合には、LINQの拡張メソッドである&msdn(netfx,member,System.Linq.Enumerable.Max){Maxメソッド};と&msdn(netfx,member,System.Linq.Enumerable.Min){Minメソッド};を使うことができます。
|
|
|
|
~ |
#tabpage(codelang=cs,container-title=Reverseメソッドを使ってSortedSet内の要素を逆順で文字列として結合する)
|
#tabpage(codelang=cs)
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
- |
|
using System.Linq;
|
|
|
|
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new SortedSet<int>() {5, 1, 0, 3, 4, 2};
|
HashSet<string> s = new HashSet<string>(new string[] {"Alice", "Eve", "Charlie", "Bob", "Dave"});
|
|
|
|
~ |
// デフォルトの順序でSortedSet内の要素を文字列として結合する
|
// 最小値と最大値を表示
|
~ |
Console.WriteLine(string.Join(", ", s));
|
Console.WriteLine("Mix: {0}", s.Min());
|
~ |
|
Console.WriteLine("Max: {0}", s.Max());
|
+ |
// デフォルトとは逆順でSortedSet内の要素を文字列として結合する
|
|
+ |
Console.WriteLine(string.Join(", ", s.Reverse()));
|
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
2016,44 |
1342,54 |
|
#code{{
|
#code{{
|
|
Imports System
|
Imports System
|
|
Imports System.Collections.Generic
|
Imports System.Collections.Generic
|
- |
|
Imports System.Linq
|
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
Dim s As New SortedSet(Of Integer) From {5, 1, 0, 3, 4, 2}
|
Dim s As New HashSet(Of String)(New String() {"Alice", "Eve", "Charlie", "Bob", "Dave"})
|
|
|
|
~ |
' デフォルトの順序でSortedSet内の要素を文字列として結合する
|
' 最小値と最大値を表示
|
~ |
Console.WriteLine(String.Join(", ", s))
|
Console.WriteLine("Mix: {0}", s.Min)
|
~ |
|
Console.WriteLine("Max: {0}", s.Max)
|
+ |
' デフォルトとは逆順でSortedSet内の要素を文字列として結合する
|
|
+ |
Console.WriteLine(String.Join(", ", s.Reverse()))
|
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
0, 1, 2, 3, 4, 5
|
Mix: Adams
|
~ |
5, 4, 3, 2, 1, 0
|
Max: Eve
|
|
}}
|
}}
|
|
|
|
~ |
#tabpage(codelang=cs,container-title=Reverseメソッドを使ってSortedSet内の要素の一部分を逆順で取得する)
|
#remarks
|
- |
|
SortedSetに対してもLINQのMaxメソッド・Minメソッドを用いることはできます。 ただし、LINQのMaxメソッド・Minメソッドでは全要素を操作した上で最大・最小の要素を返すため無駄が多く、SortedListではMaxプロパティ・Minプロパティの代わりにMaxメソッド・Minメソッドを用いる利点はありません。
|
- |
|
#remarks-end
|
- |
|
|
- |
|
|
- |
|
**逆順での列挙
|
- |
|
&msdn(netfx,member,System.Collections.Generic.SortedSet`1.Reverse){Reverseメソッド};は、SortedSetを通常とは逆順に列挙する列挙子(IEnumerator<T>)を返します。 &msdn(netfx,member,System.Array.Reverse){Array.Reverseメソッド};や&msdn(netfx,member,System.Collections.Generic.List`1.Reverse){List.Reverseメソッド};とは異なり、コレクション内の要素の並びは変わりません。 Reverseメソッドを呼び出してもコレクション内の要素は逆順にはならず、あくまで''逆順に列挙する列挙子を返すだけ''という点に注意してください。
|
- |
|
|
- |
|
#tabpage(codelang=cs,container-title=Reverseメソッドを使ってSortedSet内の要素を逆順で列挙する)
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
+ |
using System.Linq;
|
|
|
|
|
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new SortedSet<int>() {5, 1, 0, 3, 4, 2};
|
SortedSet<int> s = new SortedSet<int>(new int[] {5, 1, 0, 3, 4, 2});
|
- |
|
|
- |
|
foreach (int e in s.Reverse()) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
- |
|
|
- |
|
Console.WriteLine();
|
|
|
|
~ |
// デフォルトの順序でSortedSet内の要素の一部分を取得する
|
foreach (int e in s) {
|
~ |
// (最初の要素から1つスキップしたのち、3つ分を取得する)
|
Console.Write("{0}, ", e);
|
~ |
Console.WriteLine(string.Join(", ", s.Skip(1).Take(3)));
|
}
|
|
|
|
~ |
// デフォルトとは逆順でSortedSet内の要素の一部分を取得する
|
Console.WriteLine();
|
+ |
// (最初の要素から1つスキップしたのち、3つ分を取得する)
|
|
+ |
Console.WriteLine(string.Join(", ", s.Reverse().Skip(1).Take(3)));
|
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
2061,35 |
1397,37 |
|
#code{{
|
#code{{
|
|
Imports System
|
Imports System
|
|
Imports System.Collections.Generic
|
Imports System.Collections.Generic
|
+ |
Imports System.Linq
|
|
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
Dim s As New SortedSet(Of Integer) From {5, 1, 0, 3, 4, 2}
|
Dim s As New SortedSet(Of Integer)(New Integer() {5, 1, 0, 3, 4, 2})
|
- |
|
|
- |
|
For Each e As Integer In s.Reverse()
|
- |
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
- |
|
|
- |
|
Console.WriteLine()
|
|
|
|
~ |
' デフォルトの順序でSortedSet内の要素の一部分を取得する
|
For Each e As Integer In s
|
~ |
' (最初の要素から1つスキップしたのち、3つ分を取得する)
|
Console.Write("{0}, ", e)
|
~ |
Console.WriteLine(String.Join(", ", s.Skip(1).Take(3)))
|
Next
|
|
|
|
~ |
' デフォルトとは逆順でSortedSet内の要素の一部分を取得する
|
Console.WriteLine()
|
+ |
' (最初の要素から1つスキップしたのち、3つ分を取得する)
|
|
+ |
Console.WriteLine(String.Join(", ", s.Reverse().Skip(1).Take(3)))
|
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
1, 2, 3
|
5, 4, 3, 2, 1, 0,
|
~ |
4, 3, 2
|
0, 1, 2, 3, 4, 5,
|
|
}}
|
}}
|
|
|
|
|
|
|
+ |
|
|
|
**部分集合(サブセット)の取得
|
**部分集合(サブセット)の取得
|
~ |
&msdn(netfx,member,System.Collections.Generic.SortedSet`1.GetViewBetween){GetViewBetweenメソッド};を使うと、指定した範囲に該当する部分集合をSortedSet<T>として取得出来ます。
|
SortedListクラスでは、&msdn(netfx,member,System.Collections.Generic.SortedSet`1.GetViewBetween){GetViewBetweenメソッド};を使うと、指定した範囲に該当する部分集合をSortedSet<T>で取得出来ます。
|
|
|
|
~ |
#tabpage(codelang=cs,container-title=GetViewBetweenメソッドを使ってSortedSet内の指定した範囲にある数値の部分集合を取得する)
|
#tabpage(codelang=cs,container-title=GetViewBetweenメソッドを使ってSortedSetの指定した範囲の部分集合を取得する)
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
|
2097,53 |
1435,30 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new SortedSet<int>() {5, 1, 0, 3, 4, 2};
|
SortedSet<string> s = new SortedSet<string>(new string[] {"Adams", "Cyndy", "Dave", "Bob", "Charlie", "Elliott", "Becky", "Alice", "Diana", "Eve"});
|
- |
|
|
- |
|
Console.Write("Set: ");
|
|
|
|
~ |
// 最小で1、最大で4の範囲に該当する部分集合を取得する
|
Print(s);
|
+ |
Console.WriteLine(string.Join(", ", s.GetViewBetween(1, 4)));
|
|
+ |
}
|
|
+ |
}
|
|
+ |
}}
|
|
+ |
#tabpage(codelang=vb)
|
|
+ |
#code{{
|
|
+ |
Imports System
|
|
+ |
Imports System.Collections.Generic
|
|
|
|
|
~ |
Class Sample
|
// 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
~ |
Shared Sub Main()
|
Console.Write("GetViewBetween(B, E): ");
|
+ |
Dim s As New SortedSet(Of Integer) From {5, 1, 0, 3, 4, 2}
|
|
|
|
|
~ |
' 最小で1、最大で4の範囲に該当する部分集合を取得する
|
Print(s.GetViewBetween("B", "E"));
|
+ |
Console.WriteLine(String.Join(", ", s.GetViewBetween(1, 4)))
|
|
+ |
End Sub
|
|
+ |
End Class
|
|
+ |
}}
|
|
+ |
#tabpage-end
|
|
|
|
|
~ |
#prompt(実行結果){{
|
// 最小で"Ae"、最大で"Bz"の範囲に該当する部分集合を取得する
|
~ |
1, 2, 3, 4
|
Console.Write("GetViewBetween(Ae, Bz): ");
|
+ |
}}
|
|
|
|
|
~ |
#tabpage(codelang=cs,container-title=GetViewBetweenメソッドを使ってSortedSet内の指定した範囲にある文字列の部分集合を取得する)
|
Print(s.GetViewBetween("Ae", "Bz"));
|
~ |
#code{{
|
}
|
+ |
using System;
|
|
+ |
using System.Collections.Generic;
|
|
|
|
|
~ |
class Sample {
|
static void Print(SortedSet<string> s)
|
+ |
static void Main()
|
|
|
{
|
{
|
~ |
var s = new SortedSet<string>() {"Adams", "Cyndy", "Dave", "Bob", "Charlie", "Elliott", "Becky", "Alice", "Diana", "Eve"};
|
foreach (string e in s) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
|
|
|
~ |
Console.Write("Set: ");
|
Console.WriteLine();
|
+ |
Console.WriteLine(string.Join(", ", s));
|
|
+ |
|
|
+ |
// 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
|
+ |
Console.Write("GetViewBetween(B, E): ");
|
|
+ |
Console.WriteLine(string.Join(", ", s.GetViewBetween("B", "E")));
|
|
+ |
|
|
+ |
// 最小で"Ae"、最大で"Bz"の範囲に該当する部分集合を取得する
|
|
+ |
Console.Write("GetViewBetween(Ae, Bz): ");
|
|
+ |
Console.WriteLine(string.Join(", ", s.GetViewBetween("Ae", "Bz")));
|
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
2154,34 |
1469,45 |
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
Dim s As New SortedSet(Of String) From {"Adams", "Cyndy", "Dave", "Bob", "Charlie", "Elliott", "Becky", "Alice", "Diana", "Eve"}
|
Dim s As New SortedSet(Of String)(New String() {"Adams", "Cyndy", "Dave", "Bob", "Charlie", "Elliott", "Becky", "Alice", "Diana", "Eve"})
|
|
|
|
|
Console.Write("Set: ")
|
Console.Write("Set: ")
|
~ |
Console.WriteLine(String.Join(", ", s))
|
|
- |
|
Print(s)
|
|
|
|
|
' 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
' 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
|
Console.Write("GetViewBetween(B, E): ")
|
Console.Write("GetViewBetween(B, E): ")
|
~ |
Console.WriteLine(String.Join(", ", s.GetViewBetween("B", "E")))
|
|
- |
|
Print(s.GetViewBetween("B", "E"))
|
|
|
|
|
' 最小で"Ae"、最大で"Bz"の範囲に該当する部分集合を取得する
|
' 最小で"Ae"、最大で"Bz"の範囲に該当する部分集合を取得する
|
|
Console.Write("GetViewBetween(Ae, Bz): ")
|
Console.Write("GetViewBetween(Ae, Bz): ")
|
~ |
Console.WriteLine(String.Join(", ", s.GetViewBetween("Ae", "Bz")))
|
|
- |
|
Print(s.GetViewBetween("Ae", "Bz"))
|
- |
|
End Sub
|
- |
|
|
- |
|
Shared Sub Print(ByVal s As SortedSet(Of String))
|
- |
|
For Each e As String In s
|
- |
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
- |
|
|
- |
|
Console.WriteLine()
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
Set: Adams, Alice, Becky, Bob, Charlie, Cyndy, Dave, Diana, Elliott, Eve
|
Set: Adams, Alice, Becky, Bob, Charlie, Cyndy, Dave, Diana, Elliott, Eve,
|
~ |
GetViewBetween(B, E): Becky, Bob, Charlie, Cyndy, Dave, Diana
|
GetViewBetween(B, E): Becky, Bob, Charlie, Cyndy, Dave, Diana,
|
~ |
GetViewBetween(Ae, Bz): Alice, Becky, Bob
|
GetViewBetween(Ae, Bz): Alice, Becky, Bob,
|
|
}}
|
}}
|
|
|
|
|
|
|
+ |
***部分集合のSortedSetと元のSortedSetに対する変更
|
|
+ |
GetViewBetweenメソッドは、引数で指定した範囲に該当する部分の''ビュー''を返します。 SortedSetの一部をコピーしたものが返されるわけではないため、元になったSortedSetに変更を加えると、GetViewBetweenメソッドで取得したサブセットにも反映されます。
|
|
|
|
|
~ |
#tabpage(codelang=cs,container-title=GetViewBetweenメソッドで部分集合を取得した後に元のSortedSetに変更を加える)
|
GetViewBetweenメソッドは、引数で指定した範囲に該当する部分の''ビュー''を返します。 SortedSetの一部をコピーしたものが返されるわけではないため、元になったSortedSetに変更を加えるとGetViewBetweenメソッドで取得したサブセットにも反映されます。
|
- |
|
|
- |
|
#tabpage(codelang=cs,container-title=GetViewBetweenメソッドで部分集合を取得した後に元の集合に変更を加える)
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
|
2189,23 |
1515,34 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new SortedSet<string>() {"Alice", "Eve", "Charlie", "Bob", "Dave"};
|
SortedSet<string> s = new SortedSet<string>(new string[] {"Alice", "Eve", "Charlie", "Bob", "Dave"});
|
|
|
|
|
Console.Write("s: ");
|
Console.Write("s: ");
|
~ |
Console.WriteLine(string.Join(", ", s));
|
|
- |
|
Print(s);
|
|
|
|
|
// 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
// 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
~ |
var view = s.GetViewBetween("B", "E");
|
SortedSet<string> view = s.GetViewBetween("B", "E");
|
|
|
|
|
Console.Write("view: ");
|
Console.Write("view: ");
|
~ |
Console.WriteLine(string.Join(", ", view));
|
|
- |
|
Print(view);
|
|
|
|
|
// 元の集合に変更を加える
|
// 元の集合に変更を加える
|
+ |
// (上で取得した部分集合にも影響する)
|
|
|
s.Add("Diana");
|
s.Add("Diana");
|
|
|
|
|
Console.Write("view: ");
|
Console.Write("view: ");
|
~ |
Console.WriteLine(string.Join(", ", view));
|
|
- |
|
Print(view);
|
- |
|
}
|
- |
|
|
- |
|
static void Print(SortedSet<string> s)
|
- |
|
{
|
- |
|
foreach (string e in s) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
- |
|
|
- |
|
Console.WriteLine();
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
2216,37 |
1553,47 |
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
Dim s As New SortedSet(Of String)() From {"Alice", "Eve", "Charlie", "Bob", "Dave"}
|
Dim s As New SortedSet(Of String)(New String() {"Alice", "Eve", "Charlie", "Bob", "Dave"})
|
|
|
|
|
Console.Write("s: ")
|
Console.Write("s: ")
|
~ |
Console.WriteLine(String.Join(", ", s))
|
|
- |
|
Print(s)
|
|
|
|
|
' 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
' 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
|
Dim view As SortedSet(Of String) = s.GetViewBetween("B", "E")
|
Dim view As SortedSet(Of String) = s.GetViewBetween("B", "E")
|
|
|
|
|
Console.Write("view: ")
|
Console.Write("view: ")
|
~ |
Console.WriteLine(String.Join(", ", view))
|
|
- |
|
Print(view)
|
|
|
|
|
' 元の集合に変更を加える
|
' 元の集合に変更を加える
|
+ |
' (上で取得した部分集合にも影響する)
|
|
|
s.Add("Diana")
|
s.Add("Diana")
|
|
|
|
|
Console.Write("view: ")
|
Console.Write("view: ")
|
~ |
Console.WriteLine(String.Join(", ", view))
|
|
- |
|
Print(view)
|
- |
|
End Sub
|
- |
|
|
- |
|
Shared Sub Print(ByVal s As SortedSet(Of String))
|
- |
|
For Each e As String In s
|
- |
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
- |
|
|
- |
|
Console.WriteLine()
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
s: Alice, Bob, Charlie, Dave, Eve
|
s: Alice, Bob, Charlie, Dave, Eve,
|
~ |
view: Bob, Charlie, Dave
|
view: Bob, Charlie, Dave,
|
~ |
view: Bob, Charlie, Dave, Diana
|
view: Bob, Charlie, Dave, Diana,
|
|
}}
|
}}
|
|
|
|
|
|
|
|
|
|
~ |
また逆に、GetViewBetweenメソッドで取得したサブセットに変更を加えることもでき、この変更は元になったSortedSetにも反映されます。
|
また逆に、GetViewBetweenメソッドで取得したサブセットに変更を加えることもでき、変更は元になったSortedSetに反映されます。
|
|
|
|
|
#tabpage(codelang=cs,container-title=GetViewBetweenメソッドで取得した部分集合を通して元のSortedSetに変更を加える)
|
#tabpage(codelang=cs,container-title=GetViewBetweenメソッドで取得した部分集合を通して元のSortedSetに変更を加える)
|
|
#code{{
|
#code{{
|
|
2256,23 |
1603,30 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new SortedSet<string>() {"Alice", "Eve", "Charlie", "Bob", "Dave"};
|
SortedSet<string> s = new SortedSet<string>(new string[] {"Alice", "Eve", "Charlie", "Bob", "Dave"});
|
|
|
|
|
Console.Write("s: ");
|
Console.Write("s: ");
|
~ |
Console.WriteLine(string.Join(", ", s));
|
|
- |
|
Print(s);
|
|
|
|
|
// 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
// 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
~ |
var view = s.GetViewBetween("B", "E");
|
SortedSet<string> view = s.GetViewBetween("B", "E");
|
+ |
|
|
+ |
Console.Write("view: ");
|
|
+ |
Console.WriteLine(string.Join(", ", view));
|
|
|
|
|
|
// 取得した部分集合に変更を加える
|
// 取得した部分集合に変更を加える
|
+ |
// (取得した部分集合の元になるSortedSetにも影響する)
|
|
|
view.Add("Cyndy");
|
view.Add("Cyndy");
|
|
|
|
|
Console.Write("s: ");
|
Console.Write("s: ");
|
~ |
Console.WriteLine(string.Join(", ", s));
|
|
- |
|
Print(s);
|
- |
|
}
|
- |
|
|
- |
|
static void Print(SortedSet<string> s)
|
- |
|
{
|
- |
|
foreach (string e in s) {
|
- |
|
Console.Write("{0}, ", e);
|
- |
|
}
|
- |
|
|
- |
|
Console.WriteLine();
|
|
}
|
}
|
|
}
|
}
|
|
}}
|
}}
|
|
2283,23 |
1637,29 |
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
Dim s As New SortedSet(Of String)() From {"Alice", "Eve", "Charlie", "Bob", "Dave"}
|
Dim s As New SortedSet(Of String)(New String() {"Alice", "Eve", "Charlie", "Bob", "Dave"})
|
|
|
|
|
Console.Write("s: ")
|
Console.Write("s: ")
|
~ |
Console.WriteLine(String.Join(", ", s))
|
|
- |
|
Print(s)
|
|
|
|
|
' 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
' 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
|
Dim view As SortedSet(Of String) = s.GetViewBetween("B", "E")
|
Dim view As SortedSet(Of String) = s.GetViewBetween("B", "E")
|
|
|
|
+ |
Console.Write("view: ")
|
|
+ |
Console.WriteLine(String.Join(", ", view))
|
|
+ |
|
|
|
' 取得した部分集合に変更を加える
|
' 取得した部分集合に変更を加える
|
+ |
' (取得した部分集合の元になるSortedSetにも影響する)
|
|
|
view.Add("Cyndy")
|
view.Add("Cyndy")
|
|
|
|
|
Console.Write("s: ")
|
Console.Write("s: ")
|
~ |
Console.WriteLine(String.Join(", ", s))
|
|
- |
|
Print(s)
|
- |
|
End Sub
|
- |
|
|
- |
|
Shared Sub Print(ByVal s As SortedSet(Of String))
|
- |
|
For Each e As String In s
|
- |
|
Console.Write("{0}, ", e)
|
- |
|
Next
|
- |
|
|
- |
|
Console.WriteLine()
|
|
End Sub
|
End Sub
|
|
End Class
|
End Class
|
|
}}
|
}}
|
|
2310,17 |
1670,11 |
|
s: Alice, Bob, Charlie, Cyndy, Dave, Eve,
|
s: Alice, Bob, Charlie, Cyndy, Dave, Eve,
|
|
}}
|
}}
|
|
|
|
+ |
SortedSetには、そのSortedSetがGetViewBetweenメソッドによって取得されたビューなのかどうかを判断するプロパティやメソッドは用意されていません。 そのため、GetViewBetweenメソッドによって取得したSortedSetに対して変更を行う際には、影響範囲に注意する必要があります。
|
|
+ |
|
|
+ |
#remarks
|
|
+ |
.NET 5以降では、&msdn(netfx,type,System.Collections.Generic.IReadOnlySet`1){IReadOnlySet<T>インターフェイス};が新たに導入されるため、GetViewBetweenメソッドで取得したSortedSetをIReadOnlySet<T>にキャストすることにより、読み取り専用のビューとして扱うことができるようになります。
|
|
+ |
#remarks-end
|
|
|
|
|
|
|
|
- |
|
GetViewBetweenメソッドで指定した範囲外の値をサブセットに対して追加しようとした場合には&msdn(netfx,type,System.ArgumentOutOfRangeException){ArgumentOutOfRangeException};がスローされます。 GetViewBetweenメソッドで取得したサブセットからはその範囲内に対してのみ操作が行えます。
|
|
|
|
~ |
GetViewBetweenメソッドで指定した範囲外の値をサブセットに対して追加しようとした場合、&msdn(netfx,type,System.ArgumentOutOfRangeException){ArgumentOutOfRangeException};がスローされます。 GetViewBetweenメソッドで取得したサブセットに対しては、その範囲内に対する変更のみが行えます。
|
#tabpage(codelang=cs,container-title=GetViewBetweenメソッドで指定した範囲外に変更を加える)
|
+ |
|
|
+ |
#tabpage(codelang=cs,container-title=GetViewBetweenメソッドで指定した範囲外に変更を加えようとするとArgumentOutOfRangeExceptionとなる)
|
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
|
2328,10 |
1682,10 |
|
class Sample {
|
class Sample {
|
|
static void Main()
|
static void Main()
|
|
{
|
{
|
~ |
var s = new SortedSet<string>() {"Alice", "Eve", "Charlie", "Bob", "Dave"};
|
SortedSet<string> s = new SortedSet<string>(new string[] {"Alice", "Eve", "Charlie", "Bob", "Dave"});
|
|
|
|
|
// 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
// 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
~ |
var view = s.GetViewBetween("B", "E");
|
SortedSet<string> view = s.GetViewBetween("B", "E");
|
|
|
|
|
// ビューの範囲外の値を追加しようとする
|
// ビューの範囲外の値を追加しようとする
|
|
view.Add("Adams");
|
view.Add("Adams");
|
|
2345,7 |
1699,7 |
|
|
|
|
Class Sample
|
Class Sample
|
|
Shared Sub Main()
|
Shared Sub Main()
|
~ |
Dim s As New SortedSet(Of String)() From {"Alice", "Eve", "Charlie", "Bob", "Dave"}
|
Dim s As New SortedSet(Of String)(New String() {"Alice", "Eve", "Charlie", "Bob", "Dave"})
|
|
|
|
|
' 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
' 最小で"B"、最大で"E"の範囲に該当する部分集合を取得する
|
|
Dim view As SortedSet(Of String) = s.GetViewBetween("B", "E")
|
Dim view As SortedSet(Of String) = s.GetViewBetween("B", "E")
|
|
2358,30 |
1712,12 |
|
#tabpage-end
|
#tabpage-end
|
|
|
|
|
#prompt(実行結果){{
|
#prompt(実行結果){{
|
~ |
Unhandled exception. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'item')
|
System.ArgumentOutOfRangeException: 指定された引数は、有効な値の範囲内にありません。
|
- |
|
パラメータ名: collection
|
|
at System.Collections.Generic.SortedSet`1.TreeSubSet.AddIfNotPresent(T item)
|
at System.Collections.Generic.SortedSet`1.TreeSubSet.AddIfNotPresent(T item)
|
~ |
at System.Collections.Generic.SortedSet`1.Add(T item)
|
at Sample.Main(String[] args)
|
+ |
at Sample.Main() in /home/smdn/samplecodes/dotnet/cs/test.cs:line 13
|
|
|
}}
|
}}
|
|
|
|
|
|
|
+ |
|
|
+ |
*容量 [#HashSetSortedSet_Capacity]
|
|
+ |
HashSet・SortedSetでは、それ以上要素の追加や削除をする必要がなくなった場合に、&msdn(netfx,member,System.Collections.Generic.HashSet`1.TrimExcess){TrimExcessメソッド};を呼び出すことによって、HashSet・SortedSetが内部的に確保しているバッファを最小化することができ、不要な容量を減らすことができます。
|
|
+ |
|
|
+ |
また、HashSet・SortedSetに格納する最大要素数を事前に見積もれる場合は、&msdn(netfx,member,System.Collections.Generic.HashSet`1.EnsureCapacity){EnsureCapacityメソッド};を呼び出すことによって、あらかじめ指定したサイズのバッファを確保させておくことができます。 これにより、要素の追加に伴うバッファの再割当てとコピーを減らすことができます。 EnsureCapacityメソッドは、.NET Standard 2.1/.NET Core 2.1以降で利用できます。
|
|
+ |
|
|
+ |
容量と容量の縮小・確保について詳しくは、Listでの例を参照してください。
|
|
+ |
|
|
+ |
#relevantdocs(Listと容量)
|
|
+ |
|
|
+ |
-[[programming/netfx/collections/2_generic_1_list#List_Capacity]]
|
|
+ |
-[[programming/netfx/collections/2_generic_1_list#List_InitialCapacity]]
|
|
+ |
-[[programming/netfx/collections/2_generic_1_list#List_TrimExcess]]
|
|
+ |
|
|
+ |
#relevantdocs-end
|
|
+ |
|
|
+ |
|
|
+ |
|
|
|
#navi(..)
|
#navi(..)
|
|
|
|