📄 densehashtable.h
字号:
use_deleted = false; } // These are public so the iterators can use them // True if the item at position bucknum is "deleted" marker bool test_deleted(size_type bucknum) const { // The num_deleted test is crucial for read(): after read(), the ht values // are garbage, and we don't want to think some of them are deleted. return (use_deleted && num_deleted > 0 && equals(get_key(delval), get_key(table[bucknum]))); } bool test_deleted(const iterator &it) const { return (use_deleted && num_deleted > 0 && equals(get_key(delval), get_key(*it))); } bool test_deleted(const const_iterator &it) const { return (use_deleted && num_deleted > 0 && equals(get_key(delval), get_key(*it))); } // Set it so test_deleted is true. true if object didn't used to be deleted // See below (at erase()) to explain why we allow const_iterators bool set_deleted(const_iterator &it) { assert(use_deleted); // bad if set_deleted_key() wasn't called bool retval = !test_deleted(it); // &* converts from iterator to value-type set_value(const_cast<value_type*>(&(*it)), delval); return retval; } // Set it so test_deleted is false. true if object used to be deleted bool clear_deleted(const_iterator &it) { assert(use_deleted); // bad if set_deleted_key() wasn't called // happens automatically when we assign something else in its place return test_deleted(it); } // EMPTY HELPER FUNCTIONS // This lets the user describe a key that will indicate empty (unused) // table entries. This key should be an "impossible" entry -- // if you try to insert it for real, you won't be able to retrieve it! // (NB: while you pass in an entire value, only the key part is looked // at. This is just because I don't know how to assign just a key.) public: // These are public so the iterators can use them // True if the item at position bucknum is "empty" marker bool test_empty(size_type bucknum) const { assert(use_empty); // we always need to know what's empty! return equals(get_key(emptyval), get_key(table[bucknum])); } bool test_empty(const iterator &it) const { assert(use_empty); // we always need to know what's empty! return equals(get_key(emptyval), get_key(*it)); } bool test_empty(const const_iterator &it) const { assert(use_empty); // we always need to know what's empty! return equals(get_key(emptyval), get_key(*it)); } private: // You can either set a range empty or an individual element void set_empty(size_type bucknum) { assert(use_empty); set_value(&table[bucknum], emptyval); } void fill_range_with_empty(value_type* table_start, value_type* table_end) { // Like set_empty(range), but doesn't destroy previous contents STL_NAMESPACE::uninitialized_fill(table_start, table_end, emptyval); } void set_empty(size_type buckstart, size_type buckend) { assert(use_empty); destroy_buckets(buckstart, buckend); fill_range_with_empty(table + buckstart, table + buckend); } public: // TODO(csilvers): change all callers of this to pass in a key instead, // and take a const key_type instead of const value_type. void set_empty_key(const value_type &val) { // Once you set the empty key, you can't change it assert(!use_empty); // The deleted indicator (if specified) and the empty indicator // must be different. assert(!use_deleted || !equals(get_key(val), get_key(delval))); use_empty = true; set_value(&emptyval, val); assert(!table); // must set before first use // num_buckets was set in constructor even though table was NULL table = (value_type *) malloc(num_buckets * sizeof(*table)); assert(table); fill_range_with_empty(table, table + num_buckets); } // FUNCTIONS CONCERNING SIZE public: size_type size() const { return num_elements - num_deleted; } // Buckets are always a power of 2 size_type max_size() const { return (size_type(-1) >> 1U) + 1; } bool empty() const { return size() == 0; } size_type bucket_count() const { return num_buckets; } size_type max_bucket_count() const { return max_size(); } size_type nonempty_bucket_count() const { return num_elements; } private: // Because of the above, size_type(-1) is never legal; use it for errors static const size_type ILLEGAL_BUCKET = size_type(-1); private: // This is the smallest size a hashtable can be without being too crowded // If you like, you can give a min #buckets as well as a min #elts size_type min_size(size_type num_elts, size_type min_buckets_wanted) { size_type sz = HT_MIN_BUCKETS; // min buckets allowed while ( sz < min_buckets_wanted || num_elts >= sz * enlarge_resize_percent ) sz *= 2; return sz; } // Used after a string of deletes void maybe_shrink() { assert(num_elements >= num_deleted); assert((bucket_count() & (bucket_count()-1)) == 0); // is a power of two assert(bucket_count() >= HT_MIN_BUCKETS); if (shrink_threshold > 0 && (num_elements-num_deleted) < shrink_threshold && bucket_count() > HT_MIN_BUCKETS ) { size_type sz = bucket_count() / 2; // find how much we should shrink while ( sz > HT_MIN_BUCKETS && (num_elements - num_deleted) < sz * shrink_resize_percent ) sz /= 2; // stay a power of 2 dense_hashtable tmp(*this, sz); // Do the actual resizing swap(tmp); // now we are tmp } consider_shrink = false; // because we just considered it } // We'll let you resize a hashtable -- though this makes us copy all! // When you resize, you say, "make it big enough for this many more elements" void resize_delta(size_type delta, size_type min_buckets_wanted = 0) { if ( consider_shrink ) // see if lots of deletes happened maybe_shrink(); if ( bucket_count() > min_buckets_wanted && (num_elements + delta) <= enlarge_threshold ) return; // we're ok as we are // Sometimes, we need to resize just to get rid of all the // "deleted" buckets that are clogging up the hashtable. So when // deciding whether to resize, count the deleted buckets (which // are currently taking up room). But later, when we decide what // size to resize to, *don't* count deleted buckets, since they // get discarded during the resize. const size_type needed_size = min_size(num_elements + delta, min_buckets_wanted); if ( needed_size > bucket_count() ) { // we don't have enough buckets const size_type resize_to = min_size(num_elements - num_deleted + delta, min_buckets_wanted); dense_hashtable tmp(*this, resize_to); swap(tmp); // now we are tmp } } // Increase number of buckets, assuming value_type has trivial copy // constructor and destructor. (Really, we want it to have "trivial // move", because that's what realloc does. But there's no way to // capture that using type_traits, so we pretend that move(x, y) is // equivalent to "x.~T(); new(x) T(y);" which is pretty much // correct, if a bit conservative.) void expand_array(size_t resize_to, true_type) { table = (value_type *) realloc(table, resize_to * sizeof(value_type)); assert(table); fill_range_with_empty(table + num_buckets, table + resize_to); } // Increase number of buckets, without special assumptions about value_type. // TODO(austern): make this exception safe. Handle exceptions from // value_type's copy constructor. void expand_array(size_t resize_to, false_type) { value_type* new_table = (value_type *) malloc(resize_to * sizeof(value_type)); assert(new_table); STL_NAMESPACE::uninitialized_copy(table, table + num_buckets, new_table); fill_range_with_empty(new_table + num_buckets, new_table + resize_to); destroy_buckets(0, num_buckets); free(table); table = new_table; } // Used to actually do the rehashing when we grow/shrink a hashtable void copy_from(const dense_hashtable &ht, size_type min_buckets_wanted = 0) { clear(); // clear table, set num_deleted to 0 // If we need to change the size of our table, do it now const size_type resize_to = min_size(ht.size(), min_buckets_wanted); if ( resize_to > bucket_count() ) { // we don't have enough buckets typedef integral_constant<bool, (has_trivial_copy<value_type>::value && has_trivial_destructor<value_type>::value)> realloc_ok; // we pretend mv(x,y) == "x.~T(); new(x) T(y)" expand_array(resize_to, realloc_ok()); num_buckets = resize_to; reset_thresholds(); } // We use a normal iterator to get non-deleted bcks from ht // We could use insert() here, but since we know there are // no duplicates and no deleted items, we can be more efficient assert((bucket_count() & (bucket_count()-1)) == 0); // a power of two for ( const_iterator it = ht.begin(); it != ht.end(); ++it ) { size_type num_probes = 0; // how many times we've probed size_type bucknum; const size_type bucket_count_minus_one = bucket_count() - 1; for (bucknum = hash(get_key(*it)) & bucket_count_minus_one; !test_empty(bucknum); // not empty bucknum = (bucknum + JUMP_(key, num_probes)) & bucket_count_minus_one) { ++num_probes; assert(num_probes < bucket_count()); // or else the hashtable is full } set_value(&table[bucknum], *it); // copies the value to here num_elements++; } } // Required by the spec for hashed associative container public: // Though the docs say this should be num_buckets, I think it's much // more useful as req_elements. As a special feature, calling with // req_elements==0 will cause us to shrink if we can, saving space. void resize(size_type req_elements) { // resize to this or larger if ( consider_shrink || req_elements == 0 ) maybe_shrink(); if ( req_elements > num_elements ) return resize_delta(req_elements - num_elements, 0); } // Change the value of shrink_resize_percent and // enlarge_resize_percent. The description at the beginning of this // file explains how to choose the values. Setting the shrink // parameter to 0.0 ensures that the table never shrinks. void set_resizing_parameters(float shrink, float grow) { assert(shrink >= 0.0); assert(grow <= 1.0); assert(shrink <= grow/2.0); shrink_resize_percent = shrink; enlarge_resize_percent = grow; reset_thresholds(); } // CONSTRUCTORS -- as required by the specs, we take a size, // but also let you specify a hashfunction, key comparator, // and key extractor. We also define a copy constructor and =. // DESTRUCTOR -- needs to free the table explicit dense_hashtable(size_type n = 0, const HashFcn& hf = HashFcn(), const EqualKey& eql = EqualKey(), const ExtractKey& ext = ExtractKey()) : hash(hf), equals(eql), get_key(ext), num_deleted(0), use_deleted(false), use_empty(false), delval(), emptyval(), enlarge_resize_percent(HT_OCCUPANCY_FLT), shrink_resize_percent(HT_EMPTY_FLT), table(NULL), num_buckets(min_size(0, n)), num_elements(0) { // table is NULL until emptyval is set. However, we set num_buckets // here so we know how much space to allocate once emptyval is set reset_thresholds(); } // As a convenience for resize(), we allow an optional second argument // which lets you make this new hashtable a different size than ht dense_hashtable(const dense_hashtable& ht, size_type min_buckets_wanted = 0) : hash(ht.hash), equals(ht.equals), get_key(ht.get_key), num_deleted(0), use_deleted(ht.use_deleted), use_empty(ht.use_empty), delval(ht.delval), emptyval(ht.emptyval), enlarge_resize_percent(ht.enlarge_resize_percent), shrink_resize_percent(ht.shrink_resize_percent), table(NULL), num_buckets(0), num_elements(0) { reset_thresholds(); copy_from(ht, min_buckets_wanted); // copy_from() ignores deleted entries } dense_hashtable& operator= (const dense_hashtable& ht) { if (&ht == this) return *this; // don't copy onto ourselves clear(); hash = ht.hash; equals = ht.equals; get_key = ht.get_key; use_deleted = ht.use_deleted; use_empty = ht.use_empty; set_value(&delval, ht.delval); set_value(&emptyval, ht.emptyval); enlarge_resize_percent = ht.enlarge_resize_percent; shrink_resize_percent = ht.shrink_resize_percent; copy_from(ht); // sets num_deleted to 0 too return *this; } ~dense_hashtable() { if (table) { destroy_buckets(0, num_buckets); free(table); } } // Many STL algorithms use swap instead of copy constructors void swap(dense_hashtable& ht) { STL_NAMESPACE::swap(hash, ht.hash); STL_NAMESPACE::swap(equals, ht.equals); STL_NAMESPACE::swap(get_key, ht.get_key); STL_NAMESPACE::swap(num_deleted, ht.num_deleted); STL_NAMESPACE::swap(use_deleted, ht.use_deleted); STL_NAMESPACE::swap(use_empty, ht.use_empty); STL_NAMESPACE::swap(enlarge_resize_percent, ht.enlarge_resize_percent); STL_NAMESPACE::swap(shrink_resize_percent, ht.shrink_resize_percent); { value_type tmp; // for annoying reasons, swap() doesn't work set_value(&tmp, delval); set_value(&delval, ht.delval); set_value(&ht.delval, tmp); } { value_type tmp; // for annoying reasons, swap() doesn't work set_value(&tmp, emptyval); set_value(&emptyval, ht.emptyval); set_value(&ht.emptyval, tmp); } STL_NAMESPACE::swap(table, ht.table); STL_NAMESPACE::swap(num_buckets, ht.num_buckets); STL_NAMESPACE::swap(num_elements, ht.num_elements);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -