[Avida-SVN] r3103 - in development: Avida.xcodeproj source/analyze source/tools

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Sun Jan 11 11:39:40 PST 2009


Author: brysonda
Date: 2009-01-11 14:39:39 -0500 (Sun, 11 Jan 2009)
New Revision: 3103

Added:
   development/source/tools/tArrayMap.h
   development/source/tools/tKVPair.h
Modified:
   development/Avida.xcodeproj/project.pbxproj
   development/source/analyze/cAnalyzeGenotype.h
   development/source/analyze/tAnalyzeJobBatch.h
Log:
Add new tArrayMap tool class, a simple, lightweight array based map class useful for small map sizes.

Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj	2009-01-11 17:42:53 UTC (rev 3102)
+++ development/Avida.xcodeproj/project.pbxproj	2009-01-11 19:39:39 UTC (rev 3103)
@@ -395,6 +395,8 @@
 		7005A70909BA0FBE0007E16E /* cOrgInterface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cOrgInterface.h; sourceTree = "<group>"; };
 		700AE91B09DB65F200A073FD /* cTaskContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cTaskContext.h; sourceTree = "<group>"; };
 		700D9BD90F1A5D33002CC711 /* tAnalyzeJobBatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tAnalyzeJobBatch.h; sourceTree = "<group>"; };
+		700D9C170F1A7436002CC711 /* tArrayMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tArrayMap.h; sourceTree = "<group>"; };
+		700D9C1A0F1A77EC002CC711 /* tKVPair.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tKVPair.h; sourceTree = "<group>"; };
 		700E28CF0859FFD700CF158A /* tObjectFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tObjectFactory.h; sourceTree = "<group>"; };
 		700E2B83085DE50C00CF158A /* avida-viewer */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "avida-viewer"; sourceTree = BUILT_PRODUCTS_DIR; };
 		7013845F09028B3E0087ED2E /* cAvidaConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cAvidaConfig.h; sourceTree = "<group>"; };
@@ -1670,6 +1672,8 @@
 				70211A5C0ECBD531004A293A /* cRCObject.cc */,
 				70211A6C0ECBDB72004A293A /* tRCPtr.h */,
 				7069470C0EE8FA2700AD67C0 /* tArrayUtils.h */,
+				700D9C170F1A7436002CC711 /* tArrayMap.h */,
+				700D9C1A0F1A77EC002CC711 /* tKVPair.h */,
 			);
 			path = tools;
 			sourceTree = "<group>";

Modified: development/source/analyze/cAnalyzeGenotype.h
===================================================================
--- development/source/analyze/cAnalyzeGenotype.h	2009-01-11 17:42:53 UTC (rev 3102)
+++ development/source/analyze/cAnalyzeGenotype.h	2009-01-11 19:39:39 UTC (rev 3103)
@@ -37,6 +37,9 @@
 #ifndef cGenome_h
 #include "cGenome.h"
 #endif
+#ifndef cGenotypeData_h
+#include "cGenotypeData.h"
+#endif
 #ifndef cInstSet_h
 #include "cInstSet.h"
 #endif
@@ -46,6 +49,9 @@
 #ifndef cPhenPlastGenotype_h
 #include "cPhenPlastGenotype.h"
 #endif
+#ifndef cRWLock_h
+#include "cRWLock.h"
+#endif
 #ifndef cString_h
 #include "cString.h"
 #endif
@@ -58,11 +64,12 @@
 #ifndef tArray_h
 #include "tArray.h"
 #endif
-
-#include "cGenotypeData.h"
-#include "cRWLock.h"
-#include "tDictionary.h"
+#ifndef tArrayMap_h
+#include "tArrayMap.h"
+#endif
+#ifndef tRCPtr_h
 #include "tRCPtr.h"
+#endif
 
 
 // cAnalyzeGenotype    : Collection of information about loaded genotypes
