yarin0600

Sol?

Oct 20th, 2023
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. template <typename K, typename V>
  4. class interval_map
  5. {
  6.    V m_ValBegin;
  7.    std::map<K, V> m_map;
  8.  
  9. public:
  10.    interval_map(V const &val) : m_ValBegin(val)
  11.    {
  12.    }
  13.    // Assign value val to interval [keyBegin, keyEnd).
  14.    void assign(K const &keyBegin, K const &keyEnd, V const &val)
  15.    {
  16.       if (!(keyBegin < keyEnd))
  17.          return;
  18.  
  19.       // Classic case: map is empty
  20.       if (m_map.empty())
  21.       {
  22.          m_map[keyBegin] = val;
  23.          m_map[keyEnd] = m_ValBegin;
  24.          return;
  25.       }
  26.  
  27.       // Find the iterators that represent the intervals to update
  28.       auto itBegin = m_map.lower_bound(keyBegin);
  29.       auto itEnd = m_map.lower_bound(keyEnd);
  30.  
  31.       // Handle the case when the previous interval has the same value
  32.       if (itBegin != m_map.begin())
  33.       {
  34.          auto itPrev = std::prev(itBegin);
  35.          if (itPrev->second == val)
  36.          {
  37.             // If the previous interval already has the same value, we don't need to insert a new entry
  38.             itBegin = itPrev;
  39.          }
  40.          else if (itPrev->first == keyBegin)
  41.          {
  42.             // If the previous interval's start matches keyBegin, update it and erase the current interval
  43.             itBegin = itPrev;
  44.             itBegin->second = val;
  45.          }
  46.       }
  47.  
  48.       // Handle the case when the end of the current interval matches keyEnd
  49.       if (itEnd != m_map.begin() && (--itEnd)->first == keyEnd)
  50.       {
  51.          // If the end of the current interval matches keyEnd, update it
  52.          itEnd->second = val;
  53.          return;
  54.       }
  55.       else
  56.       {
  57.          // Insert a new interval for [keyBegin, keyEnd)
  58.          V new_value = (itEnd == m_map.end()) ? m_ValBegin : itEnd->second;
  59.          m_map[keyBegin] = val;
  60.          m_map[keyEnd] = new_value;
  61.       }
  62.  
  63.       // Erase any intervals completely covered by the new one
  64.       itBegin = m_map.find(keyBegin);
  65.       itEnd = m_map.lower_bound(keyEnd);
  66.       if (itBegin != itEnd)
  67.       {
  68.          m_map.erase(std::next(itBegin), itEnd);
  69.       }
  70.       auto leftSide = std::prev(itBegin);
  71.       auto rightSide = std::next(itBegin);
  72.       // merging:
  73.       if (itBegin->second == rightSide->second)
  74.       {
  75.          m_map.erase(rightSide);
  76.       }
  77.       if (itBegin->second == leftSide->second)
  78.       {
  79.          m_map.erase(itBegin);
  80.       }
  81.    }
  82.  
  83.    V const &operator[](K const &key) const
  84.    {
  85.       auto it = m_map.upper_bound(key);
  86.       if (it == m_map.begin())
  87.       {
  88.          return m_ValBegin;
  89.       }
  90.       else
  91.       {
  92.          return (--it)->second;
  93.       }
  94.    }
  95.  
  96.    const std::map<K, V> &getMap() const { return this->m_map; }
  97. };
  98.  
  99. template <typename K, typename V>
  100. void printIntervalMap(const interval_map<K, V> &imap)
  101. {
  102.    const std::map<K, V> &m = imap.getMap();
  103.    std::cout << "{" << std::endl;
  104.    for (auto pair : m)
  105.    {
  106.       std::cout << "\t(" << pair.first << ", " << pair.second << ")" << std::endl;
  107.    }
  108.    std::cout << "}" << std::endl;
  109. }
  110.  
  111. int main()
  112. {
  113.    interval_map<int, char> mp('I');
  114.    mp.assign(1, 8, 'a');
  115.    mp.assign(8, 14, 'b');
  116.    printIntervalMap(mp);
  117.    mp.assign(6, 9, 'c');
  118.    mp.assign(9, 14, 'c');
  119.    printIntervalMap(mp);
  120.    // {(1, a), (6, 'c'), (9, 'b'), (14, 'I')}
  121. }
  122.  
  123. /*
  124.  
  125.  
  126. void assign(K const &keyBegin, K const &keyEnd, V const &val)
  127.    {
  128.       if (!(keyBegin < keyEnd))
  129.          return;
  130.       // classic case map is empty
  131.       if (this->m_map.empty())
  132.       {
  133.          this->m_map[keyBegin] = val;
  134.          this->m_map[keyEnd] = this->m_ValBegin;
  135.          return;
  136.       }
  137.  
  138.       // map is not empty, atleast 2 elements are there. [2 intervals]
  139.  
  140.       // lower_bound -> Iterator pointing to the first element that is not less than key.
  141.       //                If no such element is found, a past-the-end iterator. [map.end()]
  142.  
  143.       // important knowledge -> after insertion, the iterator stands on the same element
  144.  
  145.       // TODO: before erasing -> create a function to try and 'merge_intervals' only to adjacent elements
  146.       auto left_iterator = this->m_map.lower_bound(keyBegin); // get first that is bigger / equal to keyBegin
  147.       // now two cases:
  148.       if (left_iterator != this->m_map.end())
  149.       {
  150.          // we cannot go one step backwards
  151.          if (left_iterator == this->m_map.begin())
  152.          {
  153.             // ex: add(-1, 2, 'c') with map = {(1,'a'), (8, 'b'), (14, init)}
  154.             // we will make it map = {(-1, 'c'), (2, 'a'), (8, 'b'), (14, init)}
  155.             V moving_value = left_iterator->second;
  156.             K moving_key = left_iterator->key;
  157.  
  158.             this->m_map[keyBegin] = val;
  159.             this->m_map[keyEnd] = moving_value;
  160.  
  161.             this->m_map.erase(left_iterator);
  162.          }
  163.          // we can go one step backwards
  164.          else
  165.          {
  166.             --left_iterator;
  167.             // now left_iterator->first is the biggest key which is smaller than keyBegin.
  168.             this->m_map[keyBegin] = val;
  169.          }
  170.       }
  171.       else // left_iterator == this->m_map.end
  172.       // what does it mean? it means that there is no such element bigger / equal to keyBegin
  173.       // meaning when we reach this place we add to the right side end...
  174.       {
  175.          this->m_map[keyBegin] = val;
  176.          this->m_map[keyEnd] = this->m_ValBegin;
  177.       }
  178.  
  179.       auto right_iterator = this->m_map.lower_bound(keyEnd);
  180.       // two cases
  181.       // right_iterator is not end
  182.       if (right_iterator != this->m_map.end())
  183.       {
  184.          if (right_iterator != this->m_map.begin())
  185.          // can go one step backwards
  186.          {
  187.             --right_iterator;
  188.             if (right_iterator->first)
  189.          }
  190.          else
  191.          // cannot go one step backwards
  192.          {
  193.          }
  194.       }
  195.       // right_iterator is end
  196.       else
  197.       {
  198.       }
  199.    }
  200. */
Advertisement
Add Comment
Please, Sign In to add comment