gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge
0 2 0
merge default
0 files changed with 74 insertions and 18 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -1581,172 +1581,206 @@
1581 1581

	
1582 1582
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1583 1583
  /// the keys for which the corresponding values of the two maps are
1584 1584
  /// equal.
1585 1585
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1586 1586
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1587 1587
  ///
1588 1588
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1589 1589
  /// \code
1590 1590
  ///   EqualMap<M1,M2> em(m1,m2);
1591 1591
  /// \endcode
1592 1592
  /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
1593 1593
  ///
1594 1594
  /// The simplest way of using this map is through the equalMap()
1595 1595
  /// function.
1596 1596
  ///
1597 1597
  /// \sa LessMap
1598 1598
  template<typename M1, typename M2>
1599 1599
  class EqualMap : public MapBase<typename M1::Key, bool> {
1600 1600
    const M1 &_m1;
1601 1601
    const M2 &_m2;
1602 1602
  public:
1603 1603
    typedef MapBase<typename M1::Key, bool> Parent;
1604 1604
    typedef typename Parent::Key Key;
1605 1605
    typedef typename Parent::Value Value;
1606 1606

	
1607 1607
    /// Constructor
1608 1608
    EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1609 1609
    /// \e
1610 1610
    Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
1611 1611
  };
1612 1612

	
1613 1613
  /// Returns an \ref EqualMap class
1614 1614

	
1615 1615
  /// This function just returns an \ref EqualMap class.
1616 1616
  ///
1617 1617
  /// For example, if \c m1 and \c m2 are maps with keys and values of
1618 1618
  /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
1619 1619
  /// <tt>m1[x]==m2[x]</tt>.
1620 1620
  ///
1621 1621
  /// \relates EqualMap
1622 1622
  template<typename M1, typename M2>
1623 1623
  inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
1624 1624
    return EqualMap<M1, M2>(m1,m2);
1625 1625
  }
1626 1626

	
1627 1627

	
1628 1628
  /// Combination of two maps using the \c < operator
1629 1629

	
1630 1630
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1631 1631
  /// the keys for which the corresponding value of the first map is
1632 1632
  /// less then the value of the second map.
1633 1633
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1634 1634
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1635 1635
  ///
1636 1636
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1637 1637
  /// \code
1638 1638
  ///   LessMap<M1,M2> lm(m1,m2);
1639 1639
  /// \endcode
1640 1640
  /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
1641 1641
  ///
1642 1642
  /// The simplest way of using this map is through the lessMap()
1643 1643
  /// function.
1644 1644
  ///
1645 1645
  /// \sa EqualMap
1646 1646
  template<typename M1, typename M2>
1647 1647
  class LessMap : public MapBase<typename M1::Key, bool> {
1648 1648
    const M1 &_m1;
1649 1649
    const M2 &_m2;
1650 1650
  public:
1651 1651
    typedef MapBase<typename M1::Key, bool> Parent;
1652 1652
    typedef typename Parent::Key Key;
1653 1653
    typedef typename Parent::Value Value;
1654 1654

	
1655 1655
    /// Constructor
1656 1656
    LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1657 1657
    /// \e
1658 1658
    Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
1659 1659
  };
1660 1660

	
1661 1661
  /// Returns an \ref LessMap class
1662 1662

	
1663 1663
  /// This function just returns an \ref LessMap class.
1664 1664
  ///
1665 1665
  /// For example, if \c m1 and \c m2 are maps with keys and values of
1666 1666
  /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
1667 1667
  /// <tt>m1[x]<m2[x]</tt>.
1668 1668
  ///
1669 1669
  /// \relates LessMap
1670 1670
  template<typename M1, typename M2>
1671 1671
  inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
1672 1672
    return LessMap<M1, M2>(m1,m2);
1673 1673
  }
1674 1674

	
1675 1675
  namespace _maps_bits {
1676 1676

	
1677
    template <typename Value>
1678
    struct Identity {
1679
      typedef Value argument_type;
1680
      typedef Value result_type;
1681
      Value operator()(const Value& val) const {
1682
	return val;
1683
      }
1684
    };
1685

	
1686 1677
    template <typename _Iterator, typename Enable = void>
1687 1678
    struct IteratorTraits {
1688 1679
      typedef typename std::iterator_traits<_Iterator>::value_type Value;
1689 1680
    };
1690 1681

	
1691 1682
    template <typename _Iterator>
1692 1683
    struct IteratorTraits<_Iterator,
1693 1684
      typename exists<typename _Iterator::container_type>::type>
1694 1685
    {
1695 1686
      typedef typename _Iterator::container_type::value_type Value;
1696 1687
    };
1697 1688

	
1698 1689
  }
1699 1690

	
1700 1691
  /// \brief Writable bool map for logging each \c true assigned element
1701 1692
  ///
