2009-02-23 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.C5 / UserGuideExamples / MultiCollection.cs
blob32ec13ae7d022086b8387c296b8d113eb68d9c60
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:
10 The above copyright notice and this permission notice shall be included in
11 all copies or substantial portions of the Software.
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.
23 using System;
24 using C5;
25 using SCG = System.Collections.Generic;
27 namespace MultiCollection
29 class BasicCollectionValue<T> : CollectionValueBase<T>, ICollectionValue<T>
31 SCG.IEnumerable<T> enumerable;
32 Fun<T> chooser;
33 int count;
34 //TODO: add delegate for checking validity!
36 public BasicCollectionValue(SCG.IEnumerable<T> e, Fun<T> chooser, int c) { enumerable = e; count = c; this.chooser = chooser; }
38 public override int Count { get { return count; } }
40 public override Speed CountSpeed { get { return Speed.Constant; } }
42 public override bool IsEmpty { get { return count == 0; } }
44 public override T Choose() { return chooser(); }
46 public override System.Collections.Generic.IEnumerator<T> GetEnumerator()
48 return enumerable.GetEnumerator();
52 interface IMultiCollection<K, V>
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;}
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()
68 U dict = new U();
70 public SCG.IEqualityComparer<K> KeyEqualityComparer { get { return EqualityComparer<K>.Default; } }
72 public SCG.IEqualityComparer<V> ValueEqualityComparer { get { return EqualityComparer<V>.Default; } } //TODO: depends on W!
74 public bool Add(K k, V v)
76 W w;
77 if (!dict.Find(k, out w))
78 dict.Add(k,w = new W());
79 return w.Add(v);
82 public bool Remove(K k, V v)
84 W w;
85 if (dict.Find(k, out w) && w.Remove(v))
87 if (w.Count == 0)
88 dict.Remove(k);
89 return true;
91 return false;
94 public ICollectionValue<V> this[K k] { get { return dict[k]; } }
96 public ICollectionValue<K> Keys { get { return dict.Keys; } }
98 public SCG.IEnumerable<V> Values
102 foreach (W w in dict.Values)
103 foreach (V v in w)
104 yield return v;
109 class WordIndex : BasicMultiCollection<string,int,HashSet<int>,HashDictionary<string,HashSet<int>>>
113 class MyTest
115 public static void Main(String[] args)
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)
123 Console.WriteLine(s + " -->");
124 foreach (int line in wi[s])
126 Console.WriteLine(" " + line);