![]() |
|
|||
File indexing completed on 2025-08-03 08:19:37
0001 /* This software is distributed under the GNU Lesser General Public License */ 0002 //========================================================================== 0003 // 0004 // ne_map.h - common implementation of node_map and edge_map 0005 // 0006 //========================================================================== 0007 // $Id: ne_map.h,v 1.20 2005/06/14 12:22:12 raitner Exp $ 0008 0009 #ifndef GTL_NE_MAP_H 0010 #define GTL_NE_MAP_H 0011 0012 #include <GTL/GTL.h> 0013 0014 #include <vector> 0015 #include <cassert> 0016 0017 //-------------------------------------------------------------------------- 0018 // Class declaration 0019 //-------------------------------------------------------------------------- 0020 0021 __GTL_BEGIN_NAMESPACE 0022 0023 /** 0024 * @short Baseclass for node_map and edge_map 0025 * 0026 * ne_map is the common implementation of <code>@ref node_map </code> 0027 * and <code>@ref edge_map </code> and cannot be used directly. 0028 */ 0029 0030 template <class Key, class Value, class Graph, class Alloc = allocator<Value> > 0031 class ne_map 0032 { 0033 protected: 0034 0035 //================================================== Constructors 0036 0037 /** 0038 * Constructs an empty <code>ne_map</code> not associated to any 0039 * <code>graph</code>. 0040 */ 0041 ne_map(); 0042 0043 /** 0044 * Constructs a <code>ne_map</code> associated to the 0045 * <code>graph g</code>. The value associated to each key is set 0046 * to <code>def</code>. 0047 * You may (but need not) call 0048 * <code>ne_map::init(const graph &, T)</code> to associate it to 0049 * a <code>graph</code>. 0050 * 0051 * @param <code>g</code> associated <code>graph</code> 0052 * @param <code>def</code> default value 0053 */ 0054 explicit ne_map(const Graph &g, Value def=Value()); 0055 0056 //================================================== Operations 0057 0058 public: 0059 0060 /** 0061 * Initializes the ne_map to hold information for the elements 0062 * of graph g. def is the value associated with all elements. 0063 * 0064 * @param <code>g</code> associated <code>graph</code> 0065 * @param <code>def</code> default value 0066 */ 0067 void init(const Graph &, Value def=Value()); 0068 0069 /** 0070 * @internal 0071 */ 0072 #if defined(__GTL_MSVCC) && _MSC_VER < 1310 0073 typedef Value& value_reference; 0074 #else 0075 typedef typename vector<Value, Alloc>::reference value_reference; 0076 #endif 0077 0078 /** 0079 * @internal 0080 */ 0081 #if defined(__GTL_MSVCC) && _MSC_VER < 1310 0082 typedef const Value& const_value_reference; 0083 #else 0084 typedef typename vector<Value, Alloc>::const_reference const_value_reference; 0085 #endif 0086 0087 /** 0088 * Read/write accessor function to the value associated with 0089 * <code>key</code>. 0090 * Use this function to change the value of an element in the 0091 * <code>ne_map</code>. Assume that <code>ne</code> is a 0092 * <code>ne_map<int></code>. Then you can assign the value 0093 * 5 to <code>key</code> with: 0094 * <pre> 0095 * ne[key] = 5; 0096 * </pre> 0097 * 0098 * If there is no entry in the <code>ne_map</code> associated 0099 * with <code>key</code>, one is created. 0100 * 0101 * @param key Key of the Entry to change 0102 * @return a reference to the value associated to <code>key</code>. 0103 */ 0104 value_reference operator[](Key key); 0105 0106 /** 0107 * Read-only accessor function to the value associated with 0108 * <code>key</code>. 0109 * Use this function to read the value of an element in the 0110 * <code>ne_map</code>. Assume that <code>ne</code> is a 0111 * <code>ne_map<int></code>. Then you can print the value 0112 * associated with <code>key</code> with: 0113 * <pre> 0114 * cout << ne[key]; 0115 * </pre> 0116 * 0117 * @param key Key of the Entry to look up 0118 * @return a const reference to the value associated to 0119 * <code>key</code>. 0120 */ 0121 const_value_reference operator[](Key key) const; 0122 0123 /** 0124 * Erases a elements of this nodemap 0125 */ 0126 void clear (); 0127 0128 //================================================== Implementation 0129 0130 private: 0131 vector<Value, Alloc> data; 0132 }; 0133 0134 // Implementation Begin 0135 0136 template <class Key, class Value, class Graph, class Alloc> 0137 ne_map<Key,Value,Graph,Alloc>::ne_map() 0138 { 0139 } 0140 0141 template <class Key, class Value, class Graph, class Alloc> 0142 ne_map<Key,Value,Graph,Alloc>::ne_map(const Graph &g, Value t2) : 0143 data(g.number_of_ids(Key()), t2) 0144 { 0145 } 0146 0147 template <class Key, class Value, class Graph, class Alloc> 0148 void ne_map<Key,Value,Graph,Alloc>::init(const Graph &g, Value t2) 0149 { 0150 int n = g.number_of_ids(Key()); 0151 data.resize(n); 0152 fill_n(data.begin(), n, t2); 0153 } 0154 0155 template <class Key, class Value, class Graph, class Alloc> 0156 typename ne_map<Key,Value,Graph,Alloc>::value_reference ne_map<Key,Value,Graph,Alloc>::operator[](Key t1) 0157 { 0158 if(t1.id() >= (signed)data.size()) 0159 { 0160 if (t1.id() >= (signed)data.capacity()) { 0161 data.reserve((6 * t1.id()) / 5 + 1); 0162 } 0163 0164 data.insert(data.end(), t1.id()+1-data.size(), Value()); 0165 } 0166 return data.operator[](t1.id()); 0167 } 0168 0169 template <class Key, class Value, class Graph, class Alloc> 0170 typename ne_map<Key,Value,Graph,Alloc>::const_value_reference ne_map<Key,Value,Graph,Alloc>::operator[](Key t1) const 0171 { 0172 assert(t1.id() < (signed)data.size()); 0173 return data.operator[](t1.id()); 0174 } 0175 0176 template <class Key, class Value, class Graph, class Alloc> 0177 void ne_map<Key,Value,Graph,Alloc>::clear () 0178 { 0179 data.clear(); 0180 } 0181 0182 // Implementation End 0183 0184 __GTL_END_NAMESPACE 0185 0186 #endif // GTL_NE_MAP_H 0187 0188 //-------------------------------------------------------------------------- 0189 // end of file 0190 //--------------------------------------------------------------------------
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |