473,670 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL hashmap

Hello,

I used a unique associative container such as:
//---------------
map<vector<int> ,double> X;
//---------------

In general I fill X with millions of entries.

Sorting plays no role for me. All I need is uniqueness
of the keys and speed when inserting them.
At the end I retrieve them using iterators from the beginning
to the end. Which should lead to constant complexity.

Memory requirement is important but less then speed.

Unfortunately the used "map" is too slow.
I need a faster implementation.

I found hash_map as an alterantive, but it does not work
by simply replacing "map" with "hash_map".

Can anybody help me out?
So is hash_map part of the STL?
When not where can I get a good implementation of it?

I use g++ 3.2.2 on linux.

-- thanks, daniel
Jul 22 '05 #1
4 19950
On Tue, 13 Jan 2004 12:23:44 +0100, Daniel Heiserer
<Da************ *@bmw.de> wrote:
Hello,

I used a unique associative container such as:
//---------------
map<vector<int >,double> X;
//---------------

In general I fill X with millions of entries.
What code do you use to add element? There may be a more efficient
way.

Sorting plays no role for me. All I need is uniqueness
of the keys and speed when inserting them.
Why are you using vector<int> as your key? If there a fixed maximum
length of vector? How do you create the vector? You would probably get
an improvement from a custom key class.
At the end I retrieve them using iterators from the beginning
to the end. Which should lead to constant complexity.

Memory requirement is important but less then speed.

Unfortunatel y the used "map" is too slow.
I need a faster implementation.

I found hash_map as an alterantive, but it does not work
by simply replacing "map" with "hash_map".
No, you need to provide a hashing function at least.

Can anybody help me out?
So is hash_map part of the STL?
No. unordered_map is part of the draft standard library technical
report (TR1). It is basically hash_map by another name, with a few
differences from the various different hash_map versions in use.
When not where can I get a good implementation of it?

I use g++ 3.2.2 on linux.


GCC comes with an implementation <ext/hash_map>. I don't know how good
it is. Docs are here:

http://gcc.gnu.org/onlinedocs/libstd...t/howto.html#1

You might be able to make std::map fast enough if you optimize your
use of it...

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
I used a unique associative container such as:
//---------------
map<vector<int >,double> X;
//---------------

In general I fill X with millions of entries.
What code do you use to add element? There may be a more efficient
way.


vector<int> a;
double b;
X[a]=b;
// or
X[a]+=b;
Sorting plays no role for me. All I need is uniqueness
of the keys and speed when inserting them.


Why are you using vector<int> as your key? If there a fixed maximum
length of vector? How do you create the vector? You would probably get
an improvement from a custom key class.


foreach of my maps the vectors have the same length, but I want to
use different maps. e.g. one with vectors of size 3 others with
longer or shorter ones. Before I "add" my element I "X.resize(lengt h)"
the vectors.
map<vector<int> ,double> X; // vector length 3
map<vector<int> ,double> Y; // vector lenght 5

Of course this distinction has to be made during runtime.
How much would a

hash_map<int[3],double> X;

speed that up? And how much memory would I save?
At the end I retrieve them using iterators from the beginning
to the end. Which should lead to constant complexity.

Memory requirement is important but less then speed.

Unfortunatel y the used "map" is too slow.
I need a faster implementation.

I found hash_map as an alterantive, but it does not work
by simply replacing "map" with "hash_map".


No, you need to provide a hashing function at least.


How do I define one for vectors?

Can anybody help me out?
So is hash_map part of the STL?


No. unordered_map is part of the draft standard library technical
report (TR1). It is basically hash_map by another name, with a few
differences from the various different hash_map versions in use.
When not where can I get a good implementation of it?

I use g++ 3.2.2 on linux.


GCC comes with an implementation <ext/hash_map>. I don't know how good
it is. Docs are here:

http://gcc.gnu.org/onlinedocs/libstd...t/howto.html#1

You might be able to make std::map fast enough if you optimize your
use of it...


Well a suggestion might be useful ;-)
Again: The time critical part is insertion and therefore checking
for duplicate ones. In maybe 50% of the cases the next insertion
"might" be "close" to the previous one so this might help to speed it
up.
After that I only retrieve all pairs from the beginning to the end
which should be of complexity O(1) using the iterators.

