[Avida-SVN] r2464 - in development/source: script tools

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Tue Mar 18 09:20:32 PDT 2008


Author: brysonda
Date: 2008-03-18 12:20:32 -0400 (Tue, 18 Mar 2008)
New Revision: 2464

Modified:
   development/source/script/ASTree.h
   development/source/script/cSemanticASTVisitor.cc
   development/source/tools/tHashTable.h
Log:
AS:
Construct and semantic check cASTLiteralDict objects.

Alter tHashTable HashKey methods to allow for external type specialization.


Modified: development/source/script/ASTree.h
===================================================================
--- development/source/script/ASTree.h	2008-03-18 04:12:42 UTC (rev 2463)
+++ development/source/script/ASTree.h	2008-03-18 16:20:32 UTC (rev 2464)
@@ -330,7 +330,8 @@
   cASTNode* m_code;
   
 public:
-  cASTFunctionDefinition(const cASFilePosition& fp, const sASTypeInfo& type, const cString& name, cASTVariableDefinitionList* args)
+  cASTFunctionDefinition(const cASFilePosition& fp, const sASTypeInfo& type, const cString& name,
+                         cASTVariableDefinitionList* args)
     : cASTNode(fp), m_type(type), m_name(name), m_args(args), m_code(NULL) { ; }
   ~cASTFunctionDefinition();
   
