Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #626: nullKeys.patch

File nullKeys.patch, 10.3 kB (added by Some1_2, 1 year ago)
  • collection/HashMap.d

    old new  
    1313        21Oct95  dl                 fixed error in removeAt 
    1414        9Apr97   dl                 made Serializable 
    1515        14Dec06  kb                 Converted, templated & reshaped for Tango 
     16        17Sep07  db                 Allowed null keys 
    1617         
    1718********************************************************************************/ 
    1819 
     
    305306         
    306307        public final bool containsKey(K key) 
    307308        { 
    308                 if (!isValidKey(key) || count is 0) 
     309                if (count is 0) 
    309310                    return false; 
    310311 
    311312                LLPairT p = table[hashOf(key)]; 
     
    326327         
    327328        public final bool containsPair(K key, V element) 
    328329        { 
    329                 if (!isValidKey(key) || !isValidArg(element) || count is 0) 
     330                if (!isValidArg(element) || count is 0) 
    330331                    return false; 
    331332 
    332333                LLPairT p = table[hashOf(key)]; 
     
    361362         
    362363        public final V get(K key) 
    363364        { 
    364                 checkKey(key); 
    365365                if (count !is 0) 
    366366                   { 
    367367                   LLPairT p = table[hashOf(key)]; 
     
    386386 
    387387        public bool get(K key, inout V element) 
    388388        { 
    389                 checkKey(key); 
    390389                if (count !is 0) 
    391390                   { 
    392391                   LLPairT p = table[hashOf(key)]; 
     
    552551         
    553552        public final void add (K key, V element) 
    554553        { 
    555                 checkKey(key); 
    556554                checkElement(element); 
    557555 
    558556                if (table is null) 
     
    598596         
    599597        public final void removeKey (K key) 
    600598        { 
    601                 if (!isValidKey(key) || count is 0) 
     599                if (count is 0) 
    602600                    return; 
    603601 
    604602                int h = hashOf(key); 
     
    637635         
    638636        public final void replacePair (K key, V oldElement, V newElement) 
    639637        { 
    640                 if (!isValidKey(key) || !isValidArg(oldElement) || count is 0) 
     638                if (!isValidArg(oldElement) || count is 0) 
    641639                    return; 
    642640 
    643641                LLPairT p = table[hashOf(key)]; 
     
    693691 
    694692        protected final int hashOf(K key) 
    695693        { 
    696                 return (typeid(K).getHash(&key) & 0x7FFFFFFF) % table.length; 
     694             
     695                return (((key is null) ? 0 : typeid(K).getHash(&key)) & 0x7FFFFFFF) % table.length; 
    697696        } 
    698697 
    699698 
     
    865864                        { 
    866865                        ++c; 
    867866                        assert(allows(p.element())); 
    868                         assert(allowsKey(p.key())); 
     867                        //assert(allowsKey(p.key())); 
    869868                        assert(containsKey(p.key())); 
    870869                        assert(contains(p.element())); 
    871870                        assert(instances(p.element()) >= 1); 
     
    951950debug(Test) 
    952951{ 
    953952        import tango.io.Console; 
     953        import tango.io.Stdout;      
    954954                         
    955955        void main() 
    956956        { 
    957957                auto map = new HashMap!(char[], double); 
    958958                map.add ("foo", 3.14); 
    959959                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); 
    960967 
    961968                foreach (key, value; map.keys) {typeof(key) x; x = key;} 
     969                foreach (key, value; map2.keys) {typeof(key) x; x = key;}                
    962970 
    963971                foreach (value; map.keys) {} 
     972                foreach (value; map2.keys) {}                
    964973 
    965974                foreach (value; map.elements) {} 
     975                foreach (value; map2.elements) {}                
    966976 
    967977                auto keys = map.keys(); 
    968978                while (keys.more) 
    969979                       auto v = keys.get(); 
     980                auto keys2 = map2.keys(); 
     981                while (keys2.more) 
     982                       auto v = keys2.get();                    
    970983 
    971984                foreach (value; map) {} 
     985                foreach (value; map2) {}                 
    972986 
    973987                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;                       
    975991 
    976992                map.checkImplementation(); 
     993                map2.checkImplementation();              
    977994 
    978995                Cout (map).newline; 
     996                Cout (map2).newline;                 
    979997        } 
    980998} 
  • collection/impl/LLPair.d

    old new  
    88 History: 
    99 Date     Who                What 
    1010 24Sep95  dl@cs.oswego.edu   Create from tango.util.collection.d  working file 
     11 17Sep07  db                 Allowed null keys  
    1112 
    1213*/ 
    1314 
     
    4344        private K key_; 
    4445 
    4546        /** 
     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        /** 
    4657         * Make a cell with given key, elment, and next link 
    4758        **/ 
    4859 
     
    97108 
    98109        public final int keyHash() 
    99110        { 
    100                 return typeid(K).getHash(&key_); 
     111               return (key is null) ? 0 : typeid(K).getHash(&key_); 
    101112        } 
    102113 
    103114 
     
    108119        public final LLPair findKey(K key) 
    109120        { 
    110121                for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_) 
    111                      if (p.key() == key
     122                     if (keyCompare(p.key(), key)
    112123                         return p; 
    113124                return null; 
    114125        } 
     
    120131        public final LLPair find(K key, T element) 
    121132        { 
    122133                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) 
    124135                         return p; 
    125136                return null; 
    126137        } 
     
    135146                int i = 0; 
    136147                for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_) 
    137148                    { 
    138                     if (p.key() == key
     149                    if (keyCompare(p.key(), key)
    139150                        return i; 
    140151                    else 
    141152                       ++i; 
     
    152163                int i = 0; 
    153164                for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_) 
    154165                    { 
    155                     if (p.key() == key && p.element() == element) 
     166                    if (keyCompare(p.key(), key) && p.element() == element) 
    156167                        return i; 
    157168                    else 
    158169                       ++i; 
     
    167178        { 
    168179                int c = 0; 
    169180                for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_) 
    170                      if (p.key() == key
     181                   if (keyCompare(p.key(), key)
    171182                         ++c; 
    172183                return c; 
    173184        } 
     
    179190        { 
    180191                int c = 0; 
    181192                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) 
    183194                         ++c; 
    184195                return c; 
    185196        } 
  • collection/impl/MapCollection.d

    old new  
    1111        13Oct95  dl                 Create 
    1212        28jan97  dl                 make class public 
    1313        14Dec06  kb                 adapted for Tango usage 
     14        17Sep07  db                 Allowed null keys        
    1415 
    1516********************************************************************************/ 
    1617 
     
    7879 
    7980        ************************************************************************/ 
    8081 
    81         public final bool allowsKey(K key) 
     82        /*public final bool allowsKey(K key) 
    8283        { 
    8384                return (key !is K.init); 
    84         } 
     85        }*/ 
    8586 
    86         protected final bool isValidKey(K key) 
     87        /*protected final bool isValidKey(K key) 
    8788        { 
    8889                static if (is (K : Object)) 
    8990                          { 
     
    9192                              return false; 
    9293                          } 
    9394                return true; 
    94         } 
     95        }*/ 
    9596 
    9697        /*********************************************************************** 
    9798 
     
    99100 
    100101        ************************************************************************/ 
    101102 
    102         protected final void checkKey(K key) 
     103        /*protected final void checkKey(K key) 
    103104        { 
    104105                if (!isValidKey(key)) 
    105106                   { 
    106107                   throw new IllegalElementException("Attempt to include invalid key _in Collection"); 
    107108                   } 
    108         } 
     109        }*/ 
    109110 
    110111        /*********************************************************************** 
    111112 
  • collection/model/MapView.d

    old new  
    88 History: 
    99 Date     Who                What 
    1010 24Sep95  dl@cs.oswego.edu   Create from collections.d  working file 
    11  
     11 17Sep07  db                 Allowed null keys 
    1212*/ 
    1313 
    1414 
     
    3838         * Always returns false if k is null 
    3939        **/ 
    4040 
    41         public bool allowsKey(K key); 
     41        //public bool allowsKey(K key); 
    4242 
    4343        /** 
    4444         * Report whether there exists any element with Key key.