 |
|
File nullKeys.patch, 10.3 kB
(added by Some1_2, 1 year ago)
|
|
-
collection/HashMap.d
| old |
new |
|
| 13 | 13 | 21Oct95 dl fixed error in removeAt |
|---|
| 14 | 14 | 9Apr97 dl made Serializable |
|---|
| 15 | 15 | 14Dec06 kb Converted, templated & reshaped for Tango |
|---|
| | 16 | 17Sep07 db Allowed null keys |
|---|
| 16 | 17 | |
|---|
| 17 | 18 | ********************************************************************************/ |
|---|
| 18 | 19 | |
| … | … | |
| 305 | 306 | |
|---|
| 306 | 307 | public final bool containsKey(K key) |
|---|
| 307 | 308 | { |
|---|
| 308 | | if (!isValidKey(key) || count is 0) |
|---|
| | 309 | if (count is 0) |
|---|
| 309 | 310 | return false; |
|---|
| 310 | 311 | |
|---|
| 311 | 312 | LLPairT p = table[hashOf(key)]; |
| … | … | |
| 326 | 327 | |
|---|
| 327 | 328 | public final bool containsPair(K key, V element) |
|---|
| 328 | 329 | { |
|---|
| 329 | | if (!isValidKey(key) || !isValidArg(element) || count is 0) |
|---|
| | 330 | if (!isValidArg(element) || count is 0) |
|---|
| 330 | 331 | return false; |
|---|
| 331 | 332 | |
|---|
| 332 | 333 | LLPairT p = table[hashOf(key)]; |
| … | … | |
| 361 | 362 | |
|---|
| 362 | 363 | public final V get(K key) |
|---|
| 363 | 364 | { |
|---|
| 364 | | checkKey(key); |
|---|
| 365 | 365 | if (count !is 0) |
|---|
| 366 | 366 | { |
|---|
| 367 | 367 | LLPairT p = table[hashOf(key)]; |
| … | … | |
| 386 | 386 | |
|---|
| 387 | 387 | public bool get(K key, inout V element) |
|---|
| 388 | 388 | { |
|---|
| 389 | | checkKey(key); |
|---|
| 390 | 389 | if (count !is 0) |
|---|
| 391 | 390 | { |
|---|
| 392 | 391 | LLPairT p = table[hashOf(key)]; |
| … | … | |
| 552 | 551 | |
|---|
| 553 | 552 | public final void add (K key, V element) |
|---|
| 554 | 553 | { |
|---|
| 555 | | checkKey(key); |
|---|
| 556 | 554 | checkElement(element); |
|---|
| 557 | 555 | |
|---|
| 558 | 556 | if (table is null) |
| … | … | |
| 598 | 596 | |
|---|
| 599 | 597 | public final void removeKey (K key) |
|---|
| 600 | 598 | { |
|---|
| 601 | | if (!isValidKey(key) || count is 0) |
|---|
| | 599 | if (count is 0) |
|---|
| 602 | 600 | return; |
|---|
| 603 | 601 | |
|---|
| 604 | 602 | int h = hashOf(key); |
| … | … | |
| 637 | 635 | |
|---|
| 638 | 636 | public final void replacePair (K key, V oldElement, V newElement) |
|---|
| 639 | 637 | { |
|---|
| 640 | | if (!isValidKey(key) || !isValidArg(oldElement) || count is 0) |
|---|
| | 638 | if (!isValidArg(oldElement) || count is 0) |
|---|
| 641 | 639 | return; |
|---|
| 642 | 640 | |
|---|
| 643 | 641 | LLPairT p = table[hashOf(key)]; |
| … | … | |
| 693 | 691 | |
|---|
| 694 | 692 | protected final int hashOf(K key) |
|---|
| 695 | 693 | { |
|---|
| 696 | | return (typeid(K).getHash(&key) & 0x7FFFFFFF) % table.length; |
|---|
| | 694 | |
|---|
| | 695 | return (((key is null) ? 0 : typeid(K).getHash(&key)) & 0x7FFFFFFF) % table.length; |
|---|
| 697 | 696 | } |
|---|
| 698 | 697 | |
|---|
| 699 | 698 | |
| … | … | |
| 865 | 864 | { |
|---|
| 866 | 865 | ++c; |
|---|
| 867 | 866 | assert(allows(p.element())); |
|---|
| 868 | | assert(allowsKey(p.key())); |
|---|
| | 867 | //assert(allowsKey(p.key())); |
|---|
| 869 | 868 | assert(containsKey(p.key())); |
|---|
| 870 | 869 | assert(contains(p.element())); |
|---|
| 871 | 870 | assert(instances(p.element()) >= 1); |
| … | … | |
| 951 | 950 | debug(Test) |
|---|
| 952 | 951 | { |
|---|
| 953 | 952 | import tango.io.Console; |
|---|
| | 953 | import tango.io.Stdout; |
|---|
| 954 | 954 | |
|---|
| 955 | 955 | void main() |
|---|
| 956 | 956 | { |
|---|
| 957 | 957 | auto map = new HashMap!(char[], double); |
|---|
| 958 | 958 | map.add ("foo", 3.14); |
|---|
| 959 | 959 | map.add ("bar", 6.28); |
|---|
| | 960 | map.add (null, 12.56); |
|---|
| | 961 | |
|---|
| | 962 | HashMap!(HashMap!(char[], char[]), double) map2 = new HashMap!(HashMap!(char[], char[]), double); |
|---|
| | 963 | HashMap!(char[], char[]) nullHash = null; |
|---|
| | 964 | HashMap!(char[], char[]) myHash = new HashMap!(char[], char[]); |
|---|
| | 965 | map2.add (myHash, 3.14); |
|---|
| | 966 | map2.add (nullHash, 6.28); |
|---|
| 960 | 967 | |
|---|
| 961 | 968 | foreach (key, value; map.keys) {typeof(key) x; x = key;} |
|---|
| | 969 | foreach (key, value; map2.keys) {typeof(key) x; x = key;} |
|---|
| 962 | 970 | |
|---|
| 963 | 971 | foreach (value; map.keys) {} |
|---|
| | 972 | foreach (value; map2.keys) {} |
|---|
| 964 | 973 | |
|---|
| 965 | 974 | foreach (value; map.elements) {} |
|---|
| | 975 | foreach (value; map2.elements) {} |
|---|
| 966 | 976 | |
|---|
| 967 | 977 | auto keys = map.keys(); |
|---|
| 968 | 978 | while (keys.more) |
|---|
| 969 | 979 | auto v = keys.get(); |
|---|
| | 980 | auto keys2 = map2.keys(); |
|---|
| | 981 | while (keys2.more) |
|---|
| | 982 | auto v = keys2.get(); |
|---|
| 970 | 983 | |
|---|
| 971 | 984 | foreach (value; map) {} |
|---|
| | 985 | foreach (value; map2) {} |
|---|
| 972 | 986 | |
|---|
| 973 | 987 | foreach (key, value; map) |
|---|
| 974 | | Cout (key).newline; |
|---|
| | 988 | Stdout.format("{} : {}", key is null ? "<null>" : key, value).newline; |
|---|
| | 989 | foreach (key, value; map2) |
|---|
| | 990 | Stdout.format("{}", value).newline; |
|---|
| 975 | 991 | |
|---|
| 976 | 992 | map.checkImplementation(); |
|---|
| | 993 | map2.checkImplementation(); |
|---|
| 977 | 994 | |
|---|
| 978 | 995 | Cout (map).newline; |
|---|
| | 996 | Cout (map2).newline; |
|---|
| 979 | 997 | } |
|---|
| 980 | 998 | } |
-
collection/impl/LLPair.d
| old |
new |
|
| 8 | 8 | History: |
|---|
| 9 | 9 | Date Who What |
|---|
| 10 | 10 | 24Sep95 dl@cs.oswego.edu Create from tango.util.collection.d working file |
|---|
| | 11 | 17Sep07 db Allowed null keys |
|---|
| 11 | 12 | |
|---|
| 12 | 13 | */ |
|---|
| 13 | 14 | |
| … | … | |
| 43 | 44 | private K key_; |
|---|
| 44 | 45 | |
|---|
| 45 | 46 | /** |
|---|
| | 47 | * Compare two keys for equality |
|---|
| | 48 | **/ |
|---|
| | 49 | |
|---|
| | 50 | private bool keyCompare(K first, K second) |
|---|
| | 51 | { |
|---|
| | 52 | return cast(bool)((first is null) ? (second is null) : (second is null) ? (first is null) : (first == second)); |
|---|
| | 53 | } |
|---|
| | 54 | |
|---|
| | 55 | |
|---|
| | 56 | /** |
|---|
| 46 | 57 | * Make a cell with given key, elment, and next link |
|---|
| 47 | 58 | **/ |
|---|
| 48 | 59 | |
| … | … | |
| 97 | 108 | |
|---|
| 98 | 109 | public final int keyHash() |
|---|
| 99 | 110 | { |
|---|
| 100 | | return typeid(K).getHash(&key_); |
|---|
| | 111 | return (key is null) ? 0 : typeid(K).getHash(&key_); |
|---|
| 101 | 112 | } |
|---|
| 102 | 113 | |
|---|
| 103 | 114 | |
| … | … | |
| 108 | 119 | public final LLPair findKey(K key) |
|---|
| 109 | 120 | { |
|---|
| 110 | 121 | for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_) |
|---|
| 111 | | if (p.key() == key) |
|---|
| | 122 | if (keyCompare(p.key(), key)) |
|---|
| 112 | 123 | return p; |
|---|
| 113 | 124 | return null; |
|---|
| 114 | 125 | } |
| … | … | |
| 120 | 131 | public final LLPair find(K key, T element) |
|---|
| 121 | 132 | { |
|---|
| 122 | 133 | for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_) |
|---|
| 123 | | if (p.key() == key && p.element() == element) |
|---|
| | 134 | if (keyCompare(p.key(), key) && p.element() == element) |
|---|
| 124 | 135 | return p; |
|---|
| 125 | 136 | return null; |
|---|
| 126 | 137 | } |
| … | … | |
| 135 | 146 | int i = 0; |
|---|
| 136 | 147 | for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_) |
|---|
| 137 | 148 | { |
|---|
| 138 | | if (p.key() == key) |
|---|
| | 149 | if (keyCompare(p.key(), key)) |
|---|
| 139 | 150 | return i; |
|---|
| 140 | 151 | else |
|---|
| 141 | 152 | ++i; |
| … | … | |
| 152 | 163 | int i = 0; |
|---|
| 153 | 164 | for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_) |
|---|
| 154 | 165 | { |
|---|
| 155 | | if (p.key() == key && p.element() == element) |
|---|
| | 166 | if (keyCompare(p.key(), key) && p.element() == element) |
|---|
| 156 | 167 | return i; |
|---|
| 157 | 168 | else |
|---|
| 158 | 169 | ++i; |
| … | … | |
| 167 | 178 | { |
|---|
| 168 | 179 | int c = 0; |
|---|
| 169 | 180 | for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_) |
|---|
| 170 | | if (p.key() == key) |
|---|
| | 181 | if (keyCompare(p.key(), key)) |
|---|
| 171 | 182 | ++c; |
|---|
| 172 | 183 | return c; |
|---|
| 173 | 184 | } |
| … | … | |
| 179 | 190 | { |
|---|
| 180 | 191 | int c = 0; |
|---|
| 181 | 192 | for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_) |
|---|
| 182 | | if (p.key() == key && p.element() == element) |
|---|
| | 193 | if (keyCompare(p.key(), key) && p.element() == element) |
|---|
| 183 | 194 | ++c; |
|---|
| 184 | 195 | return c; |
|---|
| 185 | 196 | } |
-
collection/impl/MapCollection.d
| old |
new |
|
| 11 | 11 | 13Oct95 dl Create |
|---|
| 12 | 12 | 28jan97 dl make class public |
|---|
| 13 | 13 | 14Dec06 kb adapted for Tango usage |
|---|
| | 14 | 17Sep07 db Allowed null keys |
|---|
| 14 | 15 | |
|---|
| 15 | 16 | ********************************************************************************/ |
|---|
| 16 | 17 | |
| … | … | |
| 78 | 79 | |
|---|
| 79 | 80 | ************************************************************************/ |
|---|
| 80 | 81 | |
|---|
| 81 | | public final bool allowsKey(K key) |
|---|
| | 82 | /*public final bool allowsKey(K key) |
|---|
| 82 | 83 | { |
|---|
| 83 | 84 | return (key !is K.init); |
|---|
| 84 | | } |
|---|
| | 85 | }*/ |
|---|
| 85 | 86 | |
|---|
| 86 | | protected final bool isValidKey(K key) |
|---|
| | 87 | /*protected final bool isValidKey(K key) |
|---|
| 87 | 88 | { |
|---|
| 88 | 89 | static if (is (K : Object)) |
|---|
| 89 | 90 | { |
| … | … | |
| 91 | 92 | return false; |
|---|
| 92 | 93 | } |
|---|
| 93 | 94 | return true; |
|---|
| 94 | | } |
|---|
| | 95 | }*/ |
|---|
| 95 | 96 | |
|---|
| 96 | 97 | /*********************************************************************** |
|---|
| 97 | 98 | |
| … | … | |
| 99 | 100 | |
|---|
| 100 | 101 | ************************************************************************/ |
|---|
| 101 | 102 | |
|---|
| 102 | | protected final void checkKey(K key) |
|---|
| | 103 | /*protected final void checkKey(K key) |
|---|
| 103 | 104 | { |
|---|
| 104 | 105 | if (!isValidKey(key)) |
|---|
| 105 | 106 | { |
|---|
| 106 | 107 | throw new IllegalElementException("Attempt to include invalid key _in Collection"); |
|---|
| 107 | 108 | } |
|---|
| 108 | | } |
|---|
| | 109 | }*/ |
|---|
| 109 | 110 | |
|---|
| 110 | 111 | /*********************************************************************** |
|---|
| 111 | 112 | |
-
collection/model/MapView.d
| old |
new |
|
| 8 | 8 | History: |
|---|
| 9 | 9 | Date Who What |
|---|
| 10 | 10 | 24Sep95 dl@cs.oswego.edu Create from collections.d working file |
|---|
| 11 | | |
|---|
| | 11 | 17Sep07 db Allowed null keys |
|---|
| 12 | 12 | */ |
|---|
| 13 | 13 | |
|---|
| 14 | 14 | |
| … | … | |
| 38 | 38 | * Always returns false if k is null |
|---|
| 39 | 39 | **/ |
|---|
| 40 | 40 | |
|---|
| 41 | | public bool allowsKey(K key); |
|---|
| | 41 | //public bool allowsKey(K key); |
|---|
| 42 | 42 | |
|---|
| 43 | 43 | /** |
|---|
| 44 | 44 | * Report whether there exists any element with Key key. |
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic