make (array) of ArrayObject return the contents
[hiphop-php.git] / hphp / util / map-walker.h
blobd3985568074f0d945a069fe0878a33d0ad798340
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_UTIL_MAPWALKER_H_
18 #define incl_HPHP_UTIL_MAPWALKER_H_
20 namespace HPHP {
23 * MapWalker is designed to iterate over an ordered map and an ordered list of
24 * keys in parallel. Once created with a reference to a map, hasNext(key) will
25 * return true iff the next item in the map matches the given key. If it
26 * returns true, next() will return a reference to that element and advance its
27 * internal iterator. hasNext() can be used to check if the internal iterator
28 * is at the end of the map.
30 template<typename Map>
31 struct MapWalker {
32 typedef typename Map::key_type Key;
33 typedef typename Map::mapped_type Value;
34 typedef typename Map::const_iterator ConstIter;
36 explicit MapWalker(const Map& m)
37 : m_map(m)
38 , m_iter(m.begin())
41 bool hasNext() const {
42 return m_iter != m_map.end();
45 bool hasNext(const Key& key) const {
46 assert(m_iter == m_map.end() || key <= m_iter->first);
47 return m_iter != m_map.end() && m_iter->first == key;
50 const Value& next() {
51 assert(m_iter != m_map.end());
52 auto const& val = m_iter->second;
53 ++m_iter;
54 return val;
57 private:
58 const Map& m_map;
59 ConstIter m_iter;
62 template<typename Map>
63 MapWalker<Map> makeMapWalker(const Map& m) {
64 return MapWalker<Map>(m);
69 #endif