46 template<
class T,
unsigned char pks = 5>
class dynamic_tas;
48 template<
class T,
unsigned char pks = 5>
struct dnt_iterator
51 typedef value_type* pointer;
52 typedef value_type& reference;
54 typedef ptrdiff_t difference_type;
55 typedef std::bidirectional_iterator_tag iterator_category;
57 typename dynamic_array<T,pks>::iterator id;
58 bit_vector::iterator ib;
62 dnt_iterator(dynamic_tas<T,pks> &da, bit_vector &bv,
size_type ii)
63 : id(da, ii), ib(bv, ii) { lt = da.index().last_true(); }
65 inline size_type index(
void)
const {
return id.index(); }
67 dnt_iterator &operator ++();
68 dnt_iterator &operator --()
69 {
while (!*(--ib)) --id; --id;
return *
this; }
70 dnt_iterator operator ++(
int)
71 { dnt_iterator tmp = *
this; ++(*this);
return tmp; }
72 dnt_iterator operator --(
int)
73 { dnt_iterator tmp = *
this; --(*this);
return tmp; }
75 difference_type
operator -(
const dnt_iterator &i)
const
79 reference operator *()
const {
return (*
id); }
80 pointer operator->()
const {
return &(operator*()); }
82 bool operator ==(
const dnt_iterator &i)
const {
return i.id==id;}
83 bool operator !=(
const dnt_iterator &i)
const {
return i.id!=id;}
84 bool operator < (
const dnt_iterator &i)
const {
return id <i.id;}
87 template<
class T,
unsigned char pks> dnt_iterator<T, pks> &
88 dnt_iterator<T, pks>::operator ++()
89 { ++ib; ++id;
while(
id.in <= lt && !*ib) {++ib; ++id; }
return *
this; }
91 template<
class T,
unsigned char pks = 5>
struct dnt_const_iterator {
93 typedef const value_type* pointer;
94 typedef const value_type& reference;
96 typedef ptrdiff_t difference_type;
97 typedef std::bidirectional_iterator_tag iterator_category;
99 typename dynamic_array<T,pks>::const_iterator id;
100 bit_vector::const_iterator ib;
103 dnt_const_iterator(
void) {}
104 dnt_const_iterator(
const dynamic_tas<T,pks> &da,
size_type ii)
105 : id(da, ii), ib(da.index(), ii) { lt = da.index().last_true(); }
106 dnt_const_iterator(
const dnt_iterator<T,pks> &it)
107 : id(it.id), ib(it.ib), lt(it.lt) { }
109 inline size_type index(
void)
const {
return id.index(); }
111 dnt_const_iterator &operator ++()
112 { ++ib; ++id;
while(
id.in <= lt && !*ib) {++ib; ++id; }
return *
this; }
113 dnt_const_iterator &operator --()
114 {
while (!*(--ib)) --id; --id;
return *
this; }
115 dnt_const_iterator operator ++(
int)
116 { dnt_const_iterator tmp = *
this; ++(*this);
return tmp; }
117 dnt_const_iterator operator --(
int)
118 { dnt_const_iterator tmp = *
this; --(*this);
return tmp; }
120 difference_type
operator -(
const dnt_const_iterator &i)
const
121 {
return id - i.id; }
123 reference operator *()
const {
return (*
id); }
124 pointer operator->()
const {
return &(operator*()); }
126 bool operator ==(
const dnt_const_iterator &i)
const
127 {
return i.id == id;}
128 bool operator !=(
const dnt_const_iterator &i)
const
129 {
return i.id != id;}
130 bool operator < (
const dnt_const_iterator &i)
const
134 template<
class T,
unsigned char pks>
class dynamic_tas
135 :
public dynamic_array<T, pks> {
140 typedef typename dynamic_array<T, pks>::iterator iterator;
141 typedef typename dynamic_array<T, pks>::const_iterator const_iterator;
142 typedef dnt_iterator<T, pks> tas_iterator;
143 typedef dnt_const_iterator<T, pks> const_tas_iterator;
144 typedef typename dynamic_array<T, pks>::size_type
size_type;
149 {
return (ind.card() == 0) ? 0 : (ind.last_true() + 1); }
151 {
return (ind.card() == 0) ? 0 : ind.first_true(); }
153 {
return (ind.card() == 0) ? 0 : ind.last_true(); }
154 size_type card(
void)
const {
return ind.card(); }
156 tas_iterator tas_begin(
void)
157 {
return tas_iterator(*
this, ind, ind_first()); }
158 const_tas_iterator tas_begin(
void)
const
159 {
return const_tas_iterator(*
this, ind_first()); }
160 tas_iterator tas_end(
void) {
return tas_iterator(*
this, ind, size()); }
161 const_tas_iterator tas_end(
void)
const
162 {
return const_tas_iterator(*
this, size()); }
164 const bit_vector &index(
void)
const {
return ind; }
165 bool index_valid(
size_type i)
const {
return ind[i]; }
166 bool empty(
void)
const {
return (ind.card() == 0); }
171 {
size_type n=ind.first_false(); ind[n]=
true; (*this)[n]=e;
return n; }
172 void add_to_index(
size_type i,
const T &e) { ind[i]=
true; (*this)[i]=e; }
173 void sup(
size_type n) { ind[n] =
false; }
177 template<
class T,
unsigned char pks>
179 bool ti = ind[i], tj = ind[j]; ind.swap(i,j);
180 if (!ti && tj) (*this)[i] = (*this)[j];
181 if (ti && !tj) (*this)[j] = (*this)[i];
182 if (ti && tj) std::swap((*
this)[i], (*
this)[j]);
185 template<
class T,
unsigned char pks>
186 void dynamic_tas<T, pks>::compact(
void) {
188 while (ind.last_true() >= ind.card())
189 swap(ind.first_false(), ind.last_true());