STL hash_map

A

Axel Gallus

Hello,

i have a question concerning STL non-standard hash_maps under Visual Studio
2005:

Microsoft STL requires a "hash_compare" object for hash_maps:

template <class Key, class Type, class Traits=hash_compare<Key, less<Key> >,
class Allocator=allocator<pair <const Key, Type> > >
class hash_map

I derived a own class from hash_compare dealing with unsigned integers and
implemented the application operator() and a compare function:

class Timestamp_Hashing : public stdext::hash_compare <unsigned int>

{


public:

/// <summary>

/// Class constructor. The constructor takes two arguments, one defining the
quality of the hash map,

/// the other to adapt to a stream's dts struct.</summary>


Timestamp_Hashing (unsigned int HFactor, unsigned int FTimestamp = 0);

/// <summary>

/// This hash-function is used automatically by "hash_map" for determining
in which bucket to put a certain timestamp.</summary>

/// <param name="Timestamp"> The Timestamp for which the hash-value is to be
caclulated. </param>

/// <return> Returns a hash-value in form of the size_t type which can be
used by iterators.</return>

SIZE_T operator () (const unsigned int & Timestamp) const;



/// <summary>

/// This function is used automatically by "hash_map" for sorting the
timestamps contained in the buckets. </summary>

/// It receives two timestamps and return true if the first one is less the
the second one.

/// <param name="Timestamp1"> First timestamp.</param>

/// <param name="Timestamp1"> Second timestamp.</param>

/// <return> Returns true if Timestamp1 < Timestamp2 </return>


bool operator () (const unsigned int & Timestamp1, const unsigned int &
Timestamp2) const;




private:

// private variables used to save state during hash_map creation

unsigned int Hash_Factor;

unsigned int First_Timestamp;


};



As you can see my derived class Timestamp_Hashing has a constructor taking
two arguments.

I have planned to initiate a certain state of an object of class
Timestamp_Hashing through this constructor which influences the
hash_function.

If i create a hash_map by

stdext::hash_map < unsigned int, unsigned int, Timestamp_Hashing>;

everything compiles well but if i use

Timestamp_Hashing Hashy(10,20);
stdext::hash_map < unsigned int, unsigned int, Hashy>;

the compiler generated an error: Error 11 error C2923: 'stdext::hash_map' :
'Hashy' is not a valid template type argument for parameter '_Tr'.



So, how is it possible to pass a hash_compare/Timestamp_Hashing object with
a certain state to the hash_map constructor - is there a workaround?



Greetings and thx in advance.

Axel
 
V

Victor Bazarov

Axel said:
i have a question concerning STL non-standard hash_maps under Visual
Studio 2005:

Microsoft STL requires a "hash_compare" object for hash_maps:

template <class Key, class Type, class Traits=hash_compare<Key,
less<Key> >, class Allocator=allocator<pair <const Key, Type> > >
class hash_map

I derived a own class from hash_compare dealing with unsigned
integers and implemented the application operator() and a compare
function:
class Timestamp_Hashing : public stdext::hash_compare <unsigned int>

{
[..]
};



As you can see my derived class Timestamp_Hashing has a constructor
taking two arguments.

I have planned to initiate a certain state of an object of class
Timestamp_Hashing through this constructor which influences the
hash_function.

If i create a hash_map by

stdext::hash_map < unsigned int, unsigned int, Timestamp_Hashing>;

everything compiles well but if i use

Timestamp_Hashing Hashy(10,20);
stdext::hash_map < unsigned int, unsigned int, Hashy>;

What is that line supposed to designate? It's not legal to write
a simple type in a statement, without declaring an object or function
with that type.

Besides, you can't pass a non-type as a type template argument.
"Hashy" is an object. The 'hash_map' template expects a *type* as
its third argument.
the compiler generated an error: Error 11 error C2923:
'stdext::hash_map' : 'Hashy' is not a valid template type argument
for parameter '_Tr'.


So, how is it possible to pass a hash_compare/Timestamp_Hashing
object with a certain state to the hash_map constructor - is there a
workaround?

Yes, most likely. Pass it to the _constructor_ when defining your
hash_map<...> _instance_:

stdext::hash_map<..., Timestamp_Hashing> myMap(Hashy);

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top