00001 /* 00002 00003 Copyright (c) 2004, Cornell University 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions are met: 00008 00009 - Redistributions of source code must retain the above copyright notice, 00010 this list of conditions and the following disclaimer. 00011 - Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in the 00013 documentation and/or other materials provided with the distribution. 00014 - Neither the name of Cornell University nor the names of its 00015 contributors may be used to endorse or promote products derived from 00016 this software without specific prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00022 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 00028 THE POSSIBILITY OF SUCH DAMAGE. 00029 00030 */ 00031 #include <memory.h> 00032 #include <string.h> 00033 00034 ///////////////////////////////////////////////////////////////////// 00035 /// 00036 /// StringMap.h 00037 /// 00038 ///////////////////////////////////////////////////////////////////// 00039 #ifndef __StringMap_H__ 00040 #define __StringMap_H__ 00041 00042 ///////////////////////////////////////////////////////////////////// 00043 /// @addtogroup GlobalFunctions 00044 /** @{ */ 00045 00046 ///////////////////////////////////////////////////////////////////// 00047 /// A data structure that acts as a two-way one-to-one mapping from integers to strings 00048 /// Optimized for SPAM to quickly retrieve customer IDs, transaction IDs, 00049 /// and item IDs from their corresponding string names, and vice versa 00050 ///////////////////////////////////////////////////////////////////// 00051 class StringMap 00052 { 00053 public: 00054 ///////////////////////////////////////////////////////////////////// 00055 /// Allocate the memory for the default initial size of the array 00056 /// 00057 ///////////////////////////////////////////////////////////////////// 00058 StringMap() 00059 { 00060 _keyMem = new int[DEFAULT_INITIAL_SIZE]; 00061 _valueMem = new const char*[DEFAULT_INITIAL_SIZE]; 00062 memSize = DEFAULT_INITIAL_SIZE; 00063 length = 0; 00064 }; 00065 00066 ///////////////////////////////////////////////////////////////////// 00067 /// Destructor 00068 ///////////////////////////////////////////////////////////////////// 00069 ~StringMap() 00070 { 00071 delete [] _keyMem; 00072 delete [] _valueMem; 00073 }; 00074 00075 ///////////////////////////////////////////////////////////////////// 00076 /// Add an item to the StringMap 00077 /// 00078 /// @param key The integer key of this item 00079 /// @param value The string value of this item 00080 ///////////////////////////////////////////////////////////////////// 00081 void Add(int key, const char* value) 00082 { 00083 if (length < memSize) 00084 { 00085 _keyMem[length] = key; 00086 _valueMem[length] = value; 00087 } 00088 else 00089 { 00090 // Double the size of our local array 00091 00092 // Allocate temp arrays with double the size 00093 int * newKeyMem = new int[memSize*2]; 00094 const char ** newValueMem = new const char*[memSize*2]; 00095 memcpy(newKeyMem, _keyMem, sizeof(int)*memSize); 00096 memcpy(newValueMem, _valueMem, sizeof(const char *)*memSize); 00097 delete [] _keyMem; 00098 delete [] _valueMem; 00099 00100 // Set our memory arrays to the temp arrays 00101 _keyMem = newKeyMem; 00102 _valueMem = newValueMem; 00103 _keyMem[length] = key; 00104 _valueMem[length] = value; 00105 memSize = memSize*2; 00106 } 00107 length++; 00108 }; 00109 00110 ///////////////////////////////////////////////////////////////////// 00111 /// Return the value associated with a given key, or null if the key/value pair 00112 /// is not in the StringMap 00113 /// 00114 /// @param key The key to look up 00115 /// @return The value associated with this key, or null if the key/value pair is not in the StringMap 00116 ///////////////////////////////////////////////////////////////////// 00117 const char * GetValue(int key) 00118 { 00119 for (int i = 0; i < length; ++i) 00120 { 00121 if (_keyMem[i] == key) 00122 return _valueMem[i]; 00123 } 00124 return 0; 00125 } 00126 00127 ///////////////////////////////////////////////////////////////////// 00128 /// Return the key associated with a given value, or null if the key/value pair 00129 /// is not in the StringMap 00130 /// 00131 /// @param value The value to look up 00132 /// @return The key associated with this value, or null if the key/value pair is not in the StringMap 00133 ///////////////////////////////////////////////////////////////////// 00134 const int * GetKey(const char* value) 00135 { 00136 for (int i = 0; i < length; ++i) 00137 { 00138 if (strcmp(_valueMem[i],value) == 0) 00139 { 00140 const int * retVal = new int(_keyMem[i]); 00141 return retVal; 00142 } 00143 } 00144 return 0; 00145 } 00146 00147 00148 private: 00149 static const int DEFAULT_INITIAL_SIZE = 16; 00150 00151 // --- private variables 00152 int * _keyMem; // The internal memory storing the keys 00153 const char ** _valueMem; // The internal memory storing the strings 00154 int memSize; // The number of key/value pairs we have allocated memory for 00155 int length; // The number of key/value pairs the user has added 00156 } 00157 ; // class StringMap 00158 00159 /** @} */ 00160 #endif 00161