| ... | ... |
@@ -1134,1999 +1134,2016 @@ |
| 1134 | 1134 |
/// |
| 1135 | 1135 |
/// \sa ScaleMap |
| 1136 | 1136 |
template<typename M, typename C = typename M::Value> |
| 1137 | 1137 |
class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
|
| 1138 | 1138 |
M &_m; |
| 1139 | 1139 |
C _v; |
| 1140 | 1140 |
public: |
| 1141 | 1141 |
///\e |
| 1142 | 1142 |
typedef typename M::Key Key; |
| 1143 | 1143 |
///\e |
| 1144 | 1144 |
typedef typename M::Value Value; |
| 1145 | 1145 |
|
| 1146 | 1146 |
/// Constructor |
| 1147 | 1147 |
|
| 1148 | 1148 |
/// Constructor. |
| 1149 | 1149 |
/// \param m The undelying map. |
| 1150 | 1150 |
/// \param v The constant value. |
| 1151 | 1151 |
ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
|
| 1152 | 1152 |
///\e |
| 1153 | 1153 |
Value operator[](const Key &k) const { return _v*_m[k]; }
|
| 1154 | 1154 |
///\e |
| 1155 | 1155 |
void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
|
| 1156 | 1156 |
}; |
| 1157 | 1157 |
|
| 1158 | 1158 |
/// Returns a \c ScaleMap class |
| 1159 | 1159 |
|
| 1160 | 1160 |
/// This function just returns a \c ScaleMap class. |
| 1161 | 1161 |
/// |
| 1162 | 1162 |
/// For example, if \c m is a map with \c double values and \c v is |
| 1163 | 1163 |
/// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to |
| 1164 | 1164 |
/// <tt>v*m[x]</tt>. |
| 1165 | 1165 |
/// |
| 1166 | 1166 |
/// \relates ScaleMap |
| 1167 | 1167 |
template<typename M, typename C> |
| 1168 | 1168 |
inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
|
| 1169 | 1169 |
return ScaleMap<M, C>(m,v); |
| 1170 | 1170 |
} |
| 1171 | 1171 |
|
| 1172 | 1172 |
/// Returns a \c ScaleWriteMap class |
| 1173 | 1173 |
|
| 1174 | 1174 |
/// This function just returns a \c ScaleWriteMap class. |
| 1175 | 1175 |
/// |
| 1176 | 1176 |
/// For example, if \c m is a map with \c double values and \c v is |
| 1177 | 1177 |
/// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to |
| 1178 | 1178 |
/// <tt>v*m[x]</tt>. |
| 1179 | 1179 |
/// Moreover it makes also possible to write the map. |
| 1180 | 1180 |
/// |
| 1181 | 1181 |
/// \relates ScaleWriteMap |
| 1182 | 1182 |
template<typename M, typename C> |
| 1183 | 1183 |
inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
|
| 1184 | 1184 |
return ScaleWriteMap<M, C>(m,v); |
| 1185 | 1185 |
} |
| 1186 | 1186 |
|
| 1187 | 1187 |
|
| 1188 | 1188 |
/// Negative of a map |
| 1189 | 1189 |
|
| 1190 | 1190 |
/// This \ref concepts::ReadMap "read-only map" returns the negative |
| 1191 | 1191 |
/// of the values of the given map (using the unary \c - operator). |
| 1192 | 1192 |
/// Its \c Key and \c Value are inherited from \c M. |
| 1193 | 1193 |
/// |
| 1194 | 1194 |
/// If M::Value is \c int, \c double etc., then |
| 1195 | 1195 |
/// \code |
| 1196 | 1196 |
/// NegMap<M> neg(m); |
| 1197 | 1197 |
/// \endcode |
| 1198 | 1198 |
/// is equivalent to |
| 1199 | 1199 |
/// \code |
| 1200 | 1200 |
/// ScaleMap<M> neg(m,-1); |
| 1201 | 1201 |
/// \endcode |
| 1202 | 1202 |
/// |
| 1203 | 1203 |
/// The simplest way of using this map is through the negMap() |
| 1204 | 1204 |
/// function. |
| 1205 | 1205 |
/// |
| 1206 | 1206 |
/// \sa NegWriteMap |
| 1207 | 1207 |
template<typename M> |
| 1208 | 1208 |
class NegMap : public MapBase<typename M::Key, typename M::Value> {
|
| 1209 | 1209 |
const M& _m; |
| 1210 | 1210 |
public: |
| 1211 | 1211 |
///\e |
| 1212 | 1212 |
typedef typename M::Key Key; |
| 1213 | 1213 |
///\e |
| 1214 | 1214 |
typedef typename M::Value Value; |
| 1215 | 1215 |
|
| 1216 | 1216 |
/// Constructor |
| 1217 | 1217 |
NegMap(const M &m) : _m(m) {}
|
| 1218 | 1218 |
///\e |
| 1219 | 1219 |
Value operator[](const Key &k) const { return -_m[k]; }
|
| 1220 | 1220 |
}; |
| 1221 | 1221 |
|
| 1222 | 1222 |
/// Negative of a map (read-write version) |
| 1223 | 1223 |
|
| 1224 | 1224 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the |
| 1225 | 1225 |
/// negative of the values of the given map (using the unary \c - |
| 1226 | 1226 |
/// operator). |
| 1227 | 1227 |
/// Its \c Key and \c Value are inherited from \c M. |
| 1228 | 1228 |
/// It makes also possible to write the map. |
| 1229 | 1229 |
/// |
| 1230 | 1230 |
/// If M::Value is \c int, \c double etc., then |
| 1231 | 1231 |
/// \code |
| 1232 | 1232 |
/// NegWriteMap<M> neg(m); |
| 1233 | 1233 |
/// \endcode |
| 1234 | 1234 |
/// is equivalent to |
| 1235 | 1235 |
/// \code |
| 1236 | 1236 |
/// ScaleWriteMap<M> neg(m,-1); |
| 1237 | 1237 |
/// \endcode |
| 1238 | 1238 |
/// |
| 1239 | 1239 |
/// The simplest way of using this map is through the negWriteMap() |
| 1240 | 1240 |
/// function. |
| 1241 | 1241 |
/// |
| 1242 | 1242 |
/// \sa NegMap |
| 1243 | 1243 |
template<typename M> |
| 1244 | 1244 |
class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
|
| 1245 | 1245 |
M &_m; |
| 1246 | 1246 |
public: |
| 1247 | 1247 |
///\e |
| 1248 | 1248 |
typedef typename M::Key Key; |
| 1249 | 1249 |
///\e |
| 1250 | 1250 |
typedef typename M::Value Value; |
| 1251 | 1251 |
|
| 1252 | 1252 |
/// Constructor |
| 1253 | 1253 |
NegWriteMap(M &m) : _m(m) {}
|
| 1254 | 1254 |
///\e |
| 1255 | 1255 |
Value operator[](const Key &k) const { return -_m[k]; }
|
| 1256 | 1256 |
///\e |
| 1257 | 1257 |
void set(const Key &k, const Value &v) { _m.set(k, -v); }
|
| 1258 | 1258 |
}; |
| 1259 | 1259 |
|
| 1260 | 1260 |
/// Returns a \c NegMap class |
| 1261 | 1261 |
|
| 1262 | 1262 |
/// This function just returns a \c NegMap class. |
| 1263 | 1263 |
/// |
| 1264 | 1264 |
/// For example, if \c m is a map with \c double values, then |
| 1265 | 1265 |
/// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>. |
| 1266 | 1266 |
/// |
| 1267 | 1267 |
/// \relates NegMap |
| 1268 | 1268 |
template <typename M> |
| 1269 | 1269 |
inline NegMap<M> negMap(const M &m) {
|
| 1270 | 1270 |
return NegMap<M>(m); |
| 1271 | 1271 |
} |
| 1272 | 1272 |
|
| 1273 | 1273 |
/// Returns a \c NegWriteMap class |
| 1274 | 1274 |
|
| 1275 | 1275 |
/// This function just returns a \c NegWriteMap class. |
| 1276 | 1276 |
/// |
| 1277 | 1277 |
/// For example, if \c m is a map with \c double values, then |
| 1278 | 1278 |
/// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>. |
| 1279 | 1279 |
/// Moreover it makes also possible to write the map. |
| 1280 | 1280 |
/// |
| 1281 | 1281 |
/// \relates NegWriteMap |
| 1282 | 1282 |
template <typename M> |
| 1283 | 1283 |
inline NegWriteMap<M> negWriteMap(M &m) {
|
| 1284 | 1284 |
return NegWriteMap<M>(m); |
| 1285 | 1285 |
} |
| 1286 | 1286 |
|
| 1287 | 1287 |
|
| 1288 | 1288 |
/// Absolute value of a map |
| 1289 | 1289 |
|
| 1290 | 1290 |
/// This \ref concepts::ReadMap "read-only map" returns the absolute |
| 1291 | 1291 |
/// value of the values of the given map. |
| 1292 | 1292 |
/// Its \c Key and \c Value are inherited from \c M. |
| 1293 | 1293 |
/// \c Value must be comparable to \c 0 and the unary \c - |
| 1294 | 1294 |
/// operator must be defined for it, of course. |
| 1295 | 1295 |
/// |
| 1296 | 1296 |
/// The simplest way of using this map is through the absMap() |
| 1297 | 1297 |
/// function. |
| 1298 | 1298 |
template<typename M> |
| 1299 | 1299 |
class AbsMap : public MapBase<typename M::Key, typename M::Value> {
|
| 1300 | 1300 |
const M &_m; |
| 1301 | 1301 |
public: |
| 1302 | 1302 |
///\e |
| 1303 | 1303 |
typedef typename M::Key Key; |
| 1304 | 1304 |
///\e |
| 1305 | 1305 |
typedef typename M::Value Value; |
| 1306 | 1306 |
|
| 1307 | 1307 |
/// Constructor |
| 1308 | 1308 |
AbsMap(const M &m) : _m(m) {}
|
| 1309 | 1309 |
///\e |
| 1310 | 1310 |
Value operator[](const Key &k) const {
|
| 1311 | 1311 |
Value tmp = _m[k]; |
| 1312 | 1312 |
return tmp >= 0 ? tmp : -tmp; |
| 1313 | 1313 |
} |
| 1314 | 1314 |
|
| 1315 | 1315 |
}; |
| 1316 | 1316 |
|
| 1317 | 1317 |
/// Returns an \c AbsMap class |
| 1318 | 1318 |
|
| 1319 | 1319 |
/// This function just returns an \c AbsMap class. |
| 1320 | 1320 |
/// |
| 1321 | 1321 |
/// For example, if \c m is a map with \c double values, then |
| 1322 | 1322 |
/// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if |
| 1323 | 1323 |
/// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is |
| 1324 | 1324 |
/// negative. |
| 1325 | 1325 |
/// |
| 1326 | 1326 |
/// \relates AbsMap |
| 1327 | 1327 |
template<typename M> |
| 1328 | 1328 |
inline AbsMap<M> absMap(const M &m) {
|
| 1329 | 1329 |
return AbsMap<M>(m); |
| 1330 | 1330 |
} |
| 1331 | 1331 |
|
| 1332 | 1332 |
/// @} |
| 1333 | 1333 |
|
| 1334 | 1334 |
// Logical maps and map adaptors: |
| 1335 | 1335 |
|
| 1336 | 1336 |
/// \addtogroup maps |
| 1337 | 1337 |
/// @{
|
| 1338 | 1338 |
|
| 1339 | 1339 |
/// Constant \c true map. |
| 1340 | 1340 |
|
| 1341 | 1341 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
| 1342 | 1342 |
/// each key. |
| 1343 | 1343 |
/// |
| 1344 | 1344 |
/// Note that |
| 1345 | 1345 |
/// \code |
| 1346 | 1346 |
/// TrueMap<K> tm; |
| 1347 | 1347 |
/// \endcode |
| 1348 | 1348 |
/// is equivalent to |
| 1349 | 1349 |
/// \code |
| 1350 | 1350 |
/// ConstMap<K,bool> tm(true); |
| 1351 | 1351 |
/// \endcode |
| 1352 | 1352 |
/// |
| 1353 | 1353 |
/// \sa FalseMap |
| 1354 | 1354 |
/// \sa ConstMap |
| 1355 | 1355 |
template <typename K> |
| 1356 | 1356 |
class TrueMap : public MapBase<K, bool> {
|
| 1357 | 1357 |
public: |
| 1358 | 1358 |
///\e |
| 1359 | 1359 |
typedef K Key; |
| 1360 | 1360 |
///\e |
| 1361 | 1361 |
typedef bool Value; |
| 1362 | 1362 |
|
| 1363 | 1363 |
/// Gives back \c true. |
| 1364 | 1364 |
Value operator[](const Key&) const { return true; }
|
| 1365 | 1365 |
}; |
| 1366 | 1366 |
|
| 1367 | 1367 |
/// Returns a \c TrueMap class |
| 1368 | 1368 |
|
| 1369 | 1369 |
/// This function just returns a \c TrueMap class. |
| 1370 | 1370 |
/// \relates TrueMap |
| 1371 | 1371 |
template<typename K> |
| 1372 | 1372 |
inline TrueMap<K> trueMap() {
|
| 1373 | 1373 |
return TrueMap<K>(); |
| 1374 | 1374 |
} |
| 1375 | 1375 |
|
| 1376 | 1376 |
|
| 1377 | 1377 |
/// Constant \c false map. |
| 1378 | 1378 |
|
| 1379 | 1379 |
/// This \ref concepts::ReadMap "read-only map" assigns \c false to |
| 1380 | 1380 |
/// each key. |
| 1381 | 1381 |
/// |
| 1382 | 1382 |
/// Note that |
| 1383 | 1383 |
/// \code |
| 1384 | 1384 |
/// FalseMap<K> fm; |
| 1385 | 1385 |
/// \endcode |
| 1386 | 1386 |
/// is equivalent to |
| 1387 | 1387 |
/// \code |
| 1388 | 1388 |
/// ConstMap<K,bool> fm(false); |
| 1389 | 1389 |
/// \endcode |
| 1390 | 1390 |
/// |
| 1391 | 1391 |
/// \sa TrueMap |
| 1392 | 1392 |
/// \sa ConstMap |
| 1393 | 1393 |
template <typename K> |
| 1394 | 1394 |
class FalseMap : public MapBase<K, bool> {
|
| 1395 | 1395 |
public: |
| 1396 | 1396 |
///\e |
| 1397 | 1397 |
typedef K Key; |
| 1398 | 1398 |
///\e |
| 1399 | 1399 |
typedef bool Value; |
| 1400 | 1400 |
|
| 1401 | 1401 |
/// Gives back \c false. |
| 1402 | 1402 |
Value operator[](const Key&) const { return false; }
|
| 1403 | 1403 |
}; |
| 1404 | 1404 |
|
| 1405 | 1405 |
/// Returns a \c FalseMap class |
| 1406 | 1406 |
|
| 1407 | 1407 |
/// This function just returns a \c FalseMap class. |
| 1408 | 1408 |
/// \relates FalseMap |
| 1409 | 1409 |
template<typename K> |
| 1410 | 1410 |
inline FalseMap<K> falseMap() {
|
| 1411 | 1411 |
return FalseMap<K>(); |
| 1412 | 1412 |
} |
| 1413 | 1413 |
|
| 1414 | 1414 |
/// @} |
| 1415 | 1415 |
|
| 1416 | 1416 |
/// \addtogroup map_adaptors |
| 1417 | 1417 |
/// @{
|
| 1418 | 1418 |
|
| 1419 | 1419 |
/// Logical 'and' of two maps |
| 1420 | 1420 |
|
| 1421 | 1421 |
/// This \ref concepts::ReadMap "read-only map" returns the logical |
| 1422 | 1422 |
/// 'and' of the values of the two given maps. |
| 1423 | 1423 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
| 1424 | 1424 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
| 1425 | 1425 |
/// |
| 1426 | 1426 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
| 1427 | 1427 |
/// \code |
| 1428 | 1428 |
/// AndMap<M1,M2> am(m1,m2); |
| 1429 | 1429 |
/// \endcode |
| 1430 | 1430 |
/// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>. |
| 1431 | 1431 |
/// |
| 1432 | 1432 |
/// The simplest way of using this map is through the andMap() |
| 1433 | 1433 |
/// function. |
| 1434 | 1434 |
/// |
| 1435 | 1435 |
/// \sa OrMap |
| 1436 | 1436 |
/// \sa NotMap, NotWriteMap |
| 1437 | 1437 |
template<typename M1, typename M2> |
| 1438 | 1438 |
class AndMap : public MapBase<typename M1::Key, bool> {
|
| 1439 | 1439 |
const M1 &_m1; |
| 1440 | 1440 |
const M2 &_m2; |
| 1441 | 1441 |
public: |
| 1442 | 1442 |
///\e |
| 1443 | 1443 |
typedef typename M1::Key Key; |
| 1444 | 1444 |
///\e |
| 1445 | 1445 |
typedef bool Value; |
| 1446 | 1446 |
|
| 1447 | 1447 |
/// Constructor |
| 1448 | 1448 |
AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 1449 | 1449 |
///\e |
| 1450 | 1450 |
Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
|
| 1451 | 1451 |
}; |
| 1452 | 1452 |
|
| 1453 | 1453 |
/// Returns an \c AndMap class |
| 1454 | 1454 |
|
| 1455 | 1455 |
/// This function just returns an \c AndMap class. |
| 1456 | 1456 |
/// |
| 1457 | 1457 |
/// For example, if \c m1 and \c m2 are both maps with \c bool values, |
| 1458 | 1458 |
/// then <tt>andMap(m1,m2)[x]</tt> will be equal to |
| 1459 | 1459 |
/// <tt>m1[x]&&m2[x]</tt>. |
| 1460 | 1460 |
/// |
| 1461 | 1461 |
/// \relates AndMap |
| 1462 | 1462 |
template<typename M1, typename M2> |
| 1463 | 1463 |
inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
|
| 1464 | 1464 |
return AndMap<M1, M2>(m1,m2); |
| 1465 | 1465 |
} |
| 1466 | 1466 |
|
| 1467 | 1467 |
|
| 1468 | 1468 |
/// Logical 'or' of two maps |
| 1469 | 1469 |
|
| 1470 | 1470 |
/// This \ref concepts::ReadMap "read-only map" returns the logical |
| 1471 | 1471 |
/// 'or' of the values of the two given maps. |
| 1472 | 1472 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
| 1473 | 1473 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
| 1474 | 1474 |
/// |
| 1475 | 1475 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
| 1476 | 1476 |
/// \code |
| 1477 | 1477 |
/// OrMap<M1,M2> om(m1,m2); |
| 1478 | 1478 |
/// \endcode |
| 1479 | 1479 |
/// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>. |
| 1480 | 1480 |
/// |
| 1481 | 1481 |
/// The simplest way of using this map is through the orMap() |
| 1482 | 1482 |
/// function. |
| 1483 | 1483 |
/// |
| 1484 | 1484 |
/// \sa AndMap |
| 1485 | 1485 |
/// \sa NotMap, NotWriteMap |
| 1486 | 1486 |
template<typename M1, typename M2> |
| 1487 | 1487 |
class OrMap : public MapBase<typename M1::Key, bool> {
|
| 1488 | 1488 |
const M1 &_m1; |
| 1489 | 1489 |
const M2 &_m2; |
| 1490 | 1490 |
public: |
| 1491 | 1491 |
///\e |
| 1492 | 1492 |
typedef typename M1::Key Key; |
| 1493 | 1493 |
///\e |
| 1494 | 1494 |
typedef bool Value; |
| 1495 | 1495 |
|
| 1496 | 1496 |
/// Constructor |
| 1497 | 1497 |
OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 1498 | 1498 |
///\e |
| 1499 | 1499 |
Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
|
| 1500 | 1500 |
}; |
| 1501 | 1501 |
|
| 1502 | 1502 |
/// Returns an \c OrMap class |
| 1503 | 1503 |
|
| 1504 | 1504 |
/// This function just returns an \c OrMap class. |
| 1505 | 1505 |
/// |
| 1506 | 1506 |
/// For example, if \c m1 and \c m2 are both maps with \c bool values, |
| 1507 | 1507 |
/// then <tt>orMap(m1,m2)[x]</tt> will be equal to |
| 1508 | 1508 |
/// <tt>m1[x]||m2[x]</tt>. |
| 1509 | 1509 |
/// |
| 1510 | 1510 |
/// \relates OrMap |
| 1511 | 1511 |
template<typename M1, typename M2> |
| 1512 | 1512 |
inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
|
| 1513 | 1513 |
return OrMap<M1, M2>(m1,m2); |
| 1514 | 1514 |
} |
| 1515 | 1515 |
|
| 1516 | 1516 |
|
| 1517 | 1517 |
/// Logical 'not' of a map |
| 1518 | 1518 |
|
| 1519 | 1519 |
/// This \ref concepts::ReadMap "read-only map" returns the logical |
| 1520 | 1520 |
/// negation of the values of the given map. |
| 1521 | 1521 |
/// Its \c Key is inherited from \c M and its \c Value is \c bool. |
| 1522 | 1522 |
/// |
| 1523 | 1523 |
/// The simplest way of using this map is through the notMap() |
| 1524 | 1524 |
/// function. |
| 1525 | 1525 |
/// |
| 1526 | 1526 |
/// \sa NotWriteMap |
| 1527 | 1527 |
template <typename M> |
| 1528 | 1528 |
class NotMap : public MapBase<typename M::Key, bool> {
|
| 1529 | 1529 |
const M &_m; |
| 1530 | 1530 |
public: |
| 1531 | 1531 |
///\e |
| 1532 | 1532 |
typedef typename M::Key Key; |
| 1533 | 1533 |
///\e |
| 1534 | 1534 |
typedef bool Value; |
| 1535 | 1535 |
|
| 1536 | 1536 |
/// Constructor |
| 1537 | 1537 |
NotMap(const M &m) : _m(m) {}
|
| 1538 | 1538 |
///\e |
| 1539 | 1539 |
Value operator[](const Key &k) const { return !_m[k]; }
|
| 1540 | 1540 |
}; |
| 1541 | 1541 |
|
| 1542 | 1542 |
/// Logical 'not' of a map (read-write version) |
| 1543 | 1543 |
|
| 1544 | 1544 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the |
| 1545 | 1545 |
/// logical negation of the values of the given map. |
| 1546 | 1546 |
/// Its \c Key is inherited from \c M and its \c Value is \c bool. |
| 1547 | 1547 |
/// It makes also possible to write the map. When a value is set, |
| 1548 | 1548 |
/// the opposite value is set to the original map. |
| 1549 | 1549 |
/// |
| 1550 | 1550 |
/// The simplest way of using this map is through the notWriteMap() |
| 1551 | 1551 |
/// function. |
| 1552 | 1552 |
/// |
| 1553 | 1553 |
/// \sa NotMap |
| 1554 | 1554 |
template <typename M> |
| 1555 | 1555 |
class NotWriteMap : public MapBase<typename M::Key, bool> {
|
| 1556 | 1556 |
M &_m; |
| 1557 | 1557 |
public: |
| 1558 | 1558 |
///\e |
| 1559 | 1559 |
typedef typename M::Key Key; |
| 1560 | 1560 |
///\e |
| 1561 | 1561 |
typedef bool Value; |
| 1562 | 1562 |
|
| 1563 | 1563 |
/// Constructor |
| 1564 | 1564 |
NotWriteMap(M &m) : _m(m) {}
|
| 1565 | 1565 |
///\e |
| 1566 | 1566 |
Value operator[](const Key &k) const { return !_m[k]; }
|
| 1567 | 1567 |
///\e |
| 1568 | 1568 |
void set(const Key &k, bool v) { _m.set(k, !v); }
|
| 1569 | 1569 |
}; |
| 1570 | 1570 |
|
| 1571 | 1571 |
/// Returns a \c NotMap class |
| 1572 | 1572 |
|
| 1573 | 1573 |
/// This function just returns a \c NotMap class. |
| 1574 | 1574 |
/// |
| 1575 | 1575 |
/// For example, if \c m is a map with \c bool values, then |
| 1576 | 1576 |
/// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>. |
| 1577 | 1577 |
/// |
| 1578 | 1578 |
/// \relates NotMap |
| 1579 | 1579 |
template <typename M> |
| 1580 | 1580 |
inline NotMap<M> notMap(const M &m) {
|
| 1581 | 1581 |
return NotMap<M>(m); |
| 1582 | 1582 |
} |
| 1583 | 1583 |
|
| 1584 | 1584 |
/// Returns a \c NotWriteMap class |
| 1585 | 1585 |
|
| 1586 | 1586 |
/// This function just returns a \c NotWriteMap class. |
| 1587 | 1587 |
/// |
| 1588 | 1588 |
/// For example, if \c m is a map with \c bool values, then |
| 1589 | 1589 |
/// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>. |
| 1590 | 1590 |
/// Moreover it makes also possible to write the map. |
| 1591 | 1591 |
/// |
| 1592 | 1592 |
/// \relates NotWriteMap |
| 1593 | 1593 |
template <typename M> |
| 1594 | 1594 |
inline NotWriteMap<M> notWriteMap(M &m) {
|
| 1595 | 1595 |
return NotWriteMap<M>(m); |
| 1596 | 1596 |
} |
| 1597 | 1597 |
|
| 1598 | 1598 |
|
| 1599 | 1599 |
/// Combination of two maps using the \c == operator |
| 1600 | 1600 |
|
| 1601 | 1601 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
| 1602 | 1602 |
/// the keys for which the corresponding values of the two maps are |
| 1603 | 1603 |
/// equal. |
| 1604 | 1604 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
| 1605 | 1605 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
| 1606 | 1606 |
/// |
| 1607 | 1607 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
| 1608 | 1608 |
/// \code |
| 1609 | 1609 |
/// EqualMap<M1,M2> em(m1,m2); |
| 1610 | 1610 |
/// \endcode |
| 1611 | 1611 |
/// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>. |
| 1612 | 1612 |
/// |
| 1613 | 1613 |
/// The simplest way of using this map is through the equalMap() |
| 1614 | 1614 |
/// function. |
| 1615 | 1615 |
/// |
| 1616 | 1616 |
/// \sa LessMap |
| 1617 | 1617 |
template<typename M1, typename M2> |
| 1618 | 1618 |
class EqualMap : public MapBase<typename M1::Key, bool> {
|
| 1619 | 1619 |
const M1 &_m1; |
| 1620 | 1620 |
const M2 &_m2; |
| 1621 | 1621 |
public: |
| 1622 | 1622 |
///\e |
| 1623 | 1623 |
typedef typename M1::Key Key; |
| 1624 | 1624 |
///\e |
| 1625 | 1625 |
typedef bool Value; |
| 1626 | 1626 |
|
| 1627 | 1627 |
/// Constructor |
| 1628 | 1628 |
EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 1629 | 1629 |
///\e |
| 1630 | 1630 |
Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
|
| 1631 | 1631 |
}; |
| 1632 | 1632 |
|
| 1633 | 1633 |
/// Returns an \c EqualMap class |
| 1634 | 1634 |
|
| 1635 | 1635 |
/// This function just returns an \c EqualMap class. |
| 1636 | 1636 |
/// |
| 1637 | 1637 |
/// For example, if \c m1 and \c m2 are maps with keys and values of |
| 1638 | 1638 |
/// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to |
| 1639 | 1639 |
/// <tt>m1[x]==m2[x]</tt>. |
| 1640 | 1640 |
/// |
| 1641 | 1641 |
/// \relates EqualMap |
| 1642 | 1642 |
template<typename M1, typename M2> |
| 1643 | 1643 |
inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
|
| 1644 | 1644 |
return EqualMap<M1, M2>(m1,m2); |
| 1645 | 1645 |
} |
| 1646 | 1646 |
|
| 1647 | 1647 |
|
| 1648 | 1648 |
/// Combination of two maps using the \c < operator |
| 1649 | 1649 |
|
| 1650 | 1650 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
| 1651 | 1651 |
/// the keys for which the corresponding value of the first map is |
| 1652 | 1652 |
/// less then the value of the second map. |
| 1653 | 1653 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
| 1654 | 1654 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
| 1655 | 1655 |
/// |
| 1656 | 1656 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
| 1657 | 1657 |
/// \code |
| 1658 | 1658 |
/// LessMap<M1,M2> lm(m1,m2); |
| 1659 | 1659 |
/// \endcode |
| 1660 | 1660 |
/// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>. |
| 1661 | 1661 |
/// |
| 1662 | 1662 |
/// The simplest way of using this map is through the lessMap() |
| 1663 | 1663 |
/// function. |
| 1664 | 1664 |
/// |
| 1665 | 1665 |
/// \sa EqualMap |
| 1666 | 1666 |
template<typename M1, typename M2> |
| 1667 | 1667 |
class LessMap : public MapBase<typename M1::Key, bool> {
|
| 1668 | 1668 |
const M1 &_m1; |
| 1669 | 1669 |
const M2 &_m2; |
| 1670 | 1670 |
public: |
| 1671 | 1671 |
///\e |
| 1672 | 1672 |
typedef typename M1::Key Key; |
| 1673 | 1673 |
///\e |
| 1674 | 1674 |
typedef bool Value; |
| 1675 | 1675 |
|
| 1676 | 1676 |
/// Constructor |
| 1677 | 1677 |
LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 1678 | 1678 |
///\e |
| 1679 | 1679 |
Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
|
| 1680 | 1680 |
}; |
| 1681 | 1681 |
|
| 1682 | 1682 |
/// Returns an \c LessMap class |
| 1683 | 1683 |
|
| 1684 | 1684 |
/// This function just returns an \c LessMap class. |
| 1685 | 1685 |
/// |
| 1686 | 1686 |
/// For example, if \c m1 and \c m2 are maps with keys and values of |
| 1687 | 1687 |
/// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to |
| 1688 | 1688 |
/// <tt>m1[x]<m2[x]</tt>. |
| 1689 | 1689 |
/// |
| 1690 | 1690 |
/// \relates LessMap |
| 1691 | 1691 |
template<typename M1, typename M2> |
| 1692 | 1692 |
inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
|
| 1693 | 1693 |
return LessMap<M1, M2>(m1,m2); |
| 1694 | 1694 |
} |
| 1695 | 1695 |
|
| 1696 | 1696 |
namespace _maps_bits {
|
| 1697 | 1697 |
|
| 1698 | 1698 |
template <typename _Iterator, typename Enable = void> |
| 1699 | 1699 |
struct IteratorTraits {
|
| 1700 | 1700 |
typedef typename std::iterator_traits<_Iterator>::value_type Value; |
| 1701 | 1701 |
}; |
| 1702 | 1702 |
|
| 1703 | 1703 |
template <typename _Iterator> |
| 1704 | 1704 |
struct IteratorTraits<_Iterator, |
| 1705 | 1705 |
typename exists<typename _Iterator::container_type>::type> |
| 1706 | 1706 |
{
|
| 1707 | 1707 |
typedef typename _Iterator::container_type::value_type Value; |
| 1708 | 1708 |
}; |
| 1709 | 1709 |
|
| 1710 | 1710 |
} |
| 1711 | 1711 |
|
| 1712 | 1712 |
/// @} |
| 1713 | 1713 |
|
| 1714 | 1714 |
/// \addtogroup maps |
| 1715 | 1715 |
/// @{
|
| 1716 | 1716 |
|
| 1717 | 1717 |
/// \brief Writable bool map for logging each \c true assigned element |
| 1718 | 1718 |
/// |
| 1719 | 1719 |
/// A \ref concepts::WriteMap "writable" bool map for logging |
| 1720 | 1720 |
/// each \c true assigned element, i.e it copies subsequently each |
| 1721 | 1721 |
/// keys set to \c true to the given iterator. |
| 1722 | 1722 |
/// The most important usage of it is storing certain nodes or arcs |
| 1723 | 1723 |
/// that were marked \c true by an algorithm. |
| 1724 | 1724 |
/// |
| 1725 | 1725 |
/// There are several algorithms that provide solutions through bool |
| 1726 | 1726 |
/// maps and most of them assign \c true at most once for each key. |
| 1727 | 1727 |
/// In these cases it is a natural request to store each \c true |
| 1728 | 1728 |
/// assigned elements (in order of the assignment), which can be |
| 1729 | 1729 |
/// easily done with LoggerBoolMap. |
| 1730 | 1730 |
/// |
| 1731 | 1731 |
/// The simplest way of using this map is through the loggerBoolMap() |
| 1732 | 1732 |
/// function. |
| 1733 | 1733 |
/// |
| 1734 | 1734 |
/// \tparam IT The type of the iterator. |
| 1735 | 1735 |
/// \tparam KEY The key type of the map. The default value set |
| 1736 | 1736 |
/// according to the iterator type should work in most cases. |
| 1737 | 1737 |
/// |
| 1738 | 1738 |
/// \note The container of the iterator must contain enough space |
| 1739 | 1739 |
/// for the elements or the iterator should be an inserter iterator. |
| 1740 | 1740 |
#ifdef DOXYGEN |
| 1741 | 1741 |
template <typename IT, typename KEY> |
| 1742 | 1742 |
#else |
| 1743 | 1743 |
template <typename IT, |
| 1744 | 1744 |
typename KEY = typename _maps_bits::IteratorTraits<IT>::Value> |
| 1745 | 1745 |
#endif |
| 1746 | 1746 |
class LoggerBoolMap : public MapBase<KEY, bool> {
|
| 1747 | 1747 |
public: |
| 1748 | 1748 |
|
| 1749 | 1749 |
///\e |
| 1750 | 1750 |
typedef KEY Key; |
| 1751 | 1751 |
///\e |
| 1752 | 1752 |
typedef bool Value; |
| 1753 | 1753 |
///\e |
| 1754 | 1754 |
typedef IT Iterator; |
| 1755 | 1755 |
|
| 1756 | 1756 |
/// Constructor |
| 1757 | 1757 |
LoggerBoolMap(Iterator it) |
| 1758 | 1758 |
: _begin(it), _end(it) {}
|
| 1759 | 1759 |
|
| 1760 | 1760 |
/// Gives back the given iterator set for the first key |
| 1761 | 1761 |
Iterator begin() const {
|
| 1762 | 1762 |
return _begin; |
| 1763 | 1763 |
} |
| 1764 | 1764 |
|
| 1765 | 1765 |
/// Gives back the the 'after the last' iterator |
| 1766 | 1766 |
Iterator end() const {
|
| 1767 | 1767 |
return _end; |
| 1768 | 1768 |
} |
| 1769 | 1769 |
|
| 1770 | 1770 |
/// The set function of the map |
| 1771 | 1771 |
void set(const Key& key, Value value) {
|
| 1772 | 1772 |
if (value) {
|
| 1773 | 1773 |
*_end++ = key; |
| 1774 | 1774 |
} |
| 1775 | 1775 |
} |
| 1776 | 1776 |
|
| 1777 | 1777 |
private: |
| 1778 | 1778 |
Iterator _begin; |
| 1779 | 1779 |
Iterator _end; |
| 1780 | 1780 |
}; |
| 1781 | 1781 |
|
| 1782 | 1782 |
/// Returns a \c LoggerBoolMap class |
| 1783 | 1783 |
|
| 1784 | 1784 |
/// This function just returns a \c LoggerBoolMap class. |
| 1785 | 1785 |
/// |
| 1786 | 1786 |
/// The most important usage of it is storing certain nodes or arcs |
| 1787 | 1787 |
/// that were marked \c true by an algorithm. |
| 1788 | 1788 |
/// For example it makes easier to store the nodes in the processing |
| 1789 | 1789 |
/// order of Dfs algorithm, as the following examples show. |
| 1790 | 1790 |
/// \code |
| 1791 | 1791 |
/// std::vector<Node> v; |
| 1792 | 1792 |
/// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run(); |
| 1793 | 1793 |
/// \endcode |
| 1794 | 1794 |
/// \code |
| 1795 | 1795 |
/// std::vector<Node> v(countNodes(g)); |
| 1796 | 1796 |
/// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run(); |
| 1797 | 1797 |
/// \endcode |
| 1798 | 1798 |
/// |
| 1799 | 1799 |
/// \note The container of the iterator must contain enough space |
| 1800 | 1800 |
/// for the elements or the iterator should be an inserter iterator. |
| 1801 | 1801 |
/// |
| 1802 | 1802 |
/// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so |
| 1803 | 1803 |
/// it cannot be used when a readable map is needed, for example as |
| 1804 | 1804 |
/// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms. |
| 1805 | 1805 |
/// |
| 1806 | 1806 |
/// \relates LoggerBoolMap |
| 1807 | 1807 |
template<typename Iterator> |
| 1808 | 1808 |
inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
|
| 1809 | 1809 |
return LoggerBoolMap<Iterator>(it); |
| 1810 | 1810 |
} |
| 1811 | 1811 |
|
| 1812 | 1812 |
/// @} |
| 1813 | 1813 |
|
| 1814 | 1814 |
/// \addtogroup graph_maps |
| 1815 | 1815 |
/// @{
|
| 1816 | 1816 |
|
| 1817 | 1817 |
/// \brief Provides an immutable and unique id for each item in a graph. |
| 1818 | 1818 |
/// |
| 1819 | 1819 |
/// IdMap provides a unique and immutable id for each item of the |
| 1820 | 1820 |
/// same type (\c Node, \c Arc or \c Edge) in a graph. This id is |
| 1821 | 1821 |
/// - \b unique: different items get different ids, |
| 1822 | 1822 |
/// - \b immutable: the id of an item does not change (even if you |
| 1823 | 1823 |
/// delete other nodes). |
| 1824 | 1824 |
/// |
| 1825 | 1825 |
/// Using this map you get access (i.e. can read) the inner id values of |
| 1826 | 1826 |
/// the items stored in the graph, which is returned by the \c id() |
| 1827 | 1827 |
/// function of the graph. This map can be inverted with its member |
| 1828 | 1828 |
/// class \c InverseMap or with the \c operator()() member. |
| 1829 | 1829 |
/// |
| 1830 | 1830 |
/// \tparam GR The graph type. |
| 1831 | 1831 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
| 1832 | 1832 |
/// \c GR::Edge). |
| 1833 | 1833 |
/// |
| 1834 | 1834 |
/// \see RangeIdMap |
| 1835 | 1835 |
template <typename GR, typename K> |
| 1836 | 1836 |
class IdMap : public MapBase<K, int> {
|
| 1837 | 1837 |
public: |
| 1838 | 1838 |
/// The graph type of IdMap. |
| 1839 | 1839 |
typedef GR Graph; |
| 1840 | 1840 |
typedef GR Digraph; |
| 1841 | 1841 |
/// The key type of IdMap (\c Node, \c Arc or \c Edge). |
| 1842 | 1842 |
typedef K Item; |
| 1843 | 1843 |
/// The key type of IdMap (\c Node, \c Arc or \c Edge). |
| 1844 | 1844 |
typedef K Key; |
| 1845 | 1845 |
/// The value type of IdMap. |
| 1846 | 1846 |
typedef int Value; |
| 1847 | 1847 |
|
| 1848 | 1848 |
/// \brief Constructor. |
| 1849 | 1849 |
/// |
| 1850 | 1850 |
/// Constructor of the map. |
| 1851 | 1851 |
explicit IdMap(const Graph& graph) : _graph(&graph) {}
|
| 1852 | 1852 |
|
| 1853 | 1853 |
/// \brief Gives back the \e id of the item. |
| 1854 | 1854 |
/// |
| 1855 | 1855 |
/// Gives back the immutable and unique \e id of the item. |
| 1856 | 1856 |
int operator[](const Item& item) const { return _graph->id(item);}
|
| 1857 | 1857 |
|
| 1858 | 1858 |
/// \brief Gives back the \e item by its id. |
| 1859 | 1859 |
/// |
| 1860 | 1860 |
/// Gives back the \e item by its id. |
| 1861 | 1861 |
Item operator()(int id) { return _graph->fromId(id, Item()); }
|
| 1862 | 1862 |
|
| 1863 | 1863 |
private: |
| 1864 | 1864 |
const Graph* _graph; |
| 1865 | 1865 |
|
| 1866 | 1866 |
public: |
| 1867 | 1867 |
|
| 1868 | 1868 |
/// \brief The inverse map type of IdMap. |
| 1869 | 1869 |
/// |
| 1870 | 1870 |
/// The inverse map type of IdMap. The subscript operator gives back |
| 1871 | 1871 |
/// an item by its id. |
| 1872 | 1872 |
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept. |
| 1873 | 1873 |
/// \see inverse() |
| 1874 | 1874 |
class InverseMap {
|
| 1875 | 1875 |
public: |
| 1876 | 1876 |
|
| 1877 | 1877 |
/// \brief Constructor. |
| 1878 | 1878 |
/// |
| 1879 | 1879 |
/// Constructor for creating an id-to-item map. |
| 1880 | 1880 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {}
|
| 1881 | 1881 |
|
| 1882 | 1882 |
/// \brief Constructor. |
| 1883 | 1883 |
/// |
| 1884 | 1884 |
/// Constructor for creating an id-to-item map. |
| 1885 | 1885 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
|
| 1886 | 1886 |
|
| 1887 | 1887 |
/// \brief Gives back an item by its id. |
| 1888 | 1888 |
/// |
| 1889 | 1889 |
/// Gives back an item by its id. |
| 1890 | 1890 |
Item operator[](int id) const { return _graph->fromId(id, Item());}
|
| 1891 | 1891 |
|
| 1892 | 1892 |
private: |
| 1893 | 1893 |
const Graph* _graph; |
| 1894 | 1894 |
}; |
| 1895 | 1895 |
|
| 1896 | 1896 |
/// \brief Gives back the inverse of the map. |
| 1897 | 1897 |
/// |
| 1898 | 1898 |
/// Gives back the inverse of the IdMap. |
| 1899 | 1899 |
InverseMap inverse() const { return InverseMap(*_graph);}
|
| 1900 | 1900 |
}; |
| 1901 | 1901 |
|
| 1902 |
/// \brief Returns an \c IdMap class. |
|
| 1903 |
/// |
|
| 1904 |
/// This function just returns an \c IdMap class. |
|
| 1905 |
/// \relates IdMap |
|
| 1906 |
template <typename K, typename GR> |
|
| 1907 |
inline IdMap<GR, K> idMap(const GR& graph) {
|
|
| 1908 |
return IdMap<GR, K>(graph); |
|
| 1909 |
} |
|
| 1902 | 1910 |
|
| 1903 | 1911 |
/// \brief General cross reference graph map type. |
| 1904 | 1912 |
|
| 1905 | 1913 |
/// This class provides simple invertable graph maps. |
| 1906 | 1914 |
/// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap) |
| 1907 | 1915 |
/// and if a key is set to a new value, then stores it in the inverse map. |
| 1908 | 1916 |
/// The graph items can be accessed by their values either using |
| 1909 | 1917 |
/// \c InverseMap or \c operator()(), and the values of the map can be |
| 1910 | 1918 |
/// accessed with an STL compatible forward iterator (\c ValueIt). |
| 1911 | 1919 |
/// |
| 1912 | 1920 |
/// This map is intended to be used when all associated values are |
| 1913 | 1921 |
/// different (the map is actually invertable) or there are only a few |
| 1914 | 1922 |
/// items with the same value. |
| 1915 | 1923 |
/// Otherwise consider to use \c IterableValueMap, which is more |
| 1916 | 1924 |
/// suitable and more efficient for such cases. It provides iterators |
| 1917 | 1925 |
/// to traverse the items with the same associated value, however |
| 1918 | 1926 |
/// it does not have \c InverseMap. |
| 1919 | 1927 |
/// |
| 1920 | 1928 |
/// This type is not reference map, so it cannot be modified with |
| 1921 | 1929 |
/// the subscript operator. |
| 1922 | 1930 |
/// |
| 1923 | 1931 |
/// \tparam GR The graph type. |
| 1924 | 1932 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
| 1925 | 1933 |
/// \c GR::Edge). |
| 1926 | 1934 |
/// \tparam V The value type of the map. |
| 1927 | 1935 |
/// |
| 1928 | 1936 |
/// \see IterableValueMap |
| 1929 | 1937 |
template <typename GR, typename K, typename V> |
| 1930 | 1938 |
class CrossRefMap |
| 1931 | 1939 |
: protected ItemSetTraits<GR, K>::template Map<V>::Type {
|
| 1932 | 1940 |
private: |
| 1933 | 1941 |
|
| 1934 | 1942 |
typedef typename ItemSetTraits<GR, K>:: |
| 1935 | 1943 |
template Map<V>::Type Map; |
| 1936 | 1944 |
|
| 1937 | 1945 |
typedef std::multimap<V, K> Container; |
| 1938 | 1946 |
Container _inv_map; |
| 1939 | 1947 |
|
| 1940 | 1948 |
public: |
| 1941 | 1949 |
|
| 1942 | 1950 |
/// The graph type of CrossRefMap. |
| 1943 | 1951 |
typedef GR Graph; |
| 1944 | 1952 |
typedef GR Digraph; |
| 1945 | 1953 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge). |
| 1946 | 1954 |
typedef K Item; |
| 1947 | 1955 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge). |
| 1948 | 1956 |
typedef K Key; |
| 1949 | 1957 |
/// The value type of CrossRefMap. |
| 1950 | 1958 |
typedef V Value; |
| 1951 | 1959 |
|
| 1952 | 1960 |
/// \brief Constructor. |
| 1953 | 1961 |
/// |
| 1954 | 1962 |
/// Construct a new CrossRefMap for the given graph. |
| 1955 | 1963 |
explicit CrossRefMap(const Graph& graph) : Map(graph) {}
|
| 1956 | 1964 |
|
| 1957 | 1965 |
/// \brief Forward iterator for values. |
| 1958 | 1966 |
/// |
| 1959 | 1967 |
/// This iterator is an STL compatible forward |
| 1960 | 1968 |
/// iterator on the values of the map. The values can |
| 1961 | 1969 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range. |
| 1962 | 1970 |
/// They are considered with multiplicity, so each value is |
| 1963 | 1971 |
/// traversed for each item it is assigned to. |
| 1964 | 1972 |
class ValueIt |
| 1965 | 1973 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
| 1966 | 1974 |
friend class CrossRefMap; |
| 1967 | 1975 |
private: |
| 1968 | 1976 |
ValueIt(typename Container::const_iterator _it) |
| 1969 | 1977 |
: it(_it) {}
|
| 1970 | 1978 |
public: |
| 1971 | 1979 |
|
| 1972 | 1980 |
/// Constructor |
| 1973 | 1981 |
ValueIt() {}
|
| 1974 | 1982 |
|
| 1975 | 1983 |
/// \e |
| 1976 | 1984 |
ValueIt& operator++() { ++it; return *this; }
|
| 1977 | 1985 |
/// \e |
| 1978 | 1986 |
ValueIt operator++(int) {
|
| 1979 | 1987 |
ValueIt tmp(*this); |
| 1980 | 1988 |
operator++(); |
| 1981 | 1989 |
return tmp; |
| 1982 | 1990 |
} |
| 1983 | 1991 |
|
| 1984 | 1992 |
/// \e |
| 1985 | 1993 |
const Value& operator*() const { return it->first; }
|
| 1986 | 1994 |
/// \e |
| 1987 | 1995 |
const Value* operator->() const { return &(it->first); }
|
| 1988 | 1996 |
|
| 1989 | 1997 |
/// \e |
| 1990 | 1998 |
bool operator==(ValueIt jt) const { return it == jt.it; }
|
| 1991 | 1999 |
/// \e |
| 1992 | 2000 |
bool operator!=(ValueIt jt) const { return it != jt.it; }
|
| 1993 | 2001 |
|
| 1994 | 2002 |
private: |
| 1995 | 2003 |
typename Container::const_iterator it; |
| 1996 | 2004 |
}; |
| 1997 | 2005 |
|
| 1998 | 2006 |
/// Alias for \c ValueIt |
| 1999 | 2007 |
typedef ValueIt ValueIterator; |
| 2000 | 2008 |
|
| 2001 | 2009 |
/// \brief Returns an iterator to the first value. |
| 2002 | 2010 |
/// |
| 2003 | 2011 |
/// Returns an STL compatible iterator to the |
| 2004 | 2012 |
/// first value of the map. The values of the |
| 2005 | 2013 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt> |
| 2006 | 2014 |
/// range. |
| 2007 | 2015 |
ValueIt beginValue() const {
|
| 2008 | 2016 |
return ValueIt(_inv_map.begin()); |
| 2009 | 2017 |
} |
| 2010 | 2018 |
|
| 2011 | 2019 |
/// \brief Returns an iterator after the last value. |
| 2012 | 2020 |
/// |
| 2013 | 2021 |
/// Returns an STL compatible iterator after the |
| 2014 | 2022 |
/// last value of the map. The values of the |
| 2015 | 2023 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt> |
| 2016 | 2024 |
/// range. |
| 2017 | 2025 |
ValueIt endValue() const {
|
| 2018 | 2026 |
return ValueIt(_inv_map.end()); |
| 2019 | 2027 |
} |
| 2020 | 2028 |
|
| 2021 | 2029 |
/// \brief Sets the value associated with the given key. |
| 2022 | 2030 |
/// |
| 2023 | 2031 |
/// Sets the value associated with the given key. |
| 2024 | 2032 |
void set(const Key& key, const Value& val) {
|
| 2025 | 2033 |
Value oldval = Map::operator[](key); |
| 2026 | 2034 |
typename Container::iterator it; |
| 2027 | 2035 |
for (it = _inv_map.equal_range(oldval).first; |
| 2028 | 2036 |
it != _inv_map.equal_range(oldval).second; ++it) {
|
| 2029 | 2037 |
if (it->second == key) {
|
| 2030 | 2038 |
_inv_map.erase(it); |
| 2031 | 2039 |
break; |
| 2032 | 2040 |
} |
| 2033 | 2041 |
} |
| 2034 | 2042 |
_inv_map.insert(std::make_pair(val, key)); |
| 2035 | 2043 |
Map::set(key, val); |
| 2036 | 2044 |
} |
| 2037 | 2045 |
|
| 2038 | 2046 |
/// \brief Returns the value associated with the given key. |
| 2039 | 2047 |
/// |
| 2040 | 2048 |
/// Returns the value associated with the given key. |
| 2041 | 2049 |
typename MapTraits<Map>::ConstReturnValue |
| 2042 | 2050 |
operator[](const Key& key) const {
|
| 2043 | 2051 |
return Map::operator[](key); |
| 2044 | 2052 |
} |
| 2045 | 2053 |
|
| 2046 | 2054 |
/// \brief Gives back an item by its value. |
| 2047 | 2055 |
/// |
| 2048 | 2056 |
/// This function gives back an item that is assigned to |
| 2049 | 2057 |
/// the given value or \c INVALID if no such item exists. |
| 2050 | 2058 |
/// If there are more items with the same associated value, |
| 2051 | 2059 |
/// only one of them is returned. |
| 2052 | 2060 |
Key operator()(const Value& val) const {
|
| 2053 | 2061 |
typename Container::const_iterator it = _inv_map.find(val); |
| 2054 | 2062 |
return it != _inv_map.end() ? it->second : INVALID; |
| 2055 | 2063 |
} |
| 2056 | 2064 |
|
| 2057 | 2065 |
/// \brief Returns the number of items with the given value. |
| 2058 | 2066 |
/// |
| 2059 | 2067 |
/// This function returns the number of items with the given value |
| 2060 | 2068 |
/// associated with it. |
| 2061 | 2069 |
int count(const Value &val) const {
|
| 2062 | 2070 |
return _inv_map.count(val); |
| 2063 | 2071 |
} |
| 2064 | 2072 |
|
| 2065 | 2073 |
protected: |
| 2066 | 2074 |
|
| 2067 | 2075 |
/// \brief Erase the key from the map and the inverse map. |
| 2068 | 2076 |
/// |
| 2069 | 2077 |
/// Erase the key from the map and the inverse map. It is called by the |
| 2070 | 2078 |
/// \c AlterationNotifier. |
| 2071 | 2079 |
virtual void erase(const Key& key) {
|
| 2072 | 2080 |
Value val = Map::operator[](key); |
| 2073 | 2081 |
typename Container::iterator it; |
| 2074 | 2082 |
for (it = _inv_map.equal_range(val).first; |
| 2075 | 2083 |
it != _inv_map.equal_range(val).second; ++it) {
|
| 2076 | 2084 |
if (it->second == key) {
|
| 2077 | 2085 |
_inv_map.erase(it); |
| 2078 | 2086 |
break; |
| 2079 | 2087 |
} |
| 2080 | 2088 |
} |
| 2081 | 2089 |
Map::erase(key); |
| 2082 | 2090 |
} |
| 2083 | 2091 |
|
| 2084 | 2092 |
/// \brief Erase more keys from the map and the inverse map. |
| 2085 | 2093 |
/// |
| 2086 | 2094 |
/// Erase more keys from the map and the inverse map. It is called by the |
| 2087 | 2095 |
/// \c AlterationNotifier. |
| 2088 | 2096 |
virtual void erase(const std::vector<Key>& keys) {
|
| 2089 | 2097 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2090 | 2098 |
Value val = Map::operator[](keys[i]); |
| 2091 | 2099 |
typename Container::iterator it; |
| 2092 | 2100 |
for (it = _inv_map.equal_range(val).first; |
| 2093 | 2101 |
it != _inv_map.equal_range(val).second; ++it) {
|
| 2094 | 2102 |
if (it->second == keys[i]) {
|
| 2095 | 2103 |
_inv_map.erase(it); |
| 2096 | 2104 |
break; |
| 2097 | 2105 |
} |
| 2098 | 2106 |
} |
| 2099 | 2107 |
} |
| 2100 | 2108 |
Map::erase(keys); |
| 2101 | 2109 |
} |
| 2102 | 2110 |
|
| 2103 | 2111 |
/// \brief Clear the keys from the map and the inverse map. |
| 2104 | 2112 |
/// |
| 2105 | 2113 |
/// Clear the keys from the map and the inverse map. It is called by the |
| 2106 | 2114 |
/// \c AlterationNotifier. |
| 2107 | 2115 |
virtual void clear() {
|
| 2108 | 2116 |
_inv_map.clear(); |
| 2109 | 2117 |
Map::clear(); |
| 2110 | 2118 |
} |
| 2111 | 2119 |
|
| 2112 | 2120 |
public: |
| 2113 | 2121 |
|
| 2114 | 2122 |
/// \brief The inverse map type of CrossRefMap. |
| 2115 | 2123 |
/// |
| 2116 | 2124 |
/// The inverse map type of CrossRefMap. The subscript operator gives |
| 2117 | 2125 |
/// back an item by its value. |
| 2118 | 2126 |
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept. |
| 2119 | 2127 |
/// \see inverse() |
| 2120 | 2128 |
class InverseMap {
|
| 2121 | 2129 |
public: |
| 2122 | 2130 |
/// \brief Constructor |
| 2123 | 2131 |
/// |
| 2124 | 2132 |
/// Constructor of the InverseMap. |
| 2125 | 2133 |
explicit InverseMap(const CrossRefMap& inverted) |
| 2126 | 2134 |
: _inverted(inverted) {}
|
| 2127 | 2135 |
|
| 2128 | 2136 |
/// The value type of the InverseMap. |
| 2129 | 2137 |
typedef typename CrossRefMap::Key Value; |
| 2130 | 2138 |
/// The key type of the InverseMap. |
| 2131 | 2139 |
typedef typename CrossRefMap::Value Key; |
| 2132 | 2140 |
|
| 2133 | 2141 |
/// \brief Subscript operator. |
| 2134 | 2142 |
/// |
| 2135 | 2143 |
/// Subscript operator. It gives back an item |
| 2136 | 2144 |
/// that is assigned to the given value or \c INVALID |
| 2137 | 2145 |
/// if no such item exists. |
| 2138 | 2146 |
Value operator[](const Key& key) const {
|
| 2139 | 2147 |
return _inverted(key); |
| 2140 | 2148 |
} |
| 2141 | 2149 |
|
| 2142 | 2150 |
private: |
| 2143 | 2151 |
const CrossRefMap& _inverted; |
| 2144 | 2152 |
}; |
| 2145 | 2153 |
|
| 2146 | 2154 |
/// \brief Gives back the inverse of the map. |
| 2147 | 2155 |
/// |
| 2148 | 2156 |
/// Gives back the inverse of the CrossRefMap. |
| 2149 | 2157 |
InverseMap inverse() const {
|
| 2150 | 2158 |
return InverseMap(*this); |
| 2151 | 2159 |
} |
| 2152 | 2160 |
|
| 2153 | 2161 |
}; |
| 2154 | 2162 |
|
| 2155 | 2163 |
/// \brief Provides continuous and unique id for the |
| 2156 | 2164 |
/// items of a graph. |
| 2157 | 2165 |
/// |
| 2158 | 2166 |
/// RangeIdMap provides a unique and continuous |
| 2159 | 2167 |
/// id for each item of a given type (\c Node, \c Arc or |
| 2160 | 2168 |
/// \c Edge) in a graph. This id is |
| 2161 | 2169 |
/// - \b unique: different items get different ids, |
| 2162 | 2170 |
/// - \b continuous: the range of the ids is the set of integers |
| 2163 | 2171 |
/// between 0 and \c n-1, where \c n is the number of the items of |
| 2164 | 2172 |
/// this type (\c Node, \c Arc or \c Edge). |
| 2165 | 2173 |
/// - So, the ids can change when deleting an item of the same type. |
| 2166 | 2174 |
/// |
| 2167 | 2175 |
/// Thus this id is not (necessarily) the same as what can get using |
| 2168 | 2176 |
/// the \c id() function of the graph or \ref IdMap. |
| 2169 | 2177 |
/// This map can be inverted with its member class \c InverseMap, |
| 2170 | 2178 |
/// or with the \c operator()() member. |
| 2171 | 2179 |
/// |
| 2172 | 2180 |
/// \tparam GR The graph type. |
| 2173 | 2181 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
| 2174 | 2182 |
/// \c GR::Edge). |
| 2175 | 2183 |
/// |
| 2176 | 2184 |
/// \see IdMap |
| 2177 | 2185 |
template <typename GR, typename K> |
| 2178 | 2186 |
class RangeIdMap |
| 2179 | 2187 |
: protected ItemSetTraits<GR, K>::template Map<int>::Type {
|
| 2180 | 2188 |
|
| 2181 | 2189 |
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map; |
| 2182 | 2190 |
|
| 2183 | 2191 |
public: |
| 2184 | 2192 |
/// The graph type of RangeIdMap. |
| 2185 | 2193 |
typedef GR Graph; |
| 2186 | 2194 |
typedef GR Digraph; |
| 2187 | 2195 |
/// The key type of RangeIdMap (\c Node, \c Arc or \c Edge). |
| 2188 | 2196 |
typedef K Item; |
| 2189 | 2197 |
/// The key type of RangeIdMap (\c Node, \c Arc or \c Edge). |
| 2190 | 2198 |
typedef K Key; |
| 2191 | 2199 |
/// The value type of RangeIdMap. |
| 2192 | 2200 |
typedef int Value; |
| 2193 | 2201 |
|
| 2194 | 2202 |
/// \brief Constructor. |
| 2195 | 2203 |
/// |
| 2196 | 2204 |
/// Constructor. |
| 2197 | 2205 |
explicit RangeIdMap(const Graph& gr) : Map(gr) {
|
| 2198 | 2206 |
Item it; |
| 2199 | 2207 |
const typename Map::Notifier* nf = Map::notifier(); |
| 2200 | 2208 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2201 | 2209 |
Map::set(it, _inv_map.size()); |
| 2202 | 2210 |
_inv_map.push_back(it); |
| 2203 | 2211 |
} |
| 2204 | 2212 |
} |
| 2205 | 2213 |
|
| 2206 | 2214 |
protected: |
| 2207 | 2215 |
|
| 2208 | 2216 |
/// \brief Adds a new key to the map. |
| 2209 | 2217 |
/// |
| 2210 | 2218 |
/// Add a new key to the map. It is called by the |
| 2211 | 2219 |
/// \c AlterationNotifier. |
| 2212 | 2220 |
virtual void add(const Item& item) {
|
| 2213 | 2221 |
Map::add(item); |
| 2214 | 2222 |
Map::set(item, _inv_map.size()); |
| 2215 | 2223 |
_inv_map.push_back(item); |
| 2216 | 2224 |
} |
| 2217 | 2225 |
|
| 2218 | 2226 |
/// \brief Add more new keys to the map. |
| 2219 | 2227 |
/// |
| 2220 | 2228 |
/// Add more new keys to the map. It is called by the |
| 2221 | 2229 |
/// \c AlterationNotifier. |
| 2222 | 2230 |
virtual void add(const std::vector<Item>& items) {
|
| 2223 | 2231 |
Map::add(items); |
| 2224 | 2232 |
for (int i = 0; i < int(items.size()); ++i) {
|
| 2225 | 2233 |
Map::set(items[i], _inv_map.size()); |
| 2226 | 2234 |
_inv_map.push_back(items[i]); |
| 2227 | 2235 |
} |
| 2228 | 2236 |
} |
| 2229 | 2237 |
|
| 2230 | 2238 |
/// \brief Erase the key from the map. |
| 2231 | 2239 |
/// |
| 2232 | 2240 |
/// Erase the key from the map. It is called by the |
| 2233 | 2241 |
/// \c AlterationNotifier. |
| 2234 | 2242 |
virtual void erase(const Item& item) {
|
| 2235 | 2243 |
Map::set(_inv_map.back(), Map::operator[](item)); |
| 2236 | 2244 |
_inv_map[Map::operator[](item)] = _inv_map.back(); |
| 2237 | 2245 |
_inv_map.pop_back(); |
| 2238 | 2246 |
Map::erase(item); |
| 2239 | 2247 |
} |
| 2240 | 2248 |
|
| 2241 | 2249 |
/// \brief Erase more keys from the map. |
| 2242 | 2250 |
/// |
| 2243 | 2251 |
/// Erase more keys from the map. It is called by the |
| 2244 | 2252 |
/// \c AlterationNotifier. |
| 2245 | 2253 |
virtual void erase(const std::vector<Item>& items) {
|
| 2246 | 2254 |
for (int i = 0; i < int(items.size()); ++i) {
|
| 2247 | 2255 |
Map::set(_inv_map.back(), Map::operator[](items[i])); |
| 2248 | 2256 |
_inv_map[Map::operator[](items[i])] = _inv_map.back(); |
| 2249 | 2257 |
_inv_map.pop_back(); |
| 2250 | 2258 |
} |
| 2251 | 2259 |
Map::erase(items); |
| 2252 | 2260 |
} |
| 2253 | 2261 |
|
| 2254 | 2262 |
/// \brief Build the unique map. |
| 2255 | 2263 |
/// |
| 2256 | 2264 |
/// Build the unique map. It is called by the |
| 2257 | 2265 |
/// \c AlterationNotifier. |
| 2258 | 2266 |
virtual void build() {
|
| 2259 | 2267 |
Map::build(); |
| 2260 | 2268 |
Item it; |
| 2261 | 2269 |
const typename Map::Notifier* nf = Map::notifier(); |
| 2262 | 2270 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2263 | 2271 |
Map::set(it, _inv_map.size()); |
| 2264 | 2272 |
_inv_map.push_back(it); |
| 2265 | 2273 |
} |
| 2266 | 2274 |
} |
| 2267 | 2275 |
|
| 2268 | 2276 |
/// \brief Clear the keys from the map. |
| 2269 | 2277 |
/// |
| 2270 | 2278 |
/// Clear the keys from the map. It is called by the |
| 2271 | 2279 |
/// \c AlterationNotifier. |
| 2272 | 2280 |
virtual void clear() {
|
| 2273 | 2281 |
_inv_map.clear(); |
| 2274 | 2282 |
Map::clear(); |
| 2275 | 2283 |
} |
| 2276 | 2284 |
|
| 2277 | 2285 |
public: |
| 2278 | 2286 |
|
| 2279 | 2287 |
/// \brief Returns the maximal value plus one. |
| 2280 | 2288 |
/// |
| 2281 | 2289 |
/// Returns the maximal value plus one in the map. |
| 2282 | 2290 |
unsigned int size() const {
|
| 2283 | 2291 |
return _inv_map.size(); |
| 2284 | 2292 |
} |
| 2285 | 2293 |
|
| 2286 | 2294 |
/// \brief Swaps the position of the two items in the map. |
| 2287 | 2295 |
/// |
| 2288 | 2296 |
/// Swaps the position of the two items in the map. |
| 2289 | 2297 |
void swap(const Item& p, const Item& q) {
|
| 2290 | 2298 |
int pi = Map::operator[](p); |
| 2291 | 2299 |
int qi = Map::operator[](q); |
| 2292 | 2300 |
Map::set(p, qi); |
| 2293 | 2301 |
_inv_map[qi] = p; |
| 2294 | 2302 |
Map::set(q, pi); |
| 2295 | 2303 |
_inv_map[pi] = q; |
| 2296 | 2304 |
} |
| 2297 | 2305 |
|
| 2298 | 2306 |
/// \brief Gives back the \e range \e id of the item |
| 2299 | 2307 |
/// |
| 2300 | 2308 |
/// Gives back the \e range \e id of the item. |
| 2301 | 2309 |
int operator[](const Item& item) const {
|
| 2302 | 2310 |
return Map::operator[](item); |
| 2303 | 2311 |
} |
| 2304 | 2312 |
|
| 2305 | 2313 |
/// \brief Gives back the item belonging to a \e range \e id |
| 2306 | 2314 |
/// |
| 2307 | 2315 |
/// Gives back the item belonging to the given \e range \e id. |
| 2308 | 2316 |
Item operator()(int id) const {
|
| 2309 | 2317 |
return _inv_map[id]; |
| 2310 | 2318 |
} |
| 2311 | 2319 |
|
| 2312 | 2320 |
private: |
| 2313 | 2321 |
|
| 2314 | 2322 |
typedef std::vector<Item> Container; |
| 2315 | 2323 |
Container _inv_map; |
| 2316 | 2324 |
|
| 2317 | 2325 |
public: |
| 2318 | 2326 |
|
| 2319 | 2327 |
/// \brief The inverse map type of RangeIdMap. |
| 2320 | 2328 |
/// |
| 2321 | 2329 |
/// The inverse map type of RangeIdMap. The subscript operator gives |
| 2322 | 2330 |
/// back an item by its \e range \e id. |
| 2323 | 2331 |
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept. |
| 2324 | 2332 |
class InverseMap {
|
| 2325 | 2333 |
public: |
| 2326 | 2334 |
/// \brief Constructor |
| 2327 | 2335 |
/// |
| 2328 | 2336 |
/// Constructor of the InverseMap. |
| 2329 | 2337 |
explicit InverseMap(const RangeIdMap& inverted) |
| 2330 | 2338 |
: _inverted(inverted) {}
|
| 2331 | 2339 |
|
| 2332 | 2340 |
|
| 2333 | 2341 |
/// The value type of the InverseMap. |
| 2334 | 2342 |
typedef typename RangeIdMap::Key Value; |
| 2335 | 2343 |
/// The key type of the InverseMap. |
| 2336 | 2344 |
typedef typename RangeIdMap::Value Key; |
| 2337 | 2345 |
|
| 2338 | 2346 |
/// \brief Subscript operator. |
| 2339 | 2347 |
/// |
| 2340 | 2348 |
/// Subscript operator. It gives back the item |
| 2341 | 2349 |
/// that the given \e range \e id currently belongs to. |
| 2342 | 2350 |
Value operator[](const Key& key) const {
|
| 2343 | 2351 |
return _inverted(key); |
| 2344 | 2352 |
} |
| 2345 | 2353 |
|
| 2346 | 2354 |
/// \brief Size of the map. |
| 2347 | 2355 |
/// |
| 2348 | 2356 |
/// Returns the size of the map. |
| 2349 | 2357 |
unsigned int size() const {
|
| 2350 | 2358 |
return _inverted.size(); |
| 2351 | 2359 |
} |
| 2352 | 2360 |
|
| 2353 | 2361 |
private: |
| 2354 | 2362 |
const RangeIdMap& _inverted; |
| 2355 | 2363 |
}; |
| 2356 | 2364 |
|
| 2357 | 2365 |
/// \brief Gives back the inverse of the map. |
| 2358 | 2366 |
/// |
| 2359 | 2367 |
/// Gives back the inverse of the RangeIdMap. |
| 2360 | 2368 |
const InverseMap inverse() const {
|
| 2361 | 2369 |
return InverseMap(*this); |
| 2362 | 2370 |
} |
| 2363 | 2371 |
}; |
| 2364 | 2372 |
|
| 2373 |
/// \brief Returns a \c RangeIdMap class. |
|
| 2374 |
/// |
|
| 2375 |
/// This function just returns an \c RangeIdMap class. |
|
| 2376 |
/// \relates RangeIdMap |
|
| 2377 |
template <typename K, typename GR> |
|
| 2378 |
inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
|
|
| 2379 |
return RangeIdMap<GR, K>(graph); |
|
| 2380 |
} |
|
| 2381 |
|
|
| 2365 | 2382 |
/// \brief Dynamic iterable \c bool map. |
| 2366 | 2383 |
/// |
| 2367 | 2384 |
/// This class provides a special graph map type which can store a |
| 2368 | 2385 |
/// \c bool value for graph items (\c Node, \c Arc or \c Edge). |
| 2369 | 2386 |
/// For both \c true and \c false values it is possible to iterate on |
| 2370 | 2387 |
/// the keys mapped to the value. |
| 2371 | 2388 |
/// |
| 2372 | 2389 |
/// This type is a reference map, so it can be modified with the |
| 2373 | 2390 |
/// subscript operator. |
| 2374 | 2391 |
/// |
| 2375 | 2392 |
/// \tparam GR The graph type. |
| 2376 | 2393 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
| 2377 | 2394 |
/// \c GR::Edge). |
| 2378 | 2395 |
/// |
| 2379 | 2396 |
/// \see IterableIntMap, IterableValueMap |
| 2380 | 2397 |
/// \see CrossRefMap |
| 2381 | 2398 |
template <typename GR, typename K> |
| 2382 | 2399 |
class IterableBoolMap |
| 2383 | 2400 |
: protected ItemSetTraits<GR, K>::template Map<int>::Type {
|
| 2384 | 2401 |
private: |
| 2385 | 2402 |
typedef GR Graph; |
| 2386 | 2403 |
|
| 2387 | 2404 |
typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt; |
| 2388 | 2405 |
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent; |
| 2389 | 2406 |
|
| 2390 | 2407 |
std::vector<K> _array; |
| 2391 | 2408 |
int _sep; |
| 2392 | 2409 |
|
| 2393 | 2410 |
public: |
| 2394 | 2411 |
|
| 2395 | 2412 |
/// Indicates that the map is reference map. |
| 2396 | 2413 |
typedef True ReferenceMapTag; |
| 2397 | 2414 |
|
| 2398 | 2415 |
/// The key type |
| 2399 | 2416 |
typedef K Key; |
| 2400 | 2417 |
/// The value type |
| 2401 | 2418 |
typedef bool Value; |
| 2402 | 2419 |
/// The const reference type. |
| 2403 | 2420 |
typedef const Value& ConstReference; |
| 2404 | 2421 |
|
| 2405 | 2422 |
private: |
| 2406 | 2423 |
|
| 2407 | 2424 |
int position(const Key& key) const {
|
| 2408 | 2425 |
return Parent::operator[](key); |
| 2409 | 2426 |
} |
| 2410 | 2427 |
|
| 2411 | 2428 |
public: |
| 2412 | 2429 |
|
| 2413 | 2430 |
/// \brief Reference to the value of the map. |
| 2414 | 2431 |
/// |
| 2415 | 2432 |
/// This class is similar to the \c bool type. It can be converted to |
| 2416 | 2433 |
/// \c bool and it provides the same operators. |
| 2417 | 2434 |
class Reference {
|
| 2418 | 2435 |
friend class IterableBoolMap; |
| 2419 | 2436 |
private: |
| 2420 | 2437 |
Reference(IterableBoolMap& map, const Key& key) |
| 2421 | 2438 |
: _key(key), _map(map) {}
|
| 2422 | 2439 |
public: |
| 2423 | 2440 |
|
| 2424 | 2441 |
Reference& operator=(const Reference& value) {
|
| 2425 | 2442 |
_map.set(_key, static_cast<bool>(value)); |
| 2426 | 2443 |
return *this; |
| 2427 | 2444 |
} |
| 2428 | 2445 |
|
| 2429 | 2446 |
operator bool() const {
|
| 2430 | 2447 |
return static_cast<const IterableBoolMap&>(_map)[_key]; |
| 2431 | 2448 |
} |
| 2432 | 2449 |
|
| 2433 | 2450 |
Reference& operator=(bool value) {
|
| 2434 | 2451 |
_map.set(_key, value); |
| 2435 | 2452 |
return *this; |
| 2436 | 2453 |
} |
| 2437 | 2454 |
Reference& operator&=(bool value) {
|
| 2438 | 2455 |
_map.set(_key, _map[_key] & value); |
| 2439 | 2456 |
return *this; |
| 2440 | 2457 |
} |
| 2441 | 2458 |
Reference& operator|=(bool value) {
|
| 2442 | 2459 |
_map.set(_key, _map[_key] | value); |
| 2443 | 2460 |
return *this; |
| 2444 | 2461 |
} |
| 2445 | 2462 |
Reference& operator^=(bool value) {
|
| 2446 | 2463 |
_map.set(_key, _map[_key] ^ value); |
| 2447 | 2464 |
return *this; |
| 2448 | 2465 |
} |
| 2449 | 2466 |
private: |
| 2450 | 2467 |
Key _key; |
| 2451 | 2468 |
IterableBoolMap& _map; |
| 2452 | 2469 |
}; |
| 2453 | 2470 |
|
| 2454 | 2471 |
/// \brief Constructor of the map with a default value. |
| 2455 | 2472 |
/// |
| 2456 | 2473 |
/// Constructor of the map with a default value. |
| 2457 | 2474 |
explicit IterableBoolMap(const Graph& graph, bool def = false) |
| 2458 | 2475 |
: Parent(graph) {
|
| 2459 | 2476 |
typename Parent::Notifier* nf = Parent::notifier(); |
| 2460 | 2477 |
Key it; |
| 2461 | 2478 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2462 | 2479 |
Parent::set(it, _array.size()); |
| 2463 | 2480 |
_array.push_back(it); |
| 2464 | 2481 |
} |
| 2465 | 2482 |
_sep = (def ? _array.size() : 0); |
| 2466 | 2483 |
} |
| 2467 | 2484 |
|
| 2468 | 2485 |
/// \brief Const subscript operator of the map. |
| 2469 | 2486 |
/// |
| 2470 | 2487 |
/// Const subscript operator of the map. |
| 2471 | 2488 |
bool operator[](const Key& key) const {
|
| 2472 | 2489 |
return position(key) < _sep; |
| 2473 | 2490 |
} |
| 2474 | 2491 |
|
| 2475 | 2492 |
/// \brief Subscript operator of the map. |
| 2476 | 2493 |
/// |
| 2477 | 2494 |
/// Subscript operator of the map. |
| 2478 | 2495 |
Reference operator[](const Key& key) {
|
| 2479 | 2496 |
return Reference(*this, key); |
| 2480 | 2497 |
} |
| 2481 | 2498 |
|
| 2482 | 2499 |
/// \brief Set operation of the map. |
| 2483 | 2500 |
/// |
| 2484 | 2501 |
/// Set operation of the map. |
| 2485 | 2502 |
void set(const Key& key, bool value) {
|
| 2486 | 2503 |
int pos = position(key); |
| 2487 | 2504 |
if (value) {
|
| 2488 | 2505 |
if (pos < _sep) return; |
| 2489 | 2506 |
Key tmp = _array[_sep]; |
| 2490 | 2507 |
_array[_sep] = key; |
| 2491 | 2508 |
Parent::set(key, _sep); |
| 2492 | 2509 |
_array[pos] = tmp; |
| 2493 | 2510 |
Parent::set(tmp, pos); |
| 2494 | 2511 |
++_sep; |
| 2495 | 2512 |
} else {
|
| 2496 | 2513 |
if (pos >= _sep) return; |
| 2497 | 2514 |
--_sep; |
| 2498 | 2515 |
Key tmp = _array[_sep]; |
| 2499 | 2516 |
_array[_sep] = key; |
| 2500 | 2517 |
Parent::set(key, _sep); |
| 2501 | 2518 |
_array[pos] = tmp; |
| 2502 | 2519 |
Parent::set(tmp, pos); |
| 2503 | 2520 |
} |
| 2504 | 2521 |
} |
| 2505 | 2522 |
|
| 2506 | 2523 |
/// \brief Set all items. |
| 2507 | 2524 |
/// |
| 2508 | 2525 |
/// Set all items in the map. |
| 2509 | 2526 |
/// \note Constant time operation. |
| 2510 | 2527 |
void setAll(bool value) {
|
| 2511 | 2528 |
_sep = (value ? _array.size() : 0); |
| 2512 | 2529 |
} |
| 2513 | 2530 |
|
| 2514 | 2531 |
/// \brief Returns the number of the keys mapped to \c true. |
| 2515 | 2532 |
/// |
| 2516 | 2533 |
/// Returns the number of the keys mapped to \c true. |
| 2517 | 2534 |
int trueNum() const {
|
| 2518 | 2535 |
return _sep; |
| 2519 | 2536 |
} |
| 2520 | 2537 |
|
| 2521 | 2538 |
/// \brief Returns the number of the keys mapped to \c false. |
| 2522 | 2539 |
/// |
| 2523 | 2540 |
/// Returns the number of the keys mapped to \c false. |
| 2524 | 2541 |
int falseNum() const {
|
| 2525 | 2542 |
return _array.size() - _sep; |
| 2526 | 2543 |
} |
| 2527 | 2544 |
|
| 2528 | 2545 |
/// \brief Iterator for the keys mapped to \c true. |
| 2529 | 2546 |
/// |
| 2530 | 2547 |
/// Iterator for the keys mapped to \c true. It works |
| 2531 | 2548 |
/// like a graph item iterator, it can be converted to |
| 2532 | 2549 |
/// the key type of the map, incremented with \c ++ operator, and |
| 2533 | 2550 |
/// if the iterator leaves the last valid key, it will be equal to |
| 2534 | 2551 |
/// \c INVALID. |
| 2535 | 2552 |
class TrueIt : public Key {
|
| 2536 | 2553 |
public: |
| 2537 | 2554 |
typedef Key Parent; |
| 2538 | 2555 |
|
| 2539 | 2556 |
/// \brief Creates an iterator. |
| 2540 | 2557 |
/// |
| 2541 | 2558 |
/// Creates an iterator. It iterates on the |
| 2542 | 2559 |
/// keys mapped to \c true. |
| 2543 | 2560 |
/// \param map The IterableBoolMap. |
| 2544 | 2561 |
explicit TrueIt(const IterableBoolMap& map) |
| 2545 | 2562 |
: Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID), |
| 2546 | 2563 |
_map(&map) {}
|
| 2547 | 2564 |
|
| 2548 | 2565 |
/// \brief Invalid constructor \& conversion. |
| 2549 | 2566 |
/// |
| 2550 | 2567 |
/// This constructor initializes the iterator to be invalid. |
| 2551 | 2568 |
/// \sa Invalid for more details. |
| 2552 | 2569 |
TrueIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2553 | 2570 |
|
| 2554 | 2571 |
/// \brief Increment operator. |
| 2555 | 2572 |
/// |
| 2556 | 2573 |
/// Increment operator. |
| 2557 | 2574 |
TrueIt& operator++() {
|
| 2558 | 2575 |
int pos = _map->position(*this); |
| 2559 | 2576 |
Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID); |
| 2560 | 2577 |
return *this; |
| 2561 | 2578 |
} |
| 2562 | 2579 |
|
| 2563 | 2580 |
private: |
| 2564 | 2581 |
const IterableBoolMap* _map; |
| 2565 | 2582 |
}; |
| 2566 | 2583 |
|
| 2567 | 2584 |
/// \brief Iterator for the keys mapped to \c false. |
| 2568 | 2585 |
/// |
| 2569 | 2586 |
/// Iterator for the keys mapped to \c false. It works |
| 2570 | 2587 |
/// like a graph item iterator, it can be converted to |
| 2571 | 2588 |
/// the key type of the map, incremented with \c ++ operator, and |
| 2572 | 2589 |
/// if the iterator leaves the last valid key, it will be equal to |
| 2573 | 2590 |
/// \c INVALID. |
| 2574 | 2591 |
class FalseIt : public Key {
|
| 2575 | 2592 |
public: |
| 2576 | 2593 |
typedef Key Parent; |
| 2577 | 2594 |
|
| 2578 | 2595 |
/// \brief Creates an iterator. |
| 2579 | 2596 |
/// |
| 2580 | 2597 |
/// Creates an iterator. It iterates on the |
| 2581 | 2598 |
/// keys mapped to \c false. |
| 2582 | 2599 |
/// \param map The IterableBoolMap. |
| 2583 | 2600 |
explicit FalseIt(const IterableBoolMap& map) |
| 2584 | 2601 |
: Parent(map._sep < int(map._array.size()) ? |
| 2585 | 2602 |
map._array.back() : INVALID), _map(&map) {}
|
| 2586 | 2603 |
|
| 2587 | 2604 |
/// \brief Invalid constructor \& conversion. |
| 2588 | 2605 |
/// |
| 2589 | 2606 |
/// This constructor initializes the iterator to be invalid. |
| 2590 | 2607 |
/// \sa Invalid for more details. |
| 2591 | 2608 |
FalseIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2592 | 2609 |
|
| 2593 | 2610 |
/// \brief Increment operator. |
| 2594 | 2611 |
/// |
| 2595 | 2612 |
/// Increment operator. |
| 2596 | 2613 |
FalseIt& operator++() {
|
| 2597 | 2614 |
int pos = _map->position(*this); |
| 2598 | 2615 |
Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID); |
| 2599 | 2616 |
return *this; |
| 2600 | 2617 |
} |
| 2601 | 2618 |
|
| 2602 | 2619 |
private: |
| 2603 | 2620 |
const IterableBoolMap* _map; |
| 2604 | 2621 |
}; |
| 2605 | 2622 |
|
| 2606 | 2623 |
/// \brief Iterator for the keys mapped to a given value. |
| 2607 | 2624 |
/// |
| 2608 | 2625 |
/// Iterator for the keys mapped to a given value. It works |
| 2609 | 2626 |
/// like a graph item iterator, it can be converted to |
| 2610 | 2627 |
/// the key type of the map, incremented with \c ++ operator, and |
| 2611 | 2628 |
/// if the iterator leaves the last valid key, it will be equal to |
| 2612 | 2629 |
/// \c INVALID. |
| 2613 | 2630 |
class ItemIt : public Key {
|
| 2614 | 2631 |
public: |
| 2615 | 2632 |
typedef Key Parent; |
| 2616 | 2633 |
|
| 2617 | 2634 |
/// \brief Creates an iterator with a value. |
| 2618 | 2635 |
/// |
| 2619 | 2636 |
/// Creates an iterator with a value. It iterates on the |
| 2620 | 2637 |
/// keys mapped to the given value. |
| 2621 | 2638 |
/// \param map The IterableBoolMap. |
| 2622 | 2639 |
/// \param value The value. |
| 2623 | 2640 |
ItemIt(const IterableBoolMap& map, bool value) |
| 2624 | 2641 |
: Parent(value ? |
| 2625 | 2642 |
(map._sep > 0 ? |
| 2626 | 2643 |
map._array[map._sep - 1] : INVALID) : |
| 2627 | 2644 |
(map._sep < int(map._array.size()) ? |
| 2628 | 2645 |
map._array.back() : INVALID)), _map(&map) {}
|
| 2629 | 2646 |
|
| 2630 | 2647 |
/// \brief Invalid constructor \& conversion. |
| 2631 | 2648 |
/// |
| 2632 | 2649 |
/// This constructor initializes the iterator to be invalid. |
| 2633 | 2650 |
/// \sa Invalid for more details. |
| 2634 | 2651 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2635 | 2652 |
|
| 2636 | 2653 |
/// \brief Increment operator. |
| 2637 | 2654 |
/// |
| 2638 | 2655 |
/// Increment operator. |
| 2639 | 2656 |
ItemIt& operator++() {
|
| 2640 | 2657 |
int pos = _map->position(*this); |
| 2641 | 2658 |
int _sep = pos >= _map->_sep ? _map->_sep : 0; |
| 2642 | 2659 |
Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID); |
| 2643 | 2660 |
return *this; |
| 2644 | 2661 |
} |
| 2645 | 2662 |
|
| 2646 | 2663 |
private: |
| 2647 | 2664 |
const IterableBoolMap* _map; |
| 2648 | 2665 |
}; |
| 2649 | 2666 |
|
| 2650 | 2667 |
protected: |
| 2651 | 2668 |
|
| 2652 | 2669 |
virtual void add(const Key& key) {
|
| 2653 | 2670 |
Parent::add(key); |
| 2654 | 2671 |
Parent::set(key, _array.size()); |
| 2655 | 2672 |
_array.push_back(key); |
| 2656 | 2673 |
} |
| 2657 | 2674 |
|
| 2658 | 2675 |
virtual void add(const std::vector<Key>& keys) {
|
| 2659 | 2676 |
Parent::add(keys); |
| 2660 | 2677 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2661 | 2678 |
Parent::set(keys[i], _array.size()); |
| 2662 | 2679 |
_array.push_back(keys[i]); |
| 2663 | 2680 |
} |
| 2664 | 2681 |
} |
| 2665 | 2682 |
|
| 2666 | 2683 |
virtual void erase(const Key& key) {
|
| 2667 | 2684 |
int pos = position(key); |
| 2668 | 2685 |
if (pos < _sep) {
|
| 2669 | 2686 |
--_sep; |
| 2670 | 2687 |
Parent::set(_array[_sep], pos); |
| 2671 | 2688 |
_array[pos] = _array[_sep]; |
| 2672 | 2689 |
Parent::set(_array.back(), _sep); |
| 2673 | 2690 |
_array[_sep] = _array.back(); |
| 2674 | 2691 |
_array.pop_back(); |
| 2675 | 2692 |
} else {
|
| 2676 | 2693 |
Parent::set(_array.back(), pos); |
| 2677 | 2694 |
_array[pos] = _array.back(); |
| 2678 | 2695 |
_array.pop_back(); |
| 2679 | 2696 |
} |
| 2680 | 2697 |
Parent::erase(key); |
| 2681 | 2698 |
} |
| 2682 | 2699 |
|
| 2683 | 2700 |
virtual void erase(const std::vector<Key>& keys) {
|
| 2684 | 2701 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2685 | 2702 |
int pos = position(keys[i]); |
| 2686 | 2703 |
if (pos < _sep) {
|
| 2687 | 2704 |
--_sep; |
| 2688 | 2705 |
Parent::set(_array[_sep], pos); |
| 2689 | 2706 |
_array[pos] = _array[_sep]; |
| 2690 | 2707 |
Parent::set(_array.back(), _sep); |
| 2691 | 2708 |
_array[_sep] = _array.back(); |
| 2692 | 2709 |
_array.pop_back(); |
| 2693 | 2710 |
} else {
|
| 2694 | 2711 |
Parent::set(_array.back(), pos); |
| 2695 | 2712 |
_array[pos] = _array.back(); |
| 2696 | 2713 |
_array.pop_back(); |
| 2697 | 2714 |
} |
| 2698 | 2715 |
} |
| 2699 | 2716 |
Parent::erase(keys); |
| 2700 | 2717 |
} |
| 2701 | 2718 |
|
| 2702 | 2719 |
virtual void build() {
|
| 2703 | 2720 |
Parent::build(); |
| 2704 | 2721 |
typename Parent::Notifier* nf = Parent::notifier(); |
| 2705 | 2722 |
Key it; |
| 2706 | 2723 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2707 | 2724 |
Parent::set(it, _array.size()); |
| 2708 | 2725 |
_array.push_back(it); |
| 2709 | 2726 |
} |
| 2710 | 2727 |
_sep = 0; |
| 2711 | 2728 |
} |
| 2712 | 2729 |
|
| 2713 | 2730 |
virtual void clear() {
|
| 2714 | 2731 |
_array.clear(); |
| 2715 | 2732 |
_sep = 0; |
| 2716 | 2733 |
Parent::clear(); |
| 2717 | 2734 |
} |
| 2718 | 2735 |
|
| 2719 | 2736 |
}; |
| 2720 | 2737 |
|
| 2721 | 2738 |
|
| 2722 | 2739 |
namespace _maps_bits {
|
| 2723 | 2740 |
template <typename Item> |
| 2724 | 2741 |
struct IterableIntMapNode {
|
| 2725 | 2742 |
IterableIntMapNode() : value(-1) {}
|
| 2726 | 2743 |
IterableIntMapNode(int _value) : value(_value) {}
|
| 2727 | 2744 |
Item prev, next; |
| 2728 | 2745 |
int value; |
| 2729 | 2746 |
}; |
| 2730 | 2747 |
} |
| 2731 | 2748 |
|
| 2732 | 2749 |
/// \brief Dynamic iterable integer map. |
| 2733 | 2750 |
/// |
| 2734 | 2751 |
/// This class provides a special graph map type which can store an |
| 2735 | 2752 |
/// integer value for graph items (\c Node, \c Arc or \c Edge). |
| 2736 | 2753 |
/// For each non-negative value it is possible to iterate on the keys |
| 2737 | 2754 |
/// mapped to the value. |
| 2738 | 2755 |
/// |
| 2739 | 2756 |
/// This map is intended to be used with small integer values, for which |
| 2740 | 2757 |
/// it is efficient, and supports iteration only for non-negative values. |
| 2741 | 2758 |
/// If you need large values and/or iteration for negative integers, |
| 2742 | 2759 |
/// consider to use \ref IterableValueMap instead. |
| 2743 | 2760 |
/// |
| 2744 | 2761 |
/// This type is a reference map, so it can be modified with the |
| 2745 | 2762 |
/// subscript operator. |
| 2746 | 2763 |
/// |
| 2747 | 2764 |
/// \note The size of the data structure depends on the largest |
| 2748 | 2765 |
/// value in the map. |
| 2749 | 2766 |
/// |
| 2750 | 2767 |
/// \tparam GR The graph type. |
| 2751 | 2768 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
| 2752 | 2769 |
/// \c GR::Edge). |
| 2753 | 2770 |
/// |
| 2754 | 2771 |
/// \see IterableBoolMap, IterableValueMap |
| 2755 | 2772 |
/// \see CrossRefMap |
| 2756 | 2773 |
template <typename GR, typename K> |
| 2757 | 2774 |
class IterableIntMap |
| 2758 | 2775 |
: protected ItemSetTraits<GR, K>:: |
| 2759 | 2776 |
template Map<_maps_bits::IterableIntMapNode<K> >::Type {
|
| 2760 | 2777 |
public: |
| 2761 | 2778 |
typedef typename ItemSetTraits<GR, K>:: |
| 2762 | 2779 |
template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent; |
| 2763 | 2780 |
|
| 2764 | 2781 |
/// The key type |
| 2765 | 2782 |
typedef K Key; |
| 2766 | 2783 |
/// The value type |
| 2767 | 2784 |
typedef int Value; |
| 2768 | 2785 |
/// The graph type |
| 2769 | 2786 |
typedef GR Graph; |
| 2770 | 2787 |
|
| 2771 | 2788 |
/// \brief Constructor of the map. |
| 2772 | 2789 |
/// |
| 2773 | 2790 |
/// Constructor of the map. It sets all values to -1. |
| 2774 | 2791 |
explicit IterableIntMap(const Graph& graph) |
| 2775 | 2792 |
: Parent(graph) {}
|
| 2776 | 2793 |
|
| 2777 | 2794 |
/// \brief Constructor of the map with a given value. |
| 2778 | 2795 |
/// |
| 2779 | 2796 |
/// Constructor of the map with a given value. |
| 2780 | 2797 |
explicit IterableIntMap(const Graph& graph, int value) |
| 2781 | 2798 |
: Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
|
| 2782 | 2799 |
if (value >= 0) {
|
| 2783 | 2800 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
| 2784 | 2801 |
lace(it); |
| 2785 | 2802 |
} |
| 2786 | 2803 |
} |
| 2787 | 2804 |
} |
| 2788 | 2805 |
|
| 2789 | 2806 |
private: |
| 2790 | 2807 |
|
| 2791 | 2808 |
void unlace(const Key& key) {
|
| 2792 | 2809 |
typename Parent::Value& node = Parent::operator[](key); |
| 2793 | 2810 |
if (node.value < 0) return; |
| 2794 | 2811 |
if (node.prev != INVALID) {
|
| 2795 | 2812 |
Parent::operator[](node.prev).next = node.next; |
| 2796 | 2813 |
} else {
|
| 2797 | 2814 |
_first[node.value] = node.next; |
| 2798 | 2815 |
} |
| 2799 | 2816 |
if (node.next != INVALID) {
|
| 2800 | 2817 |
Parent::operator[](node.next).prev = node.prev; |
| 2801 | 2818 |
} |
| 2802 | 2819 |
while (!_first.empty() && _first.back() == INVALID) {
|
| 2803 | 2820 |
_first.pop_back(); |
| 2804 | 2821 |
} |
| 2805 | 2822 |
} |
| 2806 | 2823 |
|
| 2807 | 2824 |
void lace(const Key& key) {
|
| 2808 | 2825 |
typename Parent::Value& node = Parent::operator[](key); |
| 2809 | 2826 |
if (node.value < 0) return; |
| 2810 | 2827 |
if (node.value >= int(_first.size())) {
|
| 2811 | 2828 |
_first.resize(node.value + 1, INVALID); |
| 2812 | 2829 |
} |
| 2813 | 2830 |
node.prev = INVALID; |
| 2814 | 2831 |
node.next = _first[node.value]; |
| 2815 | 2832 |
if (node.next != INVALID) {
|
| 2816 | 2833 |
Parent::operator[](node.next).prev = key; |
| 2817 | 2834 |
} |
| 2818 | 2835 |
_first[node.value] = key; |
| 2819 | 2836 |
} |
| 2820 | 2837 |
|
| 2821 | 2838 |
public: |
| 2822 | 2839 |
|
| 2823 | 2840 |
/// Indicates that the map is reference map. |
| 2824 | 2841 |
typedef True ReferenceMapTag; |
| 2825 | 2842 |
|
| 2826 | 2843 |
/// \brief Reference to the value of the map. |
| 2827 | 2844 |
/// |
| 2828 | 2845 |
/// This class is similar to the \c int type. It can |
| 2829 | 2846 |
/// be converted to \c int and it has the same operators. |
| 2830 | 2847 |
class Reference {
|
| 2831 | 2848 |
friend class IterableIntMap; |
| 2832 | 2849 |
private: |
| 2833 | 2850 |
Reference(IterableIntMap& map, const Key& key) |
| 2834 | 2851 |
: _key(key), _map(map) {}
|
| 2835 | 2852 |
public: |
| 2836 | 2853 |
|
| 2837 | 2854 |
Reference& operator=(const Reference& value) {
|
| 2838 | 2855 |
_map.set(_key, static_cast<const int&>(value)); |
| 2839 | 2856 |
return *this; |
| 2840 | 2857 |
} |
| 2841 | 2858 |
|
| 2842 | 2859 |
operator const int&() const {
|
| 2843 | 2860 |
return static_cast<const IterableIntMap&>(_map)[_key]; |
| 2844 | 2861 |
} |
| 2845 | 2862 |
|
| 2846 | 2863 |
Reference& operator=(int value) {
|
| 2847 | 2864 |
_map.set(_key, value); |
| 2848 | 2865 |
return *this; |
| 2849 | 2866 |
} |
| 2850 | 2867 |
Reference& operator++() {
|
| 2851 | 2868 |
_map.set(_key, _map[_key] + 1); |
| 2852 | 2869 |
return *this; |
| 2853 | 2870 |
} |
| 2854 | 2871 |
int operator++(int) {
|
| 2855 | 2872 |
int value = _map[_key]; |
| 2856 | 2873 |
_map.set(_key, value + 1); |
| 2857 | 2874 |
return value; |
| 2858 | 2875 |
} |
| 2859 | 2876 |
Reference& operator--() {
|
| 2860 | 2877 |
_map.set(_key, _map[_key] - 1); |
| 2861 | 2878 |
return *this; |
| 2862 | 2879 |
} |
| 2863 | 2880 |
int operator--(int) {
|
| 2864 | 2881 |
int value = _map[_key]; |
| 2865 | 2882 |
_map.set(_key, value - 1); |
| 2866 | 2883 |
return value; |
| 2867 | 2884 |
} |
| 2868 | 2885 |
Reference& operator+=(int value) {
|
| 2869 | 2886 |
_map.set(_key, _map[_key] + value); |
| 2870 | 2887 |
return *this; |
| 2871 | 2888 |
} |
| 2872 | 2889 |
Reference& operator-=(int value) {
|
| 2873 | 2890 |
_map.set(_key, _map[_key] - value); |
| 2874 | 2891 |
return *this; |
| 2875 | 2892 |
} |
| 2876 | 2893 |
Reference& operator*=(int value) {
|
| 2877 | 2894 |
_map.set(_key, _map[_key] * value); |
| 2878 | 2895 |
return *this; |
| 2879 | 2896 |
} |
| 2880 | 2897 |
Reference& operator/=(int value) {
|
| 2881 | 2898 |
_map.set(_key, _map[_key] / value); |
| 2882 | 2899 |
return *this; |
| 2883 | 2900 |
} |
| 2884 | 2901 |
Reference& operator%=(int value) {
|
| 2885 | 2902 |
_map.set(_key, _map[_key] % value); |
| 2886 | 2903 |
return *this; |
| 2887 | 2904 |
} |
| 2888 | 2905 |
Reference& operator&=(int value) {
|
| 2889 | 2906 |
_map.set(_key, _map[_key] & value); |
| 2890 | 2907 |
return *this; |
| 2891 | 2908 |
} |
| 2892 | 2909 |
Reference& operator|=(int value) {
|
| 2893 | 2910 |
_map.set(_key, _map[_key] | value); |
| 2894 | 2911 |
return *this; |
| 2895 | 2912 |
} |
| 2896 | 2913 |
Reference& operator^=(int value) {
|
| 2897 | 2914 |
_map.set(_key, _map[_key] ^ value); |
| 2898 | 2915 |
return *this; |
| 2899 | 2916 |
} |
| 2900 | 2917 |
Reference& operator<<=(int value) {
|
| 2901 | 2918 |
_map.set(_key, _map[_key] << value); |
| 2902 | 2919 |
return *this; |
| 2903 | 2920 |
} |
| 2904 | 2921 |
Reference& operator>>=(int value) {
|
| 2905 | 2922 |
_map.set(_key, _map[_key] >> value); |
| 2906 | 2923 |
return *this; |
| 2907 | 2924 |
} |
| 2908 | 2925 |
|
| 2909 | 2926 |
private: |
| 2910 | 2927 |
Key _key; |
| 2911 | 2928 |
IterableIntMap& _map; |
| 2912 | 2929 |
}; |
| 2913 | 2930 |
|
| 2914 | 2931 |
/// The const reference type. |
| 2915 | 2932 |
typedef const Value& ConstReference; |
| 2916 | 2933 |
|
| 2917 | 2934 |
/// \brief Gives back the maximal value plus one. |
| 2918 | 2935 |
/// |
| 2919 | 2936 |
/// Gives back the maximal value plus one. |
| 2920 | 2937 |
int size() const {
|
| 2921 | 2938 |
return _first.size(); |
| 2922 | 2939 |
} |
| 2923 | 2940 |
|
| 2924 | 2941 |
/// \brief Set operation of the map. |
| 2925 | 2942 |
/// |
| 2926 | 2943 |
/// Set operation of the map. |
| 2927 | 2944 |
void set(const Key& key, const Value& value) {
|
| 2928 | 2945 |
unlace(key); |
| 2929 | 2946 |
Parent::operator[](key).value = value; |
| 2930 | 2947 |
lace(key); |
| 2931 | 2948 |
} |
| 2932 | 2949 |
|
| 2933 | 2950 |
/// \brief Const subscript operator of the map. |
| 2934 | 2951 |
/// |
| 2935 | 2952 |
/// Const subscript operator of the map. |
| 2936 | 2953 |
const Value& operator[](const Key& key) const {
|
| 2937 | 2954 |
return Parent::operator[](key).value; |
| 2938 | 2955 |
} |
| 2939 | 2956 |
|
| 2940 | 2957 |
/// \brief Subscript operator of the map. |
| 2941 | 2958 |
/// |
| 2942 | 2959 |
/// Subscript operator of the map. |
| 2943 | 2960 |
Reference operator[](const Key& key) {
|
| 2944 | 2961 |
return Reference(*this, key); |
| 2945 | 2962 |
} |
| 2946 | 2963 |
|
| 2947 | 2964 |
/// \brief Iterator for the keys with the same value. |
| 2948 | 2965 |
/// |
| 2949 | 2966 |
/// Iterator for the keys with the same value. It works |
| 2950 | 2967 |
/// like a graph item iterator, it can be converted to |
| 2951 | 2968 |
/// the item type of the map, incremented with \c ++ operator, and |
| 2952 | 2969 |
/// if the iterator leaves the last valid item, it will be equal to |
| 2953 | 2970 |
/// \c INVALID. |
| 2954 | 2971 |
class ItemIt : public Key {
|
| 2955 | 2972 |
public: |
| 2956 | 2973 |
typedef Key Parent; |
| 2957 | 2974 |
|
| 2958 | 2975 |
/// \brief Invalid constructor \& conversion. |
| 2959 | 2976 |
/// |
| 2960 | 2977 |
/// This constructor initializes the iterator to be invalid. |
| 2961 | 2978 |
/// \sa Invalid for more details. |
| 2962 | 2979 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2963 | 2980 |
|
| 2964 | 2981 |
/// \brief Creates an iterator with a value. |
| 2965 | 2982 |
/// |
| 2966 | 2983 |
/// Creates an iterator with a value. It iterates on the |
| 2967 | 2984 |
/// keys mapped to the given value. |
| 2968 | 2985 |
/// \param map The IterableIntMap. |
| 2969 | 2986 |
/// \param value The value. |
| 2970 | 2987 |
ItemIt(const IterableIntMap& map, int value) : _map(&map) {
|
| 2971 | 2988 |
if (value < 0 || value >= int(_map->_first.size())) {
|
| 2972 | 2989 |
Parent::operator=(INVALID); |
| 2973 | 2990 |
} else {
|
| 2974 | 2991 |
Parent::operator=(_map->_first[value]); |
| 2975 | 2992 |
} |
| 2976 | 2993 |
} |
| 2977 | 2994 |
|
| 2978 | 2995 |
/// \brief Increment operator. |
| 2979 | 2996 |
/// |
| 2980 | 2997 |
/// Increment operator. |
| 2981 | 2998 |
ItemIt& operator++() {
|
| 2982 | 2999 |
Parent::operator=(_map->IterableIntMap::Parent:: |
| 2983 | 3000 |
operator[](static_cast<Parent&>(*this)).next); |
| 2984 | 3001 |
return *this; |
| 2985 | 3002 |
} |
| 2986 | 3003 |
|
| 2987 | 3004 |
private: |
| 2988 | 3005 |
const IterableIntMap* _map; |
| 2989 | 3006 |
}; |
| 2990 | 3007 |
|
| 2991 | 3008 |
protected: |
| 2992 | 3009 |
|
| 2993 | 3010 |
virtual void erase(const Key& key) {
|
| 2994 | 3011 |
unlace(key); |
| 2995 | 3012 |
Parent::erase(key); |
| 2996 | 3013 |
} |
| 2997 | 3014 |
|
| 2998 | 3015 |
virtual void erase(const std::vector<Key>& keys) {
|
| 2999 | 3016 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 3000 | 3017 |
unlace(keys[i]); |
| 3001 | 3018 |
} |
| 3002 | 3019 |
Parent::erase(keys); |
| 3003 | 3020 |
} |
| 3004 | 3021 |
|
| 3005 | 3022 |
virtual void clear() {
|
| 3006 | 3023 |
_first.clear(); |
| 3007 | 3024 |
Parent::clear(); |
| 3008 | 3025 |
} |
| 3009 | 3026 |
|
| 3010 | 3027 |
private: |
| 3011 | 3028 |
std::vector<Key> _first; |
| 3012 | 3029 |
}; |
| 3013 | 3030 |
|
| 3014 | 3031 |
namespace _maps_bits {
|
| 3015 | 3032 |
template <typename Item, typename Value> |
| 3016 | 3033 |
struct IterableValueMapNode {
|
| 3017 | 3034 |
IterableValueMapNode(Value _value = Value()) : value(_value) {}
|
| 3018 | 3035 |
Item prev, next; |
| 3019 | 3036 |
Value value; |
| 3020 | 3037 |
}; |
| 3021 | 3038 |
} |
| 3022 | 3039 |
|
| 3023 | 3040 |
/// \brief Dynamic iterable map for comparable values. |
| 3024 | 3041 |
/// |
| 3025 | 3042 |
/// This class provides a special graph map type which can store a |
| 3026 | 3043 |
/// comparable value for graph items (\c Node, \c Arc or \c Edge). |
| 3027 | 3044 |
/// For each value it is possible to iterate on the keys mapped to |
| 3028 | 3045 |
/// the value (\c ItemIt), and the values of the map can be accessed |
| 3029 | 3046 |
/// with an STL compatible forward iterator (\c ValueIt). |
| 3030 | 3047 |
/// The map stores a linked list for each value, which contains |
| 3031 | 3048 |
/// the items mapped to the value, and the used values are stored |
| 3032 | 3049 |
/// in balanced binary tree (\c std::map). |
| 3033 | 3050 |
/// |
| 3034 | 3051 |
/// \ref IterableBoolMap and \ref IterableIntMap are similar classes |
| 3035 | 3052 |
/// specialized for \c bool and \c int values, respectively. |
| 3036 | 3053 |
/// |
| 3037 | 3054 |
/// This type is not reference map, so it cannot be modified with |
| 3038 | 3055 |
/// the subscript operator. |
| 3039 | 3056 |
/// |
| 3040 | 3057 |
/// \tparam GR The graph type. |
| 3041 | 3058 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
| 3042 | 3059 |
/// \c GR::Edge). |
| 3043 | 3060 |
/// \tparam V The value type of the map. It can be any comparable |
| 3044 | 3061 |
/// value type. |
| 3045 | 3062 |
/// |
| 3046 | 3063 |
/// \see IterableBoolMap, IterableIntMap |
| 3047 | 3064 |
/// \see CrossRefMap |
| 3048 | 3065 |
template <typename GR, typename K, typename V> |
| 3049 | 3066 |
class IterableValueMap |
| 3050 | 3067 |
: protected ItemSetTraits<GR, K>:: |
| 3051 | 3068 |
template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
|
| 3052 | 3069 |
public: |
| 3053 | 3070 |
typedef typename ItemSetTraits<GR, K>:: |
| 3054 | 3071 |
template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent; |
| 3055 | 3072 |
|
| 3056 | 3073 |
/// The key type |
| 3057 | 3074 |
typedef K Key; |
| 3058 | 3075 |
/// The value type |
| 3059 | 3076 |
typedef V Value; |
| 3060 | 3077 |
/// The graph type |
| 3061 | 3078 |
typedef GR Graph; |
| 3062 | 3079 |
|
| 3063 | 3080 |
public: |
| 3064 | 3081 |
|
| 3065 | 3082 |
/// \brief Constructor of the map with a given value. |
| 3066 | 3083 |
/// |
| 3067 | 3084 |
/// Constructor of the map with a given value. |
| 3068 | 3085 |
explicit IterableValueMap(const Graph& graph, |
| 3069 | 3086 |
const Value& value = Value()) |
| 3070 | 3087 |
: Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
|
| 3071 | 3088 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
| 3072 | 3089 |
lace(it); |
| 3073 | 3090 |
} |
| 3074 | 3091 |
} |
| 3075 | 3092 |
|
| 3076 | 3093 |
protected: |
| 3077 | 3094 |
|
| 3078 | 3095 |
void unlace(const Key& key) {
|
| 3079 | 3096 |
typename Parent::Value& node = Parent::operator[](key); |
| 3080 | 3097 |
if (node.prev != INVALID) {
|
| 3081 | 3098 |
Parent::operator[](node.prev).next = node.next; |
| 3082 | 3099 |
} else {
|
| 3083 | 3100 |
if (node.next != INVALID) {
|
| 3084 | 3101 |
_first[node.value] = node.next; |
| 3085 | 3102 |
} else {
|
| 3086 | 3103 |
_first.erase(node.value); |
| 3087 | 3104 |
} |
| 3088 | 3105 |
} |
| 3089 | 3106 |
if (node.next != INVALID) {
|
| 3090 | 3107 |
Parent::operator[](node.next).prev = node.prev; |
| 3091 | 3108 |
} |
| 3092 | 3109 |
} |
| 3093 | 3110 |
|
| 3094 | 3111 |
void lace(const Key& key) {
|
| 3095 | 3112 |
typename Parent::Value& node = Parent::operator[](key); |
| 3096 | 3113 |
typename std::map<Value, Key>::iterator it = _first.find(node.value); |
| 3097 | 3114 |
if (it == _first.end()) {
|
| 3098 | 3115 |
node.prev = node.next = INVALID; |
| 3099 | 3116 |
_first.insert(std::make_pair(node.value, key)); |
| 3100 | 3117 |
} else {
|
| 3101 | 3118 |
node.prev = INVALID; |
| 3102 | 3119 |
node.next = it->second; |
| 3103 | 3120 |
if (node.next != INVALID) {
|
| 3104 | 3121 |
Parent::operator[](node.next).prev = key; |
| 3105 | 3122 |
} |
| 3106 | 3123 |
it->second = key; |
| 3107 | 3124 |
} |
| 3108 | 3125 |
} |
| 3109 | 3126 |
|
| 3110 | 3127 |
public: |
| 3111 | 3128 |
|
| 3112 | 3129 |
/// \brief Forward iterator for values. |
| 3113 | 3130 |
/// |
| 3114 | 3131 |
/// This iterator is an STL compatible forward |
| 3115 | 3132 |
/// iterator on the values of the map. The values can |
| 3116 | 3133 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range. |
| 3117 | 3134 |
class ValueIt |
| 3118 | 3135 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
| 3119 | 3136 |
friend class IterableValueMap; |
| 3120 | 3137 |
private: |
| 3121 | 3138 |
ValueIt(typename std::map<Value, Key>::const_iterator _it) |
| 3122 | 3139 |
: it(_it) {}
|
| 3123 | 3140 |
public: |
| 3124 | 3141 |
|
| 3125 | 3142 |
/// Constructor |
| 3126 | 3143 |
ValueIt() {}
|
| 3127 | 3144 |
|
| 3128 | 3145 |
/// \e |
| 3129 | 3146 |
ValueIt& operator++() { ++it; return *this; }
|
| 3130 | 3147 |
/// \e |
| 3131 | 3148 |
ValueIt operator++(int) {
|
| 3132 | 3149 |
ValueIt tmp(*this); |
0 comments (0 inline)