StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sparseArray.h
Go to the documentation of this file.
1 //
2 // sparseArray.h
3 // DynamicBitset
4 //
5 // Created by Paul Lott on 2/28/13.
6 // Copyright (c) 2013 Korf Lab, Genome Center, UC Davis, Davis, CA. All rights reserved.
7 //
8 
9 #ifndef __DynamicBitset__sparseArray__
10 #define __DynamicBitset__sparseArray__
11 
12 #include <iostream>
13 #include <vector>
14 #include "dynamic_bitset.h"
15 
16 namespace StochHMM {
17 
18  /*!SparseArray is a template class that stores the values in a sparse array
19  * The values in the array are indexed by a dynamic bitset. For example, to get
20  the value at iterator 5, we need to convert that iterator to the iterator of the
21  std::vector array. This is essentially the number of values that are stored
22  before the value at the 5th position.
23 
24  SparseArray uses std::vector. This means that it will incure an increased cost
25  of inserting and deleting values.
26 
27  */
28  template <typename T>
29  class sparseArray{
30  public:
31  sparseArray();
32  sparseArray(size_t sz):flag(sz){};
33  sparseArray(const sparseArray& val):flag(val.flag), array(val.array){};
34 
35  void clear();
36  void reserve(size_t);
37  void resize(size_t);
38 
39  //!Returns the number of elements stored in the array
40  //TODO: which is faster (Population count on bitset or get size from vector)
41  inline size_t elements(){return flag.count();};
42 
43  //!Returns the size of the sparseArray
44  inline size_t size(){return flag.size();}
45 
46 
47  //Reference to Elements in Array
48  T& operator[] (size_t);
49  const T& operator[] (size_t) const;
50  T& at(size_t pos){};
51  T& back();
52  const T& back()const;
53 
54  //Comparison Operators
55  bool operator== (const sparseArray& rhs);
56  bool operator!= (const sparseArray& rhs);
57 
58  //Assignment(Copy) Operator
59  sparseArray& operator= (const sparseArray& rhs);
60 
61  //!Return whether an element is defined at position
62  //!\returns true if element is defined at position
63  //!\returns false if element is not defined at position
64  inline bool defined(size_t pos){return flag[pos];}
65 
66  //Adding Elements to sparseArray
67  void insert(size_t pos, T& val);
68  void push_back(T&);
69 
70  //Removing Elements from sparseArray
71  void erase(size_t pos);
72  void pop_back(size_t pos);
73 
74  private:
76  std::vector<T> array;
77  };
78 
79  //!Removes all the elements from the array
80  template <typename T>
82  flag.clear();
83  array.clear();
84  return;
85  }
86 
87  //!Reserved memory for at least n elements in the array;
88  template <typename T>
89  void sparseArray<T>::reserve(size_t n) {
90  flag.reserve(n);
91  array.reserve(n);
92  return;
93  }
94 
95  //!Resize the sparseArray to n;
96  template <typename T>
97  void sparseArray<T>::resize(size_t n) {
98  //Increasing size only affect increase in flag size
99  if (n >= flag.size()){
100  flag.resize(n);
101  }
102  // Decrease size- need to also delete items from the array
103  else{
104  flag.resize(n);
105  //# of values to keep is equal to bits set in flag
106  size_t sz = flag.count();
107 
108  //Resize array to values needed
109  array.resize(sz);
110  }
111  return;
112  }
113 
114  //!Pushes the element on to the array
115  template <typename T>
117  flag.push_back(true);
118  array.push_back(val);
119  return;
120  }
121 
122  //!Insert an element at the position in the array
123  //!\param pos Position within the array to insert value
124  //!\param reference to value to insert
125  template <typename T>
126  void sparseArray<T>::insert(size_t pos,T& val){
127  flag.insert(pos, 1);
128  flag.set(pos);
129  size_t array_iter = flag.count_before(pos);
130  array.insert(array.begin()+array_iter,val);
131  return;
132  }
133 
134  //!Returns reference to object at position
135  //!If the position is beyond the length, the sparse array will get extended.
136  //!If it is defined it will return a reference to the position
137  //!If it is not defined and within the current length of the sparseArray and
138  //! after all the currently set values , it will get added to the end of the array
139  //!If it is not defined and within the current lenght of the sparseArray and
140  //! before other currently set values, then it will get inserted into the array
141  template <typename T>
143  //Increase size of flags if necessary
144  if (flag.size() <= pos){
145  flag.resize(pos);
146  }
147 
148  //if called on defined data then return reference
149  if (defined(pos)){
150  return array[flag.count_before(pos)];
151  }
152 
153  //if called on undefined data then that value will be added as set and returned
154  //with default value of type
155 
156  //If position is beyond the last set position then we can simply add it to
157  //the array
158  if (flag.find_last() < pos){
159  flag.set(pos);
160  array.resize(array.size()+1);
161  return array.back();
162  }
163 
164  //Otherwise, we need to insert the position into the array
165  //Uses default constructor of typename T
166  flag.set(pos);
167  size_t array_iter = flag.count_before(pos);
168  array.insert(array.begin()+array_iter,T());
169 
170  return array[array_iter];
171  }
172 
173 
174  //!Returns reference to object at position
175  //!If it is defined it will return a reference to the position
176  template <typename T>
177  const T& sparseArray<T>::operator[](size_t pos) const{
178 
179  //if called on defined data then return reference
180  if (defined(pos)){
181  return array[flag.count_before(pos)];
182  }
183 
184  }
185 
186 
187 }
188 
189 
190 #endif /* defined(__DynamicBitset__sparseArray__) */