24 #ifndef GRAPHLAB_SMALL_MAP_HPP
25 #define GRAPHLAB_SMALL_MAP_HPP
33 #include <graphlab/serialization/iarchive.hpp>
34 #include <graphlab/serialization/oarchive.hpp>
36 #include <graphlab/util/small_set.hpp>
38 #include <graphlab/macros_def.hpp>
41 template<
size_t MAX_DIM,
typename KeyT,
typename ValueT>
43 template<
size_t T1,
size_t T2 >
44 struct max_type {
enum max_value { value = T1 < T2? T2 : T1 }; };
46 bool operator()(
const std::pair<KeyT, ValueT>& a,
47 const std::pair<KeyT, ValueT>& b)
const {
48 return a.first < b.first;
56 typedef small_set< MAX_DIM, std::pair<KeyT, ValueT>, less >
58 typedef typename small_set_type::value_type value_type;
59 typedef typename small_set_type::iterator iterator;
60 typedef typename small_set_type::const_iterator const_iterator;
61 typedef KeyT key_type;
62 typedef ValueT mapped_type;
70 small_map(
const key_type& key,
const mapped_type& value) :
71 set(std::make_pair(key, value)) { }
75 inline iterator begin() {
return set.begin(); }
78 inline iterator end() {
return set.end(); }
82 inline const_iterator begin()
const {
return set.begin(); }
85 inline const_iterator end()
const {
return set.end(); }
88 inline size_t size()
const {
return set.size(); }
91 inline bool empty()
const {
return set.empty(); }
95 bool contains(
const value_type& pair)
const {
96 return set.contains(pair);
100 bool contains(
const key_type& key)
const {
101 const_iterator iter =
102 std::lower_bound(
set.begin(),
104 std::make_pair(key, mapped_type()));
105 return (iter !=
set.end()) && (iter->first == key);
109 inline bool has_key(
const key_type& key) {
110 return contains(key);
115 template<
size_t OtherDim>
116 bool contains(
const small_map<OtherDim, KeyT, ValueT>& other)
const {
117 return set.contains(other.set);
120 template<
size_t OtherDim>
121 bool operator==(
const small_map<OtherDim, KeyT, ValueT>& other)
const {
122 return set == other.set;
126 inline const mapped_type& operator[](
const key_type& key)
const {
127 value_type*
const ptr =
128 std::lower_bound(
set.begin(),
set.end(),
129 std::make_pair(key, mapped_type()),
131 ASSERT_NE(ptr,
set.end());
132 ASSERT_TURE(ptr->first == key);
137 inline mapped_type& operator[](
const key_type& key) {
139 std::lower_bound(
set.begin(),
set.end(),
140 std::make_pair(key, mapped_type()),
142 if(ptr != end() && (key == ptr->first) ) {
return ptr->second; }
144 set += std::make_pair(key, ValueT());
146 std::lower_bound(
set.begin(),
set.end(),
147 std::make_pair(key, mapped_type()),
149 ASSERT_NE(ptr,
set.end());
150 ASSERT_TRUE(ptr->first == key);
154 inline mapped_type& safe_find(
const key_type& key) {
155 value_type*
const ptr =
156 std::lower_bound(
set.begin(),
set.end(),
157 std::make_pair(key, mapped_type()),
159 ASSERT_NE(ptr,
set.end());
160 ASSERT_TRUE(ptr->first == key);
165 template<
size_t OtherDim>
166 inline small_map<max_type<OtherDim, MAX_DIM>::value, KeyT, ValueT>
167 operator+(
const small_map<OtherDim, KeyT, ValueT>& other)
const {
168 typedef small_map<max_type<OtherDim, MAX_DIM>::value, KeyT, ValueT >
171 result.set =
set + other.set;
185 template<
size_t MAX_DIM,
typename KeyT,
typename ValueT>
187 operator<<(std::ostream& out,
188 const graphlab::small_map<MAX_DIM, KeyT, ValueT>& map) {
189 typedef std::pair<KeyT, ValueT> pair_type;
192 foreach(
const pair_type& pair, map) {
193 out << pair.first <<
"->" << pair.second;
194 if(++index < map.size()) out <<
", ";
201 #include <graphlab/macros_undef.hpp>