Public
Snippet $1655247 authored by Jesse

C++ associative array

Edited
maphash.hpp
/////////////////////////////////////////////////////////////////////////////////////////////
// File: maphash.hpp
// Language: C++
// Version: 1
// Author: culb (nightfrog)
// Contact: the01culb[at]gmail[dot]com
//
// Purpose: This "library" supplies class template that is similiar to the std::map container
//
// ToDo: Create a vector like class template so there isn't a dependency on std::vector
//       Basically just remove the reliance on the STL ( Except for types )
//
// License: If this code is used in any way
//          then be courteous and include all of my information and notes
/////////////////////////////////////////////////////////////////////////////////////////////

#ifndef MAPHASH_HPP
#define MAPHASH_HPP

#include <vector>

template <class K, class T>
class maphash
{
  public:
    /// Return the number of key, value pairs
    std::uint32_t item_size() const
    {
        return hash.size();
    }

    /// Check if a key, value pair exists
    /// boolean so it can be checked for success or failure
    bool item_exists( const K key )
    {
        for( std::size_t i = 0; i < item_size(); ++i )
        {
            if( hash[i].key == key )
                return true;
        }
        return false;
    }

    /// Add a key, value pair
    /// boolean so it can be checked for success or failure
    bool item_add( const K key, const T value )
    {
        if( item_exists( key ) )
            return false;
        kvStruct d;
        d.key = key;
        d.value = value;
        hash.push_back( d );
        return true;
	}

    /// Delete a key, value pair
    /// boolean so it can be checked for success or failure
    bool item_delete( const K key ) const
    {
        for( std::size_t i = 0; i < item_size(); ++i )
        {
            if( hash[i].key == key )
            {
                hash.erase( hash.begin() + i );
                return true;
            }
        }
        return false;
    }

    /// Add a key, value pair with a bracket operator
    T& operator [] ( const K key )
    {
        for( std::size_t i = 0; i < item_size(); ++i )
        {
            if( hash[i].key == key )
                return hash[i].value;
        }
        // Declare and define this before the struct object or there is a segfault
        // Visual Studio 2015, "Expression: vector subscript out of range"
        // Rasbian 4.4.26-v7+ - "clang 3.5.0-10+rpi1" and GCC "Raspbian 4.9.2-10", malloc error
        // FreeBSD and other Linux distros are fine
        const std::uint32_t sizeOfItem = item_size();
        kvStruct d;
        d.key = key;
        hash.push_back(d);
        return hash[sizeOfItem].value;
    }

    /// FIXME, Needs a return type if the key isn't found.
    /// A function to get the value name of a key that's past in.
    T& item_value( const K key )
    {
        for( std::size_t i = 0; i < item_size(); ++i )
        {
            if( hash[i].key == key )
                return hash[i].value;
        }
        // There needs to be a return here.
    }

  private:
    struct kvStruct
    {
        T value;
        K key;
    };
    std::vector<kvStruct> hash;
};
#endif // MAPHASH_HPP