Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
8
Indexable
Never
#include <map>
#include <iostream>

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)
	{}

    void print() {

        std::cout << "[" << m_valBegin << "] ";

        for (auto i : m_map) {
            std::cout << "[" << i.first << ':' << i.second << "] ";
        }
        std::cout << std::endl;
    }

	// 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 ) {

        using namespace std;
        if (!(keyBegin < keyEnd)) return;

        if (m_map.empty()) {
            m_map.insert({keyBegin, val});
            m_map.insert({keyEnd, m_valBegin});
            return;
        }

        auto it_b = m_map.upper_bound(keyBegin);
        auto it_e = m_map.lower_bound(keyEnd);

        cout << it_b->first << " " << it_b->second << endl;
        cout << it_e->first << " " << it_e->second << endl;



        if (it_b == m_map.end()) {
            auto tmp = it_b--;

            std::cout << tmp->first << " :" << tmp->second << std::endl;
            m_map.emplace(keyBegin, val);
            m_map.emplace(std::make_pair(keyEnd, tmp->second));
        }



	}

	// 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.

int main() {

    interval_map<int, char> m('a');

    m.assign(0, 10, 'b');
    m.print();

    m.assign(20, 30, 'c');
    m.print();


    return 0;
}