5 // Cesar Lopez Nataren (cesar@ciencias.unam.mx)
7 // (C) 2003, Cesar Lopez Nataren
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Reflection;
33 using System.Collections;
35 namespace Microsoft.JScript {
38 private MemberInfo elem;
42 internal MemberInfo Element {
51 internal Node (string name, MemberInfo value, Node node)
59 public sealed class MemberInfoList {
71 internal MemberInfoList ()
73 head = new Node (String.Empty, null, null);
77 internal void Insert (string name, MemberInfo elem)
79 Node second = head.Next;
80 head.Next = new Node (name, elem, second);
84 internal bool Delete (object elem)
87 for (marker = head; marker.Next != null && !marker.Next.Element.Equals (elem); marker = marker.Next)
90 if (marker.Next != null && marker.Next.Element.Equals (elem)) {
91 marker.Next = marker.Next.Next;
97 internal Node Find (object value)
100 Node current = head.Next;
102 for (int i = 0; i < size; i++) {
104 if (current.Element.Equals (value))
107 current = current.Next;
113 internal class ListIter {
116 internal Node Current {
117 get { return current; }
120 internal ListIter (MemberInfoList list)
125 internal Node Next ()
128 current = current.Next;
132 throw new Exception ("Attemp to access beyond end of list.");
136 get { return current == null; }
140 internal class ChainHash : IEnumerable {
142 MemberInfoList [] bucket;
144 internal ChainHash (int size)
146 bucket = new MemberInfoList [size];
148 for (int i = 0; i < size; i++)
149 bucket [i] = new MemberInfoList ();
152 internal void Insert (string name, MemberInfo value)
154 bucket [Hash (name)].Insert (name, value);
157 internal object Retrieve (object value)
159 int n = bucket.Length;
160 object result = null;
162 for (int i = 0; i < n; i++) {
163 result = bucket [i].Find (value);
172 internal MemberInfoList Retrieve (string name)
174 return bucket [Hash (name)];
177 internal void Delete (object value)
180 throw new Exception ("Delete.Error, key can't be null.");
182 int n = bucket.Length;
183 bool deleted = false;
185 for (int i = 0; i < n; i++) {
186 deleted = bucket [i].Delete (value);
192 private int Hash (string name)
194 return name.GetHashCode () % bucket.Length;
197 public IEnumerator GetEnumerator ()
199 ArrayList elems = new ArrayList ();
203 foreach (MemberInfoList list in bucket) {
205 iter = new ListIter (list);
206 for (i = 0; i < n; i++)
207 elems.Add (iter.Next ());
209 return elems.GetEnumerator ();