Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HybridDictionary.cs « System.Collections.Specialized « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a8b313cbd912c37933dd2db13e5575708b8d8814 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// System.Collections.Specialized.HybridDictionary.cs
//
// Author:
//   Lawrence Pit (loz@cable.a2000.nl)
//

using System;
using System.Collections;

namespace System.Collections.Specialized {
	
	[Serializable]
	public class HybridDictionary : IDictionary, ICollection, IEnumerable {
		
		private const int switchAfter = 10;

		private ListDictionary list;
		private Hashtable hashtable;
		private bool caseInsensitive = false;

		// Constructors
		
		public HybridDictionary() : this (0, false) { }
		
		public HybridDictionary (bool caseInsensitive) : this (0, caseInsensitive) { }
		
		public HybridDictionary (int initialSize) : this (initialSize, false) { }
		
		public HybridDictionary(int initialSize, bool caseInsensitive) 
		{
			this.caseInsensitive = caseInsensitive;
		
			if (initialSize <= switchAfter)
				if (caseInsensitive)
					list = new ListDictionary (CaseInsensitiveComparer.Default);
				else
					list = new ListDictionary ();
			else
				if (caseInsensitive) 
					hashtable = new Hashtable (initialSize, 
							CaseInsensitiveHashCodeProvider.Default, 
							CaseInsensitiveComparer.Default);
				else
					hashtable = new Hashtable (initialSize);
		}		

		
		// Properties
		
		public int Count {
			get {
				if (list != null)
					return list.Count;
				return hashtable.Count;
			}
		}
		
		public bool IsFixedSize {
			get { return false; }
		}
		
		public bool IsReadOnly {
			get { return false; }
		}
		
		public bool IsSynchronized {
			get { return false; }
		}
		
		public object this [object key] {
			get { 
				if (key == null)
					throw new ArgumentNullException("key");
				if (list != null)
					return list [key];
				return hashtable [key];
			}
			set { 
				if (list != null)
					if (list.Count >= switchAfter) 
						Switch ();
					else {
						list [key] = value;
						return;
					}
				hashtable [key] = value;
			}
		}
		
		public ICollection Keys {
			get {
				if (list != null)
					return list.Keys;
				return hashtable.Keys;
			}
		}
		
		public object SyncRoot {
			get { return this; }
		}
		
		public ICollection Values {
			get { 
				if (list != null)
					return list.Values;
				return hashtable.Values;
			}
		}
		
		
		// Methods
		
		public void Add (object key, object value) 
		{
			if (list != null)
				if (list.Count >= switchAfter) 
					Switch ();
				else {
					list.Add (key, value);
					return;
				}
			hashtable.Add (key, value);
		}
		
		public void Clear ()
		{
			if (caseInsensitive)
				list = new ListDictionary (CaseInsensitiveComparer.Default);
			else
				list = new ListDictionary ();
			hashtable = null;
		}
		
		public bool Contains (object key)
		{
			if (list != null)
				return list.Contains (key);
			return hashtable.Contains (key);
		}
		
		public void CopyTo (Array array, int index)
		{
			if (list != null) 
				list.CopyTo (array, index);
			else
				hashtable.CopyTo (array, index);
		}
		
		public IDictionaryEnumerator GetEnumerator ()
		{
			if (list != null)
				return list.GetEnumerator ();
			return hashtable.GetEnumerator ();
		}

		IEnumerator IEnumerable.GetEnumerator ()
		{
			return GetEnumerator ();
		}

		public void Remove (object key)
		{
			if (list != null)
				list.Remove (key);
			hashtable.Remove (key);
		}
		
		private void Switch ()
		{
			if (caseInsensitive) 
				hashtable = new Hashtable (switchAfter + 1,
							CaseInsensitiveHashCodeProvider.Default, 
							CaseInsensitiveComparer.Default);
			else
				hashtable = new Hashtable (switchAfter + 1);
			IDictionaryEnumerator e = list.GetEnumerator ();
			while (e.MoveNext ())
				hashtable.Add (e.Key, e.Value);
			list = null;
		}
	}
}