typename T2::iterator copy_safe_apart(size_t n, const T1 & tab_src, T2 & tab_dst, size_t offset_src=0, size_t offset_dst=0) { _dbg5("copy (tab) n="<<n); if (n<1) return tab_dst.begin(); // could write both blocks below with lambda, in C++17TODO when decomposition declarations is available // source auto src_rb = tab_src.begin() + offset_src; // rb = range begin _check_input( offset_src < tab_src.size() ); _check_input( n <= tab_src.size() - offset_src ); // subtracting offset_src is valid since above auto src_rl = tab_src.begin() + offset_src + n -1; // range last _dbg5("Source range src_rb="<<to_debug(src_rb)<<" ... src_rl="<<to_debug(src_rl)); _check_abort( src_rl <= tab_src.end() ); // dest auto dst_rb = tab_dst.begin() + offset_dst; // rb = range begin _check_input( offset_dst < tab_dst.size() ); _check_input( n <= tab_dst.size() - offset_dst ); // subtracting offset_dst is valid since above auto dst_rl = tab_dst.begin() + offset_dst + n -1; // range last _dbg5("Destintion range dst_rb="<<to_debug(dst_rb)<<" ... dst_rl="<<to_debug(dst_rl)); _check_abort( dst_rl <= tab_dst.end() ); bool overlap = test_ranges_overlap_inclusive_noempty(src_rb, src_rl, dst_rb, dst_rl); _dbg5("overlap=" << overlap); _check_input(!overlap); copy_iter_and_check_no_overlap( src_rb, src_rl, dst_rb, n ); _dbg5("Copy done."); return dst_rb; }
inline void tensor_product(const T1& v1, const T2& v2, T3& m) { cf3_assert(m.getNbRows() == v1.size()); cf3_assert(m.getNbColumns() == v2.size()); const Uint v1size = v1.size(); const Uint v2size = v2.size(); for (Uint i = 0; i < v1size; ++i) { for (Uint j = 0; j < v2size; ++j) { m(i,j) = v1[i]*v2[j]; } } }
void add(T1 from, T2 to, Lambda lambda) { if (from.empty() || to.empty()) return; size_t iFrom = rand() % from.size(); size_t iTo = rand() % to.size(); lambda(iFrom, iTo); }
void deep_compare( const T1& X, const T2& Y ) { BOOST_REQUIRE( X.size() == Y.size() ); for( unsigned int nIndex=0; nIndex<X.size(); ++nIndex ) { BOOST_CHECK( equals( X[nIndex], Y[nIndex] ) ); } }
T1 binary_string_xor(T1 const & str1, T2 const& str2) { // WARNING: this function is written with less assertive code (e.g. without at()), // it MUST be checked against any errors if you would modify it. const auto size1 = str1.size(); const auto size2 = str2.size(); if (size1 != size2) throw std::runtime_error( string("Can not execute function ") + string(__func__) + string(" because different size: ") + std::to_string(size1) + " vs " + std::to_string(size2) ); // this is safe also for locked string: T1 ret( str1 ); for (size_t i=0; i<size1; ++i) ret[i] ^= str2[i]; // TODO: decltype(size1) without const assert(ret.size() == str1.size()); assert(str1.size() == str2.size()); return ret; }
inline Real inner_product (const T1& v1, const T2& v2) { cf3_assert(v1.size() == v2.size()); const Uint size = v1.size(); Real result = 0.0; for (Uint i = 0; i < size; ++i) result += v1[i]*v2[i]; return result; }
inline void cross_product (const T1& v1, const T2& v2, T3& result) { // sanity checks cf3_assert(v1.size() == 3); cf3_assert(v2.size() == 3); cf3_assert(result.size() == 3); result[0] = v1[1]*v2[2] - v1[2]*v2[1]; result[1] = -v1[0]*v2[2] + v1[2]*v2[0]; result[2] = v1[0]*v2[1] - v1[1]*v2[0]; }
inline Real get_distance(const T1& n1, const T2& n2) { cf3_assert(n1.size() == n2.size()); Real dist = 0.; const Uint size = n1.size(); for (Uint i = 0; i < size; ++i) { const Real diff = n1[i] - n2[i]; dist += diff*diff; } return std::sqrt(dist); }
inline Real mixed_product (const T1& v1, const T2& v2, const T3& v3, T4& temp) { // sanity checks cf3_assert(v1.size() == 3); cf3_assert(v2.size() == 3); cf3_assert(v3.size() == 3); cf3_assert(temp.size() == 3); cross_product(v1, v2, temp); return inner_product(v3, temp); }
void insertSort( T1& nums, T2& vecnum) { for(auto lit = nums.begin(); lit!=nums.end(); lit++) { if(vecnum.size()==0) vecnum.push_back(*lit); else { auto vit = vecnum.begin(); //vector iterator while(*vit<*lit && vit!=vecnum.end()) vit++; vecnum.insert(vit, *lit); } } }
template <typename T1, typename T2> void merge_exception_test(T1 x, T2 y) { std::size_t size = x.size() + y.size(); try { ENABLE_EXCEPTIONS; x.merge(y); } catch (...) { test::check_equivalent_keys(x); test::check_equivalent_keys(y); throw; } // Not a full check, just want to make sure the merge completed. BOOST_TEST(size == x.size() + y.size()); if (y.size()) { BOOST_TEST(test::has_unique_keys<T1>::value); for (typename T2::iterator it = y.begin(); it != y.end(); ++it) { BOOST_TEST(x.find(test::get_key<T2>(*it)) != x.end()); } } test::check_equivalent_keys(x); test::check_equivalent_keys(y); }
static bool decompress( T2 &buffer_out, const T1 &buffer_in ) { static const bool verbose = false; size_t decompressed_size = fastlz_decompress( &buffer_in.at(0), buffer_in.size(), &buffer_out.at(0), buffer_out.size() ); bool ret = ( decompressed_size == buffer_out.size() ); if( verbose ) { // std::cout << moon9::echo( ret, decompressed_size, buffer_out.size() ); } return ret; }