|
1
|
1 #pragma once
|
|
|
2
|
|
|
3 namespace pfc {
|
|
|
4 PFC_DECLARE_EXCEPTION( exception_invalid_permutation, exception_invalid_params, "Invalid permutation" );
|
|
|
5 t_size permutation_find_reverse(t_size const * order, t_size count, t_size value);
|
|
|
6
|
|
|
7 //! For critical sanity checks. Speed: O(n), allocates memory.
|
|
|
8 bool permutation_is_valid(t_size const * order, t_size count);
|
|
|
9 //! For critical sanity checks. Speed: O(n), allocates memory.
|
|
|
10 void permutation_validate(t_size const * order, t_size count);
|
|
|
11
|
|
|
12 //! Creates a permutation that moves selected items in a list box by the specified delta-offset.
|
|
|
13 void create_move_items_permutation(t_size * p_output,t_size p_count,const class bit_array & p_selection,int p_delta);
|
|
|
14
|
|
|
15 void create_move_item_permutation( size_t * p_output, size_t p_count, size_t from, size_t to );
|
|
|
16 bool create_drop_permutation(size_t * out, size_t itemCount, pfc::bit_array const & maskSelected, size_t insertMark );
|
|
|
17
|
|
|
18 bool is_identity(size_t const* order, size_t count);
|
|
|
19 }
|
|
|
20
|
|
|
21 class order_helper
|
|
|
22 {
|
|
|
23 pfc::array_t<t_size> m_data;
|
|
|
24 public:
|
|
|
25 order_helper(t_size p_size) {
|
|
|
26 m_data.set_size(p_size);
|
|
|
27 for(t_size n=0;n<p_size;n++) m_data[n]=n;
|
|
|
28 }
|
|
|
29
|
|
|
30 order_helper(const order_helper & p_order) {*this = p_order;}
|
|
|
31
|
|
|
32 static bool g_is_identity(const t_size * order, t_size count) {
|
|
|
33 for(t_size walk = 0; walk < count; ++walk) {
|
|
|
34 if (order[walk] != walk) return false;
|
|
|
35 }
|
|
|
36 return true;
|
|
|
37 }
|
|
|
38 template<typename t_array> static bool g_is_identity(const t_array & p_array) {
|
|
|
39 const t_size count = pfc::array_size_t(p_array);
|
|
|
40 for(t_size walk = 0; walk < count; ++walk) if (p_array[walk] != walk) return false;
|
|
|
41 return true;
|
|
|
42 }
|
|
|
43
|
|
|
44 template<typename t_int>
|
|
|
45 static void g_fill(t_int * p_order,const t_size p_count) {
|
|
|
46 t_size n; for(n=0;n<p_count;n++) p_order[n] = (t_int)n;
|
|
|
47 }
|
|
|
48
|
|
|
49 template<typename t_array>
|
|
|
50 static void g_fill(t_array & p_array) {
|
|
|
51 t_size n; const t_size max = pfc::array_size_t(p_array);
|
|
|
52 for(n=0;n<max;n++) p_array[n] = n;
|
|
|
53 }
|
|
|
54
|
|
|
55
|
|
|
56 t_size get_item(t_size ptr) const {return m_data[ptr];}
|
|
|
57
|
|
|
58 t_size & operator[](t_size ptr) {return m_data[ptr];}
|
|
|
59 t_size operator[](t_size ptr) const {return m_data[ptr];}
|
|
|
60
|
|
|
61 static void g_swap(t_size * p_data,t_size ptr1,t_size ptr2);
|
|
|
62 inline void swap(t_size ptr1,t_size ptr2) {pfc::swap_t(m_data[ptr1],m_data[ptr2]);}
|
|
|
63
|
|
|
64 const t_size * get_ptr() const {return m_data.get_ptr();}
|
|
|
65 t_size* get_ptr() { return m_data.get_ptr(); }
|
|
|
66
|
|
|
67 //! Insecure - may deadlock or crash on invalid permutation content. In theory faster than walking the permutation, but still O(n).
|
|
|
68 static t_size g_find_reverse(const t_size * order,t_size val);
|
|
|
69 //! Insecure - may deadlock or crash on invalid permutation content. In theory faster than walking the permutation, but still O(n).
|
|
|
70 inline t_size find_reverse(t_size val) {return g_find_reverse(m_data.get_ptr(),val);}
|
|
|
71
|
|
|
72 static void g_reverse(t_size * order,t_size base,t_size count);
|
|
|
73 inline void reverse(t_size base,t_size count) {g_reverse(m_data.get_ptr(),base,count);}
|
|
|
74
|
|
|
75 t_size get_count() const {return m_data.get_size();}
|
|
|
76 };
|