37 #ifndef GMM_SUB_INDEX_H__
38 #define GMM_SUB_INDEX_H__
48 struct basic_index :
public std::vector<size_t> {
56 return (i < size()) ? std::vector<size_t>::operator[](i) :
size_type(-1);
59 basic_index() : nb_ref(1) {}
60 basic_index(
size_type j) : std::vector<size_t>(j), nb_ref(1) {}
61 template <
typename IT> basic_index(IT b, IT e)
62 : std::vector<size_t>(e-b), nb_ref(1) { std::copy(b, e, begin()); }
63 basic_index(
const basic_index *pbi) : nb_ref(1) {
64 const_iterator it = pbi->begin(), ite = pbi->end();
66 for ( ; it != ite; ++it) i = std::max(i, *it);
68 for (it = pbi->begin(), i = 0; it != ite; ++it, ++i)
69 std::vector<size_t>::operator[](*it) = i;
72 std::swap(std::vector<size_t>::operator[](i),
73 std::vector<size_t>::operator[](j));
78 typedef basic_index *pbasic_index;
80 struct index_generator {
82 template <
typename IT>
static pbasic_index create_index(IT begin, IT end)
83 {
return new basic_index(begin, end); }
84 static pbasic_index create_rindex(pbasic_index pbi)
85 {
return new basic_index(pbi); }
86 static void attach(pbasic_index pbi) {
if (pbi) pbi->nb_ref++; }
87 static void unattach(pbasic_index pbi)
88 {
if (pbi && --(pbi->nb_ref) == 0)
delete pbi; }
95 typedef basic_index base_type;
96 typedef base_type::const_iterator const_iterator;
98 mutable pbasic_index ind;
99 mutable pbasic_index rind;
102 std::vector<size_t>::const_iterator it = ind->begin(), ite = ind->end();
103 if (it != ite) { first_=last_= *it; ++it; }
else { first_=last_= 0; }
104 for (; it != ite; ++it) {
105 first_ = std::min(first_, *it);
106 last_ = std::max(last_, *it);
110 inline void test_rind()
const
111 {
if (!rind) rind = index_generator::create_rindex(ind); }
112 size_type size()
const {
return ind->size(); }
113 size_type first()
const {
return first_; }
118 if (i < rind->size())
return (*rind)[i];
122 const_iterator begin()
const {
return ind->begin(); }
123 const_iterator end()
const {
return ind->end(); }
124 const_iterator rbegin()
const { test_rind();
return rind->begin(); }
125 const_iterator rend()
const { test_rind();
return rind->end(); }
127 sub_index() : ind(0), rind(0) {}
128 template <
typename IT> sub_index(IT it, IT ite)
129 : ind(index_generator::create_index(it, ite)), rind(0)
131 template <
typename CONT> sub_index(
const CONT &c)
132 : ind(index_generator::create_index(c.begin(), c.end())), rind(0)
135 index_generator::unattach(rind);
136 index_generator::unattach(ind);
138 sub_index(
const sub_index &si) : first_(si.first_), last_(si.last_),
139 ind(si.ind), rind(si.rind)
140 { index_generator::attach(rind); index_generator::attach(ind); }
141 sub_index &operator =(
const sub_index &si) {
142 index_generator::unattach(rind);
143 index_generator::unattach(ind);
144 ind = si.ind; rind = si.rind;
145 index_generator::attach(rind);
146 index_generator::attach(ind);
147 first_ = si.first_; last_ = si.last_;
152 struct unsorted_sub_index :
public sub_index {
153 typedef basic_index base_type;
154 typedef base_type::const_iterator const_iterator;
156 template <
typename IT> unsorted_sub_index(IT it, IT ite)
157 : sub_index(it, ite) {}
158 template <
typename CONT> unsorted_sub_index(
const CONT &c)
160 unsorted_sub_index() {}
161 unsorted_sub_index(
const unsorted_sub_index &si) : sub_index((const sub_index &)(si)) { }
162 unsorted_sub_index &operator =(
const unsorted_sub_index &si)
163 { sub_index::operator =(si);
return *
this; }
165 GMM_ASSERT2(ind->nb_ref <= 1,
"Operation not allowed on this index");
166 if (rind) rind->swap((*ind)[i], (*ind)[j]);
171 inline std::ostream &operator << (std::ostream &o,
const sub_index &si) {
173 if (si.size() != 0) o << si.index(0);
174 for (
size_type i = 1; i < si.size(); ++i) o <<
", " << si.index(i);
179 struct sub_interval {
182 size_type size()
const {
return max - min; }
188 {
if (i >= min && i < max)
return i - min;
return size_type(-1); }
193 inline std::ostream &operator << (std::ostream &o,
const sub_interval &si)
194 { o <<
"sub_interval(" << si.min <<
", " << si.size() <<
")";
return o; }
199 size_type size()
const {
return (max - min) / N; }
201 size_type last()
const {
return (min == max) ? max : max+1-N; }
205 if (i >= min && i < max) {
213 : min(mi), max(mi+l*n), N(n) {}
217 inline std::ostream &operator << (std::ostream &o,
const sub_slice &si) {
218 o <<
"sub_slice(" << si.min <<
", " << si.size() <<
", " << si.step()
222 template<
class SUBI>
struct index_is_sorted
223 {
typedef linalg_true bool_type; };
224 template<>
struct index_is_sorted<unsorted_sub_index>
225 {
typedef linalg_false bool_type; };
void resize(V &v, size_type n)
*/
Basic definitions and tools of GMM.
size_t size_type
used as the common size type in the library