I would appreciate if I could use as much of the standard container
functions as possible and avoid writing new templates and classes.
-- thanks, daniel
Jul 22 '05 #3
On Tue, 13 Jan 2004 14:26:01 +0100, Daniel Heiserer
<Da************ *@bmw.de> wrote:
>I used a unique associative container such as:
>//---------------
>map<vector<int >,double> X;
>//---------------
>
>In general I fill X with millions of entries.
What code do you use to add element? There may be a more efficient
way.


vector<int> a;
double b;
X[a]=b;
// or
X[a]+=b;


Ok, first improvement is to insert using:

X.insert(mymapt ype::value_type (a, b));
>Sorting plays no role for me. All I need is uniqueness
>of the keys and speed when inserting them.
Why are you using vector<int> as your key? If there a fixed maximum
length of vector? How do you create the vector? You would probably get
an improvement from a custom key class.


foreach of my maps the vectors have the same length, but I want to
use different maps. e.g. one with vectors of size 3 others with
longer or shorter ones. Before I "add" my element I "X.resize(lengt h)"
the vectors.
map<vector<int >,double> X; // vector length 3
map<vector<int >,double> Y; // vector lenght 5

Of course this distinction has to be made during runtime.
How much would a

hash_map<int[3],double> X;

speed that up? And how much memory would I save?


Well, the above is illegal (arrays aren't copyable), but you might try
this:

#include <cstddef>
#include <algorithm>

template <std::size_t Length>
class Key
{
int m_data[Length];
public:
static std::size_t const SIZE = Length;

Key()
{
//0 initialize
std::set(m_data , m_data + SIZE, 0);
}

int& operator[](std::size_t i)
{
assert(i < SIZE);
return m_data[i];
}

int const& operator[](std::size_t i) const
{
assert(i < Length);
return m_data[i];
}

friend bool operator<(Key const& lhs, Key const& rhs)
{
return std::lexicograp hical_compare(
lhs.m_data, lhs.m_data + SIZE,
rhs.m_data, rhs.m_data + SIZE);
}

friend bool operator==(Key const& lhs, Key const& rhs)
{
return std::equal_rang e(
lhs.m_data, lhs.m_data + SIZE,
rhs.m_data, rhs.m_data + SIZE);
}

//add anything required for convenience.
};

std::map<Key<3> , double> m;

That would add a pretty big speedup at insert time (in terms of
constant factor), and an even bigger memory saving. Key<3> takes up
12-16 bytes whereas std::vector<int > takes up 16 bytes before you even
consider the contents of the vector (another 16 at least, if not more
will allocation overhead). Using the above should roughly halve the
memory requirements and provide a major speed increase.
>At the end I retrieve them using iterators from the beginning
>to the end. Which should lead to constant complexity.
>
>Memory requirement is important but less then speed.
>
>Unfortunatel y the used "map" is too slow.
>I need a faster implementation.
>
>I found hash_map as an alterantive, but it does not work
>by simply replacing "map" with "hash_map".
No, you need to provide a hashing function at least.


How do I define one for vectors?


For your vectors you could have something like:

std::size_t hash(std::vecto r<int> const& v)
{
return std::accumulate (v.begin(), v.end(), std::size_t(0)) ;
}

That's a bad hashing algorithm, so you may want to read up on hashing
algorithms.
GCC comes with an implementation <ext/hash_map>. I don't know how good
it is. Docs are here:

http://gcc.gnu.org/onlinedocs/libstd...t/howto.html#1

You might be able to make std::map fast enough if you optimize your
use of it...


Well a suggestion might be useful ;-)
Again: The time critical part is insertion and therefore checking
for duplicate ones. In maybe 50% of the cases the next insertion
"might" be "close" to the previous one so this might help to speed it
up.
After that I only retrieve all pairs from the beginning to the end
which should be of complexity O(1) using the iterators.


Iteration over the whole container is O(n). Inserting n elements is
O(n log n).
I would appreciate if I could use as much of the standard container
functions as possible and avoid writing new templates and classes.


The standard containers are building blocks and in many cases aren't
the optimal solution. If your problem definitely requires these
multi-int keys and performance isn't good enough using the standard
approach, then first trying the Key class and then trying the Key
class in a hash_map (writing a hash function for Key) is going to work
best.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4


Daniel Heiserer wrote:
Hello,