1702
  /// A \ref concepts::ReadWriteMap "read-write" bool map for logging
1693
  /// A \ref concepts::WriteMap "writable" bool map for logging
1703 1694
  /// each \c true assigned element, i.e it copies subsequently each
1704 1695
  /// keys set to \c true to the given iterator.
1696
  /// The most important usage of it is storing certain nodes or arcs
1697
  /// that were marked \c true by an algorithm.
1705 1698
  ///
1706
  /// \tparam It the type of the Iterator.
1707
  /// \tparam Ke the type of the map's Key. The default value should
1708
  /// work in most cases.
1699
  /// There are several algorithms that provide solutions through bool
1700
  /// maps and most of them assign \c true at most once for each key.
1701
  /// In these cases it is a natural request to store each \c true
1702
  /// assigned elements (in order of the assignment), which can be
1703
  /// easily done with StoreBoolMap.
1704
  ///
1705
  /// The simplest way of using this map is through the storeBoolMap()
1706
  /// function.
1707
  ///
1708
  /// \tparam It The type of the iterator.
1709
  /// \tparam Ke The key type of the map. The default value set
1710
  /// according to the iterator type should work in most cases.
1709 1711
  ///
1710 1712
  /// \note The container of the iterator must contain enough space
1711
  /// for the elements. (Or it should be an inserter iterator).
1712
  ///
1713
  /// \todo Revise the name of this class and give an example code.
1713
  /// for the elements or the iterator should be an inserter iterator.
1714
#ifdef DOXYGEN
1715
  template <typename It, typename Ke>
1716
#else
1714 1717
  template <typename It,
1715 1718
	    typename Ke=typename _maps_bits::IteratorTraits<It>::Value>
1719
#endif
1716 1720
  class StoreBoolMap {
1717 1721
  public:
1718 1722
    typedef It Iterator;
1719 1723

	
1720 1724
    typedef Ke Key;
1721 1725
    typedef bool Value;
1722 1726

	
1723 1727
    /// Constructor
1724 1728
    StoreBoolMap(Iterator it)
1725 1729
      : _begin(it), _end(it) {}
1726 1730

	
1727 1731
    /// Gives back the given iterator set for the first key
1728 1732
    Iterator begin() const {
1729 1733
      return _begin;
1730 1734
    }
1731 1735

	
1732 1736
    /// Gives back the the 'after the last' iterator
1733 1737
    Iterator end() const {
1734 1738
      return _end;
1735 1739
    }
1736 1740

	
1737 1741
    /// The set function of the map
1738
    void set(const Key& key, Value value) const {
1742
    void set(const Key& key, Value value) {
1739 1743
      if (value) {
1740 1744
	*_end++ = key;
1741 1745
      }
1742 1746
    }
1743 1747

	
1744 1748
  private:
1745 1749
    Iterator _begin;
1746
    mutable Iterator _end;
1750
    Iterator _end;
1747 1751
  };
1752
  
1753
  /// Returns a \ref StoreBoolMap class
1754

	
1755
  /// This function just returns a \ref StoreBoolMap class.
1756
  ///
1757
  /// The most important usage of it is storing certain nodes or arcs
1758
  /// that were marked \c true by an algorithm.
1759
  /// For example it makes easier to store the nodes in the processing
1760
  /// order of Dfs algorithm, as the following examples show.
1761
  /// \code
1762
  ///   std::vector<Node> v;
1763
  ///   dfs(g,s).processedMap(storeBoolMap(std::back_inserter(v))).run();
1764
  /// \endcode
1765
  /// \code
1766
  ///   std::vector<Node> v(countNodes(g));
1767
  ///   dfs(g,s).processedMap(storeBoolMap(v.begin())).run();
1768
  /// \endcode
1769
  ///
1770
  /// \note The container of the iterator must contain enough space
1771
  /// for the elements or the iterator should be an inserter iterator.
1772
  ///
1773
  /// \note StoreBoolMap is just \ref concepts::WriteMap "writable", so
1774
  /// it cannot be used when a readable map is needed, for example as
1775
  /// \c ReachedMap for Bfs, Dfs and Dijkstra algorithms.
1776
  ///
1777
  /// \relates StoreBoolMap
1778
  template<typename Iterator>
1779
  inline StoreBoolMap<Iterator> storeBoolMap(Iterator it) {
1780
    return StoreBoolMap<Iterator>(it);
1781
  }
1748 1782

	
1749 1783
  /// @}
1750 1784
}
1751 1785

	
1752 1786
#endif // LEMON_MAPS_H
Ignore white space 192 line context
... ...
@@ -211,99 +211,121 @@
211 211
    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
212 212
  }
213 213

	
214 214
  // ForkMap
