-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterval_map.cpp
More file actions
68 lines (59 loc) · 2.1 KB
/
interval_map.cpp
File metadata and controls
68 lines (59 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <map>
#include <limits>
#include <bits/stdc++.h>
template<typename K, typename V>
class interval_map {
std::map<K,V> m_map;
public:
// constructor associates whole range of K with val by inserting (K_min, val)
// into the map
interval_map( V const& val) {
m_map.insert(m_map.end(),std::make_pair(std::numeric_limits<K>::lowest(),val));
}
// Assign value val to interval [keyBegin, keyEnd).
// Overwrite previous values in this interval.
// Conforming to the C++ Standard Library conventions, the interval
// includes keyBegin, but excludes keyEnd.
// If !( keyBegin < keyEnd ), this designates an empty interval,
// and assign must do nothing.
void assign( K const& keyBegin, K const& keyEnd, V const& val ) {
if(!(keyBegin < std::numeric_limits<K>::lowest())){
if(keyBegin>=keyEnd) return;
auto lastItr = --m_map.upper_bound(keyEnd);
auto startItr = --m_map.upper_bound(keyBegin);
auto lastVal = lastItr->second;
lastItr = m_map.insert(lastItr,std::make_pair(keyEnd,lastItr->second));
startItr = m_map.insert(startItr,std::make_pair(keyBegin,val));
lastItr->second = lastVal;
startItr->second= val;
auto prevItr = std::prev(startItr);
while(startItr!=m_map.begin() && prevItr->second == val){
--startItr;
--prevItr;
}
while(lastItr!=m_map.end() && startItr->second == lastItr->second){
++lastItr;
}
m_map.erase(++startItr,lastItr);
}
}
// look-up of the value associated with key
V const& operator[]( K const& key ) const {
return ( --m_map.upper_bound(key) )->second;
}
void show() {
std::cout << "show" << std::endl;
for(auto entry : m_map) {
std::cout << entry.first << entry.second << std::endl;
}
}
};
int main(int argc, char* argv[])
{
interval_map<unsigned int, char> m('A');
m.assign(6, 7, 'd');
m.assign(8, 12, 'd');
m.assign(11, 14, 'm');
m.show();
return 0;
}