0% found this document useful (0 votes)
184 views2 pages

Func 3

The document defines an interval_map template class that maps keys of type K to values of type V. The class allows assigning a value V to an interval of keys between keyBegin and keyEnd, overwriting previous values in that interval. It includes a show() method to print the map and an operator[] to look up a value by key. A test function IntervalMapTest() is provided to test the functionality by assigning char values to intervals of unsigned int keys and printing the results.

Uploaded by

piyushjindal98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
184 views2 pages

Func 3

The document defines an interval_map template class that maps keys of type K to values of type V. The class allows assigning a value V to an interval of keys between keyBegin and keyEnd, overwriting previous values in that interval. It includes a show() method to print the map and an operator[] to look up a value by key. A test function IntervalMapTest() is provided to test the functionality by assigning char values to intervals of unsigned int keys and printing the results.

Uploaded by

piyushjindal98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

using namespace std;


#include <assert.h>
#include <map>
#include <limits>
template<class K, class V>
class interval_map {
friend void IntervalMapTest();
private:
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.begin(),std::make_pair(std::numeric_limits<K>::lowest
(),val));
};
// Assign value val to interval [keyBegin, keyEnd).
// Overwrite previous values in this interval.
// Do not change values outside 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, const V& val ) {
// SORRY I COULD NOT TEST THE CODE AS WAS BUSY WITH OFFICE WORK
if (!( keyBegin < keyEnd )) return;
bool InsertKeyBegin = true;
bool InsertKeyEnd = true;
V ValueAtkeyEnd;
typename std::map<K,V>::iterator p1 = m_map.lower_bound(keyBegin);
typename std::map<K,V>::iterator p2 = m_map.lower_bound(keyEnd);
if ( p1 != m_map.begin() )
{ p1--; }
if ( p1->second == val )
{ InsertKeyBegin = false; }
if ( p2 != m_map.begin() )
{ p2--; }
if ( p2->second == val )
{ InsertKeyEnd = false; }
ValueAtkeyEnd = p2->second;
p1++;p2++;
m_map.erase(p1,p2);
if(InsertKeyBegin == true)
{
p1 = m_map.find (keyBegin);

if (p1 != m_map.end())
{ m_map.erase(p1); }
m_map.insert ( std::pair<K,V>(keyBegin,val) );
}
if(InsertKeyEnd == true)
{
p1 = m_map.find (keyEnd);
if (p1 != m_map.end())
{ m_map.erase(p1); }
m_map.insert ( std::pair<K,V>(keyEnd,ValueAtkeyEnd) );
}
return;
}
void show()
{
std::map<unsigned int, char>::iterator it;
for (it=m_map.begin(); it!=m_map.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
}
// look-up of the value associated with key
V const& operator[]( K const& key ) const {
return ( --m_map.upper_bound(key) )->second;
}
};
void
{

IntervalMapTest()
interval_map<unsigned int, char> ob('A');
ob.show();
ob.assign(6,11,'B');
ob.show();
ob.assign(3,8,'A');
ob.show();
ob.assign(3,5,'A');
ob.show();
ob.assign(5,10,'B');
ob.show();

}
//
//
//
//
//

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 function IntervalMapTest() here that tests the
functionality of the interval_map, for example using a map of unsigned int
intervals to char.

int main(int argc, char* argv[]) {


IntervalMapTest();
return 0;
}

You might also like