Infrared
Loading...
Searching...
No Matches
simple_map.hpp
1#ifndef SIMPLE_MAP_HH
2#define SIMPLE_MAP_HH
3
4/*
5 * InfraRed --- A generic engine for Boltzmann sampling over constraint networks
6 * (C) Sebastian Will, 2018
7 *
8 * This file is part of the InfraRed source code.
9 *
10 * InfraRed provides a generic framework for tree decomposition-based
11 * Boltzmann sampling over constraint networks
12 */
13
20#include <vector>
21#include <algorithm>
22#include <cassert>
23
24namespace ired {
25
35template<class key_t, class val_t>
36class SimpleMap: std::vector<std::pair<key_t, val_t> > {
37 typedef std::pair<key_t, val_t> key_val_t;
38 typedef std::vector<key_val_t> key_val_vec_t;
39 typedef typename key_val_vec_t::iterator iterator;
40 typedef typename key_val_vec_t::const_iterator const_iterator;
41
42 iterator
43 binsearch (iterator first, iterator last, const key_t& key)
44 {
45 first = std::lower_bound(first, last, key,
46 [](const key_val_t &x, const key_t &y) {
47 return x.first < y;});
48 if (first==last || key < first->first) {
49 return last;
50 }
51 return first;
52 }
53
54 const_iterator
55 binsearch (const_iterator first, const_iterator last, const key_t& key) const
56 {
57 first = std::lower_bound(first,last,key,
58 [](const key_val_t &x, const key_t &y) {
59 return x.first < y;});
60 if (first==last || key < first->first) {
61 return last;
62 }
63 return first;
64 }
65
66public:
68
69 const_iterator
70 find(const key_t &key) const {
71 auto it= binsearch(key_val_vec_t::begin(), key_val_vec_t::end(), key);
72 assert(it == key_val_vec_t::end() || it->first == key);
73 return it;
74 };
75
76 iterator
77 find(const key_t &key) {
78 auto it=binsearch(key_val_vec_t::begin(), key_val_vec_t::end(), key);
79 assert(it == key_val_vec_t::end() || it->first == key);
80 return it;
81 };
82
83 bool
84 exists(const key_t &key) const {
85 return find(key) != key_val_vec_t::end();
86 }
87
88 auto
89 end() const {
90 return key_val_vec_t::end();
91 }
92
100 void
101 push_ascending( const key_t &key, const val_t &val ) {
102 assert(size()==0||key > key_val_vec_t::operator[](size()-1).first);
103 key_val_vec_t::push_back(key_val_t(key,val));
104 }
105
106 void
107 erase(iterator it) {
108 //std::cout << key_val_vec_t::size()<<" "<<key_val_vec_t::capacity()<<std::endl;
109
110 // doing only this, wastes space (due to stl-vector allocation strategy)
111 // key_val_vec_t::erase(it);
112
113 // instead: copy to new space with exactly the right size
114 // (possible optimization: only mark erased entries at
115 // first and delay the copying until it pays off)
116 key_val_vec_t vec(key_val_vec_t::size()-1);
117 iterator target=copy(key_val_vec_t::begin(),it,vec.begin());
118 copy(it+1,key_val_vec_t::end(),target);
119
120 vec.swap(*this);
121 }
122
123 size_t
124 size() const {
125 return key_val_vec_t::size();
126 }
127
128 size_t
129 capacity() const {
130 return key_val_vec_t::capacity();
131 }
132
133 void
135 key_val_vec_t vec(size());
136 copy(key_val_vec_t::begin(),key_val_vec_t::end(),vec.begin());
137 vec.swap(*this);
138 }
139};
140
141} // end namespace ired
142
143#endif //SIMPLE_MAP_HH
Space saving replacement for map.
Definition simple_map.hpp:36
auto end() const
Definition simple_map.hpp:89
SimpleMap()
Definition simple_map.hpp:67
bool exists(const key_t &key) const
Definition simple_map.hpp:84
size_t capacity() const
Definition simple_map.hpp:129
size_t size() const
Definition simple_map.hpp:124
iterator find(const key_t &key)
Definition simple_map.hpp:77
const_iterator find(const key_t &key) const
Definition simple_map.hpp:70
void push_ascending(const key_t &key, const val_t &val)
push in ascending order of keys
Definition simple_map.hpp:101
void reallocate()
Definition simple_map.hpp:134
void erase(iterator it)
Definition simple_map.hpp:107
Definition assignment.hpp:29