| ... |
... |
@@ -1398,2349 +1398,2356 @@
|
| 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 |
1902 |
|
| 1903 |
1903 |
/// \brief General cross reference graph map type.
|
| 1904 |
1904 |
|
| 1905 |
1905 |
/// This class provides simple invertable graph maps.
|
| 1906 |
1906 |
/// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
|
| 1907 |
1907 |
/// and if a key is set to a new value, then stores it in the inverse map.
|
| 1908 |
1908 |
/// The graph items can be accessed by their values either using
|
| 1909 |
1909 |
/// \c InverseMap or \c operator()(), and the values of the map can be
|
| 1910 |
|
/// accessed with an STL compatible forward iterator (\c ValueIterator).
|
|
1910 |
/// accessed with an STL compatible forward iterator (\c ValueIt).
|
| 1911 |
1911 |
///
|
| 1912 |
1912 |
/// This map is intended to be used when all associated values are
|
| 1913 |
1913 |
/// different (the map is actually invertable) or there are only a few
|
| 1914 |
1914 |
/// items with the same value.
|
| 1915 |
1915 |
/// Otherwise consider to use \c IterableValueMap, which is more
|
| 1916 |
1916 |
/// suitable and more efficient for such cases. It provides iterators
|
| 1917 |
1917 |
/// to traverse the items with the same associated value, however
|
| 1918 |
1918 |
/// it does not have \c InverseMap.
|
| 1919 |
1919 |
///
|
| 1920 |
1920 |
/// This type is not reference map, so it cannot be modified with
|
| 1921 |
1921 |
/// the subscript operator.
|
| 1922 |
1922 |
///
|
| 1923 |
1923 |
/// \tparam GR The graph type.
|
| 1924 |
1924 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
| 1925 |
1925 |
/// \c GR::Edge).
|
| 1926 |
1926 |
/// \tparam V The value type of the map.
|
| 1927 |
1927 |
///
|
| 1928 |
1928 |
/// \see IterableValueMap
|
| 1929 |
1929 |
template <typename GR, typename K, typename V>
|
| 1930 |
1930 |
class CrossRefMap
|
| 1931 |
1931 |
: protected ItemSetTraits<GR, K>::template Map<V>::Type {
|
| 1932 |
1932 |
private:
|
| 1933 |
1933 |
|
| 1934 |
1934 |
typedef typename ItemSetTraits<GR, K>::
|
| 1935 |
1935 |
template Map<V>::Type Map;
|
| 1936 |
1936 |
|
| 1937 |
1937 |
typedef std::multimap<V, K> Container;
|
| 1938 |
1938 |
Container _inv_map;
|
| 1939 |
1939 |
|
| 1940 |
1940 |
public:
|
| 1941 |
1941 |
|
| 1942 |
1942 |
/// The graph type of CrossRefMap.
|
| 1943 |
1943 |
typedef GR Graph;
|
| 1944 |
1944 |
typedef GR Digraph;
|
| 1945 |
1945 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
|
| 1946 |
1946 |
typedef K Item;
|
| 1947 |
1947 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
|
| 1948 |
1948 |
typedef K Key;
|
| 1949 |
1949 |
/// The value type of CrossRefMap.
|
| 1950 |
1950 |
typedef V Value;
|
| 1951 |
1951 |
|
| 1952 |
1952 |
/// \brief Constructor.
|
| 1953 |
1953 |
///
|
| 1954 |
1954 |
/// Construct a new CrossRefMap for the given graph.
|
| 1955 |
1955 |
explicit CrossRefMap(const Graph& graph) : Map(graph) {}
|
| 1956 |
1956 |
|
| 1957 |
1957 |
/// \brief Forward iterator for values.
|
| 1958 |
1958 |
///
|
| 1959 |
1959 |
/// This iterator is an STL compatible forward
|
| 1960 |
1960 |
/// iterator on the values of the map. The values can
|
| 1961 |
1961 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range.
|
| 1962 |
1962 |
/// They are considered with multiplicity, so each value is
|
| 1963 |
1963 |
/// traversed for each item it is assigned to.
|
| 1964 |
|
class ValueIterator
|
|
1964 |
class ValueIt
|
| 1965 |
1965 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
| 1966 |
1966 |
friend class CrossRefMap;
|
| 1967 |
1967 |
private:
|
| 1968 |
|
ValueIterator(typename Container::const_iterator _it)
|
|
1968 |
ValueIt(typename Container::const_iterator _it)
|
| 1969 |
1969 |
: it(_it) {}
|
| 1970 |
1970 |
public:
|
| 1971 |
1971 |
|
| 1972 |
1972 |
/// Constructor
|
| 1973 |
|
ValueIterator() {}
|
|
1973 |
ValueIt() {}
|
| 1974 |
1974 |
|
| 1975 |
1975 |
/// \e
|
| 1976 |
|
ValueIterator& operator++() { ++it; return *this; }
|
|
1976 |
ValueIt& operator++() { ++it; return *this; }
|
| 1977 |
1977 |
/// \e
|
| 1978 |
|
ValueIterator operator++(int) {
|
| 1979 |
|
ValueIterator tmp(*this);
|
|
1978 |
ValueIt operator++(int) {
|
|
1979 |
ValueIt tmp(*this);
|
| 1980 |
1980 |
operator++();
|
| 1981 |
1981 |
return tmp;
|
| 1982 |
1982 |
}
|
| 1983 |
1983 |
|
| 1984 |
1984 |
/// \e
|
| 1985 |
1985 |
const Value& operator*() const { return it->first; }
|
| 1986 |
1986 |
/// \e
|
| 1987 |
1987 |
const Value* operator->() const { return &(it->first); }
|
| 1988 |
1988 |
|
| 1989 |
1989 |
/// \e
|
| 1990 |
|
bool operator==(ValueIterator jt) const { return it == jt.it; }
|
|
1990 |
bool operator==(ValueIt jt) const { return it == jt.it; }
|
| 1991 |
1991 |
/// \e
|
| 1992 |
|
bool operator!=(ValueIterator jt) const { return it != jt.it; }
|
|
1992 |
bool operator!=(ValueIt jt) const { return it != jt.it; }
|
| 1993 |
1993 |
|
| 1994 |
1994 |
private:
|
| 1995 |
1995 |
typename Container::const_iterator it;
|
| 1996 |
1996 |
};
|
| 1997 |
1997 |
|
|
1998 |
/// Alias for \c ValueIt
|
|
1999 |
typedef ValueIt ValueIterator;
|
|
2000 |
|
| 1998 |
2001 |
/// \brief Returns an iterator to the first value.
|
| 1999 |
2002 |
///
|
| 2000 |
2003 |
/// Returns an STL compatible iterator to the
|
| 2001 |
2004 |
/// first value of the map. The values of the
|
| 2002 |
2005 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
|
| 2003 |
2006 |
/// range.
|
| 2004 |
|
ValueIterator beginValue() const {
|
| 2005 |
|
return ValueIterator(_inv_map.begin());
|
|
2007 |
ValueIt beginValue() const {
|
|
2008 |
return ValueIt(_inv_map.begin());
|
| 2006 |
2009 |
}
|
| 2007 |
2010 |
|
| 2008 |
2011 |
/// \brief Returns an iterator after the last value.
|
| 2009 |
2012 |
///
|
| 2010 |
2013 |
/// Returns an STL compatible iterator after the
|
| 2011 |
2014 |
/// last value of the map. The values of the
|
| 2012 |
2015 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
|
| 2013 |
2016 |
/// range.
|
| 2014 |
|
ValueIterator endValue() const {
|
| 2015 |
|
return ValueIterator(_inv_map.end());
|
|
2017 |
ValueIt endValue() const {
|
|
2018 |
return ValueIt(_inv_map.end());
|
| 2016 |
2019 |
}
|
| 2017 |
2020 |
|
| 2018 |
2021 |
/// \brief Sets the value associated with the given key.
|
| 2019 |
2022 |
///
|
| 2020 |
2023 |
/// Sets the value associated with the given key.
|
| 2021 |
2024 |
void set(const Key& key, const Value& val) {
|
| 2022 |
2025 |
Value oldval = Map::operator[](key);
|
| 2023 |
2026 |
typename Container::iterator it;
|
| 2024 |
2027 |
for (it = _inv_map.equal_range(oldval).first;
|
| 2025 |
2028 |
it != _inv_map.equal_range(oldval).second; ++it) {
|
| 2026 |
2029 |
if (it->second == key) {
|
| 2027 |
2030 |
_inv_map.erase(it);
|
| 2028 |
2031 |
break;
|
| 2029 |
2032 |
}
|
| 2030 |
2033 |
}
|
| 2031 |
2034 |
_inv_map.insert(std::make_pair(val, key));
|
| 2032 |
2035 |
Map::set(key, val);
|
| 2033 |
2036 |
}
|
| 2034 |
2037 |
|
| 2035 |
2038 |
/// \brief Returns the value associated with the given key.
|
| 2036 |
2039 |
///
|
| 2037 |
2040 |
/// Returns the value associated with the given key.
|
| 2038 |
2041 |
typename MapTraits<Map>::ConstReturnValue
|
| 2039 |
2042 |
operator[](const Key& key) const {
|
| 2040 |
2043 |
return Map::operator[](key);
|
| 2041 |
2044 |
}
|
| 2042 |
2045 |
|
| 2043 |
2046 |
/// \brief Gives back an item by its value.
|
| 2044 |
2047 |
///
|
| 2045 |
2048 |
/// This function gives back an item that is assigned to
|
| 2046 |
2049 |
/// the given value or \c INVALID if no such item exists.
|
| 2047 |
2050 |
/// If there are more items with the same associated value,
|
| 2048 |
2051 |
/// only one of them is returned.
|
| 2049 |
2052 |
Key operator()(const Value& val) const {
|
| 2050 |
2053 |
typename Container::const_iterator it = _inv_map.find(val);
|
| 2051 |
2054 |
return it != _inv_map.end() ? it->second : INVALID;
|
| 2052 |
2055 |
}
|
| 2053 |
2056 |
|
| 2054 |
2057 |
/// \brief Returns the number of items with the given value.
|
| 2055 |
2058 |
///
|
| 2056 |
2059 |
/// This function returns the number of items with the given value
|
| 2057 |
2060 |
/// associated with it.
|
| 2058 |
2061 |
int count(const Value &val) const {
|
| 2059 |
2062 |
return _inv_map.count(val);
|
| 2060 |
2063 |
}
|
| 2061 |
2064 |
|
| 2062 |
2065 |
protected:
|
| 2063 |
2066 |
|
| 2064 |
2067 |
/// \brief Erase the key from the map and the inverse map.
|
| 2065 |
2068 |
///
|
| 2066 |
2069 |
/// Erase the key from the map and the inverse map. It is called by the
|
| 2067 |
2070 |
/// \c AlterationNotifier.
|
| 2068 |
2071 |
virtual void erase(const Key& key) {
|
| 2069 |
2072 |
Value val = Map::operator[](key);
|
| 2070 |
2073 |
typename Container::iterator it;
|
| 2071 |
2074 |
for (it = _inv_map.equal_range(val).first;
|
| 2072 |
2075 |
it != _inv_map.equal_range(val).second; ++it) {
|
| 2073 |
2076 |
if (it->second == key) {
|
| 2074 |
2077 |
_inv_map.erase(it);
|
| 2075 |
2078 |
break;
|
| 2076 |
2079 |
}
|
| 2077 |
2080 |
}
|
| 2078 |
2081 |
Map::erase(key);
|
| 2079 |
2082 |
}
|
| 2080 |
2083 |
|
| 2081 |
2084 |
/// \brief Erase more keys from the map and the inverse map.
|
| 2082 |
2085 |
///
|
| 2083 |
2086 |
/// Erase more keys from the map and the inverse map. It is called by the
|
| 2084 |
2087 |
/// \c AlterationNotifier.
|
| 2085 |
2088 |
virtual void erase(const std::vector<Key>& keys) {
|
| 2086 |
2089 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2087 |
2090 |
Value val = Map::operator[](keys[i]);
|
| 2088 |
2091 |
typename Container::iterator it;
|
| 2089 |
2092 |
for (it = _inv_map.equal_range(val).first;
|
| 2090 |
2093 |
it != _inv_map.equal_range(val).second; ++it) {
|
| 2091 |
2094 |
if (it->second == keys[i]) {
|
| 2092 |
2095 |
_inv_map.erase(it);
|
| 2093 |
2096 |
break;
|
| 2094 |
2097 |
}
|
| 2095 |
2098 |
}
|
| 2096 |
2099 |
}
|
| 2097 |
2100 |
Map::erase(keys);
|
| 2098 |
2101 |
}
|
| 2099 |
2102 |
|
| 2100 |
2103 |
/// \brief Clear the keys from the map and the inverse map.
|
| 2101 |
2104 |
///
|
| 2102 |
2105 |
/// Clear the keys from the map and the inverse map. It is called by the
|
| 2103 |
2106 |
/// \c AlterationNotifier.
|
| 2104 |
2107 |
virtual void clear() {
|
| 2105 |
2108 |
_inv_map.clear();
|
| 2106 |
2109 |
Map::clear();
|
| 2107 |
2110 |
}
|
| 2108 |
2111 |
|
| 2109 |
2112 |
public:
|
| 2110 |
2113 |
|
| 2111 |
2114 |
/// \brief The inverse map type of CrossRefMap.
|
| 2112 |
2115 |
///
|
| 2113 |
2116 |
/// The inverse map type of CrossRefMap. The subscript operator gives
|
| 2114 |
2117 |
/// back an item by its value.
|
| 2115 |
2118 |
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
|
| 2116 |
2119 |
/// \see inverse()
|
| 2117 |
2120 |
class InverseMap {
|
| 2118 |
2121 |
public:
|
| 2119 |
2122 |
/// \brief Constructor
|
| 2120 |
2123 |
///
|
| 2121 |
2124 |
/// Constructor of the InverseMap.
|
| 2122 |
2125 |
explicit InverseMap(const CrossRefMap& inverted)
|
| 2123 |
2126 |
: _inverted(inverted) {}
|
| 2124 |
2127 |
|
| 2125 |
2128 |
/// The value type of the InverseMap.
|
| 2126 |
2129 |
typedef typename CrossRefMap::Key Value;
|
| 2127 |
2130 |
/// The key type of the InverseMap.
|
| 2128 |
2131 |
typedef typename CrossRefMap::Value Key;
|
| 2129 |
2132 |
|
| 2130 |
2133 |
/// \brief Subscript operator.
|
| 2131 |
2134 |
///
|
| 2132 |
2135 |
/// Subscript operator. It gives back an item
|
| 2133 |
2136 |
/// that is assigned to the given value or \c INVALID
|
| 2134 |
2137 |
/// if no such item exists.
|
| 2135 |
2138 |
Value operator[](const Key& key) const {
|
| 2136 |
2139 |
return _inverted(key);
|
| 2137 |
2140 |
}
|
| 2138 |
2141 |
|
| 2139 |
2142 |
private:
|
| 2140 |
2143 |
const CrossRefMap& _inverted;
|
| 2141 |
2144 |
};
|
| 2142 |
2145 |
|
| 2143 |
2146 |
/// \brief Gives back the inverse of the map.
|
| 2144 |
2147 |
///
|
| 2145 |
2148 |
/// Gives back the inverse of the CrossRefMap.
|
| 2146 |
2149 |
InverseMap inverse() const {
|
| 2147 |
2150 |
return InverseMap(*this);
|
| 2148 |
2151 |
}
|
| 2149 |
2152 |
|
| 2150 |
2153 |
};
|
| 2151 |
2154 |
|
| 2152 |
2155 |
/// \brief Provides continuous and unique id for the
|
| 2153 |
2156 |
/// items of a graph.
|
| 2154 |
2157 |
///
|
| 2155 |
2158 |
/// RangeIdMap provides a unique and continuous
|
| 2156 |
2159 |
/// id for each item of a given type (\c Node, \c Arc or
|
| 2157 |
2160 |
/// \c Edge) in a graph. This id is
|
| 2158 |
2161 |
/// - \b unique: different items get different ids,
|
| 2159 |
2162 |
/// - \b continuous: the range of the ids is the set of integers
|
| 2160 |
2163 |
/// between 0 and \c n-1, where \c n is the number of the items of
|
| 2161 |
2164 |
/// this type (\c Node, \c Arc or \c Edge).
|
| 2162 |
2165 |
/// - So, the ids can change when deleting an item of the same type.
|
| 2163 |
2166 |
///
|
| 2164 |
2167 |
/// Thus this id is not (necessarily) the same as what can get using
|
| 2165 |
2168 |
/// the \c id() function of the graph or \ref IdMap.
|
| 2166 |
2169 |
/// This map can be inverted with its member class \c InverseMap,
|
| 2167 |
2170 |
/// or with the \c operator()() member.
|
| 2168 |
2171 |
///
|
| 2169 |
2172 |
/// \tparam GR The graph type.
|
| 2170 |
2173 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
| 2171 |
2174 |
/// \c GR::Edge).
|
| 2172 |
2175 |
///
|
| 2173 |
2176 |
/// \see IdMap
|
| 2174 |
2177 |
template <typename GR, typename K>
|
| 2175 |
2178 |
class RangeIdMap
|
| 2176 |
2179 |
: protected ItemSetTraits<GR, K>::template Map<int>::Type {
|
| 2177 |
2180 |
|
| 2178 |
2181 |
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
|
| 2179 |
2182 |
|
| 2180 |
2183 |
public:
|
| 2181 |
2184 |
/// The graph type of RangeIdMap.
|
| 2182 |
2185 |
typedef GR Graph;
|
| 2183 |
2186 |
typedef GR Digraph;
|
| 2184 |
2187 |
/// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
|
| 2185 |
2188 |
typedef K Item;
|
| 2186 |
2189 |
/// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
|
| 2187 |
2190 |
typedef K Key;
|
| 2188 |
2191 |
/// The value type of RangeIdMap.
|
| 2189 |
2192 |
typedef int Value;
|
| 2190 |
2193 |
|
| 2191 |
2194 |
/// \brief Constructor.
|
| 2192 |
2195 |
///
|
| 2193 |
2196 |
/// Constructor.
|
| 2194 |
2197 |
explicit RangeIdMap(const Graph& gr) : Map(gr) {
|
| 2195 |
2198 |
Item it;
|
| 2196 |
2199 |
const typename Map::Notifier* nf = Map::notifier();
|
| 2197 |
2200 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2198 |
2201 |
Map::set(it, _inv_map.size());
|
| 2199 |
2202 |
_inv_map.push_back(it);
|
| 2200 |
2203 |
}
|
| 2201 |
2204 |
}
|
| 2202 |
2205 |
|
| 2203 |
2206 |
protected:
|
| 2204 |
2207 |
|
| 2205 |
2208 |
/// \brief Adds a new key to the map.
|
| 2206 |
2209 |
///
|
| 2207 |
2210 |
/// Add a new key to the map. It is called by the
|
| 2208 |
2211 |
/// \c AlterationNotifier.
|
| 2209 |
2212 |
virtual void add(const Item& item) {
|
| 2210 |
2213 |
Map::add(item);
|
| 2211 |
2214 |
Map::set(item, _inv_map.size());
|
| 2212 |
2215 |
_inv_map.push_back(item);
|
| 2213 |
2216 |
}
|
| 2214 |
2217 |
|
| 2215 |
2218 |
/// \brief Add more new keys to the map.
|
| 2216 |
2219 |
///
|
| 2217 |
2220 |
/// Add more new keys to the map. It is called by the
|
| 2218 |
2221 |
/// \c AlterationNotifier.
|
| 2219 |
2222 |
virtual void add(const std::vector<Item>& items) {
|
| 2220 |
2223 |
Map::add(items);
|
| 2221 |
2224 |
for (int i = 0; i < int(items.size()); ++i) {
|
| 2222 |
2225 |
Map::set(items[i], _inv_map.size());
|
| 2223 |
2226 |
_inv_map.push_back(items[i]);
|
| 2224 |
2227 |
}
|
| 2225 |
2228 |
}
|
| 2226 |
2229 |
|
| 2227 |
2230 |
/// \brief Erase the key from the map.
|
| 2228 |
2231 |
///
|
| 2229 |
2232 |
/// Erase the key from the map. It is called by the
|
| 2230 |
2233 |
/// \c AlterationNotifier.
|
| 2231 |
2234 |
virtual void erase(const Item& item) {
|
| 2232 |
2235 |
Map::set(_inv_map.back(), Map::operator[](item));
|
| 2233 |
2236 |
_inv_map[Map::operator[](item)] = _inv_map.back();
|
| 2234 |
2237 |
_inv_map.pop_back();
|
| 2235 |
2238 |
Map::erase(item);
|
| 2236 |
2239 |
}
|
| 2237 |
2240 |
|
| 2238 |
2241 |
/// \brief Erase more keys from the map.
|
| 2239 |
2242 |
///
|
| 2240 |
2243 |
/// Erase more keys from the map. It is called by the
|
| 2241 |
2244 |
/// \c AlterationNotifier.
|
| 2242 |
2245 |
virtual void erase(const std::vector<Item>& items) {
|
| 2243 |
2246 |
for (int i = 0; i < int(items.size()); ++i) {
|
| 2244 |
2247 |
Map::set(_inv_map.back(), Map::operator[](items[i]));
|
| 2245 |
2248 |
_inv_map[Map::operator[](items[i])] = _inv_map.back();
|
| 2246 |
2249 |
_inv_map.pop_back();
|
| 2247 |
2250 |
}
|
| 2248 |
2251 |
Map::erase(items);
|
| 2249 |
2252 |
}
|
| 2250 |
2253 |
|
| 2251 |
2254 |
/// \brief Build the unique map.
|
| 2252 |
2255 |
///
|
| 2253 |
2256 |
/// Build the unique map. It is called by the
|
| 2254 |
2257 |
/// \c AlterationNotifier.
|
| 2255 |
2258 |
virtual void build() {
|
| 2256 |
2259 |
Map::build();
|
| 2257 |
2260 |
Item it;
|
| 2258 |
2261 |
const typename Map::Notifier* nf = Map::notifier();
|
| 2259 |
2262 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2260 |
2263 |
Map::set(it, _inv_map.size());
|
| 2261 |
2264 |
_inv_map.push_back(it);
|
| 2262 |
2265 |
}
|
| 2263 |
2266 |
}
|
| 2264 |
2267 |
|
| 2265 |
2268 |
/// \brief Clear the keys from the map.
|
| 2266 |
2269 |
///
|
| 2267 |
2270 |
/// Clear the keys from the map. It is called by the
|
| 2268 |
2271 |
/// \c AlterationNotifier.
|
| 2269 |
2272 |
virtual void clear() {
|
| 2270 |
2273 |
_inv_map.clear();
|
| 2271 |
2274 |
Map::clear();
|
| 2272 |
2275 |
}
|
| 2273 |
2276 |
|
| 2274 |
2277 |
public:
|
| 2275 |
2278 |
|
| 2276 |
2279 |
/// \brief Returns the maximal value plus one.
|
| 2277 |
2280 |
///
|
| 2278 |
2281 |
/// Returns the maximal value plus one in the map.
|
| 2279 |
2282 |
unsigned int size() const {
|
| 2280 |
2283 |
return _inv_map.size();
|
| 2281 |
2284 |
}
|
| 2282 |
2285 |
|
| 2283 |
2286 |
/// \brief Swaps the position of the two items in the map.
|
| 2284 |
2287 |
///
|
| 2285 |
2288 |
/// Swaps the position of the two items in the map.
|
| 2286 |
2289 |
void swap(const Item& p, const Item& q) {
|
| 2287 |
2290 |
int pi = Map::operator[](p);
|
| 2288 |
2291 |
int qi = Map::operator[](q);
|
| 2289 |
2292 |
Map::set(p, qi);
|
| 2290 |
2293 |
_inv_map[qi] = p;
|
| 2291 |
2294 |
Map::set(q, pi);
|
| 2292 |
2295 |
_inv_map[pi] = q;
|
| 2293 |
2296 |
}
|
| 2294 |
2297 |
|
| 2295 |
2298 |
/// \brief Gives back the \e range \e id of the item
|
| 2296 |
2299 |
///
|
| 2297 |
2300 |
/// Gives back the \e range \e id of the item.
|
| 2298 |
2301 |
int operator[](const Item& item) const {
|
| 2299 |
2302 |
return Map::operator[](item);
|
| 2300 |
2303 |
}
|
| 2301 |
2304 |
|
| 2302 |
2305 |
/// \brief Gives back the item belonging to a \e range \e id
|
| 2303 |
2306 |
///
|
| 2304 |
2307 |
/// Gives back the item belonging to the given \e range \e id.
|
| 2305 |
2308 |
Item operator()(int id) const {
|
| 2306 |
2309 |
return _inv_map[id];
|
| 2307 |
2310 |
}
|
| 2308 |
2311 |
|
| 2309 |
2312 |
private:
|
| 2310 |
2313 |
|
| 2311 |
2314 |
typedef std::vector<Item> Container;
|
| 2312 |
2315 |
Container _inv_map;
|
| 2313 |
2316 |
|
| 2314 |
2317 |
public:
|
| 2315 |
2318 |
|
| 2316 |
2319 |
/// \brief The inverse map type of RangeIdMap.
|
| 2317 |
2320 |
///
|
| 2318 |
2321 |
/// The inverse map type of RangeIdMap. The subscript operator gives
|
| 2319 |
2322 |
/// back an item by its \e range \e id.
|
| 2320 |
2323 |
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
|
| 2321 |
2324 |
class InverseMap {
|
| 2322 |
2325 |
public:
|
| 2323 |
2326 |
/// \brief Constructor
|
| 2324 |
2327 |
///
|
| 2325 |
2328 |
/// Constructor of the InverseMap.
|
| 2326 |
2329 |
explicit InverseMap(const RangeIdMap& inverted)
|
| 2327 |
2330 |
: _inverted(inverted) {}
|
| 2328 |
2331 |
|
| 2329 |
2332 |
|
| 2330 |
2333 |
/// The value type of the InverseMap.
|
| 2331 |
2334 |
typedef typename RangeIdMap::Key Value;
|
| 2332 |
2335 |
/// The key type of the InverseMap.
|
| 2333 |
2336 |
typedef typename RangeIdMap::Value Key;
|
| 2334 |
2337 |
|
| 2335 |
2338 |
/// \brief Subscript operator.
|
| 2336 |
2339 |
///
|
| 2337 |
2340 |
/// Subscript operator. It gives back the item
|
| 2338 |
2341 |
/// that the given \e range \e id currently belongs to.
|
| 2339 |
2342 |
Value operator[](const Key& key) const {
|
| 2340 |
2343 |
return _inverted(key);
|
| 2341 |
2344 |
}
|
| 2342 |
2345 |
|
| 2343 |
2346 |
/// \brief Size of the map.
|
| 2344 |
2347 |
///
|
| 2345 |
2348 |
/// Returns the size of the map.
|
| 2346 |
2349 |
unsigned int size() const {
|
| 2347 |
2350 |
return _inverted.size();
|
| 2348 |
2351 |
}
|
| 2349 |
2352 |
|
| 2350 |
2353 |
private:
|
| 2351 |
2354 |
const RangeIdMap& _inverted;
|
| 2352 |
2355 |
};
|
| 2353 |
2356 |
|
| 2354 |
2357 |
/// \brief Gives back the inverse of the map.
|
| 2355 |
2358 |
///
|
| 2356 |
2359 |
/// Gives back the inverse of the RangeIdMap.
|
| 2357 |
2360 |
const InverseMap inverse() const {
|
| 2358 |
2361 |
return InverseMap(*this);
|
| 2359 |
2362 |
}
|
| 2360 |
2363 |
};
|
| 2361 |
2364 |
|
| 2362 |
2365 |
/// \brief Dynamic iterable \c bool map.
|
| 2363 |
2366 |
///
|
| 2364 |
2367 |
/// This class provides a special graph map type which can store a
|
| 2365 |
2368 |
/// \c bool value for graph items (\c Node, \c Arc or \c Edge).
|
| 2366 |
2369 |
/// For both \c true and \c false values it is possible to iterate on
|
| 2367 |
2370 |
/// the keys mapped to the value.
|
| 2368 |
2371 |
///
|
| 2369 |
2372 |
/// This type is a reference map, so it can be modified with the
|
| 2370 |
2373 |
/// subscript operator.
|
| 2371 |
2374 |
///
|
| 2372 |
2375 |
/// \tparam GR The graph type.
|
| 2373 |
2376 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
| 2374 |
2377 |
/// \c GR::Edge).
|
| 2375 |
2378 |
///
|
| 2376 |
2379 |
/// \see IterableIntMap, IterableValueMap
|
| 2377 |
2380 |
/// \see CrossRefMap
|
| 2378 |
2381 |
template <typename GR, typename K>
|
| 2379 |
2382 |
class IterableBoolMap
|
| 2380 |
2383 |
: protected ItemSetTraits<GR, K>::template Map<int>::Type {
|
| 2381 |
2384 |
private:
|
| 2382 |
2385 |
typedef GR Graph;
|
| 2383 |
2386 |
|
| 2384 |
2387 |
typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
|
| 2385 |
2388 |
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
|
| 2386 |
2389 |
|
| 2387 |
2390 |
std::vector<K> _array;
|
| 2388 |
2391 |
int _sep;
|
| 2389 |
2392 |
|
| 2390 |
2393 |
public:
|
| 2391 |
2394 |
|
| 2392 |
2395 |
/// Indicates that the map is reference map.
|
| 2393 |
2396 |
typedef True ReferenceMapTag;
|
| 2394 |
2397 |
|
| 2395 |
2398 |
/// The key type
|
| 2396 |
2399 |
typedef K Key;
|
| 2397 |
2400 |
/// The value type
|
| 2398 |
2401 |
typedef bool Value;
|
| 2399 |
2402 |
/// The const reference type.
|
| 2400 |
2403 |
typedef const Value& ConstReference;
|
| 2401 |
2404 |
|
| 2402 |
2405 |
private:
|
| 2403 |
2406 |
|
| 2404 |
2407 |
int position(const Key& key) const {
|
| 2405 |
2408 |
return Parent::operator[](key);
|
| 2406 |
2409 |
}
|
| 2407 |
2410 |
|
| 2408 |
2411 |
public:
|
| 2409 |
2412 |
|
| 2410 |
2413 |
/// \brief Reference to the value of the map.
|
| 2411 |
2414 |
///
|
| 2412 |
2415 |
/// This class is similar to the \c bool type. It can be converted to
|
| 2413 |
2416 |
/// \c bool and it provides the same operators.
|
| 2414 |
2417 |
class Reference {
|
| 2415 |
2418 |
friend class IterableBoolMap;
|
| 2416 |
2419 |
private:
|
| 2417 |
2420 |
Reference(IterableBoolMap& map, const Key& key)
|
| 2418 |
2421 |
: _key(key), _map(map) {}
|
| 2419 |
2422 |
public:
|
| 2420 |
2423 |
|
| 2421 |
2424 |
Reference& operator=(const Reference& value) {
|
| 2422 |
2425 |
_map.set(_key, static_cast<bool>(value));
|
| 2423 |
2426 |
return *this;
|
| 2424 |
2427 |
}
|
| 2425 |
2428 |
|
| 2426 |
2429 |
operator bool() const {
|
| 2427 |
2430 |
return static_cast<const IterableBoolMap&>(_map)[_key];
|
| 2428 |
2431 |
}
|
| 2429 |
2432 |
|
| 2430 |
2433 |
Reference& operator=(bool value) {
|
| 2431 |
2434 |
_map.set(_key, value);
|
| 2432 |
2435 |
return *this;
|
| 2433 |
2436 |
}
|
| 2434 |
2437 |
Reference& operator&=(bool value) {
|
| 2435 |
2438 |
_map.set(_key, _map[_key] & value);
|
| 2436 |
2439 |
return *this;
|
| 2437 |
2440 |
}
|
| 2438 |
2441 |
Reference& operator|=(bool value) {
|
| 2439 |
2442 |
_map.set(_key, _map[_key] | value);
|
| 2440 |
2443 |
return *this;
|
| 2441 |
2444 |
}
|
| 2442 |
2445 |
Reference& operator^=(bool value) {
|
| 2443 |
2446 |
_map.set(_key, _map[_key] ^ value);
|
| 2444 |
2447 |
return *this;
|
| 2445 |
2448 |
}
|
| 2446 |
2449 |
private:
|
| 2447 |
2450 |
Key _key;
|
| 2448 |
2451 |
IterableBoolMap& _map;
|
| 2449 |
2452 |
};
|
| 2450 |
2453 |
|
| 2451 |
2454 |
/// \brief Constructor of the map with a default value.
|
| 2452 |
2455 |
///
|
| 2453 |
2456 |
/// Constructor of the map with a default value.
|
| 2454 |
2457 |
explicit IterableBoolMap(const Graph& graph, bool def = false)
|
| 2455 |
2458 |
: Parent(graph) {
|
| 2456 |
2459 |
typename Parent::Notifier* nf = Parent::notifier();
|
| 2457 |
2460 |
Key it;
|
| 2458 |
2461 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2459 |
2462 |
Parent::set(it, _array.size());
|
| 2460 |
2463 |
_array.push_back(it);
|
| 2461 |
2464 |
}
|
| 2462 |
2465 |
_sep = (def ? _array.size() : 0);
|
| 2463 |
2466 |
}
|
| 2464 |
2467 |
|
| 2465 |
2468 |
/// \brief Const subscript operator of the map.
|
| 2466 |
2469 |
///
|
| 2467 |
2470 |
/// Const subscript operator of the map.
|
| 2468 |
2471 |
bool operator[](const Key& key) const {
|
| 2469 |
2472 |
return position(key) < _sep;
|
| 2470 |
2473 |
}
|
| 2471 |
2474 |
|
| 2472 |
2475 |
/// \brief Subscript operator of the map.
|
| 2473 |
2476 |
///
|
| 2474 |
2477 |
/// Subscript operator of the map.
|
| 2475 |
2478 |
Reference operator[](const Key& key) {
|
| 2476 |
2479 |
return Reference(*this, key);
|
| 2477 |
2480 |
}
|
| 2478 |
2481 |
|
| 2479 |
2482 |
/// \brief Set operation of the map.
|
| 2480 |
2483 |
///
|
| 2481 |
2484 |
/// Set operation of the map.
|
| 2482 |
2485 |
void set(const Key& key, bool value) {
|
| 2483 |
2486 |
int pos = position(key);
|
| 2484 |
2487 |
if (value) {
|
| 2485 |
2488 |
if (pos < _sep) return;
|
| 2486 |
2489 |
Key tmp = _array[_sep];
|
| 2487 |
2490 |
_array[_sep] = key;
|
| 2488 |
2491 |
Parent::set(key, _sep);
|
| 2489 |
2492 |
_array[pos] = tmp;
|
| 2490 |
2493 |
Parent::set(tmp, pos);
|
| 2491 |
2494 |
++_sep;
|
| 2492 |
2495 |
} else {
|
| 2493 |
2496 |
if (pos >= _sep) return;
|
| 2494 |
2497 |
--_sep;
|
| 2495 |
2498 |
Key tmp = _array[_sep];
|
| 2496 |
2499 |
_array[_sep] = key;
|
| 2497 |
2500 |
Parent::set(key, _sep);
|
| 2498 |
2501 |
_array[pos] = tmp;
|
| 2499 |
2502 |
Parent::set(tmp, pos);
|
| 2500 |
2503 |
}
|
| 2501 |
2504 |
}
|
| 2502 |
2505 |
|
| 2503 |
2506 |
/// \brief Set all items.
|
| 2504 |
2507 |
///
|
| 2505 |
2508 |
/// Set all items in the map.
|
| 2506 |
2509 |
/// \note Constant time operation.
|
| 2507 |
2510 |
void setAll(bool value) {
|
| 2508 |
2511 |
_sep = (value ? _array.size() : 0);
|
| 2509 |
2512 |
}
|
| 2510 |
2513 |
|
| 2511 |
2514 |
/// \brief Returns the number of the keys mapped to \c true.
|
| 2512 |
2515 |
///
|
| 2513 |
2516 |
/// Returns the number of the keys mapped to \c true.
|
| 2514 |
2517 |
int trueNum() const {
|
| 2515 |
2518 |
return _sep;
|
| 2516 |
2519 |
}
|
| 2517 |
2520 |
|
| 2518 |
2521 |
/// \brief Returns the number of the keys mapped to \c false.
|
| 2519 |
2522 |
///
|
| 2520 |
2523 |
/// Returns the number of the keys mapped to \c false.
|
| 2521 |
2524 |
int falseNum() const {
|
| 2522 |
2525 |
return _array.size() - _sep;
|
| 2523 |
2526 |
}
|
| 2524 |
2527 |
|
| 2525 |
2528 |
/// \brief Iterator for the keys mapped to \c true.
|
| 2526 |
2529 |
///
|
| 2527 |
2530 |
/// Iterator for the keys mapped to \c true. It works
|
| 2528 |
2531 |
/// like a graph item iterator, it can be converted to
|
| 2529 |
2532 |
/// the key type of the map, incremented with \c ++ operator, and
|
| 2530 |
2533 |
/// if the iterator leaves the last valid key, it will be equal to
|
| 2531 |
2534 |
/// \c INVALID.
|
| 2532 |
2535 |
class TrueIt : public Key {
|
| 2533 |
2536 |
public:
|
| 2534 |
2537 |
typedef Key Parent;
|
| 2535 |
2538 |
|
| 2536 |
2539 |
/// \brief Creates an iterator.
|
| 2537 |
2540 |
///
|
| 2538 |
2541 |
/// Creates an iterator. It iterates on the
|
| 2539 |
2542 |
/// keys mapped to \c true.
|
| 2540 |
2543 |
/// \param map The IterableBoolMap.
|
| 2541 |
2544 |
explicit TrueIt(const IterableBoolMap& map)
|
| 2542 |
2545 |
: Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
|
| 2543 |
2546 |
_map(&map) {}
|
| 2544 |
2547 |
|
| 2545 |
2548 |
/// \brief Invalid constructor \& conversion.
|
| 2546 |
2549 |
///
|
| 2547 |
2550 |
/// This constructor initializes the iterator to be invalid.
|
| 2548 |
2551 |
/// \sa Invalid for more details.
|
| 2549 |
2552 |
TrueIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2550 |
2553 |
|
| 2551 |
2554 |
/// \brief Increment operator.
|
| 2552 |
2555 |
///
|
| 2553 |
2556 |
/// Increment operator.
|
| 2554 |
2557 |
TrueIt& operator++() {
|
| 2555 |
2558 |
int pos = _map->position(*this);
|
| 2556 |
2559 |
Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
|
| 2557 |
2560 |
return *this;
|
| 2558 |
2561 |
}
|
| 2559 |
2562 |
|
| 2560 |
2563 |
private:
|
| 2561 |
2564 |
const IterableBoolMap* _map;
|
| 2562 |
2565 |
};
|
| 2563 |
2566 |
|
| 2564 |
2567 |
/// \brief Iterator for the keys mapped to \c false.
|
| 2565 |
2568 |
///
|
| 2566 |
2569 |
/// Iterator for the keys mapped to \c false. It works
|
| 2567 |
2570 |
/// like a graph item iterator, it can be converted to
|
| 2568 |
2571 |
/// the key type of the map, incremented with \c ++ operator, and
|
| 2569 |
2572 |
/// if the iterator leaves the last valid key, it will be equal to
|
| 2570 |
2573 |
/// \c INVALID.
|
| 2571 |
2574 |
class FalseIt : public Key {
|
| 2572 |
2575 |
public:
|
| 2573 |
2576 |
typedef Key Parent;
|
| 2574 |
2577 |
|
| 2575 |
2578 |
/// \brief Creates an iterator.
|
| 2576 |
2579 |
///
|
| 2577 |
2580 |
/// Creates an iterator. It iterates on the
|
| 2578 |
2581 |
/// keys mapped to \c false.
|
| 2579 |
2582 |
/// \param map The IterableBoolMap.
|
| 2580 |
2583 |
explicit FalseIt(const IterableBoolMap& map)
|
| 2581 |
2584 |
: Parent(map._sep < int(map._array.size()) ?
|
| 2582 |
2585 |
map._array.back() : INVALID), _map(&map) {}
|
| 2583 |
2586 |
|
| 2584 |
2587 |
/// \brief Invalid constructor \& conversion.
|
| 2585 |
2588 |
///
|
| 2586 |
2589 |
/// This constructor initializes the iterator to be invalid.
|
| 2587 |
2590 |
/// \sa Invalid for more details.
|
| 2588 |
2591 |
FalseIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2589 |
2592 |
|
| 2590 |
2593 |
/// \brief Increment operator.
|
| 2591 |
2594 |
///
|
| 2592 |
2595 |
/// Increment operator.
|
| 2593 |
2596 |
FalseIt& operator++() {
|
| 2594 |
2597 |
int pos = _map->position(*this);
|
| 2595 |
2598 |
Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
|
| 2596 |
2599 |
return *this;
|
| 2597 |
2600 |
}
|
| 2598 |
2601 |
|
| 2599 |
2602 |
private:
|
| 2600 |
2603 |
const IterableBoolMap* _map;
|
| 2601 |
2604 |
};
|
| 2602 |
2605 |
|
| 2603 |
2606 |
/// \brief Iterator for the keys mapped to a given value.
|
| 2604 |
2607 |
///
|
| 2605 |
2608 |
/// Iterator for the keys mapped to a given value. It works
|
| 2606 |
2609 |
/// like a graph item iterator, it can be converted to
|
| 2607 |
2610 |
/// the key type of the map, incremented with \c ++ operator, and
|
| 2608 |
2611 |
/// if the iterator leaves the last valid key, it will be equal to
|
| 2609 |
2612 |
/// \c INVALID.
|
| 2610 |
2613 |
class ItemIt : public Key {
|
| 2611 |
2614 |
public:
|
| 2612 |
2615 |
typedef Key Parent;
|
| 2613 |
2616 |
|
| 2614 |
2617 |
/// \brief Creates an iterator with a value.
|
| 2615 |
2618 |
///
|
| 2616 |
2619 |
/// Creates an iterator with a value. It iterates on the
|
| 2617 |
2620 |
/// keys mapped to the given value.
|
| 2618 |
2621 |
/// \param map The IterableBoolMap.
|
| 2619 |
2622 |
/// \param value The value.
|
| 2620 |
2623 |
ItemIt(const IterableBoolMap& map, bool value)
|
| 2621 |
2624 |
: Parent(value ?
|
| 2622 |
2625 |
(map._sep > 0 ?
|
| 2623 |
2626 |
map._array[map._sep - 1] : INVALID) :
|
| 2624 |
2627 |
(map._sep < int(map._array.size()) ?
|
| 2625 |
2628 |
map._array.back() : INVALID)), _map(&map) {}
|
| 2626 |
2629 |
|
| 2627 |
2630 |
/// \brief Invalid constructor \& conversion.
|
| 2628 |
2631 |
///
|
| 2629 |
2632 |
/// This constructor initializes the iterator to be invalid.
|
| 2630 |
2633 |
/// \sa Invalid for more details.
|
| 2631 |
2634 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2632 |
2635 |
|
| 2633 |
2636 |
/// \brief Increment operator.
|
| 2634 |
2637 |
///
|
| 2635 |
2638 |
/// Increment operator.
|
| 2636 |
2639 |
ItemIt& operator++() {
|
| 2637 |
2640 |
int pos = _map->position(*this);
|
| 2638 |
2641 |
int _sep = pos >= _map->_sep ? _map->_sep : 0;
|
| 2639 |
2642 |
Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
|
| 2640 |
2643 |
return *this;
|
| 2641 |
2644 |
}
|
| 2642 |
2645 |
|
| 2643 |
2646 |
private:
|
| 2644 |
2647 |
const IterableBoolMap* _map;
|
| 2645 |
2648 |
};
|
| 2646 |
2649 |
|
| 2647 |
2650 |
protected:
|
| 2648 |
2651 |
|
| 2649 |
2652 |
virtual void add(const Key& key) {
|
| 2650 |
2653 |
Parent::add(key);
|
| 2651 |
2654 |
Parent::set(key, _array.size());
|
| 2652 |
2655 |
_array.push_back(key);
|
| 2653 |
2656 |
}
|
| 2654 |
2657 |
|
| 2655 |
2658 |
virtual void add(const std::vector<Key>& keys) {
|
| 2656 |
2659 |
Parent::add(keys);
|
| 2657 |
2660 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2658 |
2661 |
Parent::set(keys[i], _array.size());
|
| 2659 |
2662 |
_array.push_back(keys[i]);
|
| 2660 |
2663 |
}
|
| 2661 |
2664 |
}
|
| 2662 |
2665 |
|
| 2663 |
2666 |
virtual void erase(const Key& key) {
|
| 2664 |
2667 |
int pos = position(key);
|
| 2665 |
2668 |
if (pos < _sep) {
|
| 2666 |
2669 |
--_sep;
|
| 2667 |
2670 |
Parent::set(_array[_sep], pos);
|
| 2668 |
2671 |
_array[pos] = _array[_sep];
|
| 2669 |
2672 |
Parent::set(_array.back(), _sep);
|
| 2670 |
2673 |
_array[_sep] = _array.back();
|
| 2671 |
2674 |
_array.pop_back();
|
| 2672 |
2675 |
} else {
|
| 2673 |
2676 |
Parent::set(_array.back(), pos);
|
| 2674 |
2677 |
_array[pos] = _array.back();
|
| 2675 |
2678 |
_array.pop_back();
|
| 2676 |
2679 |
}
|
| 2677 |
2680 |
Parent::erase(key);
|
| 2678 |
2681 |
}
|
| 2679 |
2682 |
|
| 2680 |
2683 |
virtual void erase(const std::vector<Key>& keys) {
|
| 2681 |
2684 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2682 |
2685 |
int pos = position(keys[i]);
|
| 2683 |
2686 |
if (pos < _sep) {
|
| 2684 |
2687 |
--_sep;
|
| 2685 |
2688 |
Parent::set(_array[_sep], pos);
|
| 2686 |
2689 |
_array[pos] = _array[_sep];
|
| 2687 |
2690 |
Parent::set(_array.back(), _sep);
|
| 2688 |
2691 |
_array[_sep] = _array.back();
|
| 2689 |
2692 |
_array.pop_back();
|
| 2690 |
2693 |
} else {
|
| 2691 |
2694 |
Parent::set(_array.back(), pos);
|
| 2692 |
2695 |
_array[pos] = _array.back();
|
| 2693 |
2696 |
_array.pop_back();
|
| 2694 |
2697 |
}
|
| 2695 |
2698 |
}
|
| 2696 |
2699 |
Parent::erase(keys);
|
| 2697 |
2700 |
}
|
| 2698 |
2701 |
|
| 2699 |
2702 |
virtual void build() {
|
| 2700 |
2703 |
Parent::build();
|
| 2701 |
2704 |
typename Parent::Notifier* nf = Parent::notifier();
|
| 2702 |
2705 |
Key it;
|
| 2703 |
2706 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2704 |
2707 |
Parent::set(it, _array.size());
|
| 2705 |
2708 |
_array.push_back(it);
|
| 2706 |
2709 |
}
|
| 2707 |
2710 |
_sep = 0;
|
| 2708 |
2711 |
}
|
| 2709 |
2712 |
|
| 2710 |
2713 |
virtual void clear() {
|
| 2711 |
2714 |
_array.clear();
|
| 2712 |
2715 |
_sep = 0;
|
| 2713 |
2716 |
Parent::clear();
|
| 2714 |
2717 |
}
|
| 2715 |
2718 |
|
| 2716 |
2719 |
};
|
| 2717 |
2720 |
|
| 2718 |
2721 |
|
| 2719 |
2722 |
namespace _maps_bits {
|
| 2720 |
2723 |
template <typename Item>
|
| 2721 |
2724 |
struct IterableIntMapNode {
|
| 2722 |
2725 |
IterableIntMapNode() : value(-1) {}
|
| 2723 |
2726 |
IterableIntMapNode(int _value) : value(_value) {}
|
| 2724 |
2727 |
Item prev, next;
|
| 2725 |
2728 |
int value;
|
| 2726 |
2729 |
};
|
| 2727 |
2730 |
}
|
| 2728 |
2731 |
|
| 2729 |
2732 |
/// \brief Dynamic iterable integer map.
|
| 2730 |
2733 |
///
|
| 2731 |
2734 |
/// This class provides a special graph map type which can store an
|
| 2732 |
2735 |
/// integer value for graph items (\c Node, \c Arc or \c Edge).
|
| 2733 |
2736 |
/// For each non-negative value it is possible to iterate on the keys
|
| 2734 |
2737 |
/// mapped to the value.
|
| 2735 |
2738 |
///
|
| 2736 |
2739 |
/// This map is intended to be used with small integer values, for which
|
| 2737 |
2740 |
/// it is efficient, and supports iteration only for non-negative values.
|
| 2738 |
2741 |
/// If you need large values and/or iteration for negative integers,
|
| 2739 |
2742 |
/// consider to use \ref IterableValueMap instead.
|
| 2740 |
2743 |
///
|
| 2741 |
2744 |
/// This type is a reference map, so it can be modified with the
|
| 2742 |
2745 |
/// subscript operator.
|
| 2743 |
2746 |
///
|
| 2744 |
2747 |
/// \note The size of the data structure depends on the largest
|
| 2745 |
2748 |
/// value in the map.
|
| 2746 |
2749 |
///
|
| 2747 |
2750 |
/// \tparam GR The graph type.
|
| 2748 |
2751 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
| 2749 |
2752 |
/// \c GR::Edge).
|
| 2750 |
2753 |
///
|
| 2751 |
2754 |
/// \see IterableBoolMap, IterableValueMap
|
| 2752 |
2755 |
/// \see CrossRefMap
|
| 2753 |
2756 |
template <typename GR, typename K>
|
| 2754 |
2757 |
class IterableIntMap
|
| 2755 |
2758 |
: protected ItemSetTraits<GR, K>::
|
| 2756 |
2759 |
template Map<_maps_bits::IterableIntMapNode<K> >::Type {
|
| 2757 |
2760 |
public:
|
| 2758 |
2761 |
typedef typename ItemSetTraits<GR, K>::
|
| 2759 |
2762 |
template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
|
| 2760 |
2763 |
|
| 2761 |
2764 |
/// The key type
|
| 2762 |
2765 |
typedef K Key;
|
| 2763 |
2766 |
/// The value type
|
| 2764 |
2767 |
typedef int Value;
|
| 2765 |
2768 |
/// The graph type
|
| 2766 |
2769 |
typedef GR Graph;
|
| 2767 |
2770 |
|
| 2768 |
2771 |
/// \brief Constructor of the map.
|
| 2769 |
2772 |
///
|
| 2770 |
2773 |
/// Constructor of the map. It sets all values to -1.
|
| 2771 |
2774 |
explicit IterableIntMap(const Graph& graph)
|
| 2772 |
2775 |
: Parent(graph) {}
|
| 2773 |
2776 |
|
| 2774 |
2777 |
/// \brief Constructor of the map with a given value.
|
| 2775 |
2778 |
///
|
| 2776 |
2779 |
/// Constructor of the map with a given value.
|
| 2777 |
2780 |
explicit IterableIntMap(const Graph& graph, int value)
|
| 2778 |
2781 |
: Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
|
| 2779 |
2782 |
if (value >= 0) {
|
| 2780 |
2783 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
| 2781 |
2784 |
lace(it);
|
| 2782 |
2785 |
}
|
| 2783 |
2786 |
}
|
| 2784 |
2787 |
}
|
| 2785 |
2788 |
|
| 2786 |
2789 |
private:
|
| 2787 |
2790 |
|
| 2788 |
2791 |
void unlace(const Key& key) {
|
| 2789 |
2792 |
typename Parent::Value& node = Parent::operator[](key);
|
| 2790 |
2793 |
if (node.value < 0) return;
|
| 2791 |
2794 |
if (node.prev != INVALID) {
|
| 2792 |
2795 |
Parent::operator[](node.prev).next = node.next;
|
| 2793 |
2796 |
} else {
|
| 2794 |
2797 |
_first[node.value] = node.next;
|
| 2795 |
2798 |
}
|
| 2796 |
2799 |
if (node.next != INVALID) {
|
| 2797 |
2800 |
Parent::operator[](node.next).prev = node.prev;
|
| 2798 |
2801 |
}
|
| 2799 |
2802 |
while (!_first.empty() && _first.back() == INVALID) {
|
| 2800 |
2803 |
_first.pop_back();
|
| 2801 |
2804 |
}
|
| 2802 |
2805 |
}
|
| 2803 |
2806 |
|
| 2804 |
2807 |
void lace(const Key& key) {
|
| 2805 |
2808 |
typename Parent::Value& node = Parent::operator[](key);
|
| 2806 |
2809 |
if (node.value < 0) return;
|
| 2807 |
2810 |
if (node.value >= int(_first.size())) {
|
| 2808 |
2811 |
_first.resize(node.value + 1, INVALID);
|
| 2809 |
2812 |
}
|
| 2810 |
2813 |
node.prev = INVALID;
|
| 2811 |
2814 |
node.next = _first[node.value];
|
| 2812 |
2815 |
if (node.next != INVALID) {
|
| 2813 |
2816 |
Parent::operator[](node.next).prev = key;
|
| 2814 |
2817 |
}
|
| 2815 |
2818 |
_first[node.value] = key;
|
| 2816 |
2819 |
}
|
| 2817 |
2820 |
|
| 2818 |
2821 |
public:
|
| 2819 |
2822 |
|
| 2820 |
2823 |
/// Indicates that the map is reference map.
|
| 2821 |
2824 |
typedef True ReferenceMapTag;
|
| 2822 |
2825 |
|
| 2823 |
2826 |
/// \brief Reference to the value of the map.
|
| 2824 |
2827 |
///
|
| 2825 |
2828 |
/// This class is similar to the \c int type. It can
|
| 2826 |
2829 |
/// be converted to \c int and it has the same operators.
|
| 2827 |
2830 |
class Reference {
|
| 2828 |
2831 |
friend class IterableIntMap;
|
| 2829 |
2832 |
private:
|
| 2830 |
2833 |
Reference(IterableIntMap& map, const Key& key)
|
| 2831 |
2834 |
: _key(key), _map(map) {}
|
| 2832 |
2835 |
public:
|
| 2833 |
2836 |
|
| 2834 |
2837 |
Reference& operator=(const Reference& value) {
|
| 2835 |
2838 |
_map.set(_key, static_cast<const int&>(value));
|
| 2836 |
2839 |
return *this;
|
| 2837 |
2840 |
}
|
| 2838 |
2841 |
|
| 2839 |
2842 |
operator const int&() const {
|
| 2840 |
2843 |
return static_cast<const IterableIntMap&>(_map)[_key];
|
| 2841 |
2844 |
}
|
| 2842 |
2845 |
|
| 2843 |
2846 |
Reference& operator=(int value) {
|
| 2844 |
2847 |
_map.set(_key, value);
|
| 2845 |
2848 |
return *this;
|
| 2846 |
2849 |
}
|
| 2847 |
2850 |
Reference& operator++() {
|
| 2848 |
2851 |
_map.set(_key, _map[_key] + 1);
|
| 2849 |
2852 |
return *this;
|
| 2850 |
2853 |
}
|
| 2851 |
2854 |
int operator++(int) {
|
| 2852 |
2855 |
int value = _map[_key];
|
| 2853 |
2856 |
_map.set(_key, value + 1);
|
| 2854 |
2857 |
return value;
|
| 2855 |
2858 |
}
|
| 2856 |
2859 |
Reference& operator--() {
|
| 2857 |
2860 |
_map.set(_key, _map[_key] - 1);
|
| 2858 |
2861 |
return *this;
|
| 2859 |
2862 |
}
|
| 2860 |
2863 |
int operator--(int) {
|
| 2861 |
2864 |
int value = _map[_key];
|
| 2862 |
2865 |
_map.set(_key, value - 1);
|
| 2863 |
2866 |
return value;
|
| 2864 |
2867 |
}
|
| 2865 |
2868 |
Reference& operator+=(int value) {
|
| 2866 |
2869 |
_map.set(_key, _map[_key] + value);
|
| 2867 |
2870 |
return *this;
|
| 2868 |
2871 |
}
|
| 2869 |
2872 |
Reference& operator-=(int value) {
|
| 2870 |
2873 |
_map.set(_key, _map[_key] - value);
|
| 2871 |
2874 |
return *this;
|
| 2872 |
2875 |
}
|
| 2873 |
2876 |
Reference& operator*=(int value) {
|
| 2874 |
2877 |
_map.set(_key, _map[_key] * value);
|
| 2875 |
2878 |
return *this;
|
| 2876 |
2879 |
}
|
| 2877 |
2880 |
Reference& operator/=(int value) {
|
| 2878 |
2881 |
_map.set(_key, _map[_key] / value);
|
| 2879 |
2882 |
return *this;
|
| 2880 |
2883 |
}
|
| 2881 |
2884 |
Reference& operator%=(int value) {
|
| 2882 |
2885 |
_map.set(_key, _map[_key] % value);
|
| 2883 |
2886 |
return *this;
|
| 2884 |
2887 |
}
|
| 2885 |
2888 |
Reference& operator&=(int value) {
|
| 2886 |
2889 |
_map.set(_key, _map[_key] & value);
|
| 2887 |
2890 |
return *this;
|
| 2888 |
2891 |
}
|
| 2889 |
2892 |
Reference& operator|=(int value) {
|
| 2890 |
2893 |
_map.set(_key, _map[_key] | value);
|
| 2891 |
2894 |
return *this;
|
| 2892 |
2895 |
}
|
| 2893 |
2896 |
Reference& operator^=(int value) {
|
| 2894 |
2897 |
_map.set(_key, _map[_key] ^ value);
|
| 2895 |
2898 |
return *this;
|
| 2896 |
2899 |
}
|
| 2897 |
2900 |
Reference& operator<<=(int value) {
|
| 2898 |
2901 |
_map.set(_key, _map[_key] << value);
|
| 2899 |
2902 |
return *this;
|
| 2900 |
2903 |
}
|
| 2901 |
2904 |
Reference& operator>>=(int value) {
|
| 2902 |
2905 |
_map.set(_key, _map[_key] >> value);
|
| 2903 |
2906 |
return *this;
|
| 2904 |
2907 |
}
|
| 2905 |
2908 |
|
| 2906 |
2909 |
private:
|
| 2907 |
2910 |
Key _key;
|
| 2908 |
2911 |
IterableIntMap& _map;
|
| 2909 |
2912 |
};
|
| 2910 |
2913 |
|
| 2911 |
2914 |
/// The const reference type.
|
| 2912 |
2915 |
typedef const Value& ConstReference;
|
| 2913 |
2916 |
|
| 2914 |
2917 |
/// \brief Gives back the maximal value plus one.
|
| 2915 |
2918 |
///
|
| 2916 |
2919 |
/// Gives back the maximal value plus one.
|
| 2917 |
2920 |
int size() const {
|
| 2918 |
2921 |
return _first.size();
|
| 2919 |
2922 |
}
|
| 2920 |
2923 |
|
| 2921 |
2924 |
/// \brief Set operation of the map.
|
| 2922 |
2925 |
///
|
| 2923 |
2926 |
/// Set operation of the map.
|
| 2924 |
2927 |
void set(const Key& key, const Value& value) {
|
| 2925 |
2928 |
unlace(key);
|
| 2926 |
2929 |
Parent::operator[](key).value = value;
|
| 2927 |
2930 |
lace(key);
|
| 2928 |
2931 |
}
|
| 2929 |
2932 |
|
| 2930 |
2933 |
/// \brief Const subscript operator of the map.
|
| 2931 |
2934 |
///
|
| 2932 |
2935 |
/// Const subscript operator of the map.
|
| 2933 |
2936 |
const Value& operator[](const Key& key) const {
|
| 2934 |
2937 |
return Parent::operator[](key).value;
|
| 2935 |
2938 |
}
|
| 2936 |
2939 |
|
| 2937 |
2940 |
/// \brief Subscript operator of the map.
|
| 2938 |
2941 |
///
|
| 2939 |
2942 |
/// Subscript operator of the map.
|
| 2940 |
2943 |
Reference operator[](const Key& key) {
|
| 2941 |
2944 |
return Reference(*this, key);
|
| 2942 |
2945 |
}
|
| 2943 |
2946 |
|
| 2944 |
2947 |
/// \brief Iterator for the keys with the same value.
|
| 2945 |
2948 |
///
|
| 2946 |
2949 |
/// Iterator for the keys with the same value. It works
|
| 2947 |
2950 |
/// like a graph item iterator, it can be converted to
|
| 2948 |
2951 |
/// the item type of the map, incremented with \c ++ operator, and
|
| 2949 |
2952 |
/// if the iterator leaves the last valid item, it will be equal to
|
| 2950 |
2953 |
/// \c INVALID.
|
| 2951 |
2954 |
class ItemIt : public Key {
|
| 2952 |
2955 |
public:
|
| 2953 |
2956 |
typedef Key Parent;
|
| 2954 |
2957 |
|
| 2955 |
2958 |
/// \brief Invalid constructor \& conversion.
|
| 2956 |
2959 |
///
|
| 2957 |
2960 |
/// This constructor initializes the iterator to be invalid.
|
| 2958 |
2961 |
/// \sa Invalid for more details.
|
| 2959 |
2962 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2960 |
2963 |
|
| 2961 |
2964 |
/// \brief Creates an iterator with a value.
|
| 2962 |
2965 |
///
|
| 2963 |
2966 |
/// Creates an iterator with a value. It iterates on the
|
| 2964 |
2967 |
/// keys mapped to the given value.
|
| 2965 |
2968 |
/// \param map The IterableIntMap.
|
| 2966 |
2969 |
/// \param value The value.
|
| 2967 |
2970 |
ItemIt(const IterableIntMap& map, int value) : _map(&map) {
|
| 2968 |
2971 |
if (value < 0 || value >= int(_map->_first.size())) {
|
| 2969 |
2972 |
Parent::operator=(INVALID);
|
| 2970 |
2973 |
} else {
|
| 2971 |
2974 |
Parent::operator=(_map->_first[value]);
|
| 2972 |
2975 |
}
|
| 2973 |
2976 |
}
|
| 2974 |
2977 |
|
| 2975 |
2978 |
/// \brief Increment operator.
|
| 2976 |
2979 |
///
|
| 2977 |
2980 |
/// Increment operator.
|
| 2978 |
2981 |
ItemIt& operator++() {
|
| 2979 |
2982 |
Parent::operator=(_map->IterableIntMap::Parent::
|
| 2980 |
2983 |
operator[](static_cast<Parent&>(*this)).next);
|
| 2981 |
2984 |
return *this;
|
| 2982 |
2985 |
}
|
| 2983 |
2986 |
|
| 2984 |
2987 |
private:
|
| 2985 |
2988 |
const IterableIntMap* _map;
|
| 2986 |
2989 |
};
|
| 2987 |
2990 |
|
| 2988 |
2991 |
protected:
|
| 2989 |
2992 |
|
| 2990 |
2993 |
virtual void erase(const Key& key) {
|
| 2991 |
2994 |
unlace(key);
|
| 2992 |
2995 |
Parent::erase(key);
|
| 2993 |
2996 |
}
|
| 2994 |
2997 |
|
| 2995 |
2998 |
virtual void erase(const std::vector<Key>& keys) {
|
| 2996 |
2999 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2997 |
3000 |
unlace(keys[i]);
|
| 2998 |
3001 |
}
|
| 2999 |
3002 |
Parent::erase(keys);
|
| 3000 |
3003 |
}
|
| 3001 |
3004 |
|
| 3002 |
3005 |
virtual void clear() {
|
| 3003 |
3006 |
_first.clear();
|
| 3004 |
3007 |
Parent::clear();
|
| 3005 |
3008 |
}
|
| 3006 |
3009 |
|
| 3007 |
3010 |
private:
|
| 3008 |
3011 |
std::vector<Key> _first;
|
| 3009 |
3012 |
};
|
| 3010 |
3013 |
|
| 3011 |
3014 |
namespace _maps_bits {
|
| 3012 |
3015 |
template <typename Item, typename Value>
|
| 3013 |
3016 |
struct IterableValueMapNode {
|
| 3014 |
3017 |
IterableValueMapNode(Value _value = Value()) : value(_value) {}
|
| 3015 |
3018 |
Item prev, next;
|
| 3016 |
3019 |
Value value;
|
| 3017 |
3020 |
};
|
| 3018 |
3021 |
}
|
| 3019 |
3022 |
|
| 3020 |
3023 |
/// \brief Dynamic iterable map for comparable values.
|
| 3021 |
3024 |
///
|
| 3022 |
3025 |
/// This class provides a special graph map type which can store a
|
| 3023 |
3026 |
/// comparable value for graph items (\c Node, \c Arc or \c Edge).
|
| 3024 |
3027 |
/// For each value it is possible to iterate on the keys mapped to
|
| 3025 |
3028 |
/// the value (\c ItemIt), and the values of the map can be accessed
|
| 3026 |
|
/// with an STL compatible forward iterator (\c ValueIterator).
|
|
3029 |
/// with an STL compatible forward iterator (\c ValueIt).
|
| 3027 |
3030 |
/// The map stores a linked list for each value, which contains
|
| 3028 |
3031 |
/// the items mapped to the value, and the used values are stored
|
| 3029 |
3032 |
/// in balanced binary tree (\c std::map).
|
| 3030 |
3033 |
///
|
| 3031 |
3034 |
/// \ref IterableBoolMap and \ref IterableIntMap are similar classes
|
| 3032 |
3035 |
/// specialized for \c bool and \c int values, respectively.
|
| 3033 |
3036 |
///
|
| 3034 |
3037 |
/// This type is not reference map, so it cannot be modified with
|
| 3035 |
3038 |
/// the subscript operator.
|
| 3036 |
3039 |
///
|
| 3037 |
3040 |
/// \tparam GR The graph type.
|
| 3038 |
3041 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
| 3039 |
3042 |
/// \c GR::Edge).
|
| 3040 |
3043 |
/// \tparam V The value type of the map. It can be any comparable
|
| 3041 |
3044 |
/// value type.
|
| 3042 |
3045 |
///
|
| 3043 |
3046 |
/// \see IterableBoolMap, IterableIntMap
|
| 3044 |
3047 |
/// \see CrossRefMap
|
| 3045 |
3048 |
template <typename GR, typename K, typename V>
|
| 3046 |
3049 |
class IterableValueMap
|
| 3047 |
3050 |
: protected ItemSetTraits<GR, K>::
|
| 3048 |
3051 |
template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
|
| 3049 |
3052 |
public:
|
| 3050 |
3053 |
typedef typename ItemSetTraits<GR, K>::
|
| 3051 |
3054 |
template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
|
| 3052 |
3055 |
|
| 3053 |
3056 |
/// The key type
|
| 3054 |
3057 |
typedef K Key;
|
| 3055 |
3058 |
/// The value type
|
| 3056 |
3059 |
typedef V Value;
|
| 3057 |
3060 |
/// The graph type
|
| 3058 |
3061 |
typedef GR Graph;
|
| 3059 |
3062 |
|
| 3060 |
3063 |
public:
|
| 3061 |
3064 |
|
| 3062 |
3065 |
/// \brief Constructor of the map with a given value.
|
| 3063 |
3066 |
///
|
| 3064 |
3067 |
/// Constructor of the map with a given value.
|
| 3065 |
3068 |
explicit IterableValueMap(const Graph& graph,
|
| 3066 |
3069 |
const Value& value = Value())
|
| 3067 |
3070 |
: Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
|
| 3068 |
3071 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
| 3069 |
3072 |
lace(it);
|
| 3070 |
3073 |
}
|
| 3071 |
3074 |
}
|
| 3072 |
3075 |
|
| 3073 |
3076 |
protected:
|
| 3074 |
3077 |
|
| 3075 |
3078 |
void unlace(const Key& key) {
|
| 3076 |
3079 |
typename Parent::Value& node = Parent::operator[](key);
|
| 3077 |
3080 |
if (node.prev != INVALID) {
|
| 3078 |
3081 |
Parent::operator[](node.prev).next = node.next;
|
| 3079 |
3082 |
} else {
|
| 3080 |
3083 |
if (node.next != INVALID) {
|
| 3081 |
3084 |
_first[node.value] = node.next;
|
| 3082 |
3085 |
} else {
|
| 3083 |
3086 |
_first.erase(node.value);
|
| 3084 |
3087 |
}
|
| 3085 |
3088 |
}
|
| 3086 |
3089 |
if (node.next != INVALID) {
|
| 3087 |
3090 |
Parent::operator[](node.next).prev = node.prev;
|
| 3088 |
3091 |
}
|
| 3089 |
3092 |
}
|
| 3090 |
3093 |
|
| 3091 |
3094 |
void lace(const Key& key) {
|
| 3092 |
3095 |
typename Parent::Value& node = Parent::operator[](key);
|
| 3093 |
3096 |
typename std::map<Value, Key>::iterator it = _first.find(node.value);
|
| 3094 |
3097 |
if (it == _first.end()) {
|
| 3095 |
3098 |
node.prev = node.next = INVALID;
|
| 3096 |
3099 |
_first.insert(std::make_pair(node.value, key));
|
| 3097 |
3100 |
} else {
|
| 3098 |
3101 |
node.prev = INVALID;
|
| 3099 |
3102 |
node.next = it->second;
|
| 3100 |
3103 |
if (node.next != INVALID) {
|
| 3101 |
3104 |
Parent::operator[](node.next).prev = key;
|
| 3102 |
3105 |
}
|
| 3103 |
3106 |
it->second = key;
|
| 3104 |
3107 |
}
|
| 3105 |
3108 |
}
|
| 3106 |
3109 |
|
| 3107 |
3110 |
public:
|
| 3108 |
3111 |
|
| 3109 |
3112 |
/// \brief Forward iterator for values.
|
| 3110 |
3113 |
///
|
| 3111 |
3114 |
/// This iterator is an STL compatible forward
|
| 3112 |
3115 |
/// iterator on the values of the map. The values can
|
| 3113 |
3116 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range.
|
| 3114 |
|
class ValueIterator
|
|
3117 |
class ValueIt
|
| 3115 |
3118 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
| 3116 |
3119 |
friend class IterableValueMap;
|
| 3117 |
3120 |
private:
|
| 3118 |
|
ValueIterator(typename std::map<Value, Key>::const_iterator _it)
|
|
3121 |
ValueIt(typename std::map<Value, Key>::const_iterator _it)
|
| 3119 |
3122 |
: it(_it) {}
|
| 3120 |
3123 |
public:
|
| 3121 |
3124 |
|
| 3122 |
3125 |
/// Constructor
|
| 3123 |
|
ValueIterator() {}
|
|
3126 |
ValueIt() {}
|
| 3124 |
3127 |
|
| 3125 |
3128 |
/// \e
|
| 3126 |
|
ValueIterator& operator++() { ++it; return *this; }
|
|
3129 |
ValueIt& operator++() { ++it; return *this; }
|
| 3127 |
3130 |
/// \e
|
| 3128 |
|
ValueIterator operator++(int) {
|
| 3129 |
|
ValueIterator tmp(*this);
|
|
3131 |
ValueIt operator++(int) {
|
|
3132 |
ValueIt tmp(*this);
|
| 3130 |
3133 |
operator++();
|
| 3131 |
3134 |
return tmp;
|
| 3132 |
3135 |
}
|
| 3133 |
3136 |
|
| 3134 |
3137 |
/// \e
|
| 3135 |
3138 |
const Value& operator*() const { return it->first; }
|
| 3136 |
3139 |
/// \e
|
| 3137 |
3140 |
const Value* operator->() const { return &(it->first); }
|
| 3138 |
3141 |
|
| 3139 |
3142 |
/// \e
|
| 3140 |
|
bool operator==(ValueIterator jt) const { return it == jt.it; }
|
|
3143 |
bool operator==(ValueIt jt) const { return it == jt.it; }
|
| 3141 |
3144 |
/// \e
|
| 3142 |
|
bool operator!=(ValueIterator jt) const { return it != jt.it; }
|
|
3145 |
bool operator!=(ValueIt jt) const { return it != jt.it; }
|
| 3143 |
3146 |
|
| 3144 |
3147 |
private:
|
| 3145 |
3148 |
typename std::map<Value, Key>::const_iterator it;
|
| 3146 |
3149 |
};
|
| 3147 |
3150 |
|
| 3148 |
3151 |
/// \brief Returns an iterator to the first value.
|
| 3149 |
3152 |
///
|
| 3150 |
3153 |
/// Returns an STL compatible iterator to the
|
| 3151 |
3154 |
/// first value of the map. The values of the
|
| 3152 |
3155 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
|
| 3153 |
3156 |
/// range.
|
| 3154 |
|
ValueIterator beginValue() const {
|
| 3155 |
|
return ValueIterator(_first.begin());
|
|
3157 |
ValueIt beginValue() const {
|
|
3158 |
return ValueIt(_first.begin());
|
| 3156 |
3159 |
}
|
| 3157 |
3160 |
|
| 3158 |
3161 |
/// \brief Returns an iterator after the last value.
|
| 3159 |
3162 |
///
|
| 3160 |
3163 |
/// Returns an STL compatible iterator after the
|
| 3161 |
3164 |
/// last value of the map. The values of the
|
| 3162 |
3165 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
|
| 3163 |
3166 |
/// range.
|
| 3164 |
|
ValueIterator endValue() const {
|
| 3165 |
|
return ValueIterator(_first.end());
|
|
3167 |
ValueIt endValue() const {
|
|
3168 |
return ValueIt(_first.end());
|
| 3166 |
3169 |
}
|
| 3167 |
3170 |
|
| 3168 |
3171 |
/// \brief Set operation of the map.
|
| 3169 |
3172 |
///
|
| 3170 |
3173 |
/// Set operation of the map.
|
| 3171 |
3174 |
void set(const Key& key, const Value& value) {
|
| 3172 |
3175 |
unlace(key);
|
| 3173 |
3176 |
Parent::operator[](key).value = value;
|
| 3174 |
3177 |
lace(key);
|
| 3175 |
3178 |
}
|
| 3176 |
3179 |
|
| 3177 |
3180 |
/// \brief Const subscript operator of the map.
|
| 3178 |
3181 |
///
|
| 3179 |
3182 |
/// Const subscript operator of the map.
|
| 3180 |
3183 |
const Value& operator[](const Key& key) const {
|
| 3181 |
3184 |
return Parent::operator[](key).value;
|
| 3182 |
3185 |
}
|
| 3183 |
3186 |
|
| 3184 |
3187 |
/// \brief Iterator for the keys with the same value.
|
| 3185 |
3188 |
///
|
| 3186 |
3189 |
/// Iterator for the keys with the same value. It works
|
| 3187 |
3190 |
/// like a graph item iterator, it can be converted to
|
| 3188 |
3191 |
/// the item type of the map, incremented with \c ++ operator, and
|
| 3189 |
3192 |
/// if the iterator leaves the last valid item, it will be equal to
|
| 3190 |
3193 |
/// \c INVALID.
|
| 3191 |
3194 |
class ItemIt : public Key {
|
| 3192 |
3195 |
public:
|
| 3193 |
3196 |
typedef Key Parent;
|
| 3194 |
3197 |
|
| 3195 |
3198 |
/// \brief Invalid constructor \& conversion.
|
| 3196 |
3199 |
///
|
| 3197 |
3200 |
/// This constructor initializes the iterator to be invalid.
|
| 3198 |
3201 |
/// \sa Invalid for more details.
|
| 3199 |
3202 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 3200 |
3203 |
|
| 3201 |
3204 |
/// \brief Creates an iterator with a value.
|
| 3202 |
3205 |
///
|
| 3203 |
3206 |
/// Creates an iterator with a value. It iterates on the
|
| 3204 |
3207 |
/// keys which have the given value.
|
| 3205 |
3208 |
/// \param map The IterableValueMap
|
| 3206 |
3209 |
/// \param value The value
|
| 3207 |
3210 |
ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
|
| 3208 |
3211 |
typename std::map<Value, Key>::const_iterator it =
|
| 3209 |
3212 |
map._first.find(value);
|
| 3210 |
3213 |
if (it == map._first.end()) {
|
| 3211 |
3214 |
Parent::operator=(INVALID);
|
| 3212 |
3215 |
} else {
|
| 3213 |
3216 |
Parent::operator=(it->second);
|
| 3214 |
3217 |
}
|
| 3215 |
3218 |
}
|
| 3216 |
3219 |
|
| 3217 |
3220 |
/// \brief Increment operator.
|
| 3218 |
3221 |
///
|
| 3219 |
3222 |
/// Increment Operator.
|
| 3220 |
3223 |
ItemIt& operator++() {
|
| 3221 |
3224 |
Parent::operator=(_map->IterableValueMap::Parent::
|
| 3222 |
3225 |
operator[](static_cast<Parent&>(*this)).next);
|
| 3223 |
3226 |
return *this;
|
| 3224 |
3227 |
}
|
| 3225 |
3228 |
|
| 3226 |
3229 |
|
| 3227 |
3230 |
private:
|
| 3228 |
3231 |
const IterableValueMap* _map;
|
| 3229 |
3232 |
};
|
| 3230 |
3233 |
|
| 3231 |
3234 |
protected:
|
| 3232 |
3235 |
|
| 3233 |
3236 |
virtual void add(const Key& key) {
|
| 3234 |
3237 |
Parent::add(key);
|
| 3235 |
3238 |
unlace(key);
|
| 3236 |
3239 |
}
|
| 3237 |
3240 |
|
| 3238 |
3241 |
virtual void add(const std::vector<Key>& keys) {
|
| 3239 |
3242 |
Parent::add(keys);
|
| 3240 |
3243 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 3241 |
3244 |
lace(keys[i]);
|
| 3242 |
3245 |
}
|
| 3243 |
3246 |
}
|
| 3244 |
3247 |
|
| 3245 |
3248 |
virtual void erase(const Key& key) {
|
| 3246 |
3249 |
unlace(key);
|
| 3247 |
3250 |
Parent::erase(key);
|
| 3248 |
3251 |
}
|
| 3249 |
3252 |
|
| 3250 |
3253 |
virtual void erase(const std::vector<Key>& keys) {
|
| 3251 |
3254 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 3252 |
3255 |
unlace(keys[i]);
|
| 3253 |
3256 |
}
|
| 3254 |
3257 |
Parent::erase(keys);
|
| 3255 |
3258 |
}
|
| 3256 |
3259 |
|
| 3257 |
3260 |
virtual void build() {
|
| 3258 |
3261 |
Parent::build();
|
| 3259 |
3262 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
| 3260 |
3263 |
lace(it);
|
| 3261 |
3264 |
}
|
| 3262 |
3265 |
}
|
| 3263 |
3266 |
|
| 3264 |
3267 |
virtual void clear() {
|
| 3265 |
3268 |
_first.clear();
|
| 3266 |
3269 |
Parent::clear();
|
| 3267 |
3270 |
}
|
| 3268 |
3271 |
|
| 3269 |
3272 |
private:
|
| 3270 |
3273 |
std::map<Value, Key> _first;
|
| 3271 |
3274 |
};
|
| 3272 |
3275 |
|
| 3273 |
3276 |
/// \brief Map of the source nodes of arcs in a digraph.
|
| 3274 |
3277 |
///
|
| 3275 |
3278 |
/// SourceMap provides access for the source node of each arc in a digraph,
|
| 3276 |
3279 |
/// which is returned by the \c source() function of the digraph.
|
| 3277 |
3280 |
/// \tparam GR The digraph type.
|
| 3278 |
3281 |
/// \see TargetMap
|
| 3279 |
3282 |
template <typename GR>
|
| 3280 |
3283 |
class SourceMap {
|
| 3281 |
3284 |
public:
|
| 3282 |
3285 |
|
| 3283 |
|
///\e
|
|
3286 |
/// The key type (the \c Arc type of the digraph).
|
| 3284 |
3287 |
typedef typename GR::Arc Key;
|
| 3285 |
|
///\e
|
|
3288 |
/// The value type (the \c Node type of the digraph).
|
| 3286 |
3289 |
typedef typename GR::Node Value;
|
| 3287 |
3290 |
|
| 3288 |
3291 |
/// \brief Constructor
|
| 3289 |
3292 |
///
|
| 3290 |
3293 |
/// Constructor.
|
| 3291 |
3294 |
/// \param digraph The digraph that the map belongs to.
|
| 3292 |
3295 |
explicit SourceMap(const GR& digraph) : _graph(digraph) {}
|
| 3293 |
3296 |
|
| 3294 |
3297 |
/// \brief Returns the source node of the given arc.
|
| 3295 |
3298 |
///
|
| 3296 |
3299 |
/// Returns the source node of the given arc.
|
| 3297 |
3300 |
Value operator[](const Key& arc) const {
|
| 3298 |
3301 |
return _graph.source(arc);
|
| 3299 |
3302 |
}
|
| 3300 |
3303 |
|
| 3301 |
3304 |
private:
|
| 3302 |
3305 |
const GR& _graph;
|
| 3303 |
3306 |
};
|
| 3304 |
3307 |
|
| 3305 |
3308 |
/// \brief Returns a \c SourceMap class.
|
| 3306 |
3309 |
///
|
| 3307 |
3310 |
/// This function just returns an \c SourceMap class.
|
| 3308 |
3311 |
/// \relates SourceMap
|
| 3309 |
3312 |
template <typename GR>
|
| 3310 |
3313 |
inline SourceMap<GR> sourceMap(const GR& graph) {
|
| 3311 |
3314 |
return SourceMap<GR>(graph);
|
| 3312 |
3315 |
}
|
| 3313 |
3316 |
|
| 3314 |
3317 |
/// \brief Map of the target nodes of arcs in a digraph.
|
| 3315 |
3318 |
///
|
| 3316 |
3319 |
/// TargetMap provides access for the target node of each arc in a digraph,
|
| 3317 |
3320 |
/// which is returned by the \c target() function of the digraph.
|
| 3318 |
3321 |
/// \tparam GR The digraph type.
|
| 3319 |
3322 |
/// \see SourceMap
|
| 3320 |
3323 |
template <typename GR>
|
| 3321 |
3324 |
class TargetMap {
|
| 3322 |
3325 |
public:
|
| 3323 |
3326 |
|
| 3324 |
|
///\e
|
|
3327 |
/// The key type (the \c Arc type of the digraph).
|
| 3325 |
3328 |
typedef typename GR::Arc Key;
|
| 3326 |
|
///\e
|
|
3329 |
/// The value type (the \c Node type of the digraph).
|
| 3327 |
3330 |
typedef typename GR::Node Value;
|
| 3328 |
3331 |
|
| 3329 |
3332 |
/// \brief Constructor
|
| 3330 |
3333 |
///
|
| 3331 |
3334 |
/// Constructor.
|
| 3332 |
3335 |
/// \param digraph The digraph that the map belongs to.
|
| 3333 |
3336 |
explicit TargetMap(const GR& digraph) : _graph(digraph) {}
|
| 3334 |
3337 |
|
| 3335 |
3338 |
/// \brief Returns the target node of the given arc.
|
| 3336 |
3339 |
///
|
| 3337 |
3340 |
/// Returns the target node of the given arc.
|
| 3338 |
3341 |
Value operator[](const Key& e) const {
|
| 3339 |
3342 |
return _graph.target(e);
|
| 3340 |
3343 |
}
|
| 3341 |
3344 |
|
| 3342 |
3345 |
private:
|
| 3343 |
3346 |
const GR& _graph;
|
| 3344 |
3347 |
};
|
| 3345 |
3348 |
|
| 3346 |
3349 |
/// \brief Returns a \c TargetMap class.
|
| 3347 |
3350 |
///
|
| 3348 |
3351 |
/// This function just returns a \c TargetMap class.
|
| 3349 |
3352 |
/// \relates TargetMap
|
| 3350 |
3353 |
template <typename GR>
|
| 3351 |
3354 |
inline TargetMap<GR> targetMap(const GR& graph) {
|
| 3352 |
3355 |
return TargetMap<GR>(graph);
|
| 3353 |
3356 |
}
|
| 3354 |
3357 |
|
| 3355 |
3358 |
/// \brief Map of the "forward" directed arc view of edges in a graph.
|
| 3356 |
3359 |
///
|
| 3357 |
3360 |
/// ForwardMap provides access for the "forward" directed arc view of
|
| 3358 |
3361 |
/// each edge in a graph, which is returned by the \c direct() function
|
| 3359 |
3362 |
/// of the graph with \c true parameter.
|
| 3360 |
3363 |
/// \tparam GR The graph type.
|
| 3361 |
3364 |
/// \see BackwardMap
|
| 3362 |
3365 |
template <typename GR>
|
| 3363 |
3366 |
class ForwardMap {
|
| 3364 |
3367 |
public:
|
| 3365 |
3368 |
|
|
3369 |
/// The key type (the \c Edge type of the digraph).
|
|
3370 |
typedef typename GR::Edge Key;
|
|
3371 |
/// The value type (the \c Arc type of the digraph).
|
| 3366 |
3372 |
typedef typename GR::Arc Value;
|
| 3367 |
|
typedef typename GR::Edge Key;
|
| 3368 |
3373 |
|
| 3369 |
3374 |
/// \brief Constructor
|
| 3370 |
3375 |
///
|
| 3371 |
3376 |
/// Constructor.
|
| 3372 |
3377 |
/// \param graph The graph that the map belongs to.
|
| 3373 |
3378 |
explicit ForwardMap(const GR& graph) : _graph(graph) {}
|
| 3374 |
3379 |
|
| 3375 |
3380 |
/// \brief Returns the "forward" directed arc view of the given edge.
|
| 3376 |
3381 |
///
|
| 3377 |
3382 |
/// Returns the "forward" directed arc view of the given edge.
|
| 3378 |
3383 |
Value operator[](const Key& key) const {
|
| 3379 |
3384 |
return _graph.direct(key, true);
|
| 3380 |
3385 |
}
|
| 3381 |
3386 |
|
| 3382 |
3387 |
private:
|
| 3383 |
3388 |
const GR& _graph;
|
| 3384 |
3389 |
};
|
| 3385 |
3390 |
|
| 3386 |
3391 |
/// \brief Returns a \c ForwardMap class.
|
| 3387 |
3392 |
///
|
| 3388 |
3393 |
/// This function just returns an \c ForwardMap class.
|
| 3389 |
3394 |
/// \relates ForwardMap
|
| 3390 |
3395 |
template <typename GR>
|
| 3391 |
3396 |
inline ForwardMap<GR> forwardMap(const GR& graph) {
|
| 3392 |
3397 |
return ForwardMap<GR>(graph);
|
| 3393 |
3398 |
}
|
| 3394 |
3399 |
|
| 3395 |
3400 |
/// \brief Map of the "backward" directed arc view of edges in a graph.
|
| 3396 |
3401 |
///
|
| 3397 |
3402 |
/// BackwardMap provides access for the "backward" directed arc view of
|
| 3398 |
3403 |
/// each edge in a graph, which is returned by the \c direct() function
|
| 3399 |
3404 |
/// of the graph with \c false parameter.
|
| 3400 |
3405 |
/// \tparam GR The graph type.
|
| 3401 |
3406 |
/// \see ForwardMap
|
| 3402 |
3407 |
template <typename GR>
|
| 3403 |
3408 |
class BackwardMap {
|
| 3404 |
3409 |
public:
|
| 3405 |
3410 |
|
|
3411 |
/// The key type (the \c Edge type of the digraph).
|
|
3412 |
typedef typename GR::Edge Key;
|
|
3413 |
/// The value type (the \c Arc type of the digraph).
|
| 3406 |
3414 |
typedef typename GR::Arc Value;
|
| 3407 |
|
typedef typename GR::Edge Key;
|
| 3408 |
3415 |
|
| 3409 |
3416 |
/// \brief Constructor
|
| 3410 |
3417 |
///
|
| 3411 |
3418 |
/// Constructor.
|
| 3412 |
3419 |
/// \param graph The graph that the map belongs to.
|
| 3413 |
3420 |
explicit BackwardMap(const GR& graph) : _graph(graph) {}
|
| 3414 |
3421 |
|
| 3415 |
3422 |
/// \brief Returns the "backward" directed arc view of the given edge.
|
| 3416 |
3423 |
///
|
| 3417 |
3424 |
/// Returns the "backward" directed arc view of the given edge.
|
| 3418 |
3425 |
Value operator[](const Key& key) const {
|
| 3419 |
3426 |
return _graph.direct(key, false);
|
| 3420 |
3427 |
}
|
| 3421 |
3428 |
|
| 3422 |
3429 |
private:
|
| 3423 |
3430 |
const GR& _graph;
|
| 3424 |
3431 |
};
|
| 3425 |
3432 |
|
| 3426 |
3433 |
/// \brief Returns a \c BackwardMap class
|
| 3427 |
3434 |
|
| 3428 |
3435 |
/// This function just returns a \c BackwardMap class.
|
| 3429 |
3436 |
/// \relates BackwardMap
|
| 3430 |
3437 |
template <typename GR>
|
| 3431 |
3438 |
inline BackwardMap<GR> backwardMap(const GR& graph) {
|
| 3432 |
3439 |
return BackwardMap<GR>(graph);
|
| 3433 |
3440 |
}
|
| 3434 |
3441 |
|
| 3435 |
3442 |
/// \brief Map of the in-degrees of nodes in a digraph.
|
| 3436 |
3443 |
///
|
| 3437 |
3444 |
/// This map returns the in-degree of a node. Once it is constructed,
|
| 3438 |
3445 |
/// the degrees are stored in a standard \c NodeMap, so each query is done
|
| 3439 |
3446 |
/// in constant time. On the other hand, the values are updated automatically
|
| 3440 |
3447 |
/// whenever the digraph changes.
|
| 3441 |
3448 |
///
|
| 3442 |
3449 |
/// \warning Besides \c addNode() and \c addArc(), a digraph structure
|
| 3443 |
3450 |
/// may provide alternative ways to modify the digraph.
|
| 3444 |
3451 |
/// The correct behavior of InDegMap is not guarantied if these additional
|
| 3445 |
3452 |
/// features are used. For example the functions
|
| 3446 |
3453 |
/// \ref ListDigraph::changeSource() "changeSource()",
|
| 3447 |
3454 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and
|
| 3448 |
3455 |
/// \ref ListDigraph::reverseArc() "reverseArc()"
|
| 3449 |
3456 |
/// of \ref ListDigraph will \e not update the degree values correctly.
|
| 3450 |
3457 |
///
|
| 3451 |
3458 |
/// \sa OutDegMap
|
| 3452 |
3459 |
template <typename GR>
|
| 3453 |
3460 |
class InDegMap
|
| 3454 |
3461 |
: protected ItemSetTraits<GR, typename GR::Arc>
|
| 3455 |
3462 |
::ItemNotifier::ObserverBase {
|
| 3456 |
3463 |
|
| 3457 |
3464 |
public:
|
| 3458 |
3465 |
|
| 3459 |
3466 |
/// The graph type of InDegMap
|
| 3460 |
3467 |
typedef GR Graph;
|
| 3461 |
3468 |
typedef GR Digraph;
|
| 3462 |
3469 |
/// The key type
|
| 3463 |
3470 |
typedef typename Digraph::Node Key;
|
| 3464 |
3471 |
/// The value type
|
| 3465 |
3472 |
typedef int Value;
|
| 3466 |
3473 |
|
| 3467 |
3474 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
|
| 3468 |
3475 |
::ItemNotifier::ObserverBase Parent;
|
| 3469 |
3476 |
|
| 3470 |
3477 |
private:
|
| 3471 |
3478 |
|
| 3472 |
3479 |
class AutoNodeMap
|
| 3473 |
3480 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
|
| 3474 |
3481 |
public:
|
| 3475 |
3482 |
|
| 3476 |
3483 |
typedef typename ItemSetTraits<Digraph, Key>::
|
| 3477 |
3484 |
template Map<int>::Type Parent;
|
| 3478 |
3485 |
|
| 3479 |
3486 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
| 3480 |
3487 |
|
| 3481 |
3488 |
virtual void add(const Key& key) {
|
| 3482 |
3489 |
Parent::add(key);
|
| 3483 |
3490 |
Parent::set(key, 0);
|
| 3484 |
3491 |
}
|
| 3485 |
3492 |
|
| 3486 |
3493 |
virtual void add(const std::vector<Key>& keys) {
|
| 3487 |
3494 |
Parent::add(keys);
|
| 3488 |
3495 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 3489 |
3496 |
Parent::set(keys[i], 0);
|
| 3490 |
3497 |
}
|
| 3491 |
3498 |
}
|
| 3492 |
3499 |
|
| 3493 |
3500 |
virtual void build() {
|
| 3494 |
3501 |
Parent::build();
|
| 3495 |
3502 |
Key it;
|
| 3496 |
3503 |
typename Parent::Notifier* nf = Parent::notifier();
|
| 3497 |
3504 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 3498 |
3505 |
Parent::set(it, 0);
|
| 3499 |
3506 |
}
|
| 3500 |
3507 |
}
|
| 3501 |
3508 |
};
|
| 3502 |
3509 |
|
| 3503 |
3510 |
public:
|
| 3504 |
3511 |
|
| 3505 |
3512 |
/// \brief Constructor.
|
| 3506 |
3513 |
///
|
| 3507 |
3514 |
/// Constructor for creating an in-degree map.
|
| 3508 |
3515 |
explicit InDegMap(const Digraph& graph)
|
| 3509 |
3516 |
: _digraph(graph), _deg(graph) {
|
| 3510 |
3517 |
Parent::attach(_digraph.notifier(typename Digraph::Arc()));
|
| 3511 |
3518 |
|
| 3512 |
3519 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 3513 |
3520 |
_deg[it] = countInArcs(_digraph, it);
|
| 3514 |
3521 |
}
|
| 3515 |
3522 |
}
|
| 3516 |
3523 |
|
| 3517 |
3524 |
/// \brief Gives back the in-degree of a Node.
|
| 3518 |
3525 |
///
|
| 3519 |
3526 |
/// Gives back the in-degree of a Node.
|
| 3520 |
3527 |
int operator[](const Key& key) const {
|
| 3521 |
3528 |
return _deg[key];
|
| 3522 |
3529 |
}
|
| 3523 |
3530 |
|
| 3524 |
3531 |
protected:
|
| 3525 |
3532 |
|
| 3526 |
3533 |
typedef typename Digraph::Arc Arc;
|
| 3527 |
3534 |
|
| 3528 |
3535 |
virtual void add(const Arc& arc) {
|
| 3529 |
3536 |
++_deg[_digraph.target(arc)];
|
| 3530 |
3537 |
}
|
| 3531 |
3538 |
|
| 3532 |
3539 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 3533 |
3540 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 3534 |
3541 |
++_deg[_digraph.target(arcs[i])];
|
| 3535 |
3542 |
}
|
| 3536 |
3543 |
}
|
| 3537 |
3544 |
|
| 3538 |
3545 |
virtual void erase(const Arc& arc) {
|
| 3539 |
3546 |
--_deg[_digraph.target(arc)];
|
| 3540 |
3547 |
}
|
| 3541 |
3548 |
|
| 3542 |
3549 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 3543 |
3550 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 3544 |
3551 |
--_deg[_digraph.target(arcs[i])];
|
| 3545 |
3552 |
}
|
| 3546 |
3553 |
}
|
| 3547 |
3554 |
|
| 3548 |
3555 |
virtual void build() {
|
| 3549 |
3556 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 3550 |
3557 |
_deg[it] = countInArcs(_digraph, it);
|
| 3551 |
3558 |
}
|
| 3552 |
3559 |
}
|
| 3553 |
3560 |
|
| 3554 |
3561 |
virtual void clear() {
|
| 3555 |
3562 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 3556 |
3563 |
_deg[it] = 0;
|
| 3557 |
3564 |
}
|
| 3558 |
3565 |
}
|
| 3559 |
3566 |
private:
|
| 3560 |
3567 |
|
| 3561 |
3568 |
const Digraph& _digraph;
|
| 3562 |
3569 |
AutoNodeMap _deg;
|
| 3563 |
3570 |
};
|
| 3564 |
3571 |
|
| 3565 |
3572 |
/// \brief Map of the out-degrees of nodes in a digraph.
|
| 3566 |
3573 |
///
|
| 3567 |
3574 |
/// This map returns the out-degree of a node. Once it is constructed,
|
| 3568 |
3575 |
/// the degrees are stored in a standard \c NodeMap, so each query is done
|
| 3569 |
3576 |
/// in constant time. On the other hand, the values are updated automatically
|
| 3570 |
3577 |
/// whenever the digraph changes.
|
| 3571 |
3578 |
///
|
| 3572 |
3579 |
/// \warning Besides \c addNode() and \c addArc(), a digraph structure
|
| 3573 |
3580 |
/// may provide alternative ways to modify the digraph.
|
| 3574 |
3581 |
/// The correct behavior of OutDegMap is not guarantied if these additional
|
| 3575 |
3582 |
/// features are used. For example the functions
|
| 3576 |
3583 |
/// \ref ListDigraph::changeSource() "changeSource()",
|
| 3577 |
3584 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and
|
| 3578 |
3585 |
/// \ref ListDigraph::reverseArc() "reverseArc()"
|
| 3579 |
3586 |
/// of \ref ListDigraph will \e not update the degree values correctly.
|
| 3580 |
3587 |
///
|
| 3581 |
3588 |
/// \sa InDegMap
|
| 3582 |
3589 |
template <typename GR>
|
| 3583 |
3590 |
class OutDegMap
|
| 3584 |
3591 |
: protected ItemSetTraits<GR, typename GR::Arc>
|
| 3585 |
3592 |
::ItemNotifier::ObserverBase {
|
| 3586 |
3593 |
|
| 3587 |
3594 |
public:
|
| 3588 |
3595 |
|
| 3589 |
3596 |
/// The graph type of OutDegMap
|
| 3590 |
3597 |
typedef GR Graph;
|
| 3591 |
3598 |
typedef GR Digraph;
|
| 3592 |
3599 |
/// The key type
|
| 3593 |
3600 |
typedef typename Digraph::Node Key;
|
| 3594 |
3601 |
/// The value type
|
| 3595 |
3602 |
typedef int Value;
|
| 3596 |
3603 |
|
| 3597 |
3604 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
|
| 3598 |
3605 |
::ItemNotifier::ObserverBase Parent;
|
| 3599 |
3606 |
|
| 3600 |
3607 |
private:
|
| 3601 |
3608 |
|
| 3602 |
3609 |
class AutoNodeMap
|
| 3603 |
3610 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
|
| 3604 |
3611 |
public:
|
| 3605 |
3612 |
|
| 3606 |
3613 |
typedef typename ItemSetTraits<Digraph, Key>::
|
| 3607 |
3614 |
template Map<int>::Type Parent;
|
| 3608 |
3615 |
|
| 3609 |
3616 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
| 3610 |
3617 |
|
| 3611 |
3618 |
virtual void add(const Key& key) {
|
| 3612 |
3619 |
Parent::add(key);
|
| 3613 |
3620 |
Parent::set(key, 0);
|
| 3614 |
3621 |
}
|
| 3615 |
3622 |
virtual void add(const std::vector<Key>& keys) {
|
| 3616 |
3623 |
Parent::add(keys);
|
| 3617 |
3624 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 3618 |
3625 |
Parent::set(keys[i], 0);
|
| 3619 |
3626 |
}
|
| 3620 |
3627 |
}
|
| 3621 |
3628 |
virtual void build() {
|
| 3622 |
3629 |
Parent::build();
|
| 3623 |
3630 |
Key it;
|
| 3624 |
3631 |
typename Parent::Notifier* nf = Parent::notifier();
|
| 3625 |
3632 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 3626 |
3633 |
Parent::set(it, 0);
|
| 3627 |
3634 |
}
|
| 3628 |
3635 |
}
|
| 3629 |
3636 |
};
|
| 3630 |
3637 |
|
| 3631 |
3638 |
public:
|
| 3632 |
3639 |
|
| 3633 |
3640 |
/// \brief Constructor.
|
| 3634 |
3641 |
///
|
| 3635 |
3642 |
/// Constructor for creating an out-degree map.
|
| 3636 |
3643 |
explicit OutDegMap(const Digraph& graph)
|
| 3637 |
3644 |
: _digraph(graph), _deg(graph) {
|
| 3638 |
3645 |
Parent::attach(_digraph.notifier(typename Digraph::Arc()));
|
| 3639 |
3646 |
|
| 3640 |
3647 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 3641 |
3648 |
_deg[it] = countOutArcs(_digraph, it);
|
| 3642 |
3649 |
}
|
| 3643 |
3650 |
}
|
| 3644 |
3651 |
|
| 3645 |
3652 |
/// \brief Gives back the out-degree of a Node.
|
| 3646 |
3653 |
///
|
| 3647 |
3654 |
/// Gives back the out-degree of a Node.
|
| 3648 |
3655 |
int operator[](const Key& key) const {
|
| 3649 |
3656 |
return _deg[key];
|
| 3650 |
3657 |
}
|
| 3651 |
3658 |
|
| 3652 |
3659 |
protected:
|
| 3653 |
3660 |
|
| 3654 |
3661 |
typedef typename Digraph::Arc Arc;
|
| 3655 |
3662 |
|
| 3656 |
3663 |
virtual void add(const Arc& arc) {
|
| 3657 |
3664 |
++_deg[_digraph.source(arc)];
|
| 3658 |
3665 |
}
|
| 3659 |
3666 |
|
| 3660 |
3667 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 3661 |
3668 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 3662 |
3669 |
++_deg[_digraph.source(arcs[i])];
|
| 3663 |
3670 |
}
|
| 3664 |
3671 |
}
|
| 3665 |
3672 |
|
| 3666 |
3673 |
virtual void erase(const Arc& arc) {
|
| 3667 |
3674 |
--_deg[_digraph.source(arc)];
|
| 3668 |
3675 |
}
|
| 3669 |
3676 |
|
| 3670 |
3677 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 3671 |
3678 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 3672 |
3679 |
--_deg[_digraph.source(arcs[i])];
|
| 3673 |
3680 |
}
|
| 3674 |
3681 |
}
|
| 3675 |
3682 |
|
| 3676 |
3683 |
virtual void build() {
|
| 3677 |
3684 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 3678 |
3685 |
_deg[it] = countOutArcs(_digraph, it);
|
| 3679 |
3686 |
}
|
| 3680 |
3687 |
}
|
| 3681 |
3688 |
|
| 3682 |
3689 |
virtual void clear() {
|
| 3683 |
3690 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 3684 |
3691 |
_deg[it] = 0;
|
| 3685 |
3692 |
}
|
| 3686 |
3693 |
}
|
| 3687 |
3694 |
private:
|
| 3688 |
3695 |
|
| 3689 |
3696 |
const Digraph& _digraph;
|
| 3690 |
3697 |
AutoNodeMap _deg;
|
| 3691 |
3698 |
};
|
| 3692 |
3699 |
|
| 3693 |
3700 |
/// \brief Potential difference map
|
| 3694 |
3701 |
///
|
| 3695 |
3702 |
/// PotentialDifferenceMap returns the difference between the potentials of
|
| 3696 |
3703 |
/// the source and target nodes of each arc in a digraph, i.e. it returns
|
| 3697 |
3704 |
/// \code
|
| 3698 |
3705 |
/// potential[gr.target(arc)] - potential[gr.source(arc)].
|
| 3699 |
3706 |
/// \endcode
|
| 3700 |
3707 |
/// \tparam GR The digraph type.
|
| 3701 |
3708 |
/// \tparam POT A node map storing the potentials.
|
| 3702 |
3709 |
template <typename GR, typename POT>
|
| 3703 |
3710 |
class PotentialDifferenceMap {
|
| 3704 |
3711 |
public:
|
| 3705 |
3712 |
/// Key type
|
| 3706 |
3713 |
typedef typename GR::Arc Key;
|
| 3707 |
3714 |
/// Value type
|
| 3708 |
3715 |
typedef typename POT::Value Value;
|
| 3709 |
3716 |
|
| 3710 |
3717 |
/// \brief Constructor
|
| 3711 |
3718 |
///
|
| 3712 |
3719 |
/// Contructor of the map.
|
| 3713 |
3720 |
explicit PotentialDifferenceMap(const GR& gr,
|
| 3714 |
3721 |
const POT& potential)
|
| 3715 |
3722 |
: _digraph(gr), _potential(potential) {}
|
| 3716 |
3723 |
|
| 3717 |
3724 |
/// \brief Returns the potential difference for the given arc.
|
| 3718 |
3725 |
///
|
| 3719 |
3726 |
/// Returns the potential difference for the given arc, i.e.
|
| 3720 |
3727 |
/// \code
|
| 3721 |
3728 |
/// potential[gr.target(arc)] - potential[gr.source(arc)].
|
| 3722 |
3729 |
/// \endcode
|
| 3723 |
3730 |
Value operator[](const Key& arc) const {
|
| 3724 |
3731 |
return _potential[_digraph.target(arc)] -
|
| 3725 |
3732 |
_potential[_digraph.source(arc)];
|
| 3726 |
3733 |
}
|
| 3727 |
3734 |
|
| 3728 |
3735 |
private:
|
| 3729 |
3736 |
const GR& _digraph;
|
| 3730 |
3737 |
const POT& _potential;
|
| 3731 |
3738 |
};
|
| 3732 |
3739 |
|
| 3733 |
3740 |
/// \brief Returns a PotentialDifferenceMap.
|
| 3734 |
3741 |
///
|
| 3735 |
3742 |
/// This function just returns a PotentialDifferenceMap.
|
| 3736 |
3743 |
/// \relates PotentialDifferenceMap
|
| 3737 |
3744 |
template <typename GR, typename POT>
|
| 3738 |
3745 |
PotentialDifferenceMap<GR, POT>
|
| 3739 |
3746 |
potentialDifferenceMap(const GR& gr, const POT& potential) {
|
| 3740 |
3747 |
return PotentialDifferenceMap<GR, POT>(gr, potential);
|
| 3741 |
3748 |
}
|
| 3742 |
3749 |
|
| 3743 |
3750 |
/// @}
|
| 3744 |
3751 |
}
|
| 3745 |
3752 |
|
| 3746 |
3753 |
#endif // LEMON_MAPS_H
|