1 #ifndef PROTON_VECTOR_HEADER
2 #define PROTON_VECTOR_HEADER
13 #include <initializer_list>
18 #include <proton/pool.hpp>
32 template <
typename T,
typename A,
typename V>
33 std::vector<T,A>& operator<<(std::vector<T,A>& x, V&& val)
44 template <
typename T,
typename A,
typename V>
45 std::vector<T,A>&
operator>>(std::vector<T,A>& x, V& val)
58 template <
typename T,
typename A>
59 std::ostream& operator<<(std::ostream& s, const std::vector<T,A>& x)
74 template <
typename T,
typename A>
75 std::wostream& operator<<(std::wostream& s, const std::vector<T,A>& x)
93 template <
typename T,
typename A>
94 void sort(std::vector<T,A>& x)
96 std::sort(x.begin(), x.end());
104 template <
typename T,
typename A>
105 T&
get(std::vector<T,A>& x,
long i)
107 unsigned long s=x.size();
110 if(i<0 || (
unsigned long)i >= s )
111 PROTON_ERR(
"out of range: look up "<<i<<
" in vector whose size is " << s);
120 template <
typename T,
typename A>
121 const T&
get(
const std::vector<T,A>& x,
long i)
123 unsigned long s=x.size();
126 if(i<0 || (
unsigned long)i >= s )
127 PROTON_ERR(
"out of range: look up "<<i<<
" in vector whose size is " << s);
136 template <
typename T,
typename A>
137 std::vector<T,A> sub(
const std::vector<T,A>& x,
long first)
140 unsigned long s=x.size();
145 if((
unsigned long)first >= s )
148 auto start=x.begin();
149 std::copy(start+first, x.end(), std::back_inserter(r));
159 template <
typename T,
typename A>
160 std::vector<T,A> sub(
const std::vector<T,A>& x,
long first,
long last)
163 unsigned long s=x.size();
168 if((
unsigned long)first >= s )
176 if((
unsigned long)last>s)
178 auto start=x.begin();
179 std::copy(start+first, start+last, std::back_inserter(r));
185 template <
typename T,
typename A=smart_allocator<T> >
188 typedef std::vector<T,A> baseT;
189 typedef typename baseT::difference_type offset_t;
192 offset_t __offset(offset_t i)
const
199 offset_t offset(offset_t i)
const
202 PROTON_THROW_IF(i<0 || (
size_t)i>=this->size(),
"out of range, offset is " << i
203 <<
" while size is " << this->size() );
207 int fix_offset(offset_t begin)
const
209 offset_t size=(offset_t)this->size();
210 begin=__offset(begin);
219 void fix_range(offset_t& begin, offset_t& end)
const
221 offset_t size=(offset_t)this->size();
222 begin=__offset(begin);
224 if(begin>=size || end<=0 || end<=begin){
238 template<
typename ...argT>
vector_(argT&& ...a):baseT(a...)
256 explicit vector_(
const baseT& x):baseT(x)
259 vector_(baseT&& x)noexcept:baseT(x)
304 return reinterpret_cast<baseT&
>(*this);
311 return *(this->begin()+offset(i));
318 return *(this->begin()+offset(i));
325 auto begin=this->begin();
326 return vector_(begin+offset(i),this->end());
333 auto begin=this->begin();
335 return vector_(begin+i,begin+j);
344 r.reserve((j-i)/k+1);
345 auto it=this->begin()+i;
346 for(offset_t n=i; n<j; n+=k,it+=k)
367 return std::count(this->begin(), this->end(), x);
374 this->erase(this->begin()+offset(i));
379 void del(offset_t i, offset_t j)
382 auto begin=this->begin();
383 this->erase(begin+i, begin+j);
390 this->erase(this->begin()+offset(i), this->end());
395 template<
typename seqT>
void extend(
const seqT& x)
402 template<
typename seqT>
void extend(seqT&& x)
415 auto begin=this->begin(), end=this->end();
416 auto it=std::find(begin, end, val);
418 throw std::invalid_argument(
"The given value doesn't exist in this sequence.");
429 auto it=this->begin()+ offset(i);
433 void insert(offset_t i, T&& val)
435 auto it=this->begin()+offset(i);
446 auto it=this->begin()+offset(i);
456 void remove(
const T& val)
458 auto begin=this->begin(), end=this->end();
459 auto it=std::find(begin, end, val);
461 throw std::invalid_argument(
"The given value doesn't exist in this sequence.");
481 template<
class cmpT>
void sort(cmpT cmp)
483 std::sort(this->begin(), this->end(),cmp);
494 template<
typename T,
typename A,
typename X>
498 r.
extend(std::forward<X>(t));
502 template<
typename T,
typename A,
typename X>
503 vector_<T,A>
operator+(std::vector<T,A>&& s, X&& t)
506 r.extend(std::forward<X>(t));
512 template<
typename T,
typename A>
516 r.reserve(s.size()*n);
517 for(
size_t i=0; i<n; i++)
524 template<
typename T,
typename A>
532 template<
typename T,
typename A>
538 template<
typename T,
typename A>
539 const vector_<T,A>&
cast_(
const std::vector<T,A>& x)
541 return reinterpret_cast<const vector_<T,A>&
>(x);
544 template<
typename T,
typename A>
545 vector_<T,A>&&
cast_(std::vector<T,A>&& x)
547 return reinterpret_cast<vector_<T,A>&&
>(x);
550 template<
typename T,
typename A>
551 const vector_<T,A>&&
cast_(
const std::vector<T,A>&& x)
553 return reinterpret_cast<const vector_<T,A>&&
>(x);
561 #endif // PROTON_VECTOR_HEADER