25#ifndef AGG_ARRAY_INCLUDED
26#define AGG_ARRAY_INCLUDED
31#include "agg_basics.h"
45 unsigned size()
const {
49 const T& operator[](
unsigned i)
const {
53 T& operator[](
unsigned i) {
57 const T& at(
unsigned i)
const {
65 T value_at(
unsigned i)
const {
75template <
class T,
unsigned Size>
84 memcpy(m_array, c,
sizeof(T) * Size);
88 memcpy(m_array, c,
sizeof(T) * Size);
92 static unsigned size() {
96 const T& operator[](
unsigned i)
const {
100 T& operator[](
unsigned i) {
104 const T& at(
unsigned i)
const {
112 T value_at(
unsigned i)
const {
121template <
class T,
unsigned Size>
124 typedef T value_type;
138 void add(
const T& v) {
139 m_array[m_size++] = v;
142 void push_back(
const T& v) {
143 m_array[m_size++] = v;
146 void inc_size(
unsigned size) {
150 unsigned size()
const {
154 const T& operator[](
unsigned i)
const {
158 T& operator[](
unsigned i) {
162 const T& at(
unsigned i)
const {
170 T value_at(
unsigned i)
const {
183 typedef T value_type;
201 memcpy(m_array, v.m_array,
sizeof(T) * m_size);
204 void resize(
unsigned size) {
205 if (size != m_size) {
213 memcpy(m_array, v.m_array,
sizeof(T) * m_size);
217 unsigned size()
const {
221 const T& operator[](
unsigned i)
const {
225 T& operator[](
unsigned i) {
229 const T& at(
unsigned i)
const {
237 T value_at(
unsigned i)
const {
241 const T* data()
const {
261 typedef T value_type;
272 pod_vector(
unsigned cap,
unsigned extra_tail = 0);
280 void capacity(
unsigned cap,
unsigned extra_tail = 0);
282 unsigned capacity()
const {
288 void allocate(
unsigned size,
unsigned extra_tail = 0);
291 void resize(
unsigned new_size);
294 memset(m_array, 0,
sizeof(T) * m_size);
297 void add(
const T& v) {
298 m_array[m_size++] = v;
301 void push_back(
const T& v) {
302 m_array[m_size++] = v;
305 void insert_at(
unsigned pos,
const T& val);
307 void inc_size(
unsigned size) {
311 unsigned size()
const {
315 unsigned byte_size()
const {
316 return m_size *
sizeof(T);
319 void serialize(int8u* ptr)
const;
321 void deserialize(
const int8u* data,
unsigned byte_size);
323 const T& operator[](
unsigned i)
const {
327 T& operator[](
unsigned i) {
331 const T& at(
unsigned i)
const {
339 T value_at(
unsigned i)
const {
343 const T* data()
const {
359 void cut_at(
unsigned num) {
360 if (num < m_size) m_size = num;
373 if (cap > m_capacity) {
375 m_capacity = cap + extra_tail;
382void pod_vector<T>::allocate(
unsigned size,
unsigned extra_tail) {
383 capacity(size, extra_tail);
389void pod_vector<T>::resize(
unsigned new_size) {
390 if (new_size > m_size) {
391 if (new_size > m_capacity) {
392 T* data = pod_allocator<T>::allocate(new_size);
393 memcpy(data, m_array, m_size *
sizeof(T));
394 pod_allocator<T>::deallocate(m_array, m_capacity);
404pod_vector<T>::pod_vector(
unsigned cap,
unsigned extra_tail)
406 m_capacity(cap + extra_tail),
407 m_array(pod_allocator<T>::allocate(m_capacity)) {}
411pod_vector<T>::pod_vector(
const pod_vector<T>& v)
413 m_capacity(v.m_capacity),
414 m_array(v.m_capacity ? pod_allocator<T>::allocate(v.m_capacity) : 0) {
415 memcpy(m_array, v.m_array,
sizeof(T) * v.m_size);
420const pod_vector<T>& pod_vector<T>::operator=(
const pod_vector<T>& v) {
422 if (v.m_size) memcpy(m_array, v.m_array,
sizeof(T) * v.m_size);
428void pod_vector<T>::serialize(int8u* ptr)
const {
429 if (m_size) memcpy(ptr, m_array, m_size *
sizeof(T));
434void pod_vector<T>::deserialize(
const int8u* data,
unsigned byte_size) {
435 byte_size /=
sizeof(T);
437 if (byte_size) memcpy(m_array, data, byte_size *
sizeof(T));
442void pod_vector<T>::insert_at(
unsigned pos,
const T& val) {
444 m_array[m_size] = val;
446 memmove(m_array + pos + 1, m_array + pos, (m_size - pos) *
sizeof(T));
463template <
class T,
unsigned S = 6>
468 block_size = 1 << block_shift,
469 block_mask = block_size - 1
497 void free_tail(
unsigned size);
499 void add(
const T&
val);
501 void push_back(
const T&
val) {
505 void modify_last(
const T&
val);
511 void add_array(
const T* ptr,
unsigned num_elem) {
517 template <
class DataAccessor>
519 while (data.size()) {
525 void cut_at(
unsigned size) {
526 if (size < m_size) m_size = size;
529 unsigned size()
const {
533 const T& operator[](
unsigned i)
const {
534 return m_blocks[
i >> block_shift][
i & block_mask];
537 T& operator[](
unsigned i) {
538 return m_blocks[
i >> block_shift][
i & block_mask];
541 const T& at(
unsigned i)
const {
542 return m_blocks[
i >> block_shift][
i & block_mask];
546 return m_blocks[
i >> block_shift][
i & block_mask];
549 T value_at(
unsigned i)
const {
550 return m_blocks[
i >> block_shift][
i & block_mask];
553 const T& curr(
unsigned idx)
const {
557 T& curr(
unsigned idx) {
561 const T& prev(
unsigned idx)
const {
562 return (*
this)[(
idx + m_size - 1) % m_size];
565 T& prev(
unsigned idx) {
566 return (*
this)[(
idx + m_size - 1) % m_size];
569 const T& next(
unsigned idx)
const {
570 return (*
this)[(
idx + 1) % m_size];
573 T& next(
unsigned idx) {
574 return (*
this)[(
idx + 1) % m_size];
577 const T& last()
const {
578 return (*
this)[m_size - 1];
582 return (*
this)[m_size - 1];
585 unsigned byte_size()
const;
587 void serialize(int8u* ptr)
const;
589 void deserialize(
const int8u* data,
unsigned byte_size);
591 void deserialize(
unsigned start,
const T&
empty_val,
const int8u* data,
unsigned byte_size);
593 template <
class ByteAccessor>
599 int8u* ptr = (int8u*)data_ptr();
600 for (
unsigned j = 0;
j <
sizeof(
T); ++
j) {
608 template <
class ByteAccessor>
610 while (m_size <
start) {
618 ptr = (int8u*)(&((*
this)[
start +
i]));
620 ptr = (int8u*)data_ptr();
623 for (
unsigned j = 0;
j <
sizeof(
T); ++
j) {
630 const T* block(
unsigned nb)
const {
635 void allocate_block(
unsigned nb);
640 unsigned m_num_blocks;
641 unsigned m_max_blocks;
643 unsigned m_block_ptr_inc;
647template <
class T,
unsigned S>
650 T**
blk = m_blocks + m_num_blocks - 1;
651 while (m_num_blocks--) {
656 pod_allocator<T*>::deallocate(m_blocks, m_max_blocks);
660template <
class T,
unsigned S>
661void pod_bvector<T, S>::free_tail(
unsigned size) {
663 unsigned nb = (size + block_mask) >> block_shift;
664 while (m_num_blocks > nb) {
665 pod_allocator<T>::deallocate(m_blocks[--m_num_blocks], block_size);
667 if (m_num_blocks == 0) {
668 pod_allocator<T*>::deallocate(m_blocks, m_max_blocks);
677template <
class T,
unsigned S>
678pod_bvector<T, S>::pod_bvector()
683 m_block_ptr_inc(block_size) {}
686template <
class T,
unsigned S>
687pod_bvector<T, S>::pod_bvector(
unsigned block_ptr_inc)
692 m_block_ptr_inc(block_ptr_inc) {}
695template <
class T,
unsigned S>
696pod_bvector<T, S>::pod_bvector(
const pod_bvector<T, S>& v)
698 m_num_blocks(v.m_num_blocks),
699 m_max_blocks(v.m_max_blocks),
700 m_blocks(v.m_max_blocks ? pod_allocator<T*>::allocate(v.m_max_blocks) : 0),
701 m_block_ptr_inc(v.m_block_ptr_inc) {
703 for (i = 0; i < v.m_num_blocks; ++i) {
704 m_blocks[i] = pod_allocator<T>::allocate(block_size);
705 memcpy(m_blocks[i], v.m_blocks[i], block_size *
sizeof(T));
710template <
class T,
unsigned S>
711const pod_bvector<T, S>& pod_bvector<T, S>::operator=(
const pod_bvector<T, S>& v) {
713 for (i = m_num_blocks; i < v.m_num_blocks; ++i) {
716 for (i = 0; i < v.m_num_blocks; ++i) {
717 memcpy(m_blocks[i], v.m_blocks[i], block_size *
sizeof(T));
724template <
class T,
unsigned S>
725void pod_bvector<T, S>::allocate_block(
unsigned nb) {
726 if (nb >= m_max_blocks) {
727 T** new_blocks = pod_allocator<T*>::allocate(m_max_blocks + m_block_ptr_inc);
730 memcpy(new_blocks, m_blocks, m_num_blocks *
sizeof(T*));
732 pod_allocator<T*>::deallocate(m_blocks, m_max_blocks);
734 m_blocks = new_blocks;
735 m_max_blocks += m_block_ptr_inc;
737 m_blocks[nb] = pod_allocator<T>::allocate(block_size);
742template <
class T,
unsigned S>
743inline T* pod_bvector<T, S>::data_ptr() {
744 unsigned nb = m_size >> block_shift;
745 if (nb >= m_num_blocks) {
748 return m_blocks[nb] + (m_size & block_mask);
752template <
class T,
unsigned S>
753inline void pod_bvector<T, S>::add(
const T& val) {
759template <
class T,
unsigned S>
760inline void pod_bvector<T, S>::remove_last() {
761 if (m_size) --m_size;
765template <
class T,
unsigned S>
766void pod_bvector<T, S>::modify_last(
const T& val) {
772template <
class T,
unsigned S>
773int pod_bvector<T, S>::allocate_continuous_block(
unsigned num_elements) {
774 if (num_elements < block_size) {
776 unsigned rest = block_size - (m_size & block_mask);
778 if (num_elements <= rest) {
782 m_size += num_elements;
791 m_size += num_elements;
798template <
class T,
unsigned S>
799unsigned pod_bvector<T, S>::byte_size()
const {
800 return m_size *
sizeof(T);
804template <
class T,
unsigned S>
805void pod_bvector<T, S>::serialize(int8u* ptr)
const {
807 for (i = 0; i < m_size; i++) {
808 memcpy(ptr, &(*
this)[i],
sizeof(T));
814template <
class T,
unsigned S>
815void pod_bvector<T, S>::deserialize(
const int8u* data,
unsigned byte_size) {
817 byte_size /=
sizeof(T);
818 for (
unsigned i = 0; i < byte_size; ++i) {
820 memcpy(ptr, data,
sizeof(T));
828template <
class T,
unsigned S>
829void pod_bvector<T, S>::deserialize(
unsigned start,
const T& empty_val,
const int8u* data,
unsigned byte_size) {
830 while (m_size < start) {
834 byte_size /=
sizeof(T);
835 for (
unsigned i = 0; i < byte_size; ++i) {
836 if (start + i < m_size) {
837 memcpy(&((*
this)[start + i]), data,
sizeof(T));
840 memcpy(ptr, data,
sizeof(T));
864 block_type*
blk = m_blocks + m_num_blocks - 1;
865 while (m_num_blocks--) {
883 : m_block_size(block_size),
891 int8u* allocate(
unsigned size,
unsigned alignment = 1) {
892 if (size == 0)
return 0;
893 if (size <= m_rest) {
894 int8u* ptr = m_buf_ptr;
900 if (size <= m_rest) {
905 allocate_block(size);
917 void allocate_block(
unsigned size) {
918 if (size < m_block_size) size = m_block_size;
919 if (m_num_blocks >= m_max_blocks) {
927 m_max_blocks += m_block_ptr_inc;
930 m_blocks[m_num_blocks].size = size;
937 unsigned m_block_size;
938 unsigned m_block_ptr_inc;
939 unsigned m_num_blocks;
940 unsigned m_max_blocks;
941 block_type* m_blocks;
947enum quick_sort_threshold_e {
948 quick_sort_threshold = 9
953inline void swap_elements(T& a, T& b) {
960template <
class Array,
class Less>
961void quick_sort(Array& arr, Less less) {
962 if (arr.size() < 2)
return;
964 typename Array::value_type* e1;
965 typename Array::value_type* e2;
969 int limit = arr.size();
973 int len = limit - base;
979 if (len > quick_sort_threshold) {
981 pivot = base + len / 2;
982 swap_elements(arr[base], arr[pivot]);
990 if (less(*e1, *e2)) swap_elements(*e1, *e2);
994 if (less(*e1, *e2)) swap_elements(*e1, *e2);
998 if (less(*e1, *e2)) swap_elements(*e1, *e2);
1002 while (less(arr[i], arr[base]));
1004 while (less(arr[base], arr[j]));
1010 swap_elements(arr[i], arr[j]);
1013 swap_elements(arr[base], arr[j]);
1016 if (j - base > limit - i) {
1031 for (; i < limit; j = i, i++) {
1032 for (; less(*(e1 = &(arr[j + 1])), *(e2 = &(arr[j]))); j--) {
1033 swap_elements(*e1, *e2);
1054template <
class Array,
class Equal>
1055unsigned remove_duplicates(Array& arr, Equal equal) {
1056 if (arr.size() < 2)
return arr.size();
1059 for (i = 1, j = 1; i < arr.size(); i++) {
1060 typename Array::value_type& e = arr[i];
1061 if (!equal(e, arr[i - 1])) {
1069template <
class Array>
1070void invert_container(Array& arr) {
1072 int j = arr.size() - 1;
1074 swap_elements(arr[i++], arr[j--]);
1079template <
class Array,
class Value,
class Less>
1080unsigned binary_search_pos(
const Array& arr,
const Value& val, Less less) {
1081 if (arr.size() == 0)
return 0;
1084 unsigned end = arr.size() - 1;
1086 if (less(val, arr[0]))
return 0;
1087 if (less(arr[end], val))
return end + 1;
1089 while (end - beg > 1) {
1090 unsigned mid = (end + beg) >> 1;
1091 if (less(val, arr[mid]))
1104template <
class Array>
1107 typedef typename Array::value_type value_type;
1114 unsigned size()
const {
1118 const value_type& operator[](
unsigned i)
const {
1119 return m_array[m_start +
i];
1122 value_type& operator[](
unsigned i) {
1123 return m_array[m_start +
i];
1126 const value_type& at(
unsigned i)
const {
1127 return m_array[m_start +
i];
1130 value_type& at(
unsigned i) {
1131 return m_array[m_start +
i];
1134 value_type value_at(
unsigned i)
const {
1135 return m_array[m_start +
i];
1145inline bool int_less(
int a,
int b) {
1150inline bool int_greater(
int a,
int b) {
1155inline bool unsigned_less(
unsigned a,
unsigned b) {
1160inline bool unsigned_greater(
unsigned a,
unsigned b) {
Definition agg_array.h:855
Definition agg_array.h:37
Definition agg_array.h:181
Definition agg_array.h:76
Definition agg_array.h:122
Definition agg_array.h:464
Definition agg_array.h:259
Definition agg_array.h:1105
Definition agg_basics.h:45