StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
simpleTable.h
Go to the documentation of this file.
1 //
2 // simpleNtable.h
3 // StochHMM
4 //
5 // Created by Paul Lott on 11/20/12.
6 // Copyright (c) 2012 Korf Lab, Genome Center, UC Davis, Davis, CA. All rights reserved.
7 //
8 
9 #ifndef StochHMM_simpleNtable_h
10 #define StochHMM_simpleNtable_h
11 #include <vector>
12 #include <algorithm>
13 #include <iostream>
14 #include "sequences.h"
15 
16 namespace StochHMM{
17 
18 
19 
20 
21  template <typename T>
23  {
24  private:
25  std::vector<T>* elements; // Points to all the actual elements
26  size_t num_of_elements; // Total number of array elements
29 
30  std::vector<size_t> order;
31  std::vector<size_t> alpha;
32 
33  std::vector<std::pair<size_t,size_t>> upstream;
34 
35  std::vector<size_t> dimensions; // Sizes of the N dimensions
36  std::vector<size_t> subarray_length; // Dimensions of subarrays
37  public:
38 
39  simpleTable<T>(std::vector<size_t>& order, std::vector<size_t>& alpha, std::vector<size_t> tracks, T init): elements(NULL), num_of_elements(1){
40  num_of_alphabets = alpha.size();
41 
42  if (num_of_alphabets != order.size()){
43  std::cerr << "Differing number of orders and alphabets handed to SimpleArray" << std::endl;
44  exit(2);
45  }
46 
47  for(size_t i=0;i<num_of_alphabets;++i){
48  num_of_elements*= pow(alpha[i],order[i]+1);
49  for(size_t j=0;j<=order[i];++j){
50  dimensions.push_back(alpha[i]);
51 
52  std::pair<size_t,size_t> temp(tracks[i],order[i]-j);
53  upstream.push_back(temp);
54  }
55  }
56 
58 
59  for (size_t i=0;i<num_of_dimensions;++i){
60  size_t temp = 1;
61  for (size_t j=i+1;j<num_of_dimensions;++j){
62  temp*=dimensions[j];
63  }
64  subarray_length.push_back(temp);
65  }
66 
67  elements = new (std::nothrow) std::vector<T>(num_of_elements, init);
68 
69  if (elements == NULL){
70  std::cerr << "Unable to allocate SimpleArray" <<std::endl;
71  }
72 
73 
74  return;
75  };
76 
77 
78  ~simpleTable<T>() { delete elements; }
79 
80 
81  T get(sequences& seqs, size_t position){
82  size_t index(0);
83 
84  for(size_t i=0; i < num_of_dimensions; i++){
85  index += seqs[upstream[i].first][position-upstream[i].second] * subarray_length[i];
86  }
87  return (*elements)[index];
88  }
89 
90  const T get(sequences& seqs, size_t position) const{
91  size_t index(0);
92  for(size_t i=0; i < num_of_dimensions; i++){
93  index += seqs[upstream[i].first][position-upstream[i].second] * dimensions[i];
94  }
95 
96  return elements[index];
97  }
98 
99  void assign(T& value, size_t index){
100  if (index > num_of_elements){
101  std::cerr << "Adding element beyond range of array" << std::endl;
102  }
103  (*elements)[index] = value;
104  };
105 
106  void assign(T& value, std::vector<uint8_t>& seq){
107  size_t index(0);
108 
109  for(size_t i=0; i < num_of_dimensions; i++){
110  index += seq[i] * dimensions[i];
111  }
112 
113  (*elements)[index]=value;
114  }
115 
116  size_t size() const { return num_of_elements; }
117  bool empty() const { return num_of_elements==0; }
118 
119  // Return the size of each dimension
120  size_t size(unsigned int Dim) const{ return dimensions[Dim-1];}
121 
122  // Delete all array elements
123  void clear();
124  };
125 
126 
127 
128 
129 
130 }
131 
132 
133 
134 #endif