Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: Source/wtf/ListHashSet.h

Issue 138643003: Simpler return value of HashTable::add/HashMap:add and others (Closed)
Patch Set: Daily master update (now with base url?) Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/wtf/InstanceCounter.cpp ('k') | Source/wtf/RefPtrHashMap.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserv ed. 2 * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserv ed.
3 * Copyright (C) 2011, Benjamin Poulain <ikipou@gmail.com> 3 * Copyright (C) 2011, Benjamin Poulain <ikipou@gmail.com>
4 * 4 *
5 * This library is free software; you can redistribute it and/or 5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public 6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either 7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version. 8 * version 2 of the License, or (at your option) any later version.
9 * 9 *
10 * This library is distributed in the hope that it will be useful, 10 * This library is distributed in the hope that it will be useful,
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 typedef ValueArg ValueType; 75 typedef ValueArg ValueType;
76 76
77 typedef ListHashSetIterator<ValueType, inlineCapacity, HashArg> iterator ; 77 typedef ListHashSetIterator<ValueType, inlineCapacity, HashArg> iterator ;
78 typedef ListHashSetConstIterator<ValueType, inlineCapacity, HashArg> con st_iterator; 78 typedef ListHashSetConstIterator<ValueType, inlineCapacity, HashArg> con st_iterator;
79 friend class ListHashSetConstIterator<ValueType, inlineCapacity, HashArg >; 79 friend class ListHashSetConstIterator<ValueType, inlineCapacity, HashArg >;
80 80
81 typedef ListHashSetReverseIterator<ValueType, inlineCapacity, HashArg> r everse_iterator; 81 typedef ListHashSetReverseIterator<ValueType, inlineCapacity, HashArg> r everse_iterator;
82 typedef ListHashSetConstReverseIterator<ValueType, inlineCapacity, HashA rg> const_reverse_iterator; 82 typedef ListHashSetConstReverseIterator<ValueType, inlineCapacity, HashA rg> const_reverse_iterator;
83 friend class ListHashSetConstReverseIterator<ValueType, inlineCapacity, HashArg>; 83 friend class ListHashSetConstReverseIterator<ValueType, inlineCapacity, HashArg>;
84 84
85 typedef HashTableAddResult<iterator> AddResult; 85 template<typename ValueType> struct HashTableAddResult {
86 HashTableAddResult(Node* storedValue, bool isNewEntry) : storedValue(sto redValue), isNewEntry(isNewEntry) { }
87 Node* storedValue;
88 bool isNewEntry;
89 };
90 typedef HashTableAddResult<ValueType> AddResult;
86 91
87 ListHashSet(); 92 ListHashSet();
88 ListHashSet(const ListHashSet&); 93 ListHashSet(const ListHashSet&);
89 ListHashSet& operator=(const ListHashSet&); 94 ListHashSet& operator=(const ListHashSet&);
90 ~ListHashSet(); 95 ~ListHashSet();
91 96
92 void swap(ListHashSet&); 97 void swap(ListHashSet&);
93 98
94 unsigned size() const; 99 unsigned size() const;
95 unsigned capacity() const; 100 unsigned capacity() const;
(...skipping 23 matching lines...) Expand all
119 const_iterator find(const ValueType&) const; 124 const_iterator find(const ValueType&) const;
120 bool contains(const ValueType&) const; 125 bool contains(const ValueType&) const;
121 126
122 // An alternate version of find() that finds the object by hashing and c omparing 127 // An alternate version of find() that finds the object by hashing and c omparing
123 // with some other type, to avoid the cost of type conversion. 128 // with some other type, to avoid the cost of type conversion.
124 // The HashTranslator interface is defined in HashSet. 129 // The HashTranslator interface is defined in HashSet.
125 template<typename HashTranslator, typename T> iterator find(const T&); 130 template<typename HashTranslator, typename T> iterator find(const T&);
126 template<typename HashTranslator, typename T> const_iterator find(const T&) const; 131 template<typename HashTranslator, typename T> const_iterator find(const T&) const;
127 template<typename HashTranslator, typename T> bool contains(const T&) co nst; 132 template<typename HashTranslator, typename T> bool contains(const T&) co nst;
128 133
129 // The return value of add is a pair of an iterator to the new value's l ocation, 134 // The return value of add is a pair of a pointer to the stored value,
130 // and a bool that is true if an new entry was added. 135 // and a bool that is true if an new entry was added.
131 AddResult add(const ValueType&); 136 AddResult add(const ValueType&);
132 137
138 // Same as add() except that the return value is an
139 // iterator. Useful in cases where it's needed to have the
140 // same return value as find() and where it's not possible to
141 // use a pointer to the storedValue.
142 iterator addReturnIterator(const ValueType&);
143
133 // Add the value to the end of the collection. If the value was already in 144 // Add the value to the end of the collection. If the value was already in
134 // the list, it is moved to the end. 145 // the list, it is moved to the end.
135 AddResult appendOrMoveToLast(const ValueType&); 146 AddResult appendOrMoveToLast(const ValueType&);
136 147
137 // Add the value to the beginning of the collection. If the value was al ready in 148 // Add the value to the beginning of the collection. If the value was al ready in
138 // the list, it is moved to the beginning. 149 // the list, it is moved to the beginning.
139 AddResult prependOrMoveToFirst(const ValueType&); 150 AddResult prependOrMoveToFirst(const ValueType&);
140 151
141 AddResult insertBefore(const ValueType& beforeValue, const ValueType& ne wValue); 152 AddResult insertBefore(const ValueType& beforeValue, const ValueType& ne wValue);
142 AddResult insertBefore(iterator, const ValueType&); 153 AddResult insertBefore(iterator, const ValueType&);
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after
728 { 739 {
729 return m_impl.template contains<BaseTranslator>(value); 740 return m_impl.template contains<BaseTranslator>(value);
730 } 741 }
731 742
732 template<typename T, size_t inlineCapacity, typename U> 743 template<typename T, size_t inlineCapacity, typename U>
733 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineC apacity, U>::add(const ValueType &value) 744 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineC apacity, U>::add(const ValueType &value)
734 { 745 {
735 createAllocatorIfNeeded(); 746 createAllocatorIfNeeded();
736 typename ImplType::AddResult result = m_impl.template add<BaseTranslator >(value, m_allocator.get()); 747 typename ImplType::AddResult result = m_impl.template add<BaseTranslator >(value, m_allocator.get());
737 if (result.isNewEntry) 748 if (result.isNewEntry)
738 appendNode(*result.iterator); 749 appendNode(*result.storedValue);
739 return AddResult(makeIterator(*result.iterator), result.isNewEntry); 750 return AddResult(*result.storedValue, result.isNewEntry);
740 } 751 }
741 752
742 template<typename T, size_t inlineCapacity, typename U> 753 template<typename T, size_t inlineCapacity, typename U>
754 typename ListHashSet<T, inlineCapacity, U>::iterator ListHashSet<T, inlineCa pacity, U>::addReturnIterator(const ValueType &value)
755 {
756 return makeIterator(add(value).storedValue);
757 }
758 template<typename T, size_t inlineCapacity, typename U>
743 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineC apacity, U>::appendOrMoveToLast(const ValueType &value) 759 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineC apacity, U>::appendOrMoveToLast(const ValueType &value)
744 { 760 {
745 createAllocatorIfNeeded(); 761 createAllocatorIfNeeded();
746 typename ImplType::AddResult result = m_impl.template add<BaseTranslator >(value, m_allocator.get()); 762 typename ImplType::AddResult result = m_impl.template add<BaseTranslator >(value, m_allocator.get());
747 Node* node = *result.iterator; 763 Node* node = *result.storedValue;
748 if (!result.isNewEntry) 764 if (!result.isNewEntry)
749 unlink(node); 765 unlink(node);
750 appendNode(node); 766 appendNode(node);
751 return AddResult(makeIterator(*result.iterator), result.isNewEntry); 767 return AddResult(*result.storedValue, result.isNewEntry);
752 } 768 }
753 769
754 template<typename T, size_t inlineCapacity, typename U> 770 template<typename T, size_t inlineCapacity, typename U>
755 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineC apacity, U>::prependOrMoveToFirst(const ValueType &value) 771 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineC apacity, U>::prependOrMoveToFirst(const ValueType &value)
756 { 772 {
757 createAllocatorIfNeeded(); 773 createAllocatorIfNeeded();
758 typename ImplType::AddResult result = m_impl.template add<BaseTranslator >(value, m_allocator.get()); 774 typename ImplType::AddResult result = m_impl.template add<BaseTranslator >(value, m_allocator.get());
759 Node* node = *result.iterator; 775 Node* node = *result.storedValue;
760 if (!result.isNewEntry) 776 if (!result.isNewEntry)
761 unlink(node); 777 unlink(node);
762 prependNode(node); 778 prependNode(node);
763 return AddResult(makeIterator(*result.iterator), result.isNewEntry); 779 return AddResult(*result.storedValue, result.isNewEntry);
764 } 780 }
765 781
766 template<typename T, size_t inlineCapacity, typename U> 782 template<typename T, size_t inlineCapacity, typename U>
767 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineC apacity, U>::insertBefore(iterator it, const ValueType& newValue) 783 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineC apacity, U>::insertBefore(iterator it, const ValueType& newValue)
768 { 784 {
769 createAllocatorIfNeeded(); 785 createAllocatorIfNeeded();
770 typename ImplType::AddResult result = m_impl.template add<BaseTranslator >(newValue, m_allocator.get()); 786 typename ImplType::AddResult result = m_impl.template add<BaseTranslator >(newValue, m_allocator.get());
771 if (result.isNewEntry) 787 if (result.isNewEntry)
772 insertNodeBefore(it.node(), *result.iterator); 788 insertNodeBefore(it.node(), *result.storedValue);
773 return AddResult(makeIterator(*result.iterator), result.isNewEntry); 789 return AddResult(*result.storedValue, result.isNewEntry);
774 } 790 }
775 791
776 template<typename T, size_t inlineCapacity, typename U> 792 template<typename T, size_t inlineCapacity, typename U>
777 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineC apacity, U>::insertBefore(const ValueType& beforeValue, const ValueType& newValu e) 793 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineC apacity, U>::insertBefore(const ValueType& beforeValue, const ValueType& newValu e)
778 { 794 {
779 createAllocatorIfNeeded(); 795 createAllocatorIfNeeded();
780 return insertBefore(find(beforeValue), newValue); 796 return insertBefore(find(beforeValue), newValue);
781 } 797 }
782 798
783 template<typename T, size_t inlineCapacity, typename U> 799 template<typename T, size_t inlineCapacity, typename U>
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
931 inline void deleteAllValues(const ListHashSet<T, inlineCapacity, U>& collect ion) 947 inline void deleteAllValues(const ListHashSet<T, inlineCapacity, U>& collect ion)
932 { 948 {
933 deleteAllValues<true, typename ListHashSet<T, inlineCapacity, U>::ValueT ype>(collection.m_impl); 949 deleteAllValues<true, typename ListHashSet<T, inlineCapacity, U>::ValueT ype>(collection.m_impl);
934 } 950 }
935 951
936 } // namespace WTF 952 } // namespace WTF
937 953
938 using WTF::ListHashSet; 954 using WTF::ListHashSet;
939 955
940 #endif /* WTF_ListHashSet_h */ 956 #endif /* WTF_ListHashSet_h */
OLDNEW
« no previous file with comments | « Source/wtf/InstanceCounter.cpp ('k') | Source/wtf/RefPtrHashMap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698