@@ -559,16 +560,26 @@
 
 class cASTLiteralDict : public cASTNode
 {
-private:
+public:
+  struct sMapping {
+    cASTNode* idx;
+    cASTNode* val;
+    
+    sMapping(cASTNode* in_idx, cASTNode* in_val) : idx(in_idx), val(in_val) { ; }
+    ~sMapping() { delete idx; delete val; }
+  };
+  
+private:  
   sASTypeInfo m_type;
+  tList<sMapping> m_mappings;
   
 public:
   cASTLiteralDict(const cASFilePosition& fp) : cASTNode(fp), m_type(AS_TYPE_DICT) { ; }
   ~cASTLiteralDict() { ; }  
   
-  void AddMapping(cASTNode* idx, cASTNode* val) { /* @TODO */ }
+  void AddMapping(cASTNode* idx, cASTNode* val) { m_mappings.PushRear(new sMapping(idx, val)); }
+  tListIterator<sMapping> Iterator() { return tListIterator<sMapping>(m_mappings); }
   
-  
   const sASTypeInfo& GetType() const { return m_type; }
   
   void Accept(cASTVisitor& visitor);
@@ -680,7 +691,8 @@
   inline int GetVarID(int idx) const { return m_nodes[idx].var_id; }
   inline bool IsVarGlobal(int idx) const { return m_nodes[idx].global; }
   inline const sASTypeInfo& GetVarType(int idx) const { return m_nodes[idx].type; }
-  inline void SetVar(int idx, int var_id, bool global, const sASTypeInfo& type) { m_nodes[idx].SetVar(var_id, global, type); }
+  inline void SetVar(int idx, int var_id, bool global, const sASTypeInfo& type)
+    { m_nodes[idx].SetVar(var_id, global, type); }
   
   inline bool IsLastNamed() const { return m_last_named; }
   inline bool IsLastWild() const { return m_last_wild; }

Modified: development/source/script/cSemanticASTVisitor.cc
===================================================================
--- development/source/script/cSemanticASTVisitor.cc	2008-03-18 04:12:42 UTC (rev 2463)
+++ development/source/script/cSemanticASTVisitor.cc	2008-03-18 16:20:32 UTC (rev 2464)
@@ -341,7 +341,8 @@
       if (al->GetSize() == 0) SEMANTIC_WARNING(NO_DIMENSIONS);
       
       // If dimensions exceed type limits
-      if ((node.GetType().type == TYPE(ARRAY) && al->GetSize() > 1) || (node.GetType().type == TYPE(MATRIX) && al->GetSize() > 2)) {
+      if ((node.GetType().type == TYPE(ARRAY) && al->GetSize() > 1) ||
+          (node.GetType().type == TYPE(MATRIX) && al->GetSize() > 2)) {
         SEMANTIC_ERROR(TOO_MANY_ARGUMENTS);
         SEMANTIC_ERROR(VARIABLE_DIMENSIONS_INVALID, (const char*)node.GetName(), mapType(node.GetType()));
       }
@@ -743,7 +744,12 @@
 
 void cSemanticASTVisitor::VisitLiteralDict(cASTLiteralDict& node)
 {
-  // @TODO - literal dict
+  tListIterator<cASTLiteralDict::sMapping> it = node.Iterator();
+  cASTLiteralDict::sMapping* mapping = NULL;
+  while ((mapping = it.Next())) {
+    mapping->idx->Accept(*this);
+    mapping->val->Accept(*this);
+  }
 }
 
 

Modified: development/source/tools/tHashTable.h
===================================================================
--- development/source/tools/tHashTable.h	2008-03-18 04:12:42 UTC (rev 2463)
+++ development/source/tools/tHashTable.h	2008-03-18 16:20:32 UTC (rev 2464)
@@ -90,6 +90,43 @@
 template <class DATA_TYPE> class tList; // access
 template <class DATA_TYPE> class tListIterator; // aggregate
 
+namespace nHashTable {
+  
+  // HASH_TYPE = basic object
+  // Casts the pointer to an int, shift right last two bit positions, mod by
+  // the size of the hash table and hope for the best.  The shift is to account
+  // for typical 4-byte alignment of pointer values.  Depending on architecture
+  // this may not be true and could result in suboptimal hashing at higher
+  // order alignments.
+  template<typename HASH_TYPE> inline int HashKey(const HASH_TYPE& key, int table_size)
+  {
+    // Cast/Dereference of key as an int* tells the compiler that we really want
+    // to truncate the value to an integer, even if a pointer is larger.
+    return abs((*((int*)&key) >> 2) % table_size);    
+  }
+  
+  // HASH_TYPE = int
+  // Simply mod the into by the size of the hash table and hope for the best
+  template<> inline int HashKey<int>(const int& key, int table_size)
+  {
+    return abs(key % table_size);
+  }
+  
+  // HASH_TYPE = cString
+  // We hash a string simply by adding up the individual character values in
+  // that string and modding by the hash size.  For most applications this
+  // will work fine (and reasonably fast!) but some patterns will cause all
+  // strings to go into the same cell.  For example, "ABC"=="CBA"=="BBB".
+  template<> inline int HashKey<cString>(const cString& key, int table_size)
+  {
+    unsigned int out_hash = 0;
+    for (int i = 0; i < key.GetSize(); i++)
+      out_hash += (unsigned int) key[i];
+    return out_hash % table_size;
+  }
+};
+
+
 template <class HASH_TYPE, class DATA_TYPE> class tHashTable {
 #if USE_tMemTrack
   tMemTrack<tHashTable<HASH_TYPE, DATA_TYPE> > mt;
@@ -126,45 +163,10 @@
   // Create an iterator for entry_list
   mutable tListIterator< tHashEntry<HASH_TYPE, DATA_TYPE> > list_it;
   
-  // Create a set of HashKey methods for each of the basic data types that
-  // we allow:
-  
-  // HASH_TYPE = int
-  // Simply mod the into by the size of the hash table and hope for the best
-  int HashKey(const int& key) const
-  {
-    return abs(key % table_size);
-  }
-
-  // HASH_TYPE = void*
-  // Casts the pointer to an int, shift right last two bit positions, mod by
-  // the size of the hash table and hope for the best.  The shift is to account
-  // for typical 4-byte alignment of pointer values.  Depending on architecture
-  // this may not be true and could result in suboptimal hashing at higher
-  // order alignments.
-  int HashKey(const void* const& key) const
-  {
-    // Cast/Dereference of key as an int* tells the compiler that we really want
-    // to truncate the value to an integer, even if a pointer is larger.
-    return abs((*((int*)&key) >> 2) % table_size);
-  }
-  
-  // HASH_TYPE = cString
-  // We hash a string simply by adding up the individual character values in
-  // that string and modding by the hash size.  For most applications this
-  // will work fine (and reasonably fast!) but some patterns will cause all
-  // strings to go into the same cell.  For example, "ABC"=="CBA"=="BBB".
-  int HashKey(const cString& key) const {
-    unsigned int out_hash = 0;
-    for (int i = 0; i < key.GetSize(); i++)
-      out_hash += (unsigned int) key[i];
-    return out_hash % table_size;
-  }
-  
   // Function to find the appropriate tHashEntry for a key that is passed
   // in and return it.
   tHashEntry<HASH_TYPE, DATA_TYPE> * FindEntry(const HASH_TYPE& key) const {
-    const int bin = HashKey(key);
+    const int bin = nHashTable::HashKey<HASH_TYPE>(key, table_size);
     if (cell_array[bin] == NULL) return NULL;
     
     // Set the list iterator to the first entry of this bin.
@@ -245,7 +247,7 @@
     tHashEntry<HASH_TYPE, DATA_TYPE> * new_entry = new tHashEntry<HASH_TYPE, DATA_TYPE>;
     new_entry->key = key;
     new_entry->data = data;
-    const int bin = HashKey(key);
+    const int bin = nHashTable::HashKey<HASH_TYPE>(key, table_size);
     new_entry->id = bin;
     
     
@@ -289,9 +291,9 @@
     return false;
   }
   
-  DATA_TYPE Remove(const HASH_TYPE & key) {
+  DATA_TYPE Remove(const HASH_TYPE& key) {
     // Determine the bin that we are going to be using.
-    const int bin = HashKey(key);
+    const int bin = nHashTable::HashKey<HASH_TYPE>(key, table_size);
     
     DATA_TYPE out_data;
     assert(cell_array[bin] != NULL);
@@ -340,7 +342,7 @@
       tHashEntry<HASH_TYPE, DATA_TYPE> * cur_entry = backup_list.Pop();
       
       // determine the new bin for this entry.
-      int bin = HashKey(cur_entry->key);
+      int bin = nHashTable::HashKey<HASH_TYPE>(cur_entry->key, table_size);
       cur_entry->id = bin;
       
       if (cell_array[bin] == NULL) { list_it.Reset(); } // Reset to list start




More information about the Avida-cvs mailing list