Skip to main content

Think-Cell - interval_map

Received a message that this code failed ..... Not sure where the problem is ....
#define _AMD64_
#include <Windows.h>
#include <map>


template <typename K, typename V>
class interval_map {
    friend void IntervalMapTest();
    V m_valBegin; // default constructor, whtever it is
    std::map<K, V> m_map;

public:
    // constructor associates whole range of K with val
    interval_map( V const& val )
        : m_valBegin( 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 ) {
        // ATTENTION: *** Not sure if should be a return or a throw. It is not clear how error should be treated.
        // This code can be much better

        // Key type K. besides being copyable and assignable, is less - than comparable via operator<, and does not implement any other operations, in particular no equality comparison or arithmetic operators.
        if ( !( keyBegin < keyEnd ) ) {
            return;
        }
        // An empty map...just add it
        if ( m_map.empty() ) {
            // if V is the same as initial, do not add anything. Depending on V type, this can lead to extra execution time
            // Value type V. besides being copyable and assignable, is equality - comparable via operator==, and does not implement any other operations.
            if ( !( val == m_valBegin ) ) {
                m_map.insert( std::make_pair( keyBegin, val ) );
            }
        } else { // there is at least one entry
            // Remove all entries between Begin and End (exlcusive)
            auto it = m_map.lower_bound( keyBegin ); // Returns an iterator pointing to the first element that is not less than (i.e. greater or equal to) key
            while ( ( it != m_map.end() ) && ( it->first < keyEnd ) ) {
                it = m_map.erase( it );
            }
            // Insert new K
            m_map.insert( std::make_pair( keyBegin, val ) ); // the new pair..Maybe add a test for success?
            it = m_map.lower_bound( keyBegin ); // iterator to new element. Assuming the insert worked ;)
            auto it_next = it; // previous item (it is the current item. it has to exist, assuming the insert worked ;) )
            it_next++;
            if ( ( it_next != m_map.end() ) && ( it->second == it_next->second ) ) { // if next entry has same V, exclude it
                m_map.erase( it_next ); // remove next key, preserving this new entry
            } else if ( it != m_map.begin() ) {
                auto it_prev = it; // next item (it is the current item. it has to exist, assuming the insert worked ;) )
                it_prev--;
                if ( it_prev->second == it->second ) { // if previous entry has same V, exclude it
                    m_map.erase( it ); // remove this entry, preserving previous key....this can be an issue with ctor/dtor....
                }
            }
            if ( !m_map.empty() ) { // it should not be empty, but....
                // If first entry has same default value, remove first entry
                if ( m_map.begin()->second == m_valBegin ) {
                    m_map.erase( m_map.begin() ); // this can result on an empty map
                }
            }
        }
    }

    // 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;
        }
    }
};

// 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 test function that tests the functionality of
// the interval_map, for example using a map of int intervals to char.


void IntervalMapTest() {
    interval_map<int, char> M( 'A' );

}

main() {

}

Comments

Popular posts from this blog

Movies - The Bubble (2022)

  Back to Evolution (2001) .

IT - Fixing Windows Error 1327: Account Restrictions Are Preventing This User from Signing In

Fixing Windows Error 1327: Account Restrictions Are Preventing This User from Signing In Introduction Error 1327, “Account restrictions are preventing this user from signing in,” is a perplexing and disruptive issue that occurs on some Windows 10 and Windows 11 machines. The message typically appears at login or while connecting to remote resources, like shared folders, network drives, or remote desktops. Table of Contents Symptoms of Error 1327 Common Causes Step-by-Step Troubleshooting Advanced Fixes Automation via PowerShell Prevention Tips Further Reading Symptoms of Error 1327 Users experiencing this error may encounter one or more of the following: Login screen fails after credentials are entered. Error message appears when accessing mapped drives or network resources. Remote Desktop Connection (RDP) is rejected with the 1327 message. Group Policy logon restrictions silently block access. Co...

FamilyTree - Dr. med. Jürgen Christian Georg Berendsen [OUTDATED]

When researching my family history, I occasionally encounter an ancestor whose life deserves considerably more attention than a simple collection of names, dates and places. One such person is my great-great-grandfather, Dr. med. Jürgen Christian Georg Berendsen , born on 19 February 1855 in Schleswig and deceased on 19 April 1932 in Berlin. He lived for seventy-seven years, through a remarkable period of German history. Born in the Duchy of Schleswig before the German Empire existed, he witnessed the unification of Germany, the rapid development of modern medicine, the First World War, the collapse of the monarchy and the establishment of the Weimar Republic. His professional career unfolded during an era in which surgery, antisepsis and medical education were undergoing fundamental changes. But what interests me most is not simply the historical period in which he lived. It is the increasingly detailed picture of the man himself: a child described as physically fragile, a univ...