← Back to team overview

zorba-coders team mailing list archive

lp:~zorba-coders/zorba/objects-with-unordered-maps-and-const-char-star into lp:zorba

 

Ghislain Fourny has proposed merging lp:~zorba-coders/zorba/objects-with-unordered-maps-and-const-char-star into lp:zorba.

Requested reviews:
  Matthias Brantner (matthias-brantner)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/objects-with-unordered-maps-and-const-char-star/+merge/124225

Changing JSON object implementation to unordered maps.
-- 
https://code.launchpad.net/~zorba-coders/zorba/objects-with-unordered-maps-and-const-char-star/+merge/124225
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'src/store/naive/json_items.cpp'
--- src/store/naive/json_items.cpp	2012-09-11 22:55:05 +0000
+++ src/store/naive/json_items.cpp	2012-09-13 15:14:32 +0000
@@ -292,7 +292,7 @@
     bool accumulate)
 {
   ASSERT_INVARIANT();
-  zstring lName = aName->getStringValue();
+  const char* lName = aName->getStringValue().c_str();
 
   Keys::iterator ite = theKeys.find(lName);
 
@@ -308,7 +308,7 @@
     }
     
     csize lPosition = thePairs.size();
-    theKeys.insert(lName, lPosition);
+    theKeys.insert(std::make_pair(lName, lPosition));
     thePairs.push_back(std::make_pair(aName.getp(), lValue));
     aName->addReference();
     lValue->addReference();
