Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

new index_permutation method: remove(ix)

  • Loading branch information...
commit 5eaebad25fd64e2b0304b53d30011a5e9547576c 1 parent ed03596
@marshray authored
View
13 src/include/qak/permutation.hxx
@@ -89,6 +89,7 @@ namespace permutation_imp {
void extend(uint_type n);
void swap_two(uint_type ix_a, uint_type ix_b);
void rotate(uint_type ix_b, uint_type ix_m, uint_type ix_e);
+ void remove(uint_type ix);
void square();
void mult_by(index_permutation_imp const & that);
};
@@ -182,8 +183,8 @@ namespace permutation_imp {
//----- Lookup.
- size_type f_at(size_type ix) const { assert(0 <= ix); return from_imp(imp_.f_at(to_imp(ix))); }
- size_type r_at(size_type ix) const { assert(0 <= ix); return from_imp(imp_.r_at(to_imp(ix))); }
+ size_type f_at(size_type ix) const { assert(0 <= ix && ix < size()); return from_imp(imp_.f_at(to_imp(ix))); }
+ size_type r_at(size_type ix) const { assert(0 <= ix && ix < size()); return from_imp(imp_.r_at(to_imp(ix))); }
// The usual array container operators reflect the forward mapping.
size_type operator [] (size_type ix) const { return f_at(ix); }
@@ -224,6 +225,14 @@ namespace permutation_imp {
imp_.rotate(to_imp(ix_b), to_imp(ix_m), to_imp(ix_e));
}
+ // Removes the specified index. All higher indexes in the forward and reverse
+ // direction are shifted down by 1.
+ void remove(size_type ix)
+ {
+ assert(0 <= ix && ix < size());
+ imp_.remove(to_imp(ix));
+ }
+
// Multiplication
// Permutes the object by itself.
View
31 src/libqak/permutation.cxx
@@ -211,6 +211,37 @@ namespace permutation_imp {
r_[f_[ix]] = ix;
}
+ //-----------------------------------------------------------------------------------------------------------------|
+
+ template <int N>
+ void index_permutation_imp<N>::remove(uint_type ix)
+ {
+ assert(0 <= ix && ix < size());
+
+ uint_type new_size = size() - 1;
+
+ uint_type f_ix = ix;
+ uint_type r_ix = f_[f_ix];
+
+ for (uint_type ix = 0; ix < f_ix; ++ix)
+ if (r_ix < f_[ix])
+ --f_[ix];
+
+ for (uint_type ix = f_ix; ix + 1 < f_.size(); ++ix)
+ f_[ix] = f_[ix + 1] - (r_ix < f_[ix + 1]);
+
+ f_.resize(new_size);
+
+ for (uint_type ix = 0; ix < r_ix; ++ix)
+ if (f_ix < r_[ix])
+ --r_[ix];
+
+ for (uint_type ix = r_ix; ix + 1 < r_.size(); ++ix)
+ r_[ix] = r_[ix + 1] - (f_ix < r_[ix + 1]);
+
+ r_.resize(new_size);
+ }
+
//=================================================================================================================|
//-----------------------------------------------------------------------------------------------------------------|
View
98 src/libqak/permutation__test.cxx
@@ -621,6 +621,104 @@ namespace zzz { //==============================================================
//-----------------------------------------------------------------------------------------------------------------|
+ template <class T> void test_index_permutation_remove()
+ {
+ {
+ index_permutation<T> p(1);
+
+ QAK_verify_equal(p.size(), 1);
+ QAK_verify_equal(p.f_at(0), 0); QAK_verify_equal(p.r_at(0), 0);
+
+ p.remove(0);
+
+ QAK_verify_equal(p.size(), 0);
+ } {
+ index_permutation<T> p(3);
+ p.swap_two(1, 2);
+
+ QAK_verify_equal(p.size(), 3);
+ QAK_verify_equal(p.f_at(0), 0); QAK_verify_equal(p.r_at(0), 0);
+ QAK_verify_equal(p.f_at(1), 2); QAK_verify_equal(p.r_at(1), 2);
+ QAK_verify_equal(p.f_at(2), 1); QAK_verify_equal(p.r_at(2), 1);
+
+ p.remove(0);
+
+ QAK_verify_equal(p.size(), 2);
+ QAK_verify_equal(p.f_at(0), 1); QAK_verify_equal(p.r_at(0), 1);
+ QAK_verify_equal(p.f_at(1), 0); QAK_verify_equal(p.r_at(1), 0);
+ } {
+ index_permutation<T> p(3);
+
+ QAK_verify_equal(p.size(), 3);
+ QAK_verify_equal(p.f_at(0), 0); QAK_verify_equal(p.r_at(0), 0);
+ QAK_verify_equal(p.f_at(1), 1); QAK_verify_equal(p.r_at(1), 1);
+ QAK_verify_equal(p.f_at(2), 2); QAK_verify_equal(p.r_at(2), 2);
+
+ p.remove(0);
+
+ QAK_verify_equal(p.size(), 2);
+ QAK_verify_equal(p.f_at(0), 0); QAK_verify_equal(p.r_at(0), 0);
+ QAK_verify_equal(p.f_at(1), 1); QAK_verify_equal(p.r_at(1), 1);
+ } {
+ index_permutation<T> p(3);
+ p.swap_two(0, 1);
+
+ QAK_verify_equal(p.size(), 3);
+ QAK_verify_equal(p.f_at(0), 1); QAK_verify_equal(p.r_at(0), 1);
+ QAK_verify_equal(p.f_at(1), 0); QAK_verify_equal(p.r_at(1), 0);
+ QAK_verify_equal(p.f_at(2), 2); QAK_verify_equal(p.r_at(2), 2);
+
+ p.remove(0);
+
+ QAK_verify_equal(p.size(), 2);
+ QAK_verify_equal(p.f_at(0), 0); QAK_verify_equal(p.r_at(0), 0);
+ QAK_verify_equal(p.f_at(1), 1); QAK_verify_equal(p.r_at(1), 1);
+ } {
+ index_permutation<T> p(3);
+
+ QAK_verify_equal(p.size(), 3);
+ QAK_verify_equal(p.f_at(0), 0); QAK_verify_equal(p.r_at(0), 0);
+ QAK_verify_equal(p.f_at(1), 1); QAK_verify_equal(p.r_at(1), 1);
+ QAK_verify_equal(p.f_at(2), 2); QAK_verify_equal(p.r_at(2), 2);
+
+ p.remove(1);
+
+ QAK_verify_equal(p.size(), 2);
+ QAK_verify_equal(p.f_at(0), 0); QAK_verify_equal(p.r_at(0), 0);
+ QAK_verify_equal(p.f_at(1), 1); QAK_verify_equal(p.r_at(1), 1);
+ } {
+ index_permutation<T> p(3);
+
+ QAK_verify_equal(p.size(), 3);
+ QAK_verify_equal(p.f_at(0), 0); QAK_verify_equal(p.r_at(0), 0);
+ QAK_verify_equal(p.f_at(1), 1); QAK_verify_equal(p.r_at(1), 1);
+ QAK_verify_equal(p.f_at(2), 2); QAK_verify_equal(p.r_at(2), 2);
+
+ p.remove(2);
+
+ QAK_verify_equal(p.size(), 2);
+ QAK_verify_equal(p.f_at(0), 0); QAK_verify_equal(p.r_at(0), 0);
+ QAK_verify_equal(p.f_at(1), 1); QAK_verify_equal(p.r_at(1), 1);
+ } {
+ }
+ }
+
+ QAKtest(index_permutation_remove, "index_permutation remove().")
+ {
+ test_index_permutation_remove<signed char>();
+ test_index_permutation_remove<short int>();
+ test_index_permutation_remove<int>();
+ test_index_permutation_remove<long int>();
+ test_index_permutation_remove<long long int>();
+ test_index_permutation_remove<unsigned char>();
+ test_index_permutation_remove<unsigned short int>();
+ test_index_permutation_remove<unsigned int>();
+ test_index_permutation_remove<unsigned long int>();
+ test_index_permutation_remove<unsigned long long int>();
+ }
+
+ //-----------------------------------------------------------------------------------------------------------------|
+
} // namespace zzz ====================================================================================================|
#include "qak/test_app_post.hxx"
Please sign in to comment.
Something went wrong with that request. Please try again.