parallel_set.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "parallel_sort.h"
  18. namespace embree
  19. {
  20. /* implementation of a set of values with parallel construction */
  21. template<typename T>
  22. class parallel_set
  23. {
  24. public:
  25. /*! default constructor for the parallel set */
  26. parallel_set () {}
  27. /*! construction from vector */
  28. template<typename Vector>
  29. parallel_set (const Vector& in) { init(in); }
  30. /*! initialized the parallel set from a vector */
  31. template<typename Vector>
  32. void init(const Vector& in)
  33. {
  34. /* copy data to internal vector */
  35. vec.resize(in.size());
  36. parallel_for( size_t(0), in.size(), size_t(4*4096), [&](const range<size_t>& r) {
  37. for (size_t i=r.begin(); i<r.end(); i++)
  38. vec[i] = in[i];
  39. });
  40. /* sort the data */
  41. std::vector<T> temp(in.size());
  42. radix_sort<T>(vec.data(),temp.data(),vec.size());
  43. }
  44. /*! tests if some element is in the set */
  45. __forceinline bool lookup(const T& elt) const {
  46. return std::binary_search(vec.begin(), vec.end(), elt);
  47. }
  48. /*! clears all state */
  49. void clear() {
  50. vec.clear();
  51. }
  52. private:
  53. std::vector<T> vec; //!< vector containing sorted elements
  54. };
  55. }