Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

NameList.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr                                                                       
00003  *cr            (C) Copyright 1995-2008 The Board of Trustees of the           
00004  *cr                        University of Illinois                       
00005  *cr                         All Rights Reserved                        
00006  *cr                                                                   
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *        $RCSfile: NameList.h,v $
00013  *        $Author: johns $        $Locker:  $                $State: Exp $
00014  *        $Revision: 1.43 $        $Date: 2008/03/27 19:36:41 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *
00019  * NameList template, which stores a list of unique names indexed in the order
00020  * they are added.  For each name, which acts as a key, there is an associated
00021  * integer value.  NameList string lookups are accelerated through the use of
00022  * an internal hash table.
00023  *
00024  ***************************************************************************/
00025 #ifndef NAMELIST_TEMPLATE_H
00026 #define NAMELIST_TEMPLATE_H
00027 
00028 #include <string.h>
00029 #include "ResizeArray.h" 
00030 #include "utilities.h"   // needed for stringdup()
00031 #include "hash.h"        // needed for hash table functions
00032 
00033 #define NLISTSIZE 64
00034 
00039 template<class T>
00040 class NameList  {
00041 protected:
00042   int Num;                   
00043   ResizeArray<char *> names; 
00044   ResizeArray<T> Data;       
00045   hash_t hash;               
00046  
00047 public:
00049   // starts with no names, which are then added via the 'add_name' routine.  
00050   NameList(void) : names(NLISTSIZE), Data(NLISTSIZE) {
00051     Num = 0;
00052     hash_init(&hash, 127);
00053   }
00054 
00056   virtual ~NameList(void) {
00057     for(int i=0; i < Num; i++) {
00058       if(names[i])
00059         delete [] names[i];
00060     }
00061     hash_destroy(&hash);
00062   }
00063 
00064   int num(void) const { return Num; }    // return number of items
00065 
00066   // add a new name to the list, with a given associated value.
00067   // Return the index.  If already in the list, return the current index.
00068   int add_name(const char *nm, const T &val) {
00069     char tmpnm[128]; // temporary storage, spaces stripped from beginning + end
00070     
00071     if (!nm)
00072       return (-1);      
00073 
00074     // strip leading and trailing spaces from the name
00075     char *s = tmpnm;
00076     while (*nm && *nm == ' ')           // skip past whitespace
00077       nm++;
00078 
00079     int len = 127;
00080     while (*nm && len--)                // copy the string
00081       *(s++) = *(nm++);
00082 
00083     *s = 0;                             // terminate the copied string
00084     while(s != tmpnm && *(--s) == ' ')  // remove spaces at end of string
00085       *s = '\0';
00086 
00087     int myindex;  
00088     if ((myindex = hash_lookup(&hash, tmpnm)) != HASH_FAIL) {
00089       return myindex; 
00090     } 
00091 
00092     // if here, string not found; append new one, and return index = Num - 1
00093     names.append(stringdup(tmpnm));
00094 
00095     myindex = hash_insert(&hash, names[Num], Num); 
00096 
00097     Data.append(val);
00098     return Num++;
00099   }
00100 
00101 
00102 
00103   // return the name (null-terminated) for given typecode
00104   const char * name(int a) const {
00105     if (a >= 0 && a < Num) {
00106       return names[a];
00107     }
00108     return NULL;        // not found
00109   }
00110 
00111 
00112   // return the type index for the given name.  If the second argument is
00113   // given and is > 0, it is used as the max length of the names to check
00114   // for a match.  If is is <= 0, an exact match must be found.
00115   //        returns (-1) if no match is found
00116   int typecode(const char *nm) const {
00117     if (!nm)
00118       return -1;
00119 
00120     return hash_lookup(&hash, nm);  // returns -1 (HASH_FAIL) on no entry
00121   }
00122 
00123 
00124   // returns the data for the given name.  If the second argument is
00125   // given and is > 0, it is used as the max length of the names to check
00126   // for a match.  If is is <= 0, an exact match must be found.
00127   T data(const char *nm) const {
00128     if (!nm)
00129       return Data[0];
00130  
00131     int myindex = hash_lookup(&hash, nm);
00132     if (myindex != HASH_FAIL)
00133       return Data[myindex];
00134   
00135     return Data[0];
00136   }
00137 
00138 
00139   // returns the data for the given index
00140   T data(int a) const {
00141     if (a >= 0 && a < Num) {
00142       return Data[a];
00143     }
00144     return Data[0];
00145   }
00146 
00147 
00148   // set the data value for the given index
00149   void set_data(int a, const T &val) {
00150     if(a >= 0 && a < Num) {
00151       Data[a] = val;
00152     }
00153     // else it was an illegal index, therefore do nothing.
00154   }
00155 
00156   // change the name of an entry
00157   void set_name(int a, const char *nm) {
00158     if (a < 0 || a >= Num) 
00159       return;
00160 
00161     // delete the hash table entry...
00162     hash_delete(&hash, names[a]);
00163     delete [] names[a];
00164     names[a] = stringdup(nm);
00165     // and put the pointer in our ResizeArray into the hash table
00166     hash_insert(&hash, names[a], a);
00167   } 
00168 
00169 };
00170 
00171 
00172 // useful typedefs for making NameLists of NameLists
00173 typedef NameList<int>           *NameListIntPtr;
00174 typedef NameList<float>         *NameListFloatPtr;
00175 typedef NameList<char>          *NameListCharPtr;
00176 typedef NameList<char *>        *NameListStringPtr;
00177 
00178 #endif
00179 

Generated on Sat Aug 30 01:26:53 2008 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002