00001 //----------------------------------------------------------------------------- 00002 /** @file marker.h 00003 Classes for marking node IDs. 00004 @see marker 00005 00006 $Id: marker_8h-source.html,v 1.1.1.1 2003/08/07 19:41:39 emarkus Exp $ 00007 $Source: /usr/cvsroot/www_pathfind/libpathfind/0.1.0/doc/marker_8h-source.html,v $ 00008 */ 00009 //----------------------------------------------------------------------------- 00010 /** @page marker Marker Classes 00011 00012 Marker classes are classes for marking node IDs. 00013 The classes have different properties with respect to memory usage 00014 and time for clearing. 00015 They have identical interfaces to be used as replacable template 00016 parameters for other classes. 00017 00018 Currently implemented marker classes are: 00019 - PathFind::MarkerBoolVector 00020 - PathFind::MarkerFastClear 00021 */ 00022 //----------------------------------------------------------------------------- 00023 00024 #ifndef PATHFIND_MARKER_H 00025 #define PATHFIND_MARKER_H 00026 00027 #include <algorithm> 00028 #include <vector> 00029 00030 //----------------------------------------------------------------------------- 00031 00032 namespace PathFind 00033 { 00034 00035 using namespace std; 00036 00037 /** Array for marking node IDs. 00038 Uses a vector<bool> which is usually implemented in the standard 00039 library as a bitarray by template specialization. 00040 @see @ref marker 00041 */ 00042 class MarkerBoolVector 00043 { 00044 public: 00045 MarkerBoolVector() 00046 { 00047 m_numberNodes = 0; 00048 } 00049 00050 bool get(int nodeId) const 00051 { 00052 assert(nodeId >= 0); 00053 assert(nodeId < m_numberNodes); 00054 return m_array[nodeId]; 00055 } 00056 00057 void init(int numberNodes) 00058 { 00059 m_numberNodes = numberNodes; 00060 m_array.clear(); 00061 m_array.resize(numberNodes, false); 00062 } 00063 00064 void set(int nodeId) 00065 { 00066 assert(nodeId >= 0); 00067 assert(nodeId < m_numberNodes); 00068 m_array[nodeId] = true; 00069 } 00070 00071 void unset(int nodeId) 00072 { 00073 assert(nodeId >= 0); 00074 assert(nodeId < m_numberNodes); 00075 m_array[nodeId] = false; 00076 } 00077 00078 private: 00079 int m_numberNodes; 00080 00081 vector<bool> m_array; 00082 }; 00083 00084 /** Array for marking node IDs. 00085 Stores a counter for marked indices which is incremented after each 00086 clear(), so clear() takes constant time. 00087 @see @ref marker 00088 */ 00089 class MarkerFastClear 00090 { 00091 public: 00092 MarkerFastClear() 00093 { 00094 m_numberNodes = 0; 00095 } 00096 00097 bool get(int nodeId) const 00098 { 00099 assert(nodeId >= 0); 00100 assert(nodeId < m_numberNodes); 00101 return (m_array[nodeId] == m_counter); 00102 } 00103 00104 void init(int numberNodes) 00105 { 00106 if (m_numberNodes == numberNodes) 00107 { 00108 ++m_counter; 00109 return; 00110 } 00111 m_numberNodes = numberNodes; 00112 m_array.resize(numberNodes); 00113 fill(m_array.begin(), m_array.end(), -1); 00114 m_counter = 0; 00115 00116 } 00117 00118 void set(int nodeId) 00119 { 00120 assert(nodeId >= 0); 00121 assert(nodeId < m_numberNodes); 00122 m_array[nodeId] = m_counter; 00123 } 00124 00125 void unset(int nodeId) 00126 { 00127 assert(nodeId >= 0); 00128 assert(nodeId < m_numberNodes); 00129 m_array[nodeId] = -1; 00130 } 00131 00132 private: 00133 int m_counter; 00134 00135 int m_numberNodes; 00136 00137 vector<int> m_array; 00138 }; 00139 00140 //----------------------------------------------------------------------------- 00141 00142 } // namespace PathFind 00143 00144 #endif // PATHFIND_UTIL_H