SPH
Order.h
Go to the documentation of this file.
1 #pragma once
2 
7 
10 #include <algorithm>
11 
13 
18 class Order : public Noncopyable {
19 private:
20  Array<Size> storage;
21 
23  Order(Array<Size>&& other)
24  : storage(std::move(other)) {}
25 
26 public:
27  Order() = default;
28 
29  Order(Order&& other)
30  : storage(std::move(other.storage)) {}
31 
33  explicit Order(const Size n)
34  : storage(0, n) {
35  for (Size i = 0; i < n; ++i) {
36  storage.push(i);
37  }
38  }
39 
40  Order& operator=(Order&& other) {
41  storage = std::move(other.storage);
42  return *this;
43  }
44 
46  template <typename TBinaryPredicate>
47  void shuffle(TBinaryPredicate&& predicate) {
48  std::sort(storage.begin(), storage.end(), predicate);
49  }
50 
52  Order getInverted() const {
53  Array<Size> inverted(storage.size());
54  for (Size i = 0; i < storage.size(); ++i) {
55  inverted[storage[i]] = i;
56  }
57  return inverted;
58  }
59 
61  Order clone() const {
62  return storage.clone();
63  }
64 
66  Order compose(const Order& other) const {
67  Array<Size> composed(storage.size());
68  for (Size i = 0; i < storage.size(); ++i) {
69  composed[i] = storage[other[i]];
70  }
71  return composed;
72  }
73 
75  template <typename T>
76  Array<T> apply(const Array<T>& input) {
77  Array<T> sorted(input.size());
78  for (Size i = 0; i < input.size(); ++i) {
79  sorted[i] = input[storage[i]];
80  }
81  return sorted;
82  }
83 
84  INLINE Size operator[](const Size idx) const {
85  return storage[idx];
86  }
87 
88  INLINE Size size() const {
89  return storage.size();
90  }
91 
92  INLINE bool operator==(const Order& other) const {
93  return storage == other.storage;
94  }
95 };
96 
100 template <typename TLess = std::less<Float>>
101 INLINE Order getOrder(ArrayView<const Float> values, const TLess less = TLess{}) {
102  Order order(values.size());
103  order.shuffle([values, &less](Size i, Size j) { return less(values[i], values[j]); });
104  return order.getInverted();
105 }
106 
107 
INLINE auto less(const AntisymmetricTensor &t1, const AntisymmetricTensor &t2)
Generic dynamically allocated resizable storage.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
Vectorized computations with integral numbers.
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
INLINE Order getOrder(ArrayView< const Float > values, const TLess less=TLess{})
Finds the order of values in given array.
Definition: Order.h:101
INLINE TCounter size() const
Definition: ArrayView.h:101
INLINE Iterator< StorageType > end() noexcept
Definition: Array.h:462
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
INLINE TCounter size() const noexcept
Definition: Array.h:193
INLINE Iterator< StorageType > begin() noexcept
Definition: Array.h:450
Array clone() const
Performs a deep copy of all elements of the array.
Definition: Array.h:147
Permutation, i.e. (discrete) invertible function int->int.
Definition: Order.h:18
Array< T > apply(const Array< T > &input)
Shuffles given array using this order.
Definition: Order.h:76
INLINE bool operator==(const Order &other) const
Definition: Order.h:92
Order(const Size n)
Construct identity of given size.
Definition: Order.h:33
Order compose(const Order &other) const
Composes two orders.
Definition: Order.h:66
INLINE Size size() const
Definition: Order.h:88
Order()=default
void shuffle(TBinaryPredicate &&predicate)
Shuffles the order using a binary predicate.
Definition: Order.h:47
Order & operator=(Order &&other)
Definition: Order.h:40
INLINE Size operator[](const Size idx) const
Definition: Order.h:84
Order getInverted() const
Returns the inverted order.
Definition: Order.h:52
Order(Order &&other)
Definition: Order.h:29
Order clone() const
Clones the order.
Definition: Order.h:61
Overload of std::swap for Sph::Array.
Definition: Array.h:578
Object with deleted copy constructor and copy operator.
Definition: Object.h:54