UNIVERS  15.3
UNIVERS base processing software API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
base_separator.hpp
1 // $Id: base_separator.hpp 21022 2011-07-11 11:45:03Z urij $
2 
3 #ifndef __base_separator_hpp__
4 #define __base_separator_hpp__
5 
6 #include <stdexcept>
7 
8 
9 template<typename T>
10 class AvgBaseSep {
11 public:
12 
13  AvgBaseSep(T* array, size_t size, size_t bases);
14 
15  T* get(size_t ndx, size_t& amount) const { return mp_array + getOffset(ndx, amount); }
16 
17  size_t getOffset(size_t ndx, size_t& amount) const;
18 
19  size_t size() const { return m_bases; }
20 
21 
22 private:
23  T* mp_array; // begin of the array
24  size_t m_bases; // amount of bases
25  size_t m_dev; // number of first smaller base
26  size_t m_base_size; // size of bigger base
27 };
28 
29 
30 
31 
32 
33 
34 //----------------------------------------------------------------------------------------------------
35 //----------------------------------------------------------------------------------------------------
36 // Template's realisation
37 //----------------------------------------------------------------------------------------------------
38 //----------------------------------------------------------------------------------------------------
39 
40 template<typename T>
41 AvgBaseSep<T>::AvgBaseSep(T* array, size_t size, size_t bases) :
42  mp_array(array),
43  m_bases(bases),
44  m_dev(0),
45  m_base_size(0)
46 {
47  if (!array || bases == 0 || bases > size)
48  throw std::logic_error("AvgBaseSep's constructor: Failed because of: 1) pointer to array is null. "
49  "2) amount of bases is equal to zero or more than size of array.");
50 
51  m_dev = size % bases;
52  m_base_size = size/bases + bool(m_dev);
53 
54  if (m_base_size==0)
55  throw std::logic_error("AvgBaseSep's constructor: Empty base. (Specfied zero size for array?).");
56 };
57 
58 
59 
60 template<typename T>
61 size_t AvgBaseSep<T>::getOffset(size_t ndx, size_t& amount) const
62 {
63  amount = 0;
64 
65  // index out of range
66  if (ndx >= m_bases)
67  throw std::out_of_range("AvgBaseSep->getOffset: index out of range.");
68 
69  size_t offset = 0;
70  if (m_dev && ndx >= m_dev) {
71  offset = m_dev*m_base_size + (ndx-m_dev)*(m_base_size-1);
72  amount = m_base_size-1;
73  }
74  else {
75  offset = ndx*m_base_size;
76  amount = m_base_size;
77  }
78 
79  return offset;
80 }
81 
82 
83 #endif // __base_separator_hpp__
84 
85 
86 
87 
Definition: base_separator.hpp:10