Develop and Download Open Source Software

Browse CVS Repository

Contents of /netruby/netruby/Hash.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1.1.1 - (show annotations) (download) (vendor branch)
Mon Apr 8 13:27:22 2002 UTC (22 years ago) by arton
Branch: MAIN, vendor
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines
initial version 0.8

1 /*
2 Copyright(C) 2001-2002 arton
3
4 Permission is granted for use, copying, modification, distribution,
5 and distribution of modified versions of this work as long as the
6 above copyright notice is included.
7 */
8
9 using System;
10 using System.Text;
11 using System.Globalization;
12 using System.Collections;
13 using System.Reflection;
14 using System.Security;
15
16 namespace arton.NETRuby
17 {
18 public class RHash : RBasic, ICloneable, IEnumerable, ICollection
19 {
20 public RHash(NetRuby rb) :
21 base(rb, rb.cHash)
22 {
23 hash = new Hashtable();
24 ifnone = null;
25 }
26 public RHash(NetRuby rb, RMetaObject meta) :
27 base(rb, meta)
28 {
29 hash = new Hashtable();
30 ifnone = null;
31 }
32 public RHash(NetRuby rb, RHash org, RMetaObject meta) :
33 base(rb, meta)
34 {
35 hash = (Hashtable)org.hash.Clone();
36 ifnone = null;
37 }
38 object ifnone;
39 Hashtable hash;
40 public object this[object index]
41 {
42 get { return hash[index]; }
43 set { hash[index] = value; }
44 }
45 public object Default
46 {
47 get { return ifnone; }
48 set { Modify(); ifnone = value; }
49 }
50 public object Index(object val)
51 {
52 lock (hash.SyncRoot)
53 {
54 foreach (DictionaryEntry ent in hash)
55 {
56 if (val.Equals(ent.Value)) return ent.Key;
57 }
58 }
59 return null;
60 }
61 public RArray Indices(object[] args)
62 {
63 RArray ary = new RArray(ruby, true);
64 for (int i = 0; i < args.Length; i++)
65 {
66 ary.Add(hash[args[i]]);
67 }
68 return ary;
69 }
70 public object Clone()
71 {
72 RHash newhash = (RHash)MemberwiseClone();
73 newhash.hash = (Hashtable)hash.Clone();
74 return newhash;
75 }
76 public override RArray ToArray()
77 {
78 ArrayList a = new ArrayList();
79 lock (hash.SyncRoot)
80 {
81 foreach (DictionaryEntry ent in hash)
82 {
83 a.Add(RArray.AssocNew(ruby, ent.Key, ent.Value));
84 }
85 }
86 return new RArray(ruby, a);
87 }
88 public override string ToString()
89 {
90 return ToArray().ToString();
91 }
92 public override object Inspect()
93 {
94 if (hash.Count == 0) return "{}";
95 if (ruby.IsInspecting(this)) return "{...}";
96 return ruby.ProtectInspect(this,
97 new InspectMethod(inspect_hash),
98 new object[2] {this, 0});
99 }
100 private object inspect_hash(RBasic h, object[] args)
101 {
102 bool tainted = false;
103 bool start = false;
104 StringBuilder sb = new StringBuilder();
105 sb.Append("{");
106 lock (hash.SyncRoot)
107 {
108 foreach (DictionaryEntry ent in hash)
109 {
110 if (start == false)
111 {
112 start = true;
113 }
114 else
115 {
116 sb.Append(", ");
117 }
118 sb.AppendFormat("{0}=>{1}", ruby.Inspect(ent.Key), ruby.Inspect(ent.Value));
119 if (ent.Key is RBasic && ((RBasic)ent.Key).IsTainted)
120 tainted = true;
121 if (ent.Value is RBasic && ((RBasic)ent.Value).IsTainted)
122 tainted = true;
123 }
124 }
125 sb.Append("}");
126 if (IsTainted || tainted)
127 return new RString(ruby, sb.ToString(), true);
128 return sb.ToString();
129 }
130 public override bool Equals(object o)
131 {
132 if (this == o) return true;
133 if (o is RHash == false) return false;
134 RHash ot = (RHash)o;
135 Hashtable oh = ot.hash;
136 if (hash.Count != oh.Count) return false;
137 lock (hash.SyncRoot)
138 {
139 foreach (DictionaryEntry ent in hash)
140 {
141 if (oh.ContainsKey(ent.Key) == false) return false;
142 if (oh[ent.Key].Equals(ent.Value) == false) return false;
143 }
144 }
145 return true;
146 }
147 public override int GetHashCode() // for voiding CS0659 warning.
148 {
149 return base.GetHashCode();
150 }
151 public IEnumerator GetEnumerator()
152 {
153 return hash.GetEnumerator();
154 }
155 public int Count
156 {
157 get { return hash.Count; }
158 }
159 public bool IsSynchronized
160 {
161 get { return hash.IsSynchronized; }
162 }
163 public object SyncRoot
164 {
165 get { return hash.SyncRoot; }
166 }
167 public void CopyTo(Array array, int index)
168 {
169 hash.CopyTo(array, index);
170 }
171 public bool IsEmpty
172 {
173 get { return hash.Count == 0; }
174 }
175 public RArray Sort()
176 {
177 return ToArray().SortAt();
178 }
179 public RArray Keys
180 {
181 get {
182 ArrayList ary = new ArrayList();
183 lock (hash)
184 {
185 foreach (DictionaryEntry ent in hash)
186 {
187 ary.Add(ent.Key);
188 }
189 }
190 return new RArray(ruby, ary);
191 }
192 }
193 public RArray Values
194 {
195 get {
196 ArrayList ary = new ArrayList();
197 lock (hash)
198 {
199 foreach (DictionaryEntry ent in hash)
200 {
201 ary.Add(ent.Value);
202 }
203 }
204 return new RArray(ruby, ary);
205 }
206 }
207 public bool ContainsKey(object o)
208 {
209 return hash.ContainsKey(o);
210 }
211 public bool ContainsValue(object o)
212 {
213 return hash.ContainsValue(o);
214 }
215 private void Modify()
216 {
217 if (IsFrozen) ruby.ErrorFrozen("hash");
218 if (IsTainted == false && ruby.SafeLevel >= 4)
219 {
220 throw new SecurityException("Insecure: can't modify hash");
221 }
222 }
223 internal RHash Initialize(object[] argv)
224 {
225 Modify();
226 if (argv.Length >= 1)
227 ifnone = argv[0];
228 return this;
229 }
230 }
231
232 public class RHashClass : RClass
233 {
234 private RHashClass(NetRuby rb) :
235 base(rb, "Hash", rb.cObject)
236 {
237 }
238
239 static internal object s_new(RBasic r, params object[] args)
240 {
241 RHash hash = new RHash(r.ruby, (RMetaObject)r);
242 r.ruby.CallInit(hash, args);
243 return hash;
244 }
245 static internal object s_create(RBasic r, params object[] args)
246 {
247 RHash hash;
248 if (args.Length == 1 && args[0] is RHash)
249 {
250 hash = new RHash(r.ruby, ((RHash)args[0]), (RMetaObject)r);
251 return hash;
252 }
253 if (args.Length % 2 != 0)
254 {
255 throw new eArgError("odd number args for Hash");
256 }
257 hash = new RHash(r.ruby, (RMetaObject)r);
258 for (int i = 0; i < args.Length; i += 2)
259 {
260 hash[args[i]] = args[i + 1];
261 }
262 return hash;
263 }
264 static internal object initialize(RBasic r, params object[] args)
265 {
266 return ((RHash)r).Initialize(args);
267 }
268 static internal object s_noop(RBasic r, params object[] args)
269 {
270 return r;
271 }
272 static internal object aref(RBasic r, params object[] args)
273 {
274 RHash h = (RHash)r;
275 return h[args[0]];
276 }
277 static internal object aset(RBasic r, params object[] args)
278 {
279 RHash h = (RHash)r;
280 h[args[0]] = args[1];
281 return args[1];
282 }
283 static internal object defaultv(RBasic r, params object[] args)
284 {
285 return ((RHash)r).Default;
286 }
287 static internal object set_default(RBasic r, params object[] args)
288 {
289 ((RHash)r).Default = args[0];
290 return args[0];
291 }
292 static internal object index(RBasic r, params object[] args)
293 {
294 return ((RHash)r).Index(args[0]);
295 }
296 static internal object indices(RBasic r, params object[] args)
297 {
298 return ((RHash)r).Indices(args);
299 }
300 static internal object size(RBasic r, params object[] args)
301 {
302 return ((RHash)r).Count;
303 }
304 static internal object empty_p(RBasic r, params object[] args)
305 {
306 return ((RHash)r).IsEmpty;
307 }
308 static internal object each_pair(RBasic r, params object[] args)
309 {
310 NetRuby rb = r.ruby;
311 lock (((RHash)r).SyncRoot)
312 {
313 foreach (DictionaryEntry ent in ((RHash)r))
314 {
315 rb.Yield(RArray.AssocNew(rb, ent.Key, ent.Value));
316 }
317 }
318 return r;
319 }
320 static internal object each_key(RBasic r, params object[] args)
321 {
322 NetRuby rb = r.ruby;
323 lock (((RHash)r).SyncRoot)
324 {
325 foreach (DictionaryEntry ent in ((RHash)r))
326 {
327 rb.Yield(ent.Key);
328 }
329 }
330 return r;
331 }
332 static internal object each_value(RBasic r, params object[] args)
333 {
334 NetRuby rb = r.ruby;
335 lock (((RHash)r).SyncRoot)
336 {
337 foreach (DictionaryEntry ent in ((RHash)r))
338 {
339 rb.Yield(ent.Value);
340 }
341 }
342 return r;
343 }
344 static internal object sort(RBasic r, params object[] args)
345 {
346 return ((RHash)r).Sort();
347 }
348 static internal object keys(RBasic r, params object[] args)
349 {
350 return ((RHash)r).Keys;
351 }
352 static internal object values(RBasic r, params object[] args)
353 {
354 return ((RHash)r).Values;
355 }
356 static internal object contains_value(RBasic r, params object[] args)
357 {
358 return ((RHash)r).ContainsValue(args[0]);
359 }
360 static internal object contains_key(RBasic r, params object[] args)
361 {
362 return ((RHash)r).ContainsKey(args[0]);
363 }
364
365
366 static internal void Init(NetRuby rb)
367 {
368 RHashClass hash = new RHashClass(rb);
369 hash.DefineClass("Hash", rb.cObject);
370 IncludeModule(hash, rb.mEnumerable);
371 rb.cHash = hash;
372 hash.DefineSingletonMethod("new", new RMethod(s_new), -1);
373 hash.DefineSingletonMethod("[]", new RMethod(s_create), -1);
374
375 hash.DefineMethod("rehash", new RMethod(s_noop), 0);
376 hash.DefineMethod("to_hash", new RMethod(s_noop), 0);
377
378 hash.DefineMethod("[]", new RMethod(aref), 1);
379 hash.DefineMethod("[]=", new RMethod(aset), 2);
380 hash.DefineMethod("store", new RMethod(aset), 2);
381 hash.DefineMethod("default", new RMethod(defaultv), 0);
382 hash.DefineMethod("default=", new RMethod(set_default), 1);
383 hash.DefineMethod("index", new RMethod(index), 1);
384 hash.DefineMethod("indexes", new RMethod(indices), -1);
385 hash.DefineMethod("indices", new RMethod(indices), -1);
386 hash.DefineMethod("size", new RMethod(size), 0);
387 hash.DefineMethod("length", new RMethod(size), 0);
388 hash.DefineMethod("empty?", new RMethod(empty_p), 0);
389
390 hash.DefineMethod("each", new RMethod(each_pair), 0);
391 hash.DefineMethod("each_value", new RMethod(each_value), 0);
392 hash.DefineMethod("each_key", new RMethod(each_key), 0);
393 hash.DefineMethod("each_pair", new RMethod(each_pair), 0);
394 hash.DefineMethod("sort", new RMethod(sort), 0);
395
396 hash.DefineMethod("keys", new RMethod(keys), 0);
397 hash.DefineMethod("values", new RMethod(values), 0);
398
399 hash.DefineMethod("include?", new RMethod(contains_key), 1);
400 hash.DefineMethod("member?", new RMethod(contains_key), 1);
401 hash.DefineMethod("has_key?", new RMethod(contains_key), 1);
402 hash.DefineMethod("key?", new RMethod(contains_key), 1);
403 hash.DefineMethod("has_value?", new RMethod(contains_value), 1);
404 hash.DefineMethod("value?", new RMethod(contains_value), 1);
405
406 }
407 }
408
409 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26