|
|
|
@ -359,338 +359,5 @@ public:
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This class contains comparison methods for most fundamental types; and is used by the CommonHashMap class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// The predicates of this class do standard equality comparisons between fundamental C/STL types such as
|
|
|
|
|
/// <c>int, float</c>, and <c>std::string.</c> Usefulness of this class outside the <see cref="CommonHashMap"/>
|
|
|
|
|
/// class is limited.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <seealso cref="CommonHashMap">
|
|
|
|
|
struct CommonComparisonClass
|
|
|
|
|
{
|
|
|
|
|
bool operator()(const char* s1, const char* s2) const
|
|
|
|
|
{
|
|
|
|
|
return (s1 == s2) || (s1 && s2 && strcmp(s1, s2) == 0);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// An interface for classes that implement hashmap functionality.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This class provides interface methods for getting th hashcode of a class and checking for object
|
|
|
|
|
/// equality. It's general intent is for use in situations where you have to store *non-similar objects*
|
|
|
|
|
/// in a single unified hash map. As all object instances derive from this type, it allows the equality
|
|
|
|
|
/// comparison to use typeid or dynamic casting to check for type similarity, and then use more detailed
|
|
|
|
|
/// equality checks for similar types.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
class IHashable
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/// Obligatory Virtual destructor mess!
|
|
|
|
|
virtual ~IHashable() {};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Your basic no-thrills equality comparison; using a pointer comparison by default.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This method uses a pointer comparison by default, which is the only way to really compare objects
|
|
|
|
|
/// of unrelated types or of derrived types. When implementing this method, you may want to use typeid comparisons
|
|
|
|
|
/// if you want derived types to register as being non-equal, or <c>dynamic_cast</c> for a more robust
|
|
|
|
|
/// base-class comparison (illustrated in the example below).
|
|
|
|
|
/// Note:
|
|
|
|
|
/// It's recommended important to always do a pointer comparison as the first step of any object equality check.
|
|
|
|
|
/// It is fast and easy, and 100% reliable.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <example>
|
|
|
|
|
/// Performing non-pointer comparisons:
|
|
|
|
|
/// <code>
|
|
|
|
|
/// class Hasher : IHashable
|
|
|
|
|
/// {
|
|
|
|
|
/// int someValue;
|
|
|
|
|
///
|
|
|
|
|
/// virtual bool Equals( const IHashable& right ) const
|
|
|
|
|
/// {
|
|
|
|
|
/// // Use pointer comparison first since it's fast and accurate:
|
|
|
|
|
/// if( &right == this ) return true;
|
|
|
|
|
///
|
|
|
|
|
/// Hasher* them = dynamic_cast<Hasher*>( right );
|
|
|
|
|
/// if( them == NULL ) return false;
|
|
|
|
|
/// return someValue == them->SomeValue;
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// </code>
|
|
|
|
|
/// </example>
|
|
|
|
|
virtual bool Equals( const IHashable& right ) const
|
|
|
|
|
{
|
|
|
|
|
return ( &right == this ); // pointer comparison.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a hash value for this object; by default the hash of its pointer address.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <seealso cref="HashMap"/>
|
|
|
|
|
virtual hash_key_t GetHashCode() const
|
|
|
|
|
{
|
|
|
|
|
return GetCommonHash( this );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template< typename Key >
|
|
|
|
|
class HashSet : public google::dense_hash_set< Key, CommonHashClass >
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Both the <c>emptyKey</c>a nd c>deletedKey</c> parameters must be unique values that
|
|
|
|
|
/// are *not* used as actual values in the set.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
HashSet( Key emptyKey, Key deletedKey, int initialCapacity=33 ) :
|
|
|
|
|
google::dense_hash_set<Key, CommonHashClass>( initialCapacity )
|
|
|
|
|
{
|
|
|
|
|
set_empty_key( emptyKey );
|
|
|
|
|
set_deleted_key( deletedKey );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Defines a hashed collection of objects and provides methods for adding, removing, and reading items.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>This class is for hashing out a set data using objects as keys. Objects should derive from the
|
|
|
|
|
/// <see cref="IHashable"/> type, and in either case *must* implement the UnaryHashCode and UnaryEquals
|
|
|
|
|
/// unary classes.</para>
|
|
|
|
|
/// <para>*Details On Implementing Key Types*</para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// Custom hash keying uses what I consider a somewhat contrived method of implementing the Key type;
|
|
|
|
|
/// involving a handful of macros in the best case, and a great deal of syntaxical red tape in
|
|
|
|
|
/// the worst case. Most cases should fall within the realm of the macros, which make life a lot easier,
|
|
|
|
|
/// so that's the only implementation I will cover in detail here (see below for example).
|
|
|
|
|
/// </para>
|
|
|
|
|
/// Note:
|
|
|
|
|
/// For most hashs based on common or fundamental types or types that can be adequately compared using
|
|
|
|
|
/// the default equality operator ==, such as <c>int</c> or structs that have no padding alignment concerns,
|
|
|
|
|
/// use <see cref="HashMap" /> instead. For string-based hashs, use <see cref="Dictionary" /> or <see cref="UnicodeDictionary" />.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <example>
|
|
|
|
|
/// This is an example of making a hashable type out of a struct. This is useful in situations where
|
|
|
|
|
/// inheriting the <see cref="IHashable"/> type would cause unnecessary overhead and/or broken C/C++
|
|
|
|
|
/// compatability.
|
|
|
|
|
/// <code>
|
|
|
|
|
/// struct Point
|
|
|
|
|
/// {
|
|
|
|
|
/// int x, y;
|
|
|
|
|
///
|
|
|
|
|
/// // Empty constructor is necessary for HashMap.
|
|
|
|
|
/// // This can either be initialized to zero, or uninitialized as here:
|
|
|
|
|
/// Point() {}
|
|
|
|
|
///
|
|
|
|
|
/// // Copy Constructor is just always necessary.
|
|
|
|
|
/// Point( const Point& src ) : first( src.first ), second( src.second ) {}
|
|
|
|
|
///
|
|
|
|
|
/// // Standard content constructor (Not needed by HashMap)
|
|
|
|
|
/// Point( int xpos, int ypos ) : x( xpos ), y( ypos ) {}
|
|
|
|
|
///
|
|
|
|
|
/// /**** Begin Hashmap Interface Implementation ****/
|
|
|
|
|
///
|
|
|
|
|
/// // HashMap Requires both GetEmptyKey() and GetDeleteKey() instance member
|
|
|
|
|
/// // methods to be defined. These act as defaults. The actual values used
|
|
|
|
|
/// // can be overridden on an individual HashMap basis via the HashMap constructor.
|
|
|
|
|
///
|
|
|
|
|
/// static Point GetEmptyKey() { return Point( -0xffffff, 0xffffff ); }
|
|
|
|
|
/// static Point GetDeletedKey() { return Kerning( -0xffffee, 0xffffee ); }
|
|
|
|
|
///
|
|
|
|
|
/// // HashMap Requires an Equality Overload.
|
|
|
|
|
/// // The inequality overload is not required but is probably a good idea since
|
|
|
|
|
/// // orphaned equality (without sibling inequality) operator overloads are ugly code.
|
|
|
|
|
///
|
|
|
|
|
/// bool Equals( const Point& right ) const
|
|
|
|
|
/// {
|
|
|
|
|
/// return ( x == right.x ) && ( y == right.y );
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// hash_key_t GetHashCode() const
|
|
|
|
|
/// {
|
|
|
|
|
/// // This is a decent "universal" hash method for when you have multiple int types:
|
|
|
|
|
/// return GetCommonHash( x ) ^ GetCommonHash( y );
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// // Use a macro to expose the hash API to the HashMap templates.
|
|
|
|
|
/// // This macro creates MakeHashCode and Compare structs, which use the ()
|
|
|
|
|
/// // operator to create "unary methods" for the GetHashCode and == operator above.
|
|
|
|
|
/// // Feeling dizzy yet? Don't worry. Just follow this template. It works!
|
|
|
|
|
///
|
|
|
|
|
/// DEFINE_HASH_API( Point );
|
|
|
|
|
///
|
|
|
|
|
/// /**** End HashMap Interface Implementation ****/
|
|
|
|
|
/// };
|
|
|
|
|
/// </code>
|
|
|
|
|
/// </example>
|
|
|
|
|
template< class Key, class T >
|
|
|
|
|
class SpecializedHashMap : public google::dense_hash_map<Key, T, typename Key::UnaryHashCode, typename Key::UnaryEquals>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual ~SpecializedHashMap() {}
|
|
|
|
|
SpecializedHashMap( int initialCapacity=33, Key emptyKey=Key::GetEmptyKey(), Key deletedKey=Key::GetDeletedKey() ) :
|
|
|
|
|
google::dense_hash_map<Key, T, typename Key::UnaryHashCode, typename Key::UnaryEquals>( initialCapacity )
|
|
|
|
|
{
|
|
|
|
|
set_empty_key( emptyKey );
|
|
|
|
|
set_deleted_key( deletedKey );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tries to get a value from this hashmap; or does nothing if the Key does not exist.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// If found, the value associated with the requested key is copied into the <c>outval</c>
|
|
|
|
|
/// parameter. This is a more favorable alternative to the indexer operator since the
|
|
|
|
|
/// indexer implementation can and will create new entries for every request that
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/*void TryGetValue( const Key& key, T& outval ) const
|
|
|
|
|
{
|
|
|
|
|
// GCC doesn't like this for some reason -- says const_iterator can't be found.
|
|
|
|
|
// Fortunately nothing uses these functions yet, so I just commented them out. --air
|
|
|
|
|
const_iterator iter = find( key );
|
|
|
|
|
if( iter != end() )
|
|
|
|
|
outval = iter->second;
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
const T& GetValue( Key key ) const
|
|
|
|
|
{
|
|
|
|
|
return (find( key ))->second;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This class implements a hashmap that uses fundamental types such as <c>int</c> or <c>std::string</c>
|
|
|
|
|
/// as keys.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This class is provided so that you don't have to jump through hoops in order to use fundamental types as
|
|
|
|
|
/// hash keys. The <see cref="HashMap" /> class isn't suited to the task since it requires the key type to
|
|
|
|
|
/// include a set of unary methods. Obviously predicates cannot be added to fundamentals after the fact. :)
|
|
|
|
|
/// Note:
|
|
|
|
|
/// Do not use <c>char *</c> or <c>wchar_t *</c> as key types. Use <c>std::string</c> and <c>std::wstring</c>
|
|
|
|
|
/// instead, as performance of those types will generally be superior due to string length caching. For that
|
|
|
|
|
/// matter, don't use this class at all! Use the string-specialized classes <see cref="Dictionary" /> and
|
|
|
|
|
/// <see cref="UnicodeDictionary" />.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
template< class Key, class T, class HashFunctor=CommonHashClass >
|
|
|
|
|
class HashMap : public google::dense_hash_map<Key, T, HashFunctor>
|
|
|
|
|
{
|
|
|
|
|
DeclareNoncopyableObject( HashMap );
|
|
|
|
|
|
|
|
|
|
typedef typename google::dense_hash_map<Key, T, HashFunctor> _parent;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
using _parent::operator[];
|
|
|
|
|
using _parent::end;
|
|
|
|
|
typedef typename _parent::const_iterator const_iterator;
|
|
|
|
|
|
|
|
|
|
virtual ~HashMap() {}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Both the <c>emptyKey</c>a nd c>deletedKey</c> parameters must be unique values that
|
|
|
|
|
/// are *not* used as actual values in the set.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
HashMap( const Key& emptyKey, const Key& deletedKey, int initialCapacity=33 ) :
|
|
|
|
|
google::dense_hash_map<Key, T, HashFunctor>( initialCapacity )
|
|
|
|
|
{
|
|
|
|
|
this->set_empty_key( emptyKey );
|
|
|
|
|
this->set_deleted_key( deletedKey );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tries to get a value from this hashmap; or does nothing if the Key does not exist.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// If found, the value associated with the requested key is copied into the <c>outval</c>
|
|
|
|
|
/// parameter. This is a more favorable alternative to the indexer operator since the
|
|
|
|
|
/// indexer implementation can and will create new entries for every request that
|
|
|
|
|
/// </remarks>
|
|
|
|
|
bool TryGetValue( const Key& key, T& outval ) const
|
|
|
|
|
{
|
|
|
|
|
const_iterator iter( this->find(key) );
|
|
|
|
|
if( iter != end() )
|
|
|
|
|
{
|
|
|
|
|
outval = iter->second;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const T& GetValue( Key key ) const
|
|
|
|
|
{
|
|
|
|
|
return (find( key ))->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Find( Key key ) const
|
|
|
|
|
{
|
|
|
|
|
return find(key) != end();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A shortcut class for easy implementation of string-based hash maps.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Note:
|
|
|
|
|
/// This class does not support Unicode character sets natively. To use Unicode strings as keys,
|
|
|
|
|
/// use <see cref="UnicodeDictionary"/> instead.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
template< class T >
|
|
|
|
|
class Dictionary : public HashMap<std::string, T>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual ~Dictionary() {}
|
|
|
|
|
|
|
|
|
|
Dictionary( int initialCapacity=33, const std::string& emptyKey = "@@-EMPTY-@@", const std::string& deletedKey = "@@-DELETED-@@" )
|
|
|
|
|
: HashMap<std::string, T>( emptyKey, deletedKey, initialCapacity)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A shortcut class for easy implementation of string-based hash maps.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Note:
|
|
|
|
|
/// This class does incur some amount of additional overhead over <see cref="Dictionary"/>, as it
|
|
|
|
|
/// requires twice as much memory and much hash twice as much data.
|
|
|
|
|
/// If you're only using the hash for friendly named array access (via string constants)
|
|
|
|
|
/// then you should probably just stick to using the regular dictionary.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
template< class T >
|
|
|
|
|
class UnicodeDictionary : public HashMap<std::wstring, T>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual ~UnicodeDictionary() {}
|
|
|
|
|
|
|
|
|
|
UnicodeDictionary( int initialCapacity=33, const std::wstring& emptyKey = L"@@-EMPTY-@@", const std::wstring& deletedKey = L"@@-DELETED-@@" )
|
|
|
|
|
: HashMap<std::wstring, T>( emptyKey, deletedKey, initialCapacity)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template< class T, class HashFunctor=HashTools::CommonHashClass >
|
|
|
|
|
class pxDictionary : public HashTools::HashMap<wxString, T, HashFunctor>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual ~pxDictionary() {}
|
|
|
|
|
|
|
|
|
|
pxDictionary( int initialCapacity=33, const wxString& emptyKey = L"@@-EMPTY-@@", const wxString& deletedKey = L"@@-DELETED-@@" )
|
|
|
|
|
: HashTools::HashMap<wxString, T, HashFunctor>( emptyKey, deletedKey, initialCapacity)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|