1
|
/*
|
2
|
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
|
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
of this software and associated documentation files (the "Software"), to deal
|
5
|
in the Software without restriction, including without limitation the rights
|
6
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
copies of the Software, and to permit persons to whom the Software is
|
8
|
furnished to do so, subject to the following conditions:
|
9
|
|
10
|
The above copyright notice and this permission notice shall be included in
|
11
|
all copies or substantial portions of the Software.
|
12
|
|
13
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
SOFTWARE.
|
20
|
*/
|
21
|
|
22
|
|
23
|
using System;
|
24
|
using C5;
|
25
|
using SCG = System.Collections.Generic;
|
26
|
|
27
|
namespace MultiCollection
|
28
|
{
|
29
|
class BasicCollectionValue<T> : CollectionValueBase<T>, ICollectionValue<T>
|
30
|
{
|
31
|
SCG.IEnumerable<T> enumerable;
|
32
|
Fun<T> chooser;
|
33
|
int count;
|
34
|
//TODO: add delegate for checking validity!
|
35
|
|
36
|
public BasicCollectionValue(SCG.IEnumerable<T> e, Fun<T> chooser, int c) { enumerable = e; count = c; this.chooser = chooser; }
|
37
|
|
38
|
public override int Count { get { return count; } }
|
39
|
|
40
|
public override Speed CountSpeed { get { return Speed.Constant; } }
|
41
|
|
42
|
public override bool IsEmpty { get { return count == 0; } }
|
43
|
|
44
|
public override T Choose() { return chooser(); }
|
45
|
|
46
|
public override System.Collections.Generic.IEnumerator<T> GetEnumerator()
|
47
|
{
|
48
|
return enumerable.GetEnumerator();
|
49
|
}
|
50
|
}
|
51
|
|
52
|
interface IMultiCollection<K, V>
|
53
|
{
|
54
|
SCG.IEqualityComparer<K> KeyEqualityComparer { get;}
|
55
|
SCG.IEqualityComparer<V> ValueEqualityComparer { get;}
|
56
|
bool Add(K k, V v);
|
57
|
bool Remove(K k, V v);
|
58
|
ICollectionValue<V> this[K k] { get;}
|
59
|
ICollectionValue<K> Keys { get;}
|
60
|
SCG.IEnumerable<V> Values { get;}
|
61
|
|
62
|
}
|
63
|
|
64
|
class BasicMultiCollection<K, V, W, U> : IMultiCollection<K, V>//: IDictionary<K, W>
|
65
|
where W : ICollection<V>, new()
|
66
|
where U : IDictionary<K, W>, new()
|
67
|
{
|
68
|
U dict = new U();
|
69
|
|
70
|
public SCG.IEqualityComparer<K> KeyEqualityComparer { get { return EqualityComparer<K>.Default; } }
|
71
|
|
72
|
public SCG.IEqualityComparer<V> ValueEqualityComparer { get { return EqualityComparer<V>.Default; } } //TODO: depends on W!
|
73
|
|
74
|
public bool Add(K k, V v)
|
75
|
{
|
76
|
W w;
|
77
|
if (!dict.Find(k, out w))
|
78
|
dict.Add(k,w = new W());
|
79
|
return w.Add(v);
|
80
|
}
|
81
|
|
82
|
public bool Remove(K k, V v)
|
83
|
{
|
84
|
W w;
|
85
|
if (dict.Find(k, out w) && w.Remove(v))
|
86
|
{
|
87
|
if (w.Count == 0)
|
88
|
dict.Remove(k);
|
89
|
return true;
|
90
|
}
|
91
|
return false;
|
92
|
}
|
93
|
|
94
|
public ICollectionValue<V> this[K k] { get { return dict[k]; } }
|
95
|
|
96
|
public ICollectionValue<K> Keys { get { return dict.Keys; } }
|
97
|
|
98
|
public SCG.IEnumerable<V> Values
|
99
|
{
|
100
|
get
|
101
|
{
|
102
|
foreach (W w in dict.Values)
|
103
|
foreach (V v in w)
|
104
|
yield return v;
|
105
|
}
|
106
|
}
|
107
|
}
|
108
|
|
109
|
class WordIndex : BasicMultiCollection<string,int,HashSet<int>,HashDictionary<string,HashSet<int>>>
|
110
|
{
|
111
|
}
|
112
|
|
113
|
class MyTest
|
114
|
{
|
115
|
public static void Main(String[] args)
|
116
|
{
|
117
|
WordIndex wi = new WordIndex();
|
118
|
wi.Add("ja", 2);
|
119
|
wi.Add("nej", 4);
|
120
|
wi.Add("ja", 7);
|
121
|
foreach (string s in wi.Keys)
|
122
|
{
|
123
|
Console.WriteLine(s + " -->");
|
124
|
foreach (int line in wi[s])
|
125
|
{
|
126
|
Console.WriteLine(" " + line);
|
127
|
}
|
128
|
}
|
129
|
}
|
130
|
}
|
131
|
}
|