Changeset 13703 in webkit for trunk/JavaScriptCore/kxmlcore/HashTable.h
- Timestamp:
- Apr 5, 2006, 2:19:57 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kxmlcore/HashTable.h
r13089 r13703 137 137 #endif 138 138 139 ReferenceType operator*() const139 PointerType get() const 140 140 { 141 141 checkValidity(); 142 return *m_position; 143 } 144 PointerType operator->() const { return &**this; } 142 return m_position; 143 } 144 ReferenceType operator*() const { return *get(); } 145 PointerType operator->() const { return get(); } 145 146 146 147 const_iterator& operator++() … … 217 218 // default copy, assignment and destructor are OK 218 219 219 ReferenceType operator*() const { return const_cast<ReferenceType>(*m_iterator); } 220 PointerType operator->() const { return &**this; } 220 PointerType get() const { return const_cast<PointerType>(m_iterator.get()); } 221 ReferenceType operator*() const { return *get(); } 222 PointerType operator->() const { return get(); } 221 223 222 224 iterator& operator++() { ++m_iterator; return *this; } … … 263 265 typedef HashTableIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> iterator; 264 266 typedef HashTableConstIterator<Key, Value, Extractor, HashFunctions, Traits, KeyTraits> const_iterator; 267 typedef Traits ValueTraits; 265 268 typedef Key KeyType; 266 269 typedef Value ValueType; … … 281 284 int size() const { return m_keyCount; } 282 285 int capacity() const { return m_tableSize; } 286 bool isEmpty() const { return !m_keyCount; } 283 287 284 288 pair<iterator, bool> add(const ValueType& value) { return add<KeyType, ValueType, IdentityTranslatorType>(Extractor::extract(value), value); } … … 465 469 466 470 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits> 467 inlinetypename HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::iterator HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::find(const Key& key)471 typename HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::iterator HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::find(const Key& key) 468 472 { 469 473 if (!m_table) … … 477 481 478 482 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits> 479 inlinetypename HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::const_iterator HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::find(const Key& key) const483 typename HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::const_iterator HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::find(const Key& key) const 480 484 { 481 485 if (!m_table) … … 489 493 490 494 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits> 491 inlinebool HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::contains(const KeyType& key) const495 bool HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::contains(const KeyType& key) const 492 496 { 493 497 if (!m_table) … … 498 502 499 503 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits> 500 inlinevoid HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::remove(ValueType* pos)504 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::remove(ValueType* pos) 501 505 { 502 506 invalidateIterators(); … … 518 522 519 523 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits> 520 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::remove(const KeyType& key)521 {522 if (!m_table)523 return;524 525 remove(find(key));526 }527 528 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>529 524 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::remove(iterator it) 530 525 { … … 535 530 } 536 531 537 538 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits> 539 inline Value *HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::allocateTable(int size) 532 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits> 533 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::remove(const KeyType& key) 534 { 535 remove(find(key)); 536 } 537 538 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits> 539 Value *HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::allocateTable(int size) 540 540 { 541 541 // would use a template member function with explicit specializations here, but 542 542 // gcc doesn't appear to support that 543 543 if (Traits::emptyValueIsZero) 544 return reinterpret_cast<ValueType *>(fastCalloc(size, sizeof(ValueType))); 545 else { 546 ValueType *result = reinterpret_cast<ValueType *>(fastMalloc(size * sizeof(ValueType))); 547 for (int i = 0; i < size; i++) { 548 initializeBucket(result[i]); 549 } 550 return result; 551 } 552 } 553 554 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits> 555 inline void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::deallocateTable(ValueType *table, int size) 556 { 557 if (Traits::needsDestruction) { 558 for (int i = 0; i < size; ++i) { 559 (&table[i])->~ValueType(); 560 } 561 } 562 544 return static_cast<ValueType *>(fastCalloc(size, sizeof(ValueType))); 545 ValueType* result = static_cast<ValueType*>(fastMalloc(size * sizeof(ValueType))); 546 for (int i = 0; i < size; i++) 547 initializeBucket(result[i]); 548 return result; 549 } 550 551 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits> 552 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::deallocateTable(ValueType *table, int size) 553 { 554 if (Traits::needsDestruction) 555 for (int i = 0; i < size; ++i) 556 table[i].~ValueType(); 563 557 fastFree(table); 564 558 } 565 559 566 560 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits> 567 inlinevoid HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::expand()561 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::expand() 568 562 { 569 563 int newSize; … … 595 589 m_table = allocateTable(newTableSize); 596 590 597 for (int i = 0; i != oldTableSize; ++i) {591 for (int i = 0; i != oldTableSize; ++i) 598 592 if (!isEmptyOrDeletedBucket(oldTable[i])) 599 593 reinsert(oldTable[i]); 600 }601 594 602 595 m_deletedCount = 0; … … 608 601 609 602 template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits> 610 inlinevoid HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::clear()603 void HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::clear() 611 604 { 612 605 invalidateIterators(); … … 781 774 #endif // CHECK_HASHTABLE_ITERATORS 782 775 776 // iterator adapters 777 778 template<typename HashTableType, typename ValueType> struct HashTableConstIteratorAdapter { 779 HashTableConstIteratorAdapter(const typename HashTableType::const_iterator& impl) : m_impl(impl) {} 780 781 const ValueType* get() const { return (const ValueType*)m_impl.get(); } 782 const ValueType& operator*() const { return *get(); } 783 const ValueType* operator->() const { return get(); } 784 785 HashTableConstIteratorAdapter& operator++() { ++m_impl; return *this; } 786 // postfix ++ intentionally omitted 787 788 typename HashTableType::const_iterator m_impl; 789 }; 790 791 template<typename HashTableType, typename ValueType> struct HashTableIteratorAdapter { 792 HashTableIteratorAdapter(const typename HashTableType::iterator& impl) : m_impl(impl) {} 793 794 ValueType* get() const { return (ValueType*)m_impl.get(); } 795 ValueType& operator*() const { return *get(); } 796 ValueType* operator->() const { return get(); } 797 798 HashTableIteratorAdapter& operator++() { ++m_impl; return *this; } 799 // postfix ++ intentionally omitted 800 801 operator HashTableConstIteratorAdapter<HashTableType, ValueType>() { 802 typename HashTableType::const_iterator i = m_impl; 803 return i; 804 } 805 806 typename HashTableType::iterator m_impl; 807 }; 808 809 template<typename T, typename U> 810 inline bool operator==(const HashTableConstIteratorAdapter<T, U>& a, const HashTableConstIteratorAdapter<T, U>& b) 811 { 812 return a.m_impl == b.m_impl; 813 } 814 815 template<typename T, typename U> 816 inline bool operator!=(const HashTableConstIteratorAdapter<T, U>& a, const HashTableConstIteratorAdapter<T, U>& b) 817 { 818 return a.m_impl != b.m_impl; 819 } 820 821 template<typename T, typename U> 822 inline bool operator==(const HashTableIteratorAdapter<T, U>& a, const HashTableIteratorAdapter<T, U>& b) 823 { 824 return a.m_impl == b.m_impl; 825 } 826 827 template<typename T, typename U> 828 inline bool operator!=(const HashTableIteratorAdapter<T, U>& a, const HashTableIteratorAdapter<T, U>& b) 829 { 830 return a.m_impl != b.m_impl; 831 } 832 833 // reference count manager 834 835 template<typename ValueTraits, typename ValueStorageTraits> struct NeedsRef { 836 static const bool value = ValueTraits::needsRef && !ValueStorageTraits::needsRef; 837 }; 838 839 template<bool needsRef, typename ValueTraits> struct RefCounterBase; 840 841 template<typename ValueTraits> 842 struct RefCounterBase<false, ValueTraits> { 843 typedef typename ValueTraits::TraitType ValueType; 844 static void ref(const ValueType&) { } 845 static void deref(const ValueType&) { } 846 }; 847 848 template<typename ValueTraits> 849 struct RefCounterBase<true, ValueTraits> { 850 typedef typename ValueTraits::TraitType ValueType; 851 static void ref(const ValueType& v) { ValueTraits::ref(*(const ValueType*)&v); } 852 static void deref(const ValueType& v) { ValueTraits::deref(*(const ValueType*)&v); } 853 }; 854 855 template<typename ValueTraits, typename ValueStorageTraits> struct RefCounter { 856 typedef typename ValueTraits::TraitType ValueType; 857 typedef typename ValueStorageTraits::TraitType ValueStorageType; 858 static const bool needsRef = NeedsRef<ValueTraits, ValueStorageTraits>::value; 859 typedef RefCounterBase<needsRef, ValueTraits> Base; 860 static void ref(const ValueStorageType& v) { Base::ref(*(const ValueType*)&v); } 861 static void deref(const ValueStorageType& v) { Base::deref(*(const ValueType*)&v); } 862 }; 863 864 template<typename FirstTraits, typename SecondTraits, typename ValueStorageTraits> 865 struct RefCounter<PairBaseHashTraits<FirstTraits, SecondTraits>, ValueStorageTraits> { 866 typedef typename FirstTraits::TraitType FirstType; 867 typedef typename SecondTraits::TraitType SecondType; 868 typedef typename ValueStorageTraits::FirstTraits FirstStorageTraits; 869 typedef typename ValueStorageTraits::SecondTraits SecondStorageTraits; 870 typedef typename ValueStorageTraits::TraitType ValueStorageType; 871 static const bool firstNeedsRef = NeedsRef<FirstTraits, FirstStorageTraits>::value; 872 static const bool secondNeedsRef = NeedsRef<SecondTraits, SecondStorageTraits>::value; 873 typedef RefCounterBase<firstNeedsRef, FirstTraits> FirstBase; 874 typedef RefCounterBase<secondNeedsRef, SecondTraits> SecondBase; 875 static void ref(const ValueStorageType& v) { 876 FirstBase::ref(*(const FirstType*)&v.first); 877 SecondBase::ref(*(const SecondType*)&v.second); 878 } 879 static void deref(const ValueStorageType& v) { 880 FirstBase::deref(*(const FirstType*)&v.first); 881 SecondBase::deref(*(const SecondType*)&v.second); 882 } 883 }; 884 885 template<bool needsRef, typename HashTableType, typename ValueTraits> struct HashTableRefCounterBase; 886 887 template<typename HashTableType, typename ValueTraits> 888 struct HashTableRefCounterBase<false, HashTableType, ValueTraits> 889 { 890 static void refAll(HashTableType&) { } 891 static void derefAll(HashTableType&) { } 892 }; 893 894 template<typename HashTableType, typename ValueTraits> 895 struct HashTableRefCounterBase<true, HashTableType, ValueTraits> 896 { 897 typedef typename HashTableType::iterator iterator; 898 typedef RefCounter<ValueTraits, typename HashTableType::ValueTraits> ValueRefCounter; 899 static void refAll(HashTableType&); 900 static void derefAll(HashTableType&); 901 }; 902 903 template<typename HashTableType, typename ValueTraits> 904 void HashTableRefCounterBase<true, HashTableType, ValueTraits>::refAll(HashTableType& table) 905 { 906 iterator end = table.end(); 907 for (iterator it = table.begin(); it != end; ++it) 908 ValueRefCounter::ref(*it); 909 } 910 911 template<typename HashTableType, typename ValueTraits> 912 void HashTableRefCounterBase<true, HashTableType, ValueTraits>::derefAll(HashTableType& table) 913 { 914 iterator end = table.end(); 915 for (iterator it = table.begin(); it != end; ++it) 916 ValueRefCounter::deref(*it); 917 } 918 919 template<typename HashTableType, typename ValueTraits> struct HashTableRefCounter { 920 static const bool needsRef = NeedsRef<ValueTraits, typename HashTableType::ValueTraits>::value; 921 typedef HashTableRefCounterBase<needsRef, HashTableType, ValueTraits> Base; 922 static void refAll(HashTableType& table) { Base::refAll(table); } 923 static void derefAll(HashTableType& table) { Base::derefAll(table); } 924 }; 925 783 926 } // namespace KXMLCore 784 927
Note:
See TracChangeset
for help on using the changeset viewer.