parallel_set.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "parallel_sort.h"
  5. namespace embree
  6. {
  7. /* implementation of a set of values with parallel construction */
  8. template<typename T>
  9. class parallel_set
  10. {
  11. public:
  12. /*! default constructor for the parallel set */
  13. parallel_set () {}
  14. /*! construction from vector */
  15. template<typename Vector>
  16. parallel_set (const Vector& in) { init(in); }
  17. /*! initialized the parallel set from a vector */
  18. template<typename Vector>
  19. void init(const Vector& in)
  20. {
  21. /* copy data to internal vector */
  22. vec.resize(in.size());
  23. parallel_for( size_t(0), in.size(), size_t(4*4096), [&](const range<size_t>& r) {
  24. for (size_t i=r.begin(); i<r.end(); i++)
  25. vec[i] = in[i];
  26. });
  27. /* sort the data */
  28. std::vector<T> temp(in.size());
  29. radix_sort<T>(vec.data(),temp.data(),vec.size());
  30. }
  31. /*! tests if some element is in the set */
  32. __forceinline bool lookup(const T& elt) const {
  33. return std::binary_search(vec.begin(), vec.end(), elt);
  34. }
  35. /*! clears all state */
  36. void clear() {
  37. vec.clear();
  38. }
  39. private:
  40. std::vector<T> vec; //!< vector containing sorted elements
  41. };
  42. }