comparison dwtx/dwtxhelper/Collection.d @ 194:6d35b9960800

Crash in jface snippet 11, workaround tango HashMap cannot store null values.
author Frank Benoit <benoit@tionex.de>
date Sun, 01 Feb 2009 19:41:13 +0100
parents 293a2f22f944
children a4d38d47ddc4
comparison
equal deleted inserted replaced
193:a785420fe9ca 194:6d35b9960800
233 } 233 }
234 234
235 } 235 }
236 236
237 class HashMap : Map { 237 class HashMap : Map {
238 //FIXME "NULL" is used to work around tango ticket 1468.
239 static Object NULL;
238 // The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. 240 // The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
239 alias tango.util.container.HashMap.HashMap!(Object,Object) MapType; 241 alias tango.util.container.HashMap.HashMap!(Object,Object) MapType;
240 private MapType map; 242 private MapType map;
241 243
242 public this(){ 244 public this(){
245 if( NULL is null ) NULL = new Object();
243 map = new MapType(); 246 map = new MapType();
244 } 247 }
245 public this(int initialCapacity){ 248 public this(int initialCapacity){
246 this(); 249 this();
247 } 250 }
248 public this(int initialCapacity, float loadFactor){ 251 public this(int initialCapacity, float loadFactor){
252 if( NULL is null ) NULL = new Object();
249 map = new MapType(loadFactor); 253 map = new MapType(loadFactor);
250 } 254 }
251 public this(Map m){ 255 public this(Map m){
252 this(); 256 this();
253 putAll(m); 257 putAll(m);
261 } 265 }
262 public bool containsKey(String key){ 266 public bool containsKey(String key){
263 return containsKey(stringcast(key)); 267 return containsKey(stringcast(key));
264 } 268 }
265 public bool containsValue(Object value){ 269 public bool containsValue(Object value){
266 return map.contains(value); 270 Object v = (value is null) ? NULL : value;
271 return map.contains(v);
267 } 272 }
268 public Set entrySet(){ 273 public Set entrySet(){
269 HashSet res = new HashSet(); 274 HashSet res = new HashSet();
270 foreach( k, v; map ){ 275 foreach( k, v; map ){
271 res.add( new MapEntry(this,k)); 276 res.add( new MapEntry(this,k));
287 } 292 }
288 return false; 293 return false;
289 } 294 }
290 public Object get(Object key){ 295 public Object get(Object key){
291 if( auto v = key in map ){ 296 if( auto v = key in map ){
297 if( *v is NULL ) return null;
298 return *v;
299 }
300 return null;
301 }
302 public Object get(String key){
303 return get(stringcast(key));
304 }
305 public override hash_t toHash(){
306 return super.toHash();
307 }
308 public bool isEmpty(){
309 return map.isEmpty();
310 }
311 public Set keySet(){
312 HashSet res = new HashSet();
313 foreach( k, v; map ){
314 res.add(k);
315 }
316 return res;
317 }
318 public Object put(Object key, Object value){
319 Object v = (value is null) ? NULL : value;
320 Object res = null;
321 if( auto vold = key in map ){
322 res = *vold;
323 }
324 map[ key ] = v;
325 return res;
326 }
327 public Object put(String key, Object value){
328 return put( stringcast(key), value );
329 }
330 public Object put(Object key, String value){
331 return put( key, stringcast(value) );
332 }
333 public Object put(String key, String value){
334 return put( stringcast(key), stringcast(value) );
335 }
336 public void putAll(Map t){
337 foreach( k, v; t ){
338 Object vi = (v is null) ? NULL : v;
339 map[k] = vi;
340 }
341 }
342 public Object remove(Object key){
343 if( auto v = key in map ){
344 Object res = *v;
345 map.remove(key);
346 if( res is NULL ) return null;
347 return res;
348 }
349 map.remove(key);
350 return null;
351 }
352 public Object remove(String key){
353 return remove(stringcast(key));
354 }
355 public int size(){
356 return map.size();
357 }
358 public Collection values(){
359 ArrayList res = new ArrayList( size() );
360 foreach( k, v; map ){
361 res.add( v );
362 }
363 return res;
364 }
365
366 public int opApply (int delegate(ref Object value) dg){
367 return map.opApply( dg );
368 }
369 public int opApply (int delegate(ref Object key, ref Object value) dg){
370 return map.opApply( dg );
371 }
372
373 }
374
375 class IdentityHashMap : Map {
376 alias tango.util.container.HashMap.HashMap!(Object,Object) MapType;
377 private MapType map;
378
379 public this(){
380 implMissing(__FILE__, __LINE__ );
381 map = new MapType();
382 }
383 public this(int initialCapacity){
384 implMissing(__FILE__, __LINE__ );
385 this();
386 }
387 public this(int initialCapacity, float loadFactor){
388 implMissing(__FILE__, __LINE__ );
389 map = new MapType(loadFactor);
390 }
391 public this(Map m){
392 implMissing(__FILE__, __LINE__ );
393 this();
394 putAll(m);
395 }
396 public void clear(){
397 map.clear();
398 }
399 public bool containsKey(Object key){
400 Object v;
401 return map.get(key, v );
402 }
403 public bool containsKey(String key){
404 return containsKey(stringcast(key));
405 }
406 public bool containsValue(Object value){
407 return map.contains(value);
408 }
409 public Set entrySet(){
410 HashSet res = new HashSet();
411 foreach( k, v; map ){
412 res.add( new MapEntry(this,k));
413 }
414 return res;
415 }
416 public override int opEquals(Object o){
417 if( auto other = cast(HashMap) o ){
418 if( other.size() !is size() ){
419 return false;
420 }
421 foreach( k, v; map ){
422 Object vo = other.get(k);
423 if( v != vo ){
424 return false;
425 }
426 }
427 return true;
428 }
429 return false;
430 }
431 public Object get(Object key){
432 if( auto v = key in map ){
292 return *v; 433 return *v;
293 } 434 }
294 return null; 435 return null;
295 } 436 }
296 public Object get(String key){ 437 public Object get(String key){
358 return map.opApply( dg ); 499 return map.opApply( dg );
359 } 500 }
360 public int opApply (int delegate(ref Object key, ref Object value) dg){ 501 public int opApply (int delegate(ref Object key, ref Object value) dg){
361 return map.opApply( dg ); 502 return map.opApply( dg );
362 } 503 }
363
364 }
365
366 class IdentityHashMap : Map {
367 alias tango.util.container.HashMap.HashMap!(Object,Object) MapType;
368 private MapType map;
369
370 public this(){
371 implMissing(__FILE__, __LINE__ );
372 map = new MapType();
373 }
374 public this(int initialCapacity){
375 implMissing(__FILE__, __LINE__ );
376 this();
377 }
378 public this(int initialCapacity, float loadFactor){
379 implMissing(__FILE__, __LINE__ );
380 map = new MapType(loadFactor);
381 }
382 public this(Map m){
383 implMissing(__FILE__, __LINE__ );
384 this();
385 putAll(m);
386 }
387 public void clear(){
388 map.clear();
389 }
390 public bool containsKey(Object key){
391 Object v;
392 return map.get(key, v );
393 }
394 public bool containsKey(String key){
395 return containsKey(stringcast(key));
396 }
397 public bool containsValue(Object value){
398 return map.contains(value);
399 }
400 public Set entrySet(){
401 HashSet res = new HashSet();
402 foreach( k, v; map ){
403 res.add( new MapEntry(this,k));
404 }
405 return res;
406 }
407 public override int opEquals(Object o){
408 if( auto other = cast(HashMap) o ){
409 if( other.size() !is size() ){
410 return false;
411 }
412 foreach( k, v; map ){
413 Object vo = other.get(k);
414 if( v != vo ){
415 return false;
416 }
417 }
418 return true;
419 }
420 return false;
421 }
422 public Object get(Object key){
423 if( auto v = key in map ){
424 return *v;
425 }
426 return null;
427 }
428 public Object get(String key){
429 return get(stringcast(key));
430 }
431 public override hash_t toHash(){
432 return super.toHash();
433 }
434 public bool isEmpty(){
435 return map.isEmpty();
436 }
437 public Set keySet(){
438 HashSet res = new HashSet();
439 foreach( k, v; map ){
440 res.add(k);
441 }
442 return res;
443 }
444 public Object put(Object key, Object value){
445 Object res = null;
446 if( auto vold = key in map ){
447 res = *vold;
448 }
449 map[ key ] = value;
450 return res;
451 }
452 public Object put(String key, Object value){
453 return put( stringcast(key), value );
454 }
455 public Object put(Object key, String value){
456 return put( key, stringcast(value) );
457 }
458 public Object put(String key, String value){
459 return put( stringcast(key), stringcast(value) );
460 }
461 public void putAll(Map t){
462 foreach( k, v; t ){
463 map[k] = v;
464 }
465 }
466 public Object remove(Object key){
467 if( auto v = key in map ){
468 Object res = *v;
469 map.remove(key);
470 return res;
471 }
472 map.remove(key);
473 return null;
474 }
475 public Object remove(String key){
476 return remove(stringcast(key));
477 }
478 public int size(){
479 return map.size();
480 }
481 public Collection values(){
482 ArrayList res = new ArrayList( size() );
483 foreach( k, v; map ){
484 res.add( v );
485 }
486 return res;
487 }
488
489 public int opApply (int delegate(ref Object value) dg){
490 return map.opApply( dg );
491 }
492 public int opApply (int delegate(ref Object key, ref Object value) dg){
493 return map.opApply( dg );
494 }
495 } 504 }
496 505
497 class Dictionary { 506 class Dictionary {
498 public this(){ 507 public this(){
499 } 508 }
771 res.add( new MapEntry(this,k) ); 780 res.add( new MapEntry(this,k) );
772 } 781 }
773 return res; 782 return res;
774 } 783 }
775 public override int opEquals(Object o){ 784 public override int opEquals(Object o){
776 if( auto other = cast(HashMap) o ){ 785 if( auto other = cast(TreeMap) o ){
777 if( other.size() !is size() ){ 786 if( other.size() !is size() ){
778 return false; 787 return false;
779 } 788 }
780 foreach( k, v; map ){ 789 foreach( k, v; map ){
781 Object vo = other.get(k); 790 Object vo = other.get(k);