commit 4f4244a89f21541a8f8c9e8a2900e8c2b1b4f29d Author: gfuse Date: Mon Jan 15 09:02:58 2024 +0100 code diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6dc8821 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.18) +project(thinkcell) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(thinkcell main.cpp) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..e408626 --- /dev/null +++ b/main.cpp @@ -0,0 +1,60 @@ +#include +#include +template +class interval_map { + friend void IntervalMapTest(); + V m_valBegin; + std::map m_map; +public: + // constructor associates whole range of K with val + interval_map(V const& val, K k_range=1000) + : m_valBegin(val) { + for(K i=0; i(i,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 ) { + K Begin = keyBegin; + if(Begin < keyEnd) { + if(Begin < m_map.begin()->first)Begin=m_map.begin()->first; + auto it_begin = m_map.lower_bound(Begin); + if(it_begin == m_map.end() || m_map.key_comp()(Begin, (*it_begin).first)) { + m_map.insert(std::pair(Begin, val)); + }else { + it_begin->second = val; + } + if (it_begin != m_map.end())assign((++it_begin)->first, keyEnd, val); + } + } + + // 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; + } + } + void print() { + for(auto i : m_map)std::cerr<<"("< m('A', 100); + m.assign(-10, 50, 'B'); + m.print(); + return 0; +}