Skip to content
  • Mark OLESEN's avatar
    ENH: added HashTable 'lookup' and 'retain' methods · 61f80f78
    Mark OLESEN authored
    - lookup(): with a default value (const access)
      For example,
          Map<label> something;
          value = something.lookup(key, -1);
    
        being equivalent to the following:
    
          Map<label> something;
          value = -1;  // bad value
          if (something.found(key))
          {
              value = something[key];
          }
    
        except that lookup also makes it convenient to handle const references.
        Eg,
    
          const labelList& ids = someHash.lookup(key, labelList());
    
    - For consistency, provide a two parameter HashTable '()' operator.
      The lookup() method is, however, normally preferable when
      const-only access is to be ensured.
    
    - retain(): the counterpart to erase(), it only retains entries
      corresponding to the listed keys.
    
      For example,
          HashTable<someType> largeCache;
          wordHashSet preserve = ...;
    
          largeCache.retain(preserve);
    
        being roughly equivalent to the following two-stage process,
        but with reduced overhead and typing, and fewer potential mistakes.
    
          HashTable<someType> largeCache;
          wordHashSet preserve = ...;
    
          {
              wordHashSet cull(largeCache.toc()); // all keys
              cull.erase(preserve);               // except those to preserve
              largeCache.erase(cull);             //
          }
    
      The HashSet &= operator and retain() are functionally equivalent,
      but retain() also works with dissimilar value types.
    61f80f78