I used a unique associative container such as:
//---------------
map<vector<int> ,double> X;
//--------------- .... Unfortunately the used "map" is too slow.
I need a faster implementation.

I found hash_map as an alterantive, but it does not work
by simply replacing "map" with "hash_map".

Can anybody help me out?
So is hash_map part of the STL?
When not where can I get a good implementation of it?

I use g++ 3.2.2 on linux.

-- thanks, daniel


With this compiler hash_map is int the __gnu_cxx namespace, not int the std
namespace. hash_map isn't C++ standard. Use

__gnu_cxx::hash _map<vector<int >,double> X;

Of course, you'll need to define a hash function for your key. See
http://www.sgi.com/tech/stl/hash_map.html for details.

Paul Dubuc

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
11397
by: Welson Sun | last post by:
Hi, I am new to UML, I would like to know how can represent JAVA's ArrayList and HashMap with UML diagram? How can I represent the element's class in the ArrayList/HashMap? How is the key in the HashMap represented?
2
27902
by: dougjrs | last post by:
I have a HashMap that is storing form data that will later be inserted into a database. I have been able to create the HashMap just fine, but I wanted to be able to take my HashMap and just "dump" it out to the screen to make sure that everything is working as I expect. (It was really to easy to code so I think that I may be mising something). This is a piece of the code where I am updating the HashMap: if ( map.containsKey(temp)) {...
1
16795
by: Christian Gollwitzer | last post by:
Hi, I'm trying to loop over the elements in a hashmap of the STL-implementation by SGI. The point is, that because the key/value pair is stored as std::pair in the COntainer, the code becomes very ugly and unreadable soon. I'm aware that there exists the for_each-template, but this doesn't make the code any better because the body of the for-loop must then live in an extra function (consider two nested loops). Now I tried to mimic the...
4
5758
by: David | last post by:
Hi, I have created the following HashMap class. class HashMap: public hash_map<string, string, HashString, HashStringCompare> { public: HashMap(): hash_map<string, string, HashString, HashStringCompare>() {} };
2
10762
by: xor | last post by:
I'm doing up a school project using java, and am a little new to it (I've worked with other languages for years though). I've seen code posted by the instructor using HashMap like this... public HashMap<Position, Integer> boardNumbers; .... and ... HashMap<Integer, Integer> allNumbers = new HashMap<Integer, Integer>();
6
2928
by: bumrag | last post by:
This is the car dealership object relating to the coursework, there is also a separate object named car that i think i need to link to. The problem is on the addcar() method. Any help would be greatly appreciated. /** A CarDealer object represents a business that buys and resells used cars */ import java.util.ArrayList; import java.util.HashMap; public class CarDealer { private int dayNumber; // Day number from start of operation of...
4
1935
by: panos100m | last post by:
Hi these are the conents of my hashmap printing out the entrySet. entrySet1: OrderDate=10/30/2007, entrySet2: Level_0={Item_0={ItemTotal= 3.99, ItemName=test® in, ShipDate=10/31/2007, ItemPrice= 3.99, ItemNumber=8504, ItemQuantity=1}, ShipMethodID=75, ItemCount=1, Items_StatusID=2, Items_Status=Shipped, ShipMethod=, Tracking_TrackingNumber=, ShippingLevelID=1} entrySet3: Outcome={ReturnCode=0, ReturnMessage}
15
5665
by: lbrtchx | last post by:
Hi, ~ I have found myself in need of some code resembling a Hashmap ~ This is easily done in Java this way: ~ import java.util.*; // __ public class JMith00Test{
3
5400
dlite922
by: dlite922 | last post by:
I could have this done in php in three lines of code but in Java here's what I need to do: English Looping through list of words I call a function that returns a HashSet of movie titles that contain that particular word. I want to insert these titles into a HashMap with their value being a counter of how many times this title occured. (if two words return the same title, that title's value (counter) would be 2) Relative Code:
1
5611
by: evelina | last post by:
Hello, I need help. I have the following hashmap: HashMap<HashMap<Dimension, Integer>, String> mapList = new HashMap<HashMap<Dimension, Integer>, String>(); I want to extract Dimesion from the key, where the Integer is "1", and String from the value. How could I iterate it? I wrote that, but it doesn't work at the way I expected: HashMap<Dimension, String> singleValues = new HashMap<Dimension, String>();
0
8386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8901
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8660
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7415
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6213
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1792
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.