Title: [102084] trunk/Source/_javascript_Core
Revision
102084
Author
[email protected]
Date
2011-12-05 18:17:22 -0800 (Mon, 05 Dec 2011)

Log Message

Add HashMap::keys() and HashMap::values() for easy iteration of hash map keys and values in C++11.

Reviewed by Darin Adler.

* wtf/HashMap.h:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (102083 => 102084)


--- trunk/Source/_javascript_Core/ChangeLog	2011-12-06 02:07:23 UTC (rev 102083)
+++ trunk/Source/_javascript_Core/ChangeLog	2011-12-06 02:17:22 UTC (rev 102084)
@@ -1,3 +1,11 @@
+2011-12-05  Anders Carlsson  <[email protected]>
+
+        Add HashMap::keys() and HashMap::values() for easy iteration of hash map keys and values in C++11.
+
+        Reviewed by Darin Adler.
+
+        * wtf/HashMap.h:
+
 2011-12-05  Michael Saboff  <[email protected]>
 
         Create StringImpl::empty() as an 8 bit string

Modified: trunk/Source/_javascript_Core/wtf/HashMap.h (102083 => 102084)


--- trunk/Source/_javascript_Core/wtf/HashMap.h	2011-12-06 02:07:23 UTC (rev 102083)
+++ trunk/Source/_javascript_Core/wtf/HashMap.h	2011-12-06 02:17:22 UTC (rev 102084)
@@ -60,10 +60,14 @@
         typedef HashTable<KeyType, ValueType, PairFirstExtractor<ValueType>,
             HashFunctions, ValueTraits, KeyTraits> HashTableType;
 
+        class HashMapKeysProxy;
+        class HashMapValuesProxy;
+
     public:
         typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator;
         typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator;
 
+    public:
         void swap(HashMap&);
 
         int size() const;
@@ -76,6 +80,12 @@
         const_iterator begin() const;
         const_iterator end() const;
 
+        HashMapKeysProxy& keys() { return static_cast<HashMapKeysProxy&>(*this); }
+        const HashMapKeysProxy& keys() const { return static_cast<const HashMapKeysProxy&>(*this); }
+
+        HashMapValuesProxy& values() { return static_cast<HashMapValuesProxy&>(*this); }
+        const HashMapValuesProxy& values() const { return static_cast<const HashMapValuesProxy&>(*this); }
+
         iterator find(const KeyType&);
         const_iterator find(const KeyType&) const;
         bool contains(const KeyType&) const;
@@ -119,6 +129,76 @@
     private:
         pair<iterator, bool> inlineAdd(const KeyType&, MappedPassInReferenceType);
 
+        class HashMapKeysProxy : private HashMap {
+        public:
+            typedef typename HashMap::iterator::Keys iterator;
+            typedef typename HashMap::const_iterator::Keys const_iterator;
+            
+            iterator begin()
+            {
+                return HashMap::begin().keys();
+            }
+            
+            iterator end()
+            {
+                return HashMap::end().keys();
+            }
+
+            const_iterator begin() const
+            {
+                return HashMap::begin().keys();
+            }
+            
+            const_iterator end() const
+            {
+                return HashMap::end().keys();
+            }
+
+        private:
+            friend class HashMap;
+
+            // These are intentionally not implemented.
+            HashMapKeysProxy();
+            HashMapKeysProxy(const HashMapKeysProxy&);
+            HashMapKeysProxy& operator=(const HashMapKeysProxy&);
+            ~HashMapKeysProxy();
+        };
+
+        class HashMapValuesProxy : private HashMap {
+        public:
+            typedef typename HashMap::iterator::Values iterator;
+            typedef typename HashMap::const_iterator::Values const_iterator;
+            
+            iterator begin()
+            {
+                return HashMap::begin().values();
+            }
+            
+            iterator end()
+            {
+                return HashMap::end().values();
+            }
+
+            const_iterator begin() const
+            {
+                return HashMap::begin().values();
+            }
+            
+            const_iterator end() const
+            {
+                return HashMap::end().values();
+            }
+
+        private:
+            friend class HashMap;
+
+            // These are intentionally not implemented.
+            HashMapValuesProxy();
+            HashMapValuesProxy(const HashMapValuesProxy&);
+            HashMapValuesProxy& operator=(const HashMapValuesProxy&);
+            ~HashMapValuesProxy();
+        };
+
         HashTableType m_impl;
     };
 
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to