use map for config cache
authorJim Morris <morris@wolfman.com>
Sat, 12 Apr 2014 03:54:36 +0000 (20:54 -0700)
committerJim Morris <morris@wolfman.com>
Sat, 12 Apr 2014 03:54:36 +0000 (20:54 -0700)
src/libs/Config.cpp
src/libs/ConfigCache.cpp
src/libs/ConfigCache.h
src/libs/ConfigValue.cpp
src/libs/ConfigValue.h

index 73fd954..d1df1fa 100644 (file)
@@ -105,6 +105,8 @@ ConfigValue *Config::value(uint16_t check_sum_a, uint16_t check_sum_b, uint16_t
     return this->value(check_sums);
 }
 
+static ConfigValue dummyValue;
+
 // Get a value from the configuration as a string
 // Because we don't like to waste space in Flash with lengthy config parameter names, we take a checksum instead so that the name does not have to be stored
 // See get_checksum
@@ -120,8 +122,10 @@ ConfigValue *Config::value(uint16_t check_sums[])
 
     if(result == NULL) {
         // create a dummy value for this to play with, each call requires it's own value not a shared one
-        result= new ConfigValue(check_sums);
-        config_cache->add(result);
+        // result= new ConfigValue(check_sums);
+        // config_cache->add(result);
+        dummyValue.clear();
+        result = &dummyValue;
     }
 
     return result;
dissimilarity index 64%
index 764c0d8..3646bd5 100644 (file)
@@ -1,85 +1,75 @@
-#include "ConfigCache.h"
-#include "ConfigValue.h"
-
-#include "libs/StreamOutput.h"
-
-ConfigCache::ConfigCache()
-{
-}
-
-ConfigCache::~ConfigCache()
-{
-    clear();
-}
-
-void ConfigCache::clear()
-{
-    for (ConfigValue *i : store)  {
-        delete i;
-    }
-    vector<ConfigValue*>().swap(store);   //  makes sure the vector releases its memory
-}
-
-// If we find an existing value, replace it, otherwise, push it at the back of the list
-void ConfigCache::replace_or_push_back(ConfigValue *new_value)
-{
-
-    bool value_exists = false;
-
-    // For each already existing element
-    for(ConfigValue*& i: store) {
-        // If this configvalue matches the checksum
-        bool match = true;
-        unsigned int counter = 0;
-        while( counter < 3 && new_value->check_sums[counter] != 0x0000 ) {
-            if(i->check_sums[counter] != new_value->check_sums[counter]  ) {
-                match = false;
-                break;
-            }
-            counter++;
-        }
-        if( !match ) {
-            continue;
-        }
-        value_exists = true;
-
-        // Replace with the provided value
-        i=  new_value;
-        break;
-    }
-
-    // Value does not already exists, add to the list
-    if( !value_exists ) {
-        store.push_back(new_value);
-    }
-
-}
-
-ConfigValue *ConfigCache::lookup(const uint16_t *check_sums) const
-{
-   for( ConfigValue *cv : store) {
-        if(memcmp(check_sums, cv->check_sums, sizeof(cv->check_sums)) == 0)
-            return cv;
-    }
-
-    return NULL;
-}
-
-void ConfigCache::collect(uint16_t family, uint16_t cs, vector<uint16_t> *list)
-{
-    for( ConfigValue *value : store ) {
-        if( value->check_sums[2] == cs && value->check_sums[0] == family ) {
-            // We found a module enable for this family, add it's number
-            list->push_back(value->check_sums[1]);
-        }
-    }
-}
-
-void ConfigCache::dump(StreamOutput *stream)
-{
-    int l= 1;
-    for( ConfigValue *v : store ) {
-        stream->printf("%3d - %04X %04X %04X : '%s' - found: %d, default: %d, default-double: %f, default-int: %d\n",
-            l++, v->check_sums[0], v->check_sums[1], v->check_sums[2], v->value.c_str(), v->found, v->default_set, v->default_double, v->default_int );
-    }
-}
+#include "ConfigCache.h"
+#include "ConfigValue.h"
+
+#include "libs/StreamOutput.h"
+#include "md5.h"
+
+ConfigCache::ConfigCache()
+{
+}
+
+ConfigCache::~ConfigCache()
+{
+    clear();
+}
+
+static uint32_t md5sum(const uint16_t *array)
+{
+    MD5 md5;
+    md5.update((const unsigned char *)array, (size_t)3*sizeof(uint16_t));
+    md5.finalize();
+    uint32_t r;
+    md5.bindigest(&r, sizeof(r));
+    return r;
+}
+
+void ConfigCache::clear()
+{
+    for (auto &kv : store)  {
+        ConfigValue *i = kv.second;
+        delete i;
+    }
+    store.clear();
+}
+
+void ConfigCache::add(ConfigValue *v)
+{
+    uint32_t k = md5sum(v->check_sums);
+    store[k] = v;
+}
+
+// If we find an existing value, replace it, otherwise, push it at the back of the list
+void ConfigCache::replace_or_push_back(ConfigValue *new_value)
+{
+    add(new_value);
+}
+
+ConfigValue *ConfigCache::lookup(const uint16_t *check_sums) const
+{
+    uint32_t k = md5sum(check_sums);
+    storage_t::const_iterator i = store.find(k);
+    if(i == store.end())
+        return NULL;
+    return i->second;
+}
+
+void ConfigCache::collect(uint16_t family, uint16_t cs, vector<uint16_t> *list)
+{
+    for( auto& kv : store ) {
+        ConfigValue *i = kv.second;
+        if( i->check_sums[2] == cs && i->check_sums[0] == family ) {
+            // We found a module enable for this family, add it's number
+            list->push_back(i->check_sums[1]);
+        }
+    }
+}
+
+void ConfigCache::dump(StreamOutput *stream)
+{
+    int l = 1;
+    for( auto& kv : store ) {
+        ConfigValue *v= kv.second;
+        stream->printf("%3d - %04X %04X %04X : '%s' - found: %d, default: %d, default-double: %f, default-int: %d\n",
+                       l++, v->check_sums[0], v->check_sums[1], v->check_sums[2], v->value.c_str(), v->found, v->default_set, v->default_double, v->default_int );
+    }
+}
index 535175b..c09b018 100644 (file)
@@ -11,6 +11,7 @@
 using namespace std;
 #include <vector>
 #include <stdint.h>
+#include <map>
 
 class ConfigValue;
 class StreamOutput;
@@ -21,7 +22,7 @@ class ConfigCache {
         ~ConfigCache();
         void clear();
 
-        void add(ConfigValue* v) { store.push_back(v); }
+        void add(ConfigValue* v);
 
         // lookup and return the entru that matches the check sums,return NULL if not found
         ConfigValue *lookup(const uint16_t *check_sums) const;
@@ -36,7 +37,8 @@ class ConfigCache {
         void dump(StreamOutput *stream);
 
     private:
-        vector<ConfigValue*> store;
+        typedef map<uint32_t, ConfigValue*> storage_t;
+        storage_t store;
 };
 
 
index 486b04b..0b382f2 100644 (file)
 #include <stdio.h>
 
 ConfigValue::ConfigValue()
+{
+    clear();
+}
+
+void ConfigValue:: clear()
 {
     this->found = false;
     this->default_set = false;
index 092fc10..b6bcfa0 100644 (file)
@@ -17,7 +17,7 @@ class ConfigValue{
         ConfigValue(uint16_t *check_sums);
         ConfigValue(const ConfigValue& to_copy);
         ConfigValue& operator= (const ConfigValue& to_copy);
-
+        void clear();
         ConfigValue* required();
         float as_number();
         int as_int();