@@ -105,7 +112,7 @@
   struct sGenotypeDatastore : public cRCObject
   {
     cRWLock rwlock;
-    tDictionary<cGenotypeData*> dmap;
+    tArrayMap<int, cGenotypeData*> dmap;
     
     sGenotypeDatastore() { ; }
     sGenotypeDatastore(const sGenotypeDatastore& ds) : cRCObject(ds) { ; } // Note that data objects are not copied right now

Modified: development/source/analyze/tAnalyzeJobBatch.h
===================================================================
--- development/source/analyze/tAnalyzeJobBatch.h	2009-01-11 17:42:53 UTC (rev 3102)
+++ development/source/analyze/tAnalyzeJobBatch.h	2009-01-11 19:39:39 UTC (rev 3103)
@@ -3,7 +3,7 @@
  *  Avida
  *
  *  Created by David on 1/11/09.
- *  Copyright 2009 Digital Evolution Laboratory : Michigan State University. All rights reserved.
+ *  Copyright 2009 Michigan State University. All rights reserved.
  *
  *
  *  This program is free software; you can redistribute it and/or

Added: development/source/tools/tArrayMap.h
===================================================================
--- development/source/tools/tArrayMap.h	                        (rev 0)
+++ development/source/tools/tArrayMap.h	2009-01-11 19:39:39 UTC (rev 3103)
@@ -0,0 +1,110 @@
+/*
+ *  tArrayMap.h
+ *  Avida
+ *
+ *  Created by David on 1/11/09.
+ *  Copyright 2009 Michigan State University. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; version 2
+ *  of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#ifndef tArrayMap_h
+#define tArrayMap_h
+
+#ifndef tArray_h
+#include "tArray.h"
+#endif
+#ifndef tKVPair_h
+#include "tKVPair.h"
+#endif
+
+
+template<class KeyType, class ValueType> class tArrayMap
+{
+private:
+  tArray<tKVPair<KeyType, ValueType> > m_map;
+  
+public:
+  tArrayMap() { ; }
+  tArrayMap(const tArrayMap& am) : m_map(am.m_map) { ; }
+  
+  void Set(const KeyType& key, const ValueType& value)
+  {
+    for (int i = 0; i < m_map.GetSize(); i++) {
+      if (m_map[i].Key() == key) {
+        m_map[i].Value() = value;
+        return;
+      }
+    }
+    m_map.Push(tKVPair<KeyType, ValueType>(key, value));
+  }
+  
+  bool Get(const KeyType& key, ValueType& out_value) const
+  {
+    for (int i = 0; i < m_map.GetSize(); i++) {
+      if (m_map[i].Key() == key) {
+        out_value = m_map[i].Value();
+        return true;
+      }
+    }
+    return false;
+  }
+  
+  const ValueType& Get(const KeyType& key, const ValueType& default_value) const
+  {
+    for (int i = 0; i < m_map.GetSize(); i++) {
+      if (m_map[i].Key() == key) return m_map[i].Value();
+    }
+    m_map.Push(tKVPair<KeyType, ValueType>(key, default_value));
+    return m_map[m_map.GetSize() - 1].Value();
+  }
+  
+  void Remove(const KeyType& key)
+  {
+    for (int i = 0; i < m_map.GetSize(); i++) {
+      if (m_map[i].Key() == key) {
+        int lastkv = m_map.GetSize() - 1;
+        if (i != lastkv) m_map[i] = m_map[lastkv];
+        m_map.Resize(lastkv);
+      }
+    }
+  }
+
+  bool Remove(const KeyType& key, ValueType& out_value)
+  {
+    for (int i = 0; i < m_map.GetSize(); i++) {
+      if (m_map[i].Key() == key) {
+        int lastkv = m_map.GetSize() - 1;
+        out_value = m_map[i].Value();
+        if (i != lastkv) m_map[i] = m_map[lastkv];
+        m_map.Resize(lastkv);
+        return true;
+      }
+    }
+    return false;
+  }
+  
+  
+  typedef typename tArray<tKVPair<KeyType, ValueType> >::iterator iterator;
+  typedef typename tArray<tKVPair<KeyType, ValueType> >::const_iterator const_iterator;
+  
+  inline iterator begin() { m_map.begin(); }
+  inline iterator end() { m_map.end(); }
+  inline const_iterator begin() const { m_map.begin(); }
+  inline const_iterator end() const { m_map.end(); }
+};
+
+#endif

Added: development/source/tools/tKVPair.h
===================================================================
--- development/source/tools/tKVPair.h	                        (rev 0)
+++ development/source/tools/tKVPair.h	2009-01-11 19:39:39 UTC (rev 3103)
@@ -0,0 +1,50 @@
+/*
+ *  tKVPair.h
+ *  Avida
+ *
+ *  Created by David on 1/11/09.
+ *  Copyright 2009 Michigan State University. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; version 2
+ *  of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#ifndef tKVPair_h
+#define tKVPair_h
+
+template<class K, class V> class tKVPair
+{
+private:
+  K m_key;
+  V m_value;
+  
+public:
+  inline tKVPair() { ; }
+  inline tKVPair(const K& key, const V& value) : m_key(key), m_value(value) { ; }
+  inline tKVPair(const tKVPair& p) : m_key(p.m_key), m_value(p.m_value) { ; }
+  
+  inline tKVPair& operator=(const tKVPair& rhs) { m_key = rhs.m_key; m_value = rhs.m_value; }
+  
+  inline K& Key() { return m_key; }
+  inline const K& Key() const { return m_key; }
+  
+  inline V& Value() { return m_value; }
+  inline const V& Value() const { m_value; }
+  
+  inline void Set(const K& key, const V& value) { m_key = key; m_value = value; } 
+};
+
+
+#endif




More information about the Avida-cvs mailing list