Scopira  20080306
array.h
1 
2 /*
3  * Copyright (c) 2002-2004 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED_SCOPIRA_TOOL_ARRAY_H__
15 #define __INCLUDED_SCOPIRA_TOOL_ARRAY_H__
16 
17 #include <assert.h>
18 #include <stddef.h> //for size_t
19 #include <string.h> //for memset
20 
21 // THIS FILE HAS BEEN FULLY DOCUMENTED
22 
23 namespace scopira
24 {
25  namespace tool
26  {
27  template <class T> class array_view;
28  template <class T> class const_array_view;
29 
30  template <class T> class basic_array;
31  template <class T> class basic_matrix;
32  template <class T, size_t N> class fixed_array;
33  template <class T, size_t N> class hybrid_array;
34  }
35 }
36 
41 template <class T> class scopira::tool::array_view
42 {
43  public:
44  typedef T data_type;
45  typedef T* iterator;
46  typedef const T* const_iterator;
47  protected:
48  T *dm_ary, *dm_end;
49 
50  public:
55  array_view(basic_array<T> &rhs) : dm_ary(rhs.begin()), dm_end(rhs.end()) { }
60  array_view(T *b, T *e) : dm_ary(b), dm_end(e) { }
65  template <size_t N>
66  array_view(fixed_array<T, N> &rhs) : dm_ary(rhs.begin()), dm_end(rhs.end()) { }
71  template <size_t N>
72  array_view(hybrid_array<T, N> &rhs) : dm_ary(rhs.begin()), dm_end(rhs.end()) { }
77  T * array(void) { return dm_ary; }
82  const T * array(void) const { return dm_ary; }
87  T * c_array(void) { return dm_ary; }
92  const T * c_array(void) const { return dm_ary; }
97  iterator begin(void) { return dm_ary; }
102  iterator end(void) { return dm_end; }
107  const_iterator begin(void) const { return dm_ary; }
112  const_iterator end(void) const { return dm_end; }
113 
120  void clear_zero(void) { ::memset(dm_ary, 0, sizeof(T)*(size())); }
121 
126  size_t size(void) const { return dm_end - dm_ary; }
131  bool empty(void) const { return dm_ary == dm_end; }
132 
137  void set(size_t idx, const T &v) {
138  assert("[array_view element access was out of bounds]" && (dm_ary+idx)<dm_end);
139  dm_ary[idx] = v;
140  }
145  const T & get(size_t idx) const {
146  assert("[array_view element access was out of bounds]" && (dm_ary+idx)<dm_end);
147  return dm_ary[idx];
148  }
153  T & get(size_t idx) {
154  assert("[array_view element access was out of bounds]" && (dm_ary+idx)<dm_end);
155  return dm_ary[idx];
156  }
161  const T & operator[](size_t idx) const {
162  assert("[array_view element access was out of bounds]" && (dm_ary+idx)<dm_end);
163  return dm_ary[idx];
164  }
169  T & operator[](size_t idx) {
170  assert("[array_view element access was out of bounds]" && (dm_ary+idx)<dm_end);
171  return dm_ary[idx];
172  }
173 };
174 
179 template <class T> class scopira::tool::const_array_view
180 {
181  public:
182  typedef T data_type;
183  typedef const T* const_iterator;
184  protected:
185  const T *dm_ary, *dm_end;
186 
187  public:
193  const_array_view(array_view<T> rhs) : dm_ary(rhs.begin()), dm_end(rhs.end()) { }
198  const_array_view(const basic_array<T> &rhs) : dm_ary(rhs.begin()), dm_end(rhs.end()) { }
203  const_array_view(const T *b, const T *e) : dm_ary(b), dm_end(e) { }
208  template <size_t N>
209  const_array_view(const fixed_array<T, N> &rhs) : dm_ary(rhs.begin()), dm_end(rhs.end()) { }
214  template <size_t N>
215  const_array_view(const hybrid_array<T, N> &rhs) : dm_ary(rhs.begin()), dm_end(rhs.end()) { }
220  const T * array(void) const { return dm_ary; }
225  const T * c_array(void) const { return dm_ary; }
230  const_iterator begin(void) const { return dm_ary; }
235  const_iterator end(void) const { return dm_end; }
236 
241  size_t size(void) const { return dm_end - dm_ary; }
246  bool empty(void) const { return dm_ary == dm_end; }
247 
252  const T & get(size_t idx) const {
253  assert("[const_array_view element access was out of bounds]" && (dm_ary+idx)<dm_end);
254  return dm_ary[idx];
255  }
260  const T & operator[](size_t idx) const {
261  assert("[const_array_view element access was out of bounds]" && (dm_ary+idx)<dm_end);
262  return dm_ary[idx];
263  }
264 };
265 
275 template <class T> class scopira::tool::basic_array
276 {
277  public:
278  typedef T data_type;
279  typedef T* iterator;
280  typedef const T* const_iterator;
281  protected:
282  T *dm_ary;
283  size_t dm_len;
284  public:
289  basic_array(void) : dm_ary(0), dm_len(0) { }
298  explicit basic_array(size_t len) : dm_ary(0), dm_len(0) { resize(len); }
303  basic_array(const basic_array &rhs);
305  ~basic_array() { resize(0); }
306 
318  void resize(size_t newlen);
319 
329  void array_swap(basic_array<T> &other) {
330  T *ary = dm_ary;
331  size_t len = dm_len;
332  dm_ary = other.dm_ary;
333  dm_len = other.dm_len;
334  other.dm_ary = ary;
335  other.dm_len = len;
336  }
341  T * array(void) { return dm_ary; }
346  const T * array(void) const { return dm_ary; }
351  T * c_array(void) { return dm_ary; }
356  const T * c_array(void) const { return dm_ary; }
361  iterator begin(void) { return dm_ary; }
366  iterator end(void) { return dm_ary+dm_len; }
371  const_iterator begin(void) const { return dm_ary; }
376  const_iterator end(void) const { return dm_ary+dm_len; }
377 
382  basic_array & operator=(const basic_array &rhs);
387  void clear(void) { resize(0); }
394  void clear_zero(void) { ::memset(dm_ary, 0, sizeof(T)*dm_len); }
395 
400  size_t size(void) const { return dm_len; }
405  bool empty(void) const { return dm_len == 0; }
406 
411  void set(size_t idx, const T &v) {
412  assert("[basic_array element access was out of bounds]" && idx<dm_len);
413  dm_ary[idx] = v;
414  }
419  const T & get(size_t idx) const {
420  assert("[basic_array element access was out of bounds]" && idx<dm_len);
421  return dm_ary[idx];
422  }
427  T & get(size_t idx) {
428  assert("[basic_array element access was out of bounds]" && idx<dm_len);
429  return dm_ary[idx];
430  }
435  const T & operator[](size_t idx) const {
436  assert("[basic_array element access was out of bounds]" && idx<dm_len);
437  return dm_ary[idx];
438  }
443  T & operator[](size_t idx) {
444  assert("[basic_array element access was out of bounds]" && idx<dm_len);
445  return dm_ary[idx];
446  }
447 };
448 
449 template <class T>
451  : dm_ary(0), dm_len(0)
452 {
453  if (rhs.dm_len>0) {
454  resize(rhs.dm_len);
455  iterator ii, endii = end();
456  const_iterator jj;
457  // copy
458  for (ii=begin(), jj=rhs.begin(); ii != endii; ++ii, ++jj)
459  *ii = *jj;
460  }
461 }
462 
463 template <class T>
465 {
466  if (dm_len == newlen)
467  return; // no need to change anything
468  if (dm_len > 0)
469  delete []dm_ary;
470  dm_len = newlen;
471  if (dm_len == 0)
472  dm_ary = 0;
473  else
474  dm_ary = new T[dm_len];
475 }
476 
477 template <class T>
479 {
480  resize(rhs.dm_len);
481 
482  iterator ii, endii = end();
483  const_iterator jj;
484  // copy
485  for (ii=begin(), jj=rhs.begin(); ii != endii; ++ii, ++jj)
486  *ii = *jj;
487 
488  return *this;
489 }
490 
496 template <class T> class scopira::tool::basic_matrix : public scopira::tool::basic_array<T>
497 {
498  private:
499  typedef scopira::tool::basic_array<T> parent_type;
500  protected:
501  size_t dm_w, dm_h;
502  public:
507  basic_matrix(void) {
508  dm_w = dm_h = 0;
509  }
514  basic_matrix(size_t w, size_t h) {
515  dm_w = w;
516  dm_h = h;
517  resize(dm_w*dm_h);
518  }
525  void resize(size_t w, size_t h) {
526  dm_w = w;
527  dm_h = h;
528  resize(dm_w*dm_h);
529  }
530 
535  size_t width(void) const { return dm_w; }
540  size_t height(void) const { return dm_h; }
541 
546  void set(size_t x, size_t y, const T &v) {
547  assert("[basic_matrix x axis out of bounds]" && x<dm_w);
548  assert("[basic_matrix y axis out of bounds]" && y<dm_h);
549  basic_array<T>::dm_ary[y*dm_w+x] = v;
550  }
555  const T & get(size_t x, size_t y) const {
556  assert("[basic_matrix x axis out of bounds]" && x<dm_w);
557  assert("[basic_matrix y axis out of bounds]" && y<dm_h);
558  return basic_array<T>::dm_ary[y*dm_w+x];
559  }
564  T & get(size_t x, size_t y) {
565  assert("[basic_matrix x axis out of bounds]" && x<dm_w);
566  assert("[basic_matrix y axis out of bounds]" && y<dm_h);
567  return basic_array<T>::dm_ary[y*dm_w+x];
568  }
573  const T & operator()(size_t x, size_t y) const {
574  assert("[basic_matrix x axis out of bounds]" && x<dm_w);
575  assert("[basic_matrix y axis out of bounds]" && y<dm_h);
576  return basic_array<T>::dm_ary[y*dm_w+x];
577  }
582  T & operator()(size_t x, size_t y) {
583  assert("[basic_matrix x axis out of bounds]" && x<dm_w);
584  assert("[basic_matrix y axis out of bounds]" && y<dm_h);
585  return basic_array<T>::dm_ary[y*dm_w+x];
586  }
587 
588  protected:
589  // lets hide this from the 2D users
590  void resize(size_t newlen) {
591  parent_type::resize(newlen);
592  }
593 };
594 
607 template <class T, size_t N> class scopira::tool::fixed_array
608 {
609  public:
610  typedef T data_type;
611  typedef T* iterator;
612  enum { size_c = N };
613  typedef const T* const_iterator;
614  protected:
615  T dm_ary[N];
616 
617  public:
622  T * array(void) { return dm_ary; }
627  const T * array(void) const { return dm_ary; }
632  T * c_array(void) { return dm_ary; }
637  const T * c_array(void) const { return dm_ary; }
642  iterator begin(void) { return dm_ary; }
647  iterator end(void) { return dm_ary+N; }
652  const_iterator begin(void) const { return dm_ary; }
657  const_iterator end(void) const { return dm_ary+N; }
664  void clear_zero(void) { ::memset(dm_ary, 0, sizeof(T)*N); }
669  size_t size(void) const { return N; }
674  bool empty(void) const { return false; }
675 
680  void set(size_t idx, const T &v) {
681  assert("[fixed_array element access out of bounds]" && idx<N);
682  dm_ary[idx] = v;
683  }
688  const T & get(size_t idx) const {
689  assert("[fixed_array element access out of bounds]" && idx<N);
690  return dm_ary[idx];
691  }
696  T & get(size_t idx) {
697  assert("[fixed_array element access out of bounds]" && idx<N);
698  return dm_ary[idx];
699  }
704  const T & operator[](size_t idx) const {
705  assert("[fixed_array element access out of bounds]" && idx<N);
706  return dm_ary[idx];
707  }
712  T & operator[](size_t idx) {
713  assert("[fixed_array element access out of bounds]" && idx<N);
714  return dm_ary[idx];
715  }
716 };
717 
733 template <class T, size_t RES> class scopira::tool::hybrid_array
734 {
735  public:
736  typedef T data_type;
737  typedef T* iterator;
738  typedef const T* const_iterator;
739  protected:
740  fixed_array<T, RES> dm_fixed;
741  basic_array<T> dm_basic;
742 
743  T *dm_ary;
744  size_t dm_len;
745 
746  public:
751  hybrid_array(void);
760  explicit hybrid_array(size_t len);
765  hybrid_array(const hybrid_array &rhs);
773 
785  void resize(size_t newlen);
790  T * array(void) { return dm_ary; }
795  const T * array(void) const { return dm_ary; }
800  T * c_array(void) { return dm_ary; }
805  const T * c_array(void) const { return dm_ary; }
810  iterator begin(void) { return dm_ary; }
815  iterator end(void) { return dm_ary+dm_len; }
820  const_iterator begin(void) const { return dm_ary; }
825  const_iterator end(void) const { return dm_ary+dm_len; }
826 
831  hybrid_array & operator=(const hybrid_array &rhs);
836  hybrid_array & operator=(const_array_view<T> rhs);
841  void clear(void) { resize(0); }
848  void clear_zero(void) { ::memset(dm_ary, 0, sizeof(T)*dm_len); }
849 
854  size_t size(void) const { return dm_len; }
859  bool empty(void) const { return dm_len == 0; }
860 
865  void set(size_t idx, const T &v) {
866  assert("[hybrid_array element access was out of bounds]" && idx<dm_len);
867  dm_ary[idx] = v;
868  }
873  const T & get(size_t idx) const {
874  assert("[hybrid_array element access was out of bounds]" && idx<dm_len);
875  return dm_ary[idx];
876  }
881  T & get(size_t idx) {
882  assert("[hybrid_array element access was out of bounds]" && idx<dm_len);
883  return dm_ary[idx];
884  }
889  const T & operator[](size_t idx) const {
890  assert("[hybrid_array element access was out of bounds]" && idx<dm_len);
891  return dm_ary[idx];
892  }
897  T & operator[](size_t idx) {
898  assert("[hybrid_array element access was out of bounds]" && idx<dm_len);
899  return dm_ary[idx];
900  }
901 };
902 
903 template <class T, size_t RES>
905  : dm_ary(0), dm_len(0)
906 {
907 }
908 
909 template <class T, size_t RES>
911  : dm_ary(0), dm_len(0)
912 {
913  resize(len);
914 }
915 
916 template <class T, size_t RES>
918  : dm_ary(0), dm_len(0)
919 {
920  resize(rhs.size());
921  if (!empty()) {
922  iterator ii, endii = end();
923  const_iterator jj;
924  // copy
925  for (ii=begin(), jj=rhs.begin(); ii != endii; ++ii, ++jj)
926  *ii = *jj;
927  }
928 }
929 
930 template <class T, size_t RES>
932  : dm_ary(0), dm_len(0)
933 {
934  resize(rhs.size());
935  if (!empty()) {
936  iterator ii, endii = end();
937  const_iterator jj;
938  // copy
939  for (ii=begin(), jj=rhs.begin(); ii != endii; ++ii, ++jj)
940  *ii = *jj;
941  }
942 }
943 
944 template <class T, size_t RES>
946 {
947  if (dm_len == newlen)
948  return; // no need to change anything
949  if (dm_len > RES)
950  dm_basic.resize(0);
951  dm_len = newlen;
952  if (dm_len == 0)
953  dm_ary = 0;
954  else if (dm_len <= RES)
955  dm_ary = dm_fixed.c_array();
956  else {
957  dm_basic.resize(dm_len);
958  dm_ary = dm_basic.c_array();
959  }
960 }
961 
962 template <class T, size_t RES>
964 {
965  resize(rhs.dm_len);
966 
967  iterator ii, endii = end();
968  const_iterator jj;
969  // copy
970  for (ii=begin(), jj=rhs.begin(); ii != endii; ++ii, ++jj)
971  *ii = *jj;
972 
973  return *this;
974 }
975 
976 template <class T, size_t RES>
978 {
979  resize(rhs.size());
980 
981  iterator ii, endii = end();
982  const_iterator jj;
983  // copy
984  for (ii=begin(), jj=rhs.begin(); ii != endii; ++ii, ++jj)
985  *ii = *jj;
986 
987  return *this;
988 }
989 
1082 #endif
1083 
const_array_view(array_view< T > rhs)
Definition: array.h:193
iterator begin(void)
Definition: array.h:97
T * c_array(void)
Definition: array.h:351
const_iterator end(void) const
Definition: array.h:657
size_t size(void) const
Definition: array.h:241
const T * array(void) const
Definition: array.h:346
const_iterator end(void) const
Definition: array.h:235
void clear_zero(void)
Definition: array.h:848
const_array_view(const hybrid_array< T, N > &rhs)
Definition: array.h:215
array_view(hybrid_array< T, N > &rhs)
Definition: array.h:72
basic_array(void)
Definition: array.h:289
const T & operator[](size_t idx) const
Definition: array.h:704
bool empty(void) const
Definition: array.h:674
Definition: archiveflow.h:20
T & operator[](size_t idx)
Definition: array.h:443
const T * c_array(void) const
Definition: array.h:225
bool empty(void) const
Definition: array.h:131
T * c_array(void)
Definition: array.h:800
const T * array(void) const
Definition: array.h:795
iterator begin(void)
Definition: array.h:361
const T & operator[](size_t idx) const
Definition: array.h:889
const T & operator[](size_t idx) const
Definition: array.h:435
Definition: array.h:30
size_t size(void) const
Definition: array.h:669
hybrid_array & operator=(const hybrid_array &rhs)
Definition: array.h:963
const T * c_array(void) const
Definition: array.h:805
const T * array(void) const
Definition: array.h:82
T * c_array(void)
Definition: array.h:632
iterator end(void)
Definition: array.h:647
const_array_view(const T *b, const T *e)
Definition: array.h:203
void clear_zero(void)
Definition: array.h:120
Definition: array.h:31
hybrid_array(void)
Definition: array.h:904
size_t width(void) const
Definition: array.h:535
iterator end(void)
Definition: array.h:366
void array_swap(basic_array< T > &other)
Definition: array.h:329
const_iterator begin(void) const
Definition: array.h:820
const T * array(void) const
Definition: array.h:220
const_iterator end(void) const
Definition: array.h:825
iterator end(void)
Definition: array.h:102
~hybrid_array()
Destructor.
Definition: array.h:772
void clear_zero(void)
Definition: array.h:664
~basic_array()
Destructor.
Definition: array.h:305
const T * array(void) const
Definition: array.h:627
Definition: array.h:28
void clear(void)
Definition: array.h:387
const_iterator begin(void) const
Definition: array.h:371
void clear_zero(void)
Definition: array.h:394
const_iterator begin(void) const
Definition: array.h:230
basic_array & operator=(const basic_array &rhs)
Definition: array.h:478
void resize(size_t newlen)
Definition: array.h:945
Definition: array.h:33
T & operator[](size_t idx)
Definition: array.h:897
const_iterator begin(void) const
Definition: array.h:652
const T & operator()(size_t x, size_t y) const
Definition: array.h:573
const T * c_array(void) const
Definition: array.h:92
iterator end(void)
Definition: array.h:815
T * array(void)
Definition: array.h:622
T * array(void)
Definition: array.h:341
const_iterator end(void) const
Definition: array.h:376
const T & operator[](size_t idx) const
Definition: array.h:161
size_t size(void) const
Definition: array.h:126
const T * c_array(void) const
Definition: array.h:356
T & operator[](size_t idx)
Definition: array.h:712
size_t size(void) const
Definition: array.h:854
const_iterator end(void) const
Definition: array.h:112
array_view(T *b, T *e)
Definition: array.h:60
const T & operator[](size_t idx) const
Definition: array.h:260
Definition: array.h:32
basic_array(size_t len)
Definition: array.h:298
size_t height(void) const
Definition: array.h:540
T * array(void)
Definition: array.h:790
T * c_array(void)
Definition: array.h:87
const_array_view(const fixed_array< T, N > &rhs)
Definition: array.h:209
iterator begin(void)
Definition: array.h:810
void clear(void)
Definition: array.h:841
void resize(size_t w, size_t h)
Definition: array.h:525
basic_matrix(void)
Definition: array.h:507
bool empty(void) const
Definition: array.h:405
basic_matrix(size_t w, size_t h)
Definition: array.h:514
array_view(fixed_array< T, N > &rhs)
Definition: array.h:66
array_view(basic_array< T > &rhs)
Definition: array.h:55
Definition: array.h:27
T * array(void)
Definition: array.h:77
bool empty(void) const
Definition: array.h:859
T & operator()(size_t x, size_t y)
Definition: array.h:582
iterator begin(void)
Definition: array.h:642
const_iterator begin(void) const
Definition: array.h:107
size_t size(void) const
Definition: array.h:400
T & operator[](size_t idx)
Definition: array.h:169
void resize(size_t newlen)
Definition: array.h:464
const_array_view(const basic_array< T > &rhs)
Definition: array.h:198
const T * c_array(void) const
Definition: array.h:637
bool empty(void) const
Definition: array.h:246