Question: The amount of code you'll need for a correct solution is relatively small, and you'll spend more time thinking than typing. Please provide best solution!
The amount of code you'll need for a correct solution is relatively small, and you'll spend more time thinking than typing. Please provide best solution!
Task Description
intervalmap is a data structure that associates keys of type K with values of type V It is designed to be used efficiently in situations where intervals of consecutive keys are associated with the same value. Your task is to implement the assign member function of this data structure, which is outlined below.
intervalmap is implemented on top of std::map. For more information on std::map, you may refer to cppreference.com.
Each keyvalue pair kv in intervalmap::mmap means that the value v is associated with all keys from k including to the next key excluding in mmap. The member intervalmap::mvalBegin holds the value that is associated with all keys less than the first key in mmap.
Example: Let M be an instance of intervalmap where
MmvalBeginA
MmmapBA
Then M represents the mapping
A
A
A
B
B
A
A
A
The representation in the std::map must be canonical, that is consecutive map entries must not contain the same value: AA is not allowed. Likewise, the first entry in mmap must not contain the same value as mvalBegin. Initially, the whole range of K is associated with a given initial value, passed to the constructor of the intervalmap data structure.
Key type K
supports copy and move construction, as well as copy and move assignment,
is lessthan comparable via operator
does not implement any other operations, in particular no equality comparison or arithmetic operators.
Value type V
supports copy and move construction, as well as copy and move assignment,
is equalitycomparable via operator
does not implement any other operations.
For using Compiler Output GCC :
You are given the following source code:
#include template class intervalmap friend void IntervalMapTest; V mvalBegin; std::map mmap; public: constructor associates whole range of K with val template intervalmapVforward&& val : mvalBeginstd::forwardval 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. template void assign K const& keyBegin, K const& keyEnd, Vforward&& val requires std::issame, V::value
Here
lookup of the value associated with key V const& operator K const& key const auto itmmap.upperboundkey; ifitmmap.begin return mvalBegin; else return itsecond; ; Many solutions are incorrect. Consider using a randomized test to discover the cases that your implementation does not handle correctly. I recommend to implement a test function that tests the functionality of the intervalmap, for example using a map of int intervals to char.
Task is to implement the function assign. Your implementation is according to the following criteria, in this order:
Type requirements: You must adhere to the specification of the key and value type given above.
Correctness: Your program should produce a working intervalmap with the behavior described above. In particular, pay attention to the validity of iterators. It is illegal to dereference end iterators. Consider using a checking STL implementation, such as the one shipped with Visual C or GCC
Canonicity: The representation in mmap must be canonical.
Running time: Imagine your implementation is part of a library and should therefore be bigO optimal:
The assign function should have amortized time complexity of Olog N where N is the number of elements in mmap.Minimize the number of copyconstructions and copyassignments performed on K and V Make use of std::forward where applicable.Only one operation may have amortized time complexity of Olog N The rest must have amortized time complexity of OGiven the constraints above, avoid comparing K and V more often than necessary because you don't know how fast these operations are.Otherwise, favor simplicity over minor speed improvements.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
