Skip to main content
Prev version seg faults in lookup operation when keyBegin is std::numeric_limits<K>::lowest(), under assumption there is always at least one entry that starts at lowest. If lookup implementation can work when map is empty, it should be added here as well (and the m_map.empty() I suggested, removed))
Source Link

This solution is CORRECT. But it does more than one log(N) time operations on the std::map (which uses Red-Balck-Tree implementation in the standard library) and this is a sub-optimal solution:

  void assign(K const& keyBegin, K const& keyEnd, V const& val) {
    if (!(keyBegin < keyEnd))
      return;
    V old_end_value = (*this)[keyEnd];
    m_map.erase(m_map.lower_bound(keyBegin), m_map.upper_bound(keyEnd));
    if (m_map.empty() || !((*this)[keyBegin] == val)) {
      m_map.insert({keyBegin, val});
    }
    if (!((*this)[keyEnd] == old_end_value)) {
      m_map.insert({keyEnd, old_end_value});
    }
  }

My approach:

  1. Save the old end value of the new range endpoint which might be used in the 4th step.
  2. Delete all the boundary values in the map, in the interval [keyBegin, keyEnd).
  3. Insert the new start value if that position is not covering the same value already.
  4. Insert the new end value only if the old end value has changed (after the third step).

This code generally handles all the cases. Trying to handle different cases specifically, you will miss many edge cases.

This solution is CORRECT. But it does more than one log(N) time operations on the std::map (which uses Red-Balck-Tree implementation in the standard library) and this is a sub-optimal solution:

  void assign(K const& keyBegin, K const& keyEnd, V const& val) {
    if (!(keyBegin < keyEnd))
      return;
    V old_end_value = (*this)[keyEnd];
    m_map.erase(m_map.lower_bound(keyBegin), m_map.upper_bound(keyEnd));
    if (!((*this)[keyBegin] == val)) {
      m_map.insert({keyBegin, val});
    }
    if (!((*this)[keyEnd] == old_end_value)) {
      m_map.insert({keyEnd, old_end_value});
    }
  }

My approach:

  1. Save the old end value of the new range endpoint which might be used in the 4th step.
  2. Delete all the boundary values in the map, in the interval [keyBegin, keyEnd).
  3. Insert the new start value if that position is not covering the same value already.
  4. Insert the new end value only if the old end value has changed (after the third step).

This code generally handles all the cases. Trying to handle different cases specifically, you will miss many edge cases.

This solution is CORRECT. But it does more than one log(N) time operations on the std::map (which uses Red-Balck-Tree implementation in the standard library) and this is a sub-optimal solution:

  void assign(K const& keyBegin, K const& keyEnd, V const& val) {
    if (!(keyBegin < keyEnd))
      return;
    V old_end_value = (*this)[keyEnd];
    m_map.erase(m_map.lower_bound(keyBegin), m_map.upper_bound(keyEnd));
    if (m_map.empty() || !((*this)[keyBegin] == val)) {
      m_map.insert({keyBegin, val});
    }
    if (!((*this)[keyEnd] == old_end_value)) {
      m_map.insert({keyEnd, old_end_value});
    }
  }

My approach:

  1. Save the old end value of the new range endpoint which might be used in the 4th step.
  2. Delete all the boundary values in the map, in the interval [keyBegin, keyEnd).
  3. Insert the new start value if that position is not covering the same value already.
  4. Insert the new end value only if the old end value has changed (after the third step).

This code generally handles all the cases. Trying to handle different cases specifically, you will miss many edge cases.

deleted 5 characters in body
Source Link
Amila Senadheera
  • 13.4k
  • 16
  • 31
  • 48

This solution is CORRECT. But it does more than one log(N) time operations on the unordered_mapstd::map (which uses Red-Balck-Tree implementation in the standard library) and this is a sub-optimal solution:

  void assign(K const& keyBegin, K const& keyEnd, V const& val) {
    if (!(keyBegin < keyEnd))
      return;
    V old_end_value = (*this)[keyEnd];
    m_map.erase(m_map.lower_bound(keyBegin), m_map.upper_bound(keyEnd));
    if (!((*this)[keyBegin] == val)) {
      m_map.insert({keyBegin, val});
    }
    if (!((*this)[keyEnd] == old_end_value)) {
      m_map.insert({keyEnd, old_end_value});
    }
  }

My approach:

  1. Save the old end value of the new range endpoint which might be used in the 4th step.
  2. Delete all the boundary values in the map, in the interval [keyBegin, keyEnd).
  3. Insert the new start value if that position is not covering the same value already.
  4. Insert the new end value only if the old end value has changed (after the third step).

This code generally handles all the cases. Trying to handle different cases specifically, you will miss many edge cases.

