1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.ActiveRecord
.Framework
18 using System
.Collections
;
21 /// Maps keys to position in the values array.
22 /// Basically key -> index
24 public class DictionaryAdapter
: IDictionary
26 private int _tablesize
;
27 private Entry
[] _table
;
28 private object[] _values
;
29 private string[] _keys
;
32 /// Initializes a new instance of the <see cref="DictionaryAdapter"/> class.
34 /// <param name="names">The names.</param>
35 /// <param name="values">The values.</param>
36 public DictionaryAdapter(String
[] names
, object[] values
)
38 _tablesize
= names
.Length
;
39 _table
= new Entry
[_tablesize
];
43 for(int i
=0; i
<_tablesize
; i
++)
45 InternalAdd(names
[i
], i
);
49 private void InternalAdd(String key
, int index
)
51 int position
= CalculateHash(key
);
52 System
.Diagnostics
.Debug
.Assert(position
>= 0);
54 if (_table
[position
] == null)
56 _table
[position
] = new Entry(key
, index
);
60 _table
[position
].AddEntry(new Entry(key
, index
));
64 private int CalculateHash(String key
)
66 uint result
= (uint)key
.GetHashCode();
67 return (int) Math
.Abs(result
% _tablesize
);
71 /// Simple link list entry
80 /// Initializes a new instance of the <see cref="Entry"/> class.
82 /// <param name="key">The key.</param>
83 /// <param name="index">The index.</param>
84 public Entry(String key
, int index
)
90 internal void AddEntry(Entry entry
)
94 while(walker
.nextEntry
!= null)
96 walker
= walker
.nextEntry
;
99 walker
.nextEntry
= entry
;
103 /// Finds the specified key.
105 /// <param name="key">The key.</param>
106 /// <returns></returns>
107 public int Find(string key
)
111 while(walker
!= null && !walker
.key
.Equals(key
))
113 walker
= walker
.nextEntry
;
116 return walker
!= null ? walker
.index
: -1;
121 /// Determines whether the <see cref="T:System.Collections.IDictionary"></see> object contains an element with the specified key.
123 /// <param name="key">The key to locate in the <see cref="T:System.Collections.IDictionary"></see> object.</param>
125 /// true if the <see cref="T:System.Collections.IDictionary"></see> contains an element with the key; otherwise, false.
127 /// <exception cref="T:System.ArgumentNullException">key is null. </exception>
128 public bool Contains(object key
)
130 return GetValuesIndexByKey(key
) != -1;
134 /// Adds an element with the provided key and value to the <see cref="T:System.Collections.IDictionary"></see> object.
136 /// <param name="key">The <see cref="T:System.Object"></see> to use as the key of the element to add.</param>
137 /// <param name="value">The <see cref="T:System.Object"></see> to use as the value of the element to add.</param>
138 /// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.IDictionary"></see> object. </exception>
139 /// <exception cref="T:System.ArgumentNullException">key is null. </exception>
140 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"></see> is read-only.-or- The <see cref="T:System.Collections.IDictionary"></see> has a fixed size. </exception>
141 public void Add(object key
, object value)
143 throw new NotSupportedException();
147 /// Removes all elements from the <see cref="T:System.Collections.IDictionary"></see> object.
149 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"></see> object is read-only. </exception>
152 throw new NotSupportedException();
156 /// Returns an <see cref="T:System.Collections.IDictionaryEnumerator"></see> object for the <see cref="T:System.Collections.IDictionary"></see> object.
159 /// An <see cref="T:System.Collections.IDictionaryEnumerator"></see> object for the <see cref="T:System.Collections.IDictionary"></see> object.
161 IDictionaryEnumerator IDictionary
.GetEnumerator()
163 throw new NotSupportedException();
167 /// Removes the element with the specified key from the <see cref="T:System.Collections.IDictionary"></see> object.
169 /// <param name="key">The key of the element to remove.</param>
170 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"></see> object is read-only.-or- The <see cref="T:System.Collections.IDictionary"></see> has a fixed size. </exception>
171 /// <exception cref="T:System.ArgumentNullException">key is null. </exception>
172 public void Remove(object key
)
174 throw new NotSupportedException();
178 /// Gets an <see cref="T:System.Collections.ICollection"></see> object containing the keys of the <see cref="T:System.Collections.IDictionary"></see> object.
181 /// <returns>An <see cref="T:System.Collections.ICollection"></see> object containing the keys of the <see cref="T:System.Collections.IDictionary"></see> object.</returns>
182 public ICollection Keys
191 /// Gets an <see cref="T:System.Collections.ICollection"></see> object containing the values in the <see cref="T:System.Collections.IDictionary"></see> object.
194 /// <returns>An <see cref="T:System.Collections.ICollection"></see> object containing the values in the <see cref="T:System.Collections.IDictionary"></see> object.</returns>
195 public ICollection Values
197 get { throw new NotSupportedException(); }
201 /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"></see> object is read-only.
204 /// <returns>true if the <see cref="T:System.Collections.IDictionary"></see> object is read-only; otherwise, false.</returns>
205 public bool IsReadOnly
211 /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"></see> object has a fixed size.
214 /// <returns>true if the <see cref="T:System.Collections.IDictionary"></see> object has a fixed size; otherwise, false.</returns>
215 public bool IsFixedSize
221 /// Gets or sets the <see cref="System.Object"/> with the specified key.
224 public object this[object key
]
228 if (_values
== null || _values
.Length
.Equals(0))
231 int index
= GetValuesIndexByKey(key
);
235 return _values
[index
];
242 int index
= GetValuesIndexByKey(key
);
246 _values
[index
] = value;
250 throw new ArgumentException(
251 "DictionaryAdapter: Could not find value related to key {" + key + "}. " +
252 "This column is probably not mapped on ActiveRecord");
257 private int GetValuesIndexByKey(object key
)
259 int pos
= CalculateHash(key
.ToString());
261 if (_table
[pos
] == null) return -1;
263 return _table
[pos
].Find(key
.ToString());
267 /// Copies the elements of the <see cref="T:System.Collections.ICollection"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index.
269 /// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
270 /// <param name="index">The zero-based index in array at which copying begins.</param>
271 /// <exception cref="T:System.ArgumentNullException">array is null. </exception>
272 /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than zero. </exception>
273 /// <exception cref="T:System.ArgumentException">array is multidimensional.-or- index is equal to or greater than the length of array.-or- The number of elements in the source <see cref="T:System.Collections.ICollection"></see> is greater than the available space from index to the end of the destination array. </exception>
274 /// <exception cref="T:System.InvalidCastException">The type of the source <see cref="T:System.Collections.ICollection"></see> cannot be cast automatically to the type of the destination array. </exception>
275 public void CopyTo(Array array
, int index
)
277 throw new NotSupportedException();
281 /// Gets the number of elements contained in the <see cref="T:System.Collections.ICollection"></see>.
284 /// <returns>The number of elements contained in the <see cref="T:System.Collections.ICollection"></see>.</returns>
287 get { return _tablesize; }
291 /// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"></see>.
294 /// <returns>An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"></see>.</returns>
295 public object SyncRoot
301 /// Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection"></see> is synchronized (thread safe).
304 /// <returns>true if access to the <see cref="T:System.Collections.ICollection"></see> is synchronized (thread safe); otherwise, false.</returns>
305 public bool IsSynchronized
307 get { return false; }
311 /// Returns an enumerator that iterates through a collection.
314 /// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
316 public IEnumerator
GetEnumerator()
318 throw new NotSupportedException();