implement hash map in c [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
i need to impediment key and value in c like we used in c++ using map.
Please share simplest example to store unique key and value in store in c. key my be any string and value will be string.
Thanks

Creating a hash map requires a bit of work, but is definitely an interesting thing to do.
The general approach to build a hash map is to create an array called buckets that contains keys and values. However, this structure is not able to deal with collisions. A collision between two values may arise when different keys are assigned by the hash function to the same value.
To address this problem a third field, usually a pointer, is added to the bucket. When a collision arises, the new value is added in the data structure pointed at by the third field, usually a linked list or binary tree. For more information about how to resolve collisions, read this link.
An example of the Hash map Structure described above (note the collision at index 153):
To access the hash table, a custom hash function, which returns an integer which identifies the array index location, is applied to the key. Finally, you check if the key used to access the element and the key stored in the array at the index returned by the hash function match. If they do, you have found the right element.
This is just an example; you can find different ways to implement a hash map.
In addition, this question has already been asked here.

Related

Is it faster to lookup in set than unordered_map [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a question about lookup speed. I want to know which STL container can produce the fastest lookup time in C++. unordered_map comes to my mind since it is implemented by hash map, but I am afraid its performance is penalized because it contains key-value pair, whereas set contains only key. I guess the answer will depends on 1) the data type of key; and 2) the STL implementation of set.
In other words, which container is faster to search for the existence of an key, is it set, unordered_map, or something else?
Edit:
Would appreciate the answer with more explanation on the implementation or mechanism of the container. For instance, unordered_map is fast because it's implemented with hashmap. That will be more helpful than saying "it depends on the need". Thanks!
This depends to a large degree on the distribution of your data, the size of your dataset, the compiler, the toolchain...
The only way you can know is to measure it for your use case.
Do this after selecting the appropriate container for your task, then switch to something else only if you find that you need to and that you can get better performance for your use case by doing so.
Based on your question, I'd say the choice is between set and unsorted_set. On the other hand, if you don't actually know yet whether your data have both keys and values, then you're probably not ready to start profiling your solution.
Consider this perspective from a different angle,
Since performance is what you're interested in here, you might want to design your data structure in a way to optimally utilise cacheline
If the number of elements is not too high, then a vector will outperform all other containers. This is the case, because vectors store elements in contiguous memory locations and your cache loves contiguous memory allocation
You also mentioned about key-value pairs hampering lookup speed. One way to get around this from cacheline perspective is to store the keys in a contiguous data structure, do the lookup with keys alone. The only when you have a hit, you might want to read the corresponding value from your key-value pair
Check out this talk by Mike acton for more on this

Is there a language (or language spec) to extract elements from a DAG [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
I have a problem where I have a huge number of DAGs and I have to visit each DAG, and extract many different sets of items.
This may sound a bit abstract so let me use an example. Xpath or JSONPath are languages that specify how i can extract an element from an xml document or a json document. They are an efficient way to represent a specific element from such a document. So if I had a huge number of xml documents and I wanted to allow consumers to specify what they want from each document, I would have them provide me with an xpath expression and I would use that to provide each of them the element they are looking for from every document.
In this case I have many DAGs and my consumers each want to extract several items from each DAG (not just one item). I'm trying to design a specification whereby people can represent what they want from each such DAG (with the ability to specify specific elements, with predicates of different complexity). I'm wondering if there already is a language spec that makes sense for the purpose I'm describing.
If this does not exist for DAGs, but exists for Trees, that too would be very useful.

A boost or stl api for hashing a strings array in the form of string=>array-index [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I'm looking for a boost or a stl API that mimics this one:
https://mxr.mozilla.org/mozilla-central/source/xpcom/ds/nsStaticNameTable.h
In short, to improve the lookup performance, it creates a map (hash) from a strings (char*) array in which the keys are the array indices.
I know it's easy to implement on top of other APIs.
First of all, have you benchmarked that the current implementation is particular slow? You should always benchmark before trying to optimize.
Anyway, it would seem that boost::bimap<> is just what you asked for. Then you simply replace the array with a boost::bimap, and make up your own indices on the fly.
Given an array of const char*s like that accepted by the nsStaticCaseInsensitiveNameTable::Init function, you can initialise an std::unordered_map<>:
std::unordered_map<const char*, int, my_equal_to<const char*>> string_to_index;
for (int i = 0; i < count; ++i)
string_to_index[aNames[i]] = i;
Unfortunately, the default hashing function - std::hash() - will has const char* addresses rather than the pointed-to ASCIIZ textual data, so you'll have to write your own or copy one. There's one called "my_equal_to" edited into the question at C++ unordered_map with char* as key
The only difference here is that the std::unordered_map's equivalents to "Lookup" (you could use operator[] if you knew the string would appear somewhere, otherwise it's best to use find) are not case insensitive, so you'd need to call a to_lower() string-processing function on your key if you weren't sure it was already lower case. (Note that per the nsStaticCastInsensitiveNameTable header's documentation, the strings it's asked to store must be lowercase as a precondition of use). If having to call some manner of to_lower() function bothers you, you could trivially wrap the unordered_map in your own class, that called to_lower() before forwarding to the search functions.

Set data structure reference [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was asked to create a game using c++, but before I proceed to implement the game I need to research the set data structure from the c++ standard library (STL). I'm looking for a c++ reference that will show how to use this data structure. Also of use would be some information on what it is typically used for, how it works, and what are the pros and cons of using this data structure as opposed to something else.
A set is a data structure that holds unique items. A set in C++ is implemented with a binary search tree, so you need to have an order on the items (comparison function). You can pass in a comparison function to the constructor, or as a template parameter. Here's a resource to get you started on comparisons: http://fusharblog.com/3-ways-to-define-comparison-functions-in-cpp/
The most important things to do with a set are putting in elements, taking out elements, and checking if an element is already in there. These functions are called insert, delete, and find, respectively, for C++.
To list out all the elements, you need to go through them one by one. The begin and end iterator functions let you use a for loop to access each element one by one.
Here are references for the member functions. They contain examples of use.
http://en.cppreference.com/w/cpp/container/set
http://www.cplusplus.com/reference/set/set/
A google search will reveal a lot of links that will give you a description of the data structure.
Here is a link that shows a hands on example; this should get you going...

Pair of vectors instead of a vector<pair>? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
I'm looking for a lib that would implement something similar to std::map and multimap, but without tree but vectors instead, like a pair<vector<key>,vector<value>>. The lib would have to keep the vectors synchronized and sorted.
I'd like to compare the performance of maps, vector<pair<key,value>> and such a lib. I'm not going to try to make a general benchmark, I only want what's faster for my application.
I could just use map like everyone else, but -and I don't know I'm talking about- I fear for too many cache misses. My wild, uneducated guess is that my app, doing few, grouped inserts and many scattered searches, would benefit from more compact and contiguous keys.
As a matter of fact, if I was to code the thing, I'd do it with two classes. An unsorted version with only fast, push_back inserts permitted, then a transformation into a sorted version that would take the unsorted version, sort it, maybe check for duplicity, and then allow fast searches and slower, sorted inserts. This is actually Scott Meyers' Effective STL's Item 23: "Consider replacing associative containers with sorted vectors".
Edit: As pointed out by the Boost documentation about flat_(multi)map/set, Matt Austern, not Scott Meyers first suggested replacing maps and sets with vectors: http://lafstern.org/matt/col1.pdf
Maybe you are looking something like flat_(multi)map/set from Boost.Container ?
You should look into std::make_heap and associated functions, which manages a collection as a sorted heap. It fits almost perfectly your requirement of fast, unsorted inserts, and quick retrieval from a sorted collection.