| 96 | std::fill( table_.begin(), table_.end(), -1 ); |
| 97 | } |
| 98 | int find( const short * k, bool create = false ){ |
| 99 | if (2*filled_ >= capacity_) grow(); |
| 100 | // Get the hash value |
| 101 | size_t h = hash( k ) % capacity_; |
| 102 | // Find the element with he right key, using linear probing |
| 103 | while(1){ |
| 104 | int e = table_[h]; |
| 105 | if (e==-1){ |
| 106 | if (create){ |
| 107 | // Insert a new key and return the new id |
| 108 | for( size_t i=0; i<key_size_; i++ ) |
| 109 | keys_[ filled_*key_size_+i ] = k[i]; |
| 110 | return table_[h] = filled_++; |
| 111 | } |
| 112 | else |
| 113 | return -1; |
| 114 | } |
| 115 | // Check if the current key is The One |
| 116 | bool good = true; |
| 117 | for( size_t i=0; i<key_size_ && good; i++ ) |
| 118 | if (keys_[ e*key_size_+i ] != k[i]) |
| 119 | good = false; |
| 120 | if (good) |
| 121 | return e; |
| 122 | // Continue searching |
| 123 | h++; |
| 124 | if (h==capacity_) h = 0; |
| 125 | } |
| 126 | } |
| 127 | const short * getKey( int i ) const{ |
| 128 | return &keys_[i*key_size_]; |
| 129 | } |