[Avida-cvs] [Avida2-svn] r258 - trunk/source/tools
ofria@myxo.css.msu.edu
ofria at myxo.css.msu.edu
Sun Jul 24 22:08:20 PDT 2005
Author: ofria
Date: 2005-07-25 01:08:20 -0400 (Mon, 25 Jul 2005)
New Revision: 258
Modified:
trunk/source/tools/tDictionary.hh
trunk/source/tools/tList.hh
Log:
Added a command to tDictionary called AsLists(), which takes two empty lists
as arguments and fills them with the dictionary contents, sorted in
alphabetical order. The specific format is:
void AsLists(tList<cString> & name_list, tList<T> & value_list)
Modified: trunk/source/tools/tDictionary.hh
===================================================================
--- trunk/source/tools/tDictionary.hh 2005-07-24 16:36:45 UTC (rev 257)
+++ trunk/source/tools/tDictionary.hh 2005-07-25 05:08:20 UTC (rev 258)
@@ -1,5 +1,5 @@
//////////////////////////////////////////////////////////////////////////////
-// Copyright (C) 1993 - 2003 California Institute of Technology //
+// Copyright (C) 1993 - 2005 California Institute of Technology //
// //
// Read the COPYING and README files, or contact 'avida at alife.org', //
// before continuing. SOME RESTRICTIONS MAY APPLY TO USE OF THIS FILE. //
@@ -13,6 +13,20 @@
* that fits in its cell. If there are no entries that fit in the cell, the
* has table contains a NULL pointer at that location.
*
+ * INTERFACE:
+ * tDictionary(int in_hash_size=DICTIONARY_HASH_DEFAULT) // Constructor
+ * ~tDictionary() // Destructor
+ * int GetSize() // Get dictionary size
+ * void Add(const cString & name, T data) // Add new entry
+ * bool HasEntry(const cString & name) // Test if key exists
+ * bool Find(const cString & name, T & out_data) // Find entry for key
+ * T Remove(const cString & name) // Remove entry
+ * cString NearMatch(const cString name) // Find closest key
+ * void SetHash(int _hash) // Change hash table size
+ *
+ *
+ * IMPLEMENTATION NOTES:
+ *
* On INSERT: If a cell already has at least one entry in it, the new entry
* gets inserted into the linked list before the existing entry. If the cell
* is currently empty, the new entry gets placed at the end of the linked
@@ -158,20 +172,16 @@
new_entry->id = bin;
- // Determine where this new entry should go; either at the beginning of
+ // Determine where this new entry should go; either at the end of
// the list (if there are no others in the bin) or following another
// entry in the bin.
- if (cell_array[bin] == NULL) {
- list_it.Reset(); // Reset to list start
- entry_list.Insert(list_it, new_entry); // Place new entry in the list
- list_it.Next(); // Advance to new entry
- cell_array[bin] = list_it.GetPos(); // Record position
- } else {
- list_it.Set(cell_array[bin]); // Setup insert point
- entry_list.Insert(list_it, new_entry); // Place new entry in the list
- list_it.Next(); // Advance to new entry
- }
+ if (cell_array[bin] == NULL) { list_it.Reset(); } // Reset to list start
+ else { list_it.Set(cell_array[bin]); } // Else find insert point
+ entry_list.Insert(list_it, new_entry); // Place new entry in the list
+ list_it.Prev(); // Back up to new entry
+ cell_array[bin] = list_it.GetPos(); // Record position
+
// Update our entry count...
dict_size++;
}
@@ -257,19 +267,43 @@
int bin = HashString(cur_entry->name);
cur_entry->id = bin;
- if (cell_array[bin] == NULL) {
- list_it.Reset(); // Reset to list start
- entry_list.Insert(list_it, cur_entry); // Place new entry in the list
- list_it.Next(); // Advance to new entry
- cell_array[bin] = list_it.GetPos(); // Record position
- } else {
- list_it.Set(cell_array[bin]); // Setup insert point
- entry_list.Insert(list_it, cur_entry); // Place new entry in the list
- list_it.Next(); // Advance to new entry
+ if (cell_array[bin] == NULL) { list_it.Reset(); } // Reset to list start
+ else { list_it.Set(cell_array[bin]); } // Else find insert point
+
+ entry_list.Insert(list_it, cur_entry); // Place new entry in the list
+ list_it.Prev(); // Back up to new entry
+ cell_array[bin] = list_it.GetPos(); // Record position
+ }
+ }
+
+ // The following method allows the user to convert the dictionary contents
+ // into lists. Empty lists show be passed in as arguments and the method
+ // will fill in their contents.
+ void AsLists(tList<cString> & name_list, tList<T> & value_list) {
+ // Setup the lists to fill in.
+ assert(name_list.GetSize() == 0);
+ assert(value_list.GetSize() == 0);
+ tListIterator<cString> name_it(name_list);
+ tListIterator<T> value_it(value_list);
+
+ // Loop through the current entries and included them into the output
+ // list one at a time.
+ list_it.Reset();
+ while (list_it.Next() != NULL) {
+ // Grab the info about the current entry.
+ cString & cur_name = list_it.Get()->name;
+ T & cur_value = list_it.Get()->data;
+
+ // Find the position to place this in the lists.
+ name_it.Reset();
+ value_it.Reset();
+ value_it.Next();
+ while (name_it.Next() != NULL && cur_name > *(name_it.Get())) {
+ value_it.Next();
}
-
+ name_list.Insert(name_it, &cur_name);
+ value_list.Insert(value_it, &cur_value);
}
-
}
};
Modified: trunk/source/tools/tList.hh
===================================================================
--- trunk/source/tools/tList.hh 2005-07-24 16:36:45 UTC (rev 257)
+++ trunk/source/tools/tList.hh 2005-07-25 05:08:20 UTC (rev 258)
@@ -235,11 +235,11 @@
tListNode<T> * new_node = new tListNode<T>;
new_node->data = in_data;
- // Insert the new node after the iterator...
- new_node->next = cur_node->next;
- new_node->prev = cur_node;
- cur_node->next->prev = new_node;
- cur_node->next = new_node;
+ // Insert the new node before the iterator...
+ new_node->next = cur_node;
+ new_node->prev = cur_node->prev;
+ cur_node->prev->next = new_node;
+ cur_node->prev = new_node;
size++;
return in_data;
More information about the Avida-cvs
mailing list