📄 _hashtable.c
字号:
return _Pii(const_iterator(__first, this), const_iterator((_Node*)_M_buckets[__m], this)); return _Pii(const_iterator(__first, this), end()); } } return _Pii(end(), end());}template <class _Val, class _Key, class _HF, class _ExK, class _EqK, class _All>__size_type__ hashtable<_Val,_Key,_HF,_ExK,_EqK,_All>::erase(const key_type& __key){ const size_type __n = _M_bkt_num_key(__key); _Node* __first = (_Node*)_M_buckets[__n]; size_type __erased = 0; if (__first) { _Node* __cur = __first; _Node* __next = __cur->_M_next; while (__next) { if (_M_equals(_M_get_key(__next->_M_val), __key)) { __cur->_M_next = __next->_M_next; _M_delete_node(__next); __next = __cur->_M_next; ++__erased; --_M_num_elements._M_data; } else { __cur = __next; __next = __cur->_M_next; } } if (_M_equals(_M_get_key(__first->_M_val), __key)) { _M_buckets[__n] = __first->_M_next; _M_delete_node(__first); ++__erased; --_M_num_elements._M_data; } } return __erased;}template <class _Val, class _Key, class _HF, class _ExK, class _EqK, class _All>void hashtable<_Val,_Key,_HF,_ExK,_EqK,_All>::erase(const const_iterator& __it){ // const iterator& __it = __REINTERPRET_CAST(const iterator&,_c_it); const _Node* __p = __it._M_cur; if (__p) { const size_type __n = _M_bkt_num(__p->_M_val); _Node* __cur = (_Node*)_M_buckets[__n]; if (__cur == __p) { _M_buckets[__n] = __cur->_M_next; _M_delete_node(__cur); --_M_num_elements._M_data; } else { _Node* __next = __cur->_M_next; while (__next) { if (__next == __p) { __cur->_M_next = __next->_M_next; _M_delete_node(__next); --_M_num_elements._M_data; break; } else { __cur = __next; __next = __cur->_M_next; } } } }}template <class _Val, class _Key, class _HF, class _ExK, class _EqK, class _All>void hashtable<_Val,_Key,_HF,_ExK,_EqK,_All> ::erase(const_iterator _c_first, const_iterator _c_last){ iterator& __first = (iterator&)_c_first; iterator& __last = (iterator&)_c_last; size_type __f_bucket = __first._M_cur ? _M_bkt_num(__first._M_cur->_M_val) : _M_buckets.size(); size_type __l_bucket = __last._M_cur ? _M_bkt_num(__last._M_cur->_M_val) : _M_buckets.size(); if (__first._M_cur == __last._M_cur) return; else if (__f_bucket == __l_bucket) _M_erase_bucket(__f_bucket, __first._M_cur, __last._M_cur); else { _M_erase_bucket(__f_bucket, __first._M_cur, 0); for (size_type __n = __f_bucket + 1; __n < __l_bucket; ++__n) _M_erase_bucket(__n, 0); if (__l_bucket != _M_buckets.size()) _M_erase_bucket(__l_bucket, __last._M_cur); }}template <class _Val, class _Key, class _HF, class _ExK, class _EqK, class _All>void hashtable<_Val,_Key,_HF,_ExK,_EqK,_All> ::resize(size_type __num_elements_hint){ const size_type __old_n = _M_buckets.size(); if (__num_elements_hint > __old_n) { const size_type __n = _M_next_size(__num_elements_hint); if (__n > __old_n) { _BucketVector __tmp(__n, (void*)(0), _M_buckets.get_allocator()); _STLP_TRY { for (size_type __bucket = 0; __bucket < __old_n; ++__bucket) { _Node* __first = (_Node*)_M_buckets[__bucket]; while (__first) { size_type __new_bucket = _M_bkt_num(__first->_M_val, __n); _M_buckets[__bucket] = __first->_M_next; __first->_M_next = (_Node*)__tmp[__new_bucket]; __tmp[__new_bucket] = __first; __first = (_Node*)_M_buckets[__bucket]; } } _M_buckets.swap(__tmp); }# ifdef _STLP_USE_EXCEPTIONS catch(...) { for (size_type __bucket = 0; __bucket < __tmp.size(); ++__bucket) { while (__tmp[__bucket]) { _Node* __next = ((_Node*)__tmp[__bucket])->_M_next; _M_delete_node((_Node*)__tmp[__bucket]); __tmp[__bucket] = __next; } } throw; }# endif /* _STLP_USE_EXCEPTIONS */ } }}template <class _Val, class _Key, class _HF, class _ExK, class _EqK, class _All>void hashtable<_Val,_Key,_HF,_ExK,_EqK,_All> ::_M_erase_bucket(const size_type __n, _Node* __first, _Node* __last){ _Node* __cur = (_Node*)_M_buckets[__n]; if (__cur == __first) _M_erase_bucket(__n, __last); else { _Node* __next; for (__next = __cur->_M_next; __next != __first; __cur = __next, __next = __cur->_M_next) ; while (__next != __last) { __cur->_M_next = __next->_M_next; _M_delete_node(__next); __next = __cur->_M_next; --_M_num_elements._M_data; } }}template <class _Val, class _Key, class _HF, class _ExK, class _EqK, class _All>void hashtable<_Val,_Key,_HF,_ExK,_EqK,_All> ::_M_erase_bucket(const size_type __n, _Node* __last){ _Node* __cur = (_Node*)_M_buckets[__n]; while (__cur && __cur != __last) { _Node* __next = __cur->_M_next; _M_delete_node(__cur); __cur = __next; _M_buckets[__n] = __cur; --_M_num_elements._M_data; }}template <class _Val, class _Key, class _HF, class _ExK, class _EqK, class _All>void hashtable<_Val,_Key,_HF,_ExK,_EqK,_All>::clear(){ for (size_type __i = 0; __i < _M_buckets.size(); ++__i) { _Node* __cur = (_Node*)_M_buckets[__i]; while (__cur != 0) { _Node* __next = __cur->_M_next; _M_delete_node(__cur); __cur = __next; } _M_buckets[__i] = 0; } _M_num_elements._M_data = 0;} template <class _Val, class _Key, class _HF, class _ExK, class _EqK, class _All>void hashtable<_Val,_Key,_HF,_ExK,_EqK,_All> ::_M_copy_from(const hashtable<_Val,_Key,_HF,_ExK,_EqK,_All>& __ht){ _M_buckets.clear(); _M_buckets.reserve(__ht._M_buckets.size()); _M_buckets.insert(_M_buckets.end(), __ht._M_buckets.size(), (void*) 0); _STLP_TRY { for (size_type __i = 0; __i < __ht._M_buckets.size(); ++__i) { const _Node* __cur = (_Node*)__ht._M_buckets[__i]; if (__cur) { _Node* __xcopy = _M_new_node(__cur->_M_val); _M_buckets[__i] = __xcopy; for (_Node* __next = __cur->_M_next; __next; __cur = __next, __next = __cur->_M_next) { __xcopy->_M_next = _M_new_node(__next->_M_val); __xcopy = __xcopy->_M_next; } } } _M_num_elements._M_data = __ht._M_num_elements._M_data; } _STLP_UNWIND(clear());}# undef __iterator__ # undef const_iterator# undef __size_type__# undef __reference__# undef size_type # undef value_type # undef key_type # undef _Node # undef __stl_num_primes# undef hashtable_STLP_END_NAMESPACE#endif /* _STLP_HASHTABLE_C */// Local Variables:// mode:C++// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -