long index(types::list<T> &seq, T const &x) { long index_value = seq.index(x); if (index_value == seq.size()) throw types::ValueError(__builtin__::anonymous::str(x) + " is not in list"); return index_value; }
types::none_type insert(types::list<T> &seq, long n, F &&value) { n = n % (1 + seq.size()); // +1 because we want to be able to insert at // the end of seq if (n < 0) n += seq.size(); seq.insert(n, std::forward<F>(value)); return __builtin__::None; }
types::ndarray<typename U::type, U::value> select(types::list<T> const &condlist, types::list<U> const &choicelist, typename U::dtype _default) { constexpr size_t N = U::value; auto &&choicelist0_shape = choicelist[0].shape(); types::ndarray<T, N> out(choicelist0_shape, _default); types::ndarray<T, N> selected(choicelist0_shape(), false); long size = selected.flat_size(); for (long i = 0; i < condlist.size() && size != 0; i++) size = _select(choicelist[i].begin(), choicelist[i].end(), out.begin(), selected.begin(), condlist.begin(), size, utils::int_<N>()); return out; }
types::ndarray<T, N> select(types::list<types::ndarray<U, N>> const &condlist, types::list<types::ndarray<T, N>> const &choicelist, T _default) { types::ndarray<T, N> out(choicelist[0].shape(), _default); for (long i = 0; i < out.flat_size(); ++i) for (long j = 0; j < condlist.size(); ++j) if (condlist[j].buffer[i]) { out.buffer[i] = choicelist[j].buffer[i]; break; } return out; }
/* Generate next permutation * * If the size of the permutation is smaller than the size of the * pool, we may have to iterate multiple times */ permutations_iterator& operator++() { if(_size!=pool.size()) { // Slow path, the iterator is a "view" of a prefix smaller // than the the pool size // FIXME a better implementation would be to avoid // std::next_permutation, but only in the slow path types::list<int> prev_permut(curr_permut.begin(), curr_permut.begin()+_size); types::list<int> new_permut; while((end = std::next_permutation(curr_permut.begin(), curr_permut.end()))) { // Check if the prefix of the new permutation is // different of the previous one types::list<int> new_permut(curr_permut.begin(), curr_permut.begin()+_size); if(!(prev_permut==new_permut)) break; } } else { end = std::next_permutation(curr_permut.begin(), curr_permut.end()); } return *this; }
types::none_type append(types::list<T> &seq, F &&value) { seq.push_back(std::forward<F>(value)); return __builtin__::None; }
types::none_type sort(types::list<T> &seq) { std::sort(seq.begin(),seq.end()); return __builtin__::None; }
long index(types::list<T> &seq, T const& x) { return seq.index(x); }
types::none_type reverse(types::list<T> &seq) { std::reverse(seq.begin(),seq.end()); return __builtin__::None; }
bool operator==(permutations_iterator const& other) const { if(other.end != end) return false; return std::equal(curr_permut.begin(), curr_permut.end(), other.curr_permut.begin()); }