helpers.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "helpers.h"
  2. #include <algorithm>
  3. #include <iterator>
  4. namespace NErasure {
  5. TPartIndexList MakeSegment(int begin, int end) {
  6. TPartIndexList result(end - begin);
  7. for (int i = begin; i < end; ++i) {
  8. result[i - begin] = i;
  9. }
  10. return result;
  11. }
  12. TPartIndexList MakeSingleton(int elem) {
  13. TPartIndexList result;
  14. result.push_back(elem);
  15. return result;
  16. }
  17. TPartIndexList Difference(int begin, int end, const TPartIndexList& subtrahend) {
  18. size_t pos = 0;
  19. TPartIndexList result;
  20. for (int i = begin; i < end; ++i) {
  21. while (pos < subtrahend.size() && subtrahend[pos] < i) {
  22. pos += 1;
  23. }
  24. if (pos == subtrahend.size() || subtrahend[pos] != i) {
  25. result.push_back(i);
  26. }
  27. }
  28. return result;
  29. }
  30. TPartIndexList Difference(const TPartIndexList& first, const TPartIndexList& second) {
  31. TPartIndexList result;
  32. std::set_difference(first.begin(), first.end(), second.begin(), second.end(), std::back_inserter(result));
  33. return result;
  34. }
  35. TPartIndexList Difference(const TPartIndexList& set, int subtrahend) {
  36. return Difference(set, MakeSingleton(subtrahend));
  37. }
  38. TPartIndexList Intersection(const TPartIndexList& first, const TPartIndexList& second) {
  39. TPartIndexList result;
  40. std::set_intersection(first.begin(), first.end(), second.begin(), second.end(), std::back_inserter(result));
  41. return result;
  42. }
  43. TPartIndexList Union(const TPartIndexList& first, const TPartIndexList& second) {
  44. TPartIndexList result;
  45. std::set_union(first.begin(), first.end(), second.begin(), second.end(), std::back_inserter(result));
  46. return result;
  47. }
  48. bool Contains(const TPartIndexList& set, int elem) {
  49. return std::binary_search(set.begin(), set.end(), elem);
  50. }
  51. TPartIndexList UniqueSortedIndices(const TPartIndexList& indices) {
  52. TPartIndexList copy = indices;
  53. std::sort(copy.begin(), copy.end());
  54. copy.erase(std::unique(copy.begin(), copy.end()), copy.end());
  55. return copy;
  56. }
  57. TPartIndexList ExtractRows(const TPartIndexList& matrix, int width, const TPartIndexList& rows) {
  58. Y_ASSERT(matrix.size() % width == 0);
  59. TPartIndexList result(width * rows.size());
  60. for (size_t i = 0; i < rows.size(); ++i) {
  61. auto start = matrix.begin() + rows[i] * width;
  62. std::copy(start, start + width, result.begin() + i * width);
  63. }
  64. return result;
  65. }
  66. } // namespace NErasure