30 typedef typename Real_::DoubleType Double_;
41 typedef function<bool (const vector<const T*>&)> Func;
45 State(
const vector<T>& list,
const Func& func =
nullptr)
46 : list(list), func(func),
count(0),
countMax(0), k(-1), n(0) {}
48 const vector<T>& list;
50 vector<const T*> perm;
80 vector<const T*>&
operator*()
const {
return _ps->perm; }
81 vector<const T*>*
operator->()
const {
return &_ps->perm; }
89 bool isEnd()
const {
return !_ps; }
96 static Range_<Iter<T>, Iter<T>>
97 range(
const vector<T>& list,
const typename Iter<T>::Func& func =
nullptr)
106 if (isEnd() || _ps->list.size() == 0)
109 _ps->n = _ps->list.size();
112 _ps->a.resize(_ps->n+1, -1);
113 _ps->l.resize(_ps->n+1, -1);
114 _ps->u.resize(_ps->n+1, -1);
116 for (
sdt i = 0; i < _ps->n; ++i)
143 _ps->a[_ps->k] = _ps->q;
149 _ps->perm.resize(_ps->k);
150 for (
sdt i = 1; i <= _ps->k; ++i)
151 _ps->perm[i-1] = &_ps->list[_ps->a[i]-1];
153 func = _ps->func(const_cast<
const vector<const T*>&>(_ps->perm));
156 if (func && _ps->k < _ps->n)
159 _ps->u[_ps->k] = _ps->p;
160 _ps->l[_ps->p] = _ps->l[_ps->q];
175 _ps->perm.resize(_ps->k);
176 for (
sdt i = 1; i <= _ps->k; ++i)
177 _ps->perm[i-1] = &_ps->list[_ps->a[i]-1];
184 _ps->q = _ps->l[_ps->p];
196 _ps->p = _ps->u[_ps->k];
197 _ps->q = _ps->a[_ps->k];
198 _ps->l[_ps->p] = _ps->q;
202 _ps->q = _ps->l[_ps->p];
218 if (!isEnd() && !rhs.isEnd())
219 return _ps == rhs._ps;
220 if (isEnd() && rhs.isEnd())
224 const Iter& it = isEnd() ? rhs : *
this;
225 return it._ps->k < 0;
const vector< const T * > & reference
Definition: Permute.h:68
Algebra.
Definition: Alge.h:13
Permute_< Double > Permute_d
Definition: Permute.h:231
std::forward_iterator_tag iterator_category
Definition: Permute.h:64
Generate all permutations of a list. A functor can be specified for fast culling of entire subtrees o...
Definition: Permute.h:27
sdt count() const
Get current permutation number. Every perm has a unique associated number: the first perm is 1...
Definition: Permute.h:84
ptrdiff_t sdt
Size difference type, shorthand for ptrdiff_t.
Definition: Core.h:92
sdt countMax() const
Get total number of permutations for this list.
Definition: Permute.h:86
Reference-counted object for intrusive shared pointers.
Definition: SharedPtr.h:93
std::enable_if< mt::isIterator< Iter1 >::value, Range_< Iter1, Iter2 > >::type range(Iter1 &&first, Iter2 &&last)
Range from iterators [first, last)
Definition: Range.h:116
bool operator!=(const Iter &rhs) const
Definition: Permute.h:77
const vector< const T * > value_type
Definition: Permute.h:65
Permute_< Float > Permute_f
Definition: Permute.h:230
static Range_< Iter< T >, Iter< T > > range(const vector< T > &list, const typename Iter< T >::Func &func=nullptr)
Create a permutation range.
Definition: Permute.h:97
Permute_< Real > Permute
Definition: Permute.h:229
vector< const T * > * operator->() const
Definition: Permute.h:81
const vector< const T * > * pointer
Definition: Permute.h:67
static Real factorial(Real n)
Factorial, N!. N can be any Real including fractional numbers.
Definition: Gamma.cpp:407
Numeric type information, use numeral() to get instance safely from a static context.
Definition: Numeral.h:17
bool operator==(const Iter &rhs) const
Definition: Permute.h:216
sdt difference_type
Definition: Permute.h:66
Iter & operator++()
Definition: Permute.h:128
double Real
Definition: Real.h:14
vector< const T * > & operator*() const
Get current permutation list.
Definition: Permute.h:80
bool operator==(const String &lhs, const String &rhs)
Definition: String.h:139
Iter operator++(int)
Definition: Permute.h:74
Global Honeycomb namespace.
Iter for permutations of a list.
Definition: Permute.h:38