@@ -318,7 +318,7 @@
   }
   else if (accumulate)
   {
-    csize lPosition = ite.getValue();
+    csize lPosition = ite->second;
 
     assert(thePairs[lPosition].first->getStringValue() == lName);
 
@@ -359,15 +359,16 @@
 {
   ASSERT_INVARIANT();
 
-  zstring lName = aName->getStringValue();
-  csize lPosition = 0;
+  const char* lName = aName->getStringValue().c_str();
   store::Item_t lValue;
 
-  if (!theKeys.get(lName, lPosition))
+  Keys::iterator lIterator = theKeys.find(lName);
+  if (lIterator == theKeys.end())
   {
     ASSERT_INVARIANT();
     return 0;
   }
+  csize lPosition = lIterator->second;
   
   store::Item* lKey;
 
@@ -393,10 +394,10 @@
     Keys::iterator lKeysEnd = theKeys.end();
     for (; lKeysIte != lKeysEnd; ++lKeysIte)
     {
-      csize lPos = lKeysIte.getValue();
+      csize lPos = lKeysIte->second;
       if (lPos > lPosition)
       {
-        lKeysIte.setValue(lPos - 1);
+        lKeysIte->second = lPos - 1;
       }
     }
   }
@@ -414,14 +415,15 @@
     const store::Item_t& aValue)
 {
   ASSERT_INVARIANT();
-  zstring lName = aName->getStringValue();
-  csize lPosition = 0;
+  const char* lName = aName->getStringValue().c_str();
 
-  if (!theKeys.get(lName, lPosition))
+  Keys::iterator lIterator = theKeys.find(lName);
+  if (lIterator == theKeys.end())
   {
     ASSERT_INVARIANT();
-    return NULL;
+    return 0;
   }
+  csize lPosition = lIterator->second;
 
   assert(thePairs[lPosition].first->getStringValue() == lName);
 
@@ -461,10 +463,10 @@
     const store::Item_t& aNewName)
 {
   ASSERT_INVARIANT();
-  zstring lName = aName->getStringValue();
-  zstring lNewName = aNewName->getStringValue();
+  const char* lName = aName->getStringValue().c_str();
+  const char* lNewName = aNewName->getStringValue().c_str();
 
-  if (theKeys.exists(lNewName))
+  if (theKeys.find(lNewName) != theKeys.end())
   {
     ASSERT_INVARIANT();
     return false;
@@ -478,14 +480,14 @@
     return false;
   }
 
-  csize lPosition = ite.getValue();
+  csize lPosition = ite->second;
   assert(thePairs[lPosition].first->getStringValue() == lName);
   
   thePairs[lPosition].first->removeReference();
   aNewName->addReference();
   thePairs[lPosition].first = aNewName.getp();
   theKeys.erase(ite);
-  theKeys.insert(lNewName, lPosition);
+  theKeys.insert(std::make_pair(lNewName, lPosition));
 
   ASSERT_INVARIANT();
   return true;
@@ -560,13 +562,14 @@
 store::Item_t SimpleJSONObject::getObjectValue(const store::Item_t& aKey) const
 {
   ASSERT_INVARIANT();
-  zstring lName = aKey->getStringValue();
+  const char* lName = aKey->getStringValue().c_str();
 
-  csize lPosition = 0;
-  if (!theKeys.get(lName, lPosition))
+  Keys::const_iterator lIterator = theKeys.find(lName);
+  if (lIterator == theKeys.end())
   {
     return NULL;
   }
+  csize lPosition = lIterator->second;
 
   assert(thePairs[lPosition].first->equals(aKey));
   return thePairs[lPosition].second;
@@ -593,15 +596,16 @@
   JSONItem::assertInvariant();
   assert(theKeys.size() == thePairs.size());
 
-  for(Keys::iterator lIter = theKeys.begin();
+  for(Keys::const_iterator lIter = theKeys.begin();
       lIter != theKeys.end();
       ++lIter)
   {
-    csize lPosition = lIter.getValue();
+    csize lPosition = lIter->second;
     assert(lPosition < thePairs.size());
     assert(thePairs[lPosition].first != NULL);
     assert(thePairs[lPosition].first->isAtomic());
-    assert(thePairs[lPosition].first->getStringValue() == lIter.getKey());
+    assert(thePairs[lPosition].first->getStringValue().c_str() ==
+           lIter->first);
     assert(thePairs[lPosition].second != NULL);
   }
 }

=== modified file 'src/store/naive/json_items.h'
--- src/store/naive/json_items.h	2012-09-11 22:55:05 +0000
+++ src/store/naive/json_items.h	2012-09-13 15:14:32 +0000
@@ -17,12 +17,11 @@
 #ifndef ZORBA_STORE_JSON_ITEMS_H
 #define ZORBA_STORE_JSON_ITEMS_H
 
-#include <set>
 #include <vector>
 
 #include <zorba/config.h>
 #include <zorbautils/hashmap_zstring.h>
-
+#include <util/unordered_map.h>
 #include "store/api/item_handle.h"
 #include "store/api/iterator.h"
 
@@ -241,7 +240,29 @@
 class SimpleJSONObject : public JSONObject
 {
 protected:
-  ZSTRING_HASH_MAP(csize, Keys);
+  class ConstCharStarHash
+  {
+  public:
+    typedef size_t result_type;
+    size_t operator()(const char* a) const
+    {
+      size_t hash = 0;
+      while (*a)
+      {
+          hash = hash * 101  +  *a++;
+      }
+      return hash;
+    }
+  };
+  class ConstCharStarComparator
+  {
+  public:
+    bool operator()(const char* a, const char* b) const
+    {
+      return strcmp(a, b) == 0;
+    }
+  };
+  typedef std::unordered_map<const char*, csize, ConstCharStarHash, ConstCharStarComparator> Keys;
   typedef std::vector<std::pair<store::Item*, store::Item*> > Pairs;
 
   class KeyIterator : public store::Iterator
@@ -271,7 +292,12 @@
 public:
   SimpleJSONObject()
     :
+<<<<<<< TREE
     theKeys(64, false)
+=======
+    theCollection(NULL),
+    theRoot(NULL)
+>>>>>>> MERGE-SOURCE
   {
   }
 
@@ -487,7 +513,13 @@
 };
 
 
-#ifndef NDEBUG
+<<<<<<< TREE
+#ifndef NDEBUG
+=======
+void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
+    
+#ifndef NDEBUG
+>>>>>>> MERGE-SOURCE
 #define ASSERT_INVARIANT() assertInvariant()
 #else
 #define ASSERT_INVARIANT()


Follow ups