215 215
  {
216 216
    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
217 217

	
218 218
    typedef RangeMap<double> RM;
219 219
    typedef SparseMap<int, double> SM;
220 220
    RM m1(10, -1);
221 221
    SM m2(-1);
222 222
    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
223 223
    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
224 224
    ForkMap<RM, SM> map1(m1,m2);
225 225
    ForkMap<SM, RM> map2 = forkMap(m2,m1);
226 226
    map2.set(5, 10);
227 227
    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
228 228
          "Something is wrong with ForkMap");
229 229
  }
230 230

	
231 231
  // Arithmetic maps:
232 232
  // - AddMap, SubMap, MulMap, DivMap
233 233
  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
234 234
  // - NegMap, NegWriteMap, AbsMap
235 235
  {
236 236
    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
237 237
    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
238 238
    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
239 239
    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
240 240

	
241 241
    ConstMap<int, double> c1(1.0), c2(3.14);
242 242
    IdentityMap<int> im;
243 243
    ConvertMap<IdentityMap<int>, double> id(im);
244 244
    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0, "Something is wrong with AddMap");
245 245
    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,  "Something is wrong with SubMap");
246 246
    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28, "Something is wrong with MulMap");
247 247
    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57, "Something is wrong with DivMap");
248 248

	
249 249
    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
250 250
    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
251 251
    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
252 252
    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
253 253
    checkConcept<DoubleMap, NegMap<DoubleMap> >();
254 254
    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
255 255
    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
256 256

	
257 257
    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
258 258
          "Something is wrong with ShiftMap");
259 259
    check(shiftWriteMap(id, 2.0)[1] == 3.0 && shiftWriteMap(id, 2.0)[10] == 12.0,
260 260
          "Something is wrong with ShiftWriteMap");
261 261
    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
262 262
          "Something is wrong with ScaleMap");
263 263
    check(scaleWriteMap(id, 2.0)[1] == 2.0 && scaleWriteMap(id, 2.0)[10] == 20.0,
264 264
          "Something is wrong with ScaleWriteMap");
265 265
    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
266 266
          "Something is wrong with NegMap");
267 267
    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
268 268
          "Something is wrong with NegWriteMap");
269 269
    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
270 270
          "Something is wrong with AbsMap");
271 271
  }
272 272

	
273 273
  // Logical maps:
274 274
  // - TrueMap, FalseMap
275 275
  // - AndMap, OrMap
276 276
  // - NotMap, NotWriteMap
277 277
  // - EqualMap, LessMap
278 278
  {
279 279
    checkConcept<BoolMap, TrueMap<A> >();
280 280
    checkConcept<BoolMap, FalseMap<A> >();
281 281
    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
282 282
    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
283 283
    checkConcept<BoolMap, NotMap<BoolMap> >();
284 284
    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
285 285
    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
286 286
    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
287 287

	
288 288
    TrueMap<int> tm;
289 289
    FalseMap<int> fm;
290 290
    RangeMap<bool> rm(2);
291 291
    rm[0] = true; rm[1] = false;
292 292
    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] && !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
293 293
          "Something is wrong with AndMap");
294 294
    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] && orMap(fm,rm)[0] && !orMap(fm,rm)[1],
295 295
          "Something is wrong with OrMap");
296 296
    check(!notMap(rm)[0] && notMap(rm)[1], "Something is wrong with NotMap");
297 297
    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1], "Something is wrong with NotWriteMap");
298 298

	
299 299
    ConstMap<int, double> cm(2.0);
300 300
    IdentityMap<int> im;
301 301
    ConvertMap<IdentityMap<int>, double> id(im);
302 302
    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
303 303
          "Something is wrong with LessMap");
304 304
    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
305 305
          "Something is wrong with EqualMap");
306 306
  }
307
  
308
  // StoreBoolMap
309
  {
310
    typedef std::vector<int> vec;
311
    vec v1;
312
    vec v2(10);
313
    StoreBoolMap<std::back_insert_iterator<vec> > map1(std::back_inserter(v1));
314
    StoreBoolMap<vec::iterator> map2(v2.begin());
315
    map1.set(10, false);
316
    map1.set(20, true);   map2.set(20, true);
317
    map1.set(30, false);  map2.set(40, false);
318
    map1.set(50, true);   map2.set(50, true);
319
    map1.set(60, true);   map2.set(60, true);
320
    check(v1.size() == 3 && v2.size() == 10 &&
321
          v1[0]==20 && v1[1]==50 && v1[2]==60 && v2[0]==20 && v2[1]==50 && v2[2]==60,
322
          "Something is wrong with StoreBoolMap");
323
          
324
    int i = 0;
325
    for ( StoreBoolMap<vec::iterator>::Iterator it = map2.begin();
326
          it != map2.end(); ++it )
327
      check(v1[i++] == *it, "Something is wrong with StoreBoolMap");
328
  }
307 329

	
308 330
  return 0;
309 331
}
0 comments (0 inline)