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

Movie - The Wizard of Oz (1939)

  My views Plot In rural  Kansas ,  Dorothy Gale  lives on a farm owned by her Uncle Henry and Aunt Em, and wishes she could be somewhere else. Dorothy's neighbor, Almira Gulch, who had been bitten by Dorothy's dog, Toto, obtains a sheriff's order authorizing her to seize Toto. Toto escapes and returns to Dorothy, who runs away to protect him. Professor Marvel, a charlatan fortune-teller, convinces Dorothy that Em is heartbroken, which prompts Dorothy to return home. She returns just as a  tornado  approaches the farm. Unable to get into the locked storm cellar, Dorothy takes cover in the farmhouse and is knocked unconscious. She seemingly awakens to find the house moving through the air, with her and Toto still inside it. The house comes down in an unknown land, and Dorothy is greeted by a good witch named  Glinda , who floats down in a bubble and explains that Dorothy has landed in Munchkinland in the  Land of Oz , and that the Munchkins are cel...

Movie - Se7en (1995)

  My views Plot In an unnamed city overcome with violent crime and corruption, disillusioned police Detective Lieutenant William Somerset is one week from retirement. He is partnered with David Mills, a young, short-tempered, idealistic detective who recently relocated to the city with his wife, Tracy. On Monday, Somerset and Mills investigate an obese man who was forced to eat until his stomach burst, killing him. The detectives find the word " gluttony " written on a wall. Somerset, considering the case too extreme for his last investigation, asks to be reassigned, but his request is denied. The following day, another victim, who had been forced to cut one pound (0.45 kg) of flesh from his body, is found; the crime scene is marked " greed ." Clues at the scene lead Somerset and Mills to the  sloth  victim, a drug-dealing  pederast  whom they find emaciated and restrained to a bed. Photographs reveal the victim was restrained for precisely one year. Somers...

IT - Which Is Faster: find | cpio -pdvm OR rsync?

To determine which is faster between find | cpio -pdvm and rsync for copying a large directory tree locally, we need to consider several factors: the nature of the operation, the tools' design, the system environment, and the specific use case. Let’s break this down based on the information provided in the web results and general knowledge about these tools. Overview of the Tools find | cpio -pdvm : find : Recursively lists all files and directories in a given path. cpio : A tool for copying files into or out of a cpio or tar archive. In this case, with the -pdvm options: -p : Pass-through mode (copy files from one directory tree to another). -d : Create directories as needed. -v : Verbose mod...