#include <map>
template<typename K, typename V>
class interval_map {
friend void IntervalMapTest();
V m_valBegin;
std::map<K,V> m_map;
public:
// constructor associates whole range of K with val
interval_map(V const& val)
: m_valBegin(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 < keyEnd)) return; // Empty interval, do nothing
auto it = m_map.lower_bound(keyBegin);
if (it != m_map.begin() && (--it)->second == val) {
++it; // Skip overlapping entries with the same value
}
while (it != m_map.end() && it->first < keyEnd) {
if (it->second == val) {
++it; // Skip overlapping entries with the same value
} else {
it = m_map.erase(it); // Remove overlapping entries with
different values
}
}
if (it != m_map.begin() && (--it)->second == val) {
++it; // Skip overlapping entries with the same value
}
if (it != m_map.end() && it->first == keyBegin) {
if (it->second == val) {
++it; // Skip overlapping entries with the same value
} else {
it = m_map.erase(it); // Remove overlapping entries with
different values
}
}
if (it != m_map.end() && it->first == keyEnd) {
if (it->second == val) {
++it; // Skip overlapping entries with the same value
} else {
it = m_map.erase(it); // Remove overlapping entries with
different values
}
}
if (it != m_map.end() && it->first > keyEnd) {
m_map.emplace_hint(it, keyEnd, it->second); // Insert new entry
for keyEnd
}
if (it != m_map.begin() && (--it)->first < keyBegin) {
m_map.emplace_hint(it, keyBegin, val); // Insert new entry for
keyBegin
}
if (it == m_map.begin() || (--it)->first < keyBegin) {
m_map.emplace_hint(it, keyBegin, val); // Insert new entry for
keyBegin
}
}
// look-up of the value associated with key
V const& operator[]( K const& key ) const {
auto it=m_map.upper_bound(key);
if(it==m_map.begin()) {
return m_valBegin;
} else {
return (--it)->second;
}
}
};
// Many solutions we receive are incorrect. Consider using a randomized test
// to discover the cases that your implementation does not handle correctly.
// We recommend to implement a test function that tests the functionality of
// the interval_map, for example using a map of int intervals to char.