In .:
[mono.git] / mcs / class / corlib / System.Collections.ObjectModel / KeyedCollection.cs
1 // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 //
3 // System.Collections.ObjectModel.KeyedCollection
4 //
5 // Author:
6 //    Zoltan Varga (vargaz@gmail.com)
7 //
8 // (C) 2005 Novell, Inc.
9 //
10
11 //
12 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 #if NET_2_0
35 using System;
36 using System.Collections.Generic;
37 using System.Runtime.InteropServices;
38
39 namespace System.Collections.ObjectModel
40 {
41         [ComVisible(false)]
42         [Serializable]
43         public abstract class KeyedCollection<TKey, TItem> : Collection<TItem>
44         {
45                 private Dictionary<TKey, TItem> dictionary;
46                 private IEqualityComparer<TKey> comparer;
47                 private int dictionaryCreationThreshold;
48
49                 protected KeyedCollection ()
50                         : this (null, 0)
51                 { 
52                 }
53
54                 protected KeyedCollection (IEqualityComparer<TKey> comparer)
55                         : this(comparer, 0)
56                 {
57                 }
58
59                 protected KeyedCollection (IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold)
60                 {
61                         if (comparer != null)
62                                 this.comparer = comparer;
63                         else
64                                 this.comparer = EqualityComparer<TKey>.Default;
65
66                         this.dictionaryCreationThreshold = dictionaryCreationThreshold;
67
68                         if (dictionaryCreationThreshold == 0)
69                                 dictionary = new Dictionary<TKey, TItem> (this.comparer);
70                 }
71
72                 public bool Contains (TKey key)
73                 {
74                         if (dictionary != null)
75                                 return dictionary.ContainsKey (key);
76                         return IndexOfKey (key) >= 0;
77                 }
78
79                 private int IndexOfKey (TKey key)
80                 {
81                         for (int i = Count - 1; i >= 0; i--)
82                         {
83                                 TKey lkey = GetKeyForItem (this [i]);
84                                 if (comparer.Equals (key, lkey))
85                                         return i;
86                         }
87                         return -1;
88                 }
89
90                 public bool Remove (TKey key)
91                 {
92                         TItem item;
93                         if (dictionary != null)
94                         {
95                                 if (dictionary.TryGetValue (key, out item))
96                                         return base.Remove(item);
97                                 else
98                                         return false;
99                         }
100
101                         int idx = IndexOfKey (key);
102
103                         if (idx == -1)
104                                 return false;
105                         
106                         RemoveAt(idx);
107                         return true;
108                 }
109
110                 public IEqualityComparer<TKey> Comparer {
111                         get {
112                                 return comparer;
113                         }
114                 }
115
116                 public TItem this [TKey key] {
117                         get {
118                                 if (dictionary != null)
119                                         return dictionary [key];
120
121                                 int idx = IndexOfKey (key);
122                                 if (idx >= 0)
123                                         return base [idx];
124                                 else
125                                         throw new KeyNotFoundException();
126                         }
127                 }
128
129                 protected void ChangeItemKey (TItem item, TKey newKey)
130                 {
131                         if (!Contains(item)) throw new ArgumentException();
132
133                         TKey oldKey = GetKeyForItem (item);
134                         if (comparer.Equals (oldKey, newKey)) return;
135
136                         if (Contains (newKey)) throw new ArgumentException();
137                         if (dictionary != null)
138                         {
139
140                                 if (!dictionary.Remove (oldKey))
141                                         throw new ArgumentException();
142
143                                 dictionary.Add (newKey, item);
144                         }
145                 }
146
147                 protected override void ClearItems ()
148                 {
149                         if (dictionary != null)
150                         {
151                                 dictionary.Clear();
152                         }
153
154                         base.ClearItems ();
155                 }
156
157                 protected abstract TKey GetKeyForItem (TItem item);
158
159                 protected override void InsertItem (int index, TItem item)
160                 {
161                         if (dictionary != null)
162                         {
163                                 dictionary.Add (GetKeyForItem (item), item);
164                         }
165                         else
166                         {
167                                 if (dictionaryCreationThreshold != -1 && Count + 1 > dictionaryCreationThreshold)
168                                 {
169                                         dictionary = new Dictionary<TKey, TItem> (comparer);
170
171                                         for (int i = Count - 1; i >= 0; i--)
172                                         {
173                                                 TItem dictitem = this[i];
174                                                 dictionary.Add(GetKeyForItem(dictitem), dictitem);
175                                         }
176
177                                         dictionary.Add (GetKeyForItem (item), item);
178                                 }
179                         }
180                         base.InsertItem (index, item);
181                 }
182
183                 protected override void RemoveItem (int index)
184                 {
185                         if (dictionary != null)
186                         {
187                                 TKey key = GetKeyForItem (this [index]);
188                                 dictionary.Remove (key);
189                         }
190                         base.RemoveItem (index);
191                 }
192
193                 protected override void SetItem (int index, TItem item)
194                 {
195                         if (dictionary != null)
196                         {
197                                 dictionary.Remove (GetKeyForItem (this [index]));
198                                 dictionary.Add (GetKeyForItem (item), item);
199                         }
200                         base.SetItem (index, item);
201                 }
202
203                 protected IDictionary<TKey, TItem> Dictionary {
204                         get {
205                                 return dictionary;
206                         }
207                 }
208         }
209 }
210 #endif