This solution is CORRECT. But it does more than one log(N) time operations on the unordered_map (which uses Red-Balck-Tree implementation in the standard library) and this is a sub-optimal solution:

  void assign(K const& keyBegin, K const& keyEnd, V const& val) {
    if (!(keyBegin < keyEnd))
      return;
    V old_end_value = (*this)[keyEnd];
    m_map.erase(m_map.lower_bound(keyBegin), m_map.upper_bound(keyEnd));
    if (!((*this)[keyBegin] == val)) {
      m_map.insert({keyBegin, val});
    }
    if (!((*this)[keyEnd] == old_end_value)) {
      m_map.insert({keyEnd, old_end_value});
    }
  }

My approach:

  1. Save the old end value of the new range endpoint which might be used in the 4th step.
  2. Delete all the boundary values in the map, in the interval [keyBegin, keyEnd).
  3. Insert the new start value if that position is not covering the same value already.
  4. Insert the new end value only if the old end value has changed (after the third step).

This code generally handles all the cases. Trying to handle different cases specifically, you will miss many edge cases.

This solution is CORRECT. But it does more than one log(N) time operations on the std::map (which uses Red-Balck-Tree implementation in the standard library) and this is a sub-optimal solution:

  void assign(K const& keyBegin, K const& keyEnd, V const& val) {
    if (!(keyBegin < keyEnd))
      return;
    V old_end_value = (*this)[keyEnd];
    m_map.erase(m_map.lower_bound(keyBegin), m_map.upper_bound(keyEnd));
    if (!((*this)[keyBegin] == val)) {
      m_map.insert({keyBegin, val});
    }
    if (!((*this)[keyEnd] == old_end_value)) {
      m_map.insert({keyEnd, old_end_value});
    }
  }

My approach:

  1. Save the old end value of the new range endpoint which might be used in the 4th step.
  2. Delete all the boundary values in the map, in the interval [keyBegin, keyEnd).
  3. Insert the new start value if that position is not covering the same value already.
  4. Insert the new end value only if the old end value has changed (after the third step).

This code generally handles all the cases. Trying to handle different cases specifically, you will miss many edge cases.

added 531 characters in body
Source Link
Amila Senadheera
  • 13.4k
  • 16
  • 31
  • 48

This solution is correctCORRECT. But it does more than one log(N) time operations on the unordered_map (which uses Red-Balck-Tree implementation in the standard library) and this is a sub-optimal solution:

  void assign(K const& keyBegin, K const& keyEnd, V const& val) {
    if (!(keyBegin < keyEnd))
      return;
    V old_end_value = (*this)[keyEnd];
    m_map.erase(m_map.lower_bound(keyBegin), m_map.upper_bound(keyEnd));
    if (!((*this)[keyBegin] == val)) {
      m_map.insert({keyBegin, val});
    }
    if (!((*this)[keyEnd] == old_end_value)) {
      m_map.insert({keyEnd, old_end_value});
    }
  }

My approach:

  1. Save the old end value of the new range endpoint which might be used in the 4th step.
  2. Delete all the boundary values in the map, in the interval [keyBegin, keyEnd).
  3. Insert the new start value if that position is not covering the same value already.
  4. Insert the new end value only if the old end value has changed (after the third step).

This code generally handles all the cases. Trying to handle different cases specifically, you will miss many edge cases.

This solution is correct. But it does more than one log(N) time operations on the unordered_map (which uses Red-Balck-Tree implementation in the standard library) and this is a sub-optimal solution:

  void assign(K const& keyBegin, K const& keyEnd, V const& val) {
    if (!(keyBegin < keyEnd))
      return;
    V old_end_value = (*this)[keyEnd];
    m_map.erase(m_map.lower_bound(keyBegin), m_map.upper_bound(keyEnd));
    if (!((*this)[keyBegin] == val)) {
      m_map.insert({keyBegin, val});
    }
    if (!((*this)[keyEnd] == old_end_value)) {
      m_map.insert({keyEnd, old_end_value});
    }
  }

This solution is CORRECT. But it does more than one log(N) time operations on the unordered_map (which uses Red-Balck-Tree implementation in the standard library) and this is a sub-optimal solution:

  void assign(K const& keyBegin, K const& keyEnd, V const& val) {
    if (!(keyBegin < keyEnd))
      return;
    V old_end_value = (*this)[keyEnd];
    m_map.erase(m_map.lower_bound(keyBegin), m_map.upper_bound(keyEnd));
    if (!((*this)[keyBegin] == val)) {
      m_map.insert({keyBegin, val});
    }
    if (!((*this)[keyEnd] == old_end_value)) {
      m_map.insert({keyEnd, old_end_value});
    }
  }

My approach:

  1. Save the old end value of the new range endpoint which might be used in the 4th step.
  2. Delete all the boundary values in the map, in the interval [keyBegin, keyEnd).
  3. Insert the new start value if that position is not covering the same value already.
  4. Insert the new end value only if the old end value has changed (after the third step).

This code generally handles all the cases. Trying to handle different cases specifically, you will miss many edge cases.

Source Link
Amila Senadheera
  • 13.4k
  • 16
  • 31
  • 48
Loading