简体   繁体   English

我如何实现MySQL的DISTINCT关键字以使用线程并行获取不同的值

[英]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.

相关问题 如何实现有效的算法来计算大型数据集的多个不同值? - How to implement efficient algorith for calculating multiple distinct values for large dataset? 如何用不同的值填充数组 - How to fill an array with distinct values 如何从不同大小的数组中获得不同的值? - How to get distinct values from an arrays of different sizes? 仅用3个不同的值优化数组? 如何设计? - optimize array with only 3 distinct values? How to design? 我可以不使用new关键字以某种方式实现它吗? C ++ - Can I implement this somehow without using the new keyword? C++ 并行搜索不同的值? - Parallel search of distinct values? 在等式x + y = t中搜索y时如何获得不同的值? - How to get distinct values when searching for y in the equation x+y=t? 如何将std :: set_intersection用于2种不同但相关的类型并输出为另一种类型 - How can I use std::set_intersection for 2 distinct but related types and output into another type 如何调用具有多个不同签名的函数(就像glibc调用main一样)? - How can I invoke a function with multiple distinct signatures (as glibc invokes main)? 我如何指示/打印这个基于斐波那契数的程序的不同组合? - How can I indicate/print the distinct combinations to this Fibonacci number-based program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM