| // umlrthashmap.hh |
| |
| /******************************************************************************* |
| * Copyright (c) 2015 Zeligsoft (2009) Limited and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| *******************************************************************************/ |
| |
| #ifndef UMLRTHASHMAP_HH |
| #define UMLRTHASHMAP_HH |
| |
| #include "umlrtmutex.hh" |
| #include <stdlib.h> |
| |
| // Maps keys to entries. The map-name, keys and values are stored as values. |
| // WARNING: The memory pointed to by map-name, key and value (if it is memory) is owned by the user and must persist. |
| // The #remove method return the key which can be deallocated by the user if need be. |
| |
| class UMLRTHashMap |
| { |
| public: |
| typedef int (*key_compare_t)( const void * k1, const void * k2 ); |
| |
| // Two utility functions for creating maps with binary-value keys and string keys. |
| static int compareValue( const void * k1, const void * k2 ); |
| static int compareString( const void * k1, const void * k2 ); |
| |
| class Iterator { |
| public: |
| Iterator( const UMLRTHashMap * map_, int index_ ) : map(map_), index(index_) {} |
| Iterator end() const; |
| Iterator next() const; |
| const void * getKey() const; |
| void * getObject() const; |
| bool operator==(const UMLRTHashMap::Iterator &other) const { return (this->map == other.map) && (this->index == -1); } |
| bool operator!=(const UMLRTHashMap::Iterator &other) const { return (this->map != other.map) || (this->index != -1); } |
| private: |
| const UMLRTHashMap * map; |
| int index; |
| }; |
| |
| UMLRTHashMap( const char * name_, key_compare_t compare_) : name(name_), mapSize(0), map(NULL), compare(compare_) {} |
| ~UMLRTHashMap(); |
| |
| Iterator getIterator() const; |
| |
| int getSize() const { return mapSize; } |
| bool isEmpty() const { return mapSize == 0; } |
| void insert( const void * key, void * object ); // O(n) insert |
| const void * remove( const void * key ); // O(n) remove |
| void * getObject( const void * key ) const; // O(log n) object fetch. |
| void * getFirstObject() const; |
| |
| protected: |
| const void * getKey( int location ) const; |
| void * getObject( int location ) const; |
| |
| private: |
| UMLRTHashMap() : mapSize(0), map(NULL), compare(NULL) {} |
| |
| struct MapEntry { |
| const void * key; // If keys point to memory, the memory is owned by the app. |
| void * object; // This memory is owned by the HashMap and is a copy of the object inserted. |
| }; |
| |
| void removeEntry( int location ); // O(n) remove. |
| MapEntry * getEntry( const void * key ) const; |
| int locate( const void * key ) const; // O(log n) binary search. If key not found, returns index of where the key would be located. |
| |
| const char * name; |
| mutable UMLRTMutex mutex; |
| int mapSize; |
| MapEntry * map; |
| key_compare_t compare; |
| }; |
| |
| #endif // UMLRTHASHMAP_HH |