[英]How can I implement DISTINCT keyword of mysql to get distinct values parallely using threads
I would like to create a functionality DISTINCT keyword of mysql in c++ using hashmap or unordered_map. 我想使用hashmap或unordered_map在c ++中创建mysql的功能DISTINCT关键字。
I have to do this in parallel eg initially I have input integers in an array. 我必须并行执行此操作,例如,最初我在数组中具有输入整数。
Now I have to find distinct number in array (in parallel) 现在我必须在数组中找到不同的数字(并行)
If the original array doesn't change and assuming the available g++
extension for hash_map
: 如果原始数组不变,并假定
hash_map
具有可用的g++
扩展名:
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: 由于基础数据不会更改,
distinct_elems
也不会更改,因此代码如下:
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. 您可以从任何数量的线程中执行此操作而无需锁定,因为数据现在基本上是恒定的。
Use a hashmap and put all the integers there from your array. 使用哈希图,然后将所有整数放在数组中。 But add an extra check before you add them to the map so you don't re add them if they exist.
但在将它们添加到地图之前,请添加额外的检查,这样就不会重新添加它们(如果存在)。 At the end, iterate over all the integers in the map and you have your unique items:
最后,遍历地图中的所有整数,您将拥有唯一的项目:
Pseudocode: 伪代码:
for (int i=0..array.length){
if (hashmap.contains(array[i]) continue;
hashmap.put(i,i);
}
for (iterator over map) {
print("unique values: " + *it)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.