big - 5 months ago 8x

SQL Question

I would like to create a functionality DISTINCT keyword of mysql in c++ using hashmap or unordered_map.

I have to do this in parallel e.g. initially I have input integers in an array.

Now I have to find distinct number in array (in parallel)

Answer

If the original array doesn't change and assuming the available `g++`

extension for `hash_map`

:

```
hash_map<int, int> distinct_elems;
for (int i = 0 ; i < num_elems ; ++i)
{
distinct_elems[i] = i;
}
```

Since the underlying data doesn't change the `distinct_elems`

won't change either, hence the code:

```
hash_map<int, int>::iterator de_itr;
for( de_itr = distinct_elems.begin() ; de_itr != distinct_elems.end() ; ++de_itr)
{
print("%d\n", de_itr->second);
}
```

or if you simply want to look up the value within the hashmap:

```
hash_map<int, int>::iterator de_itr = distinct_elems.find(value);
if(de_itr != distinct_elems.end())
{
<do some work>
}
```

You can do this from any number of threads with no locking since data is now basically constant.

Source (Stackoverflow)

Comments