runlen.hpp
Go to the documentation of this file.
1 #ifndef OPM_VERTEQ_RUNLEN_HPP_INCLUDED
2 #define OPM_VERTEQ_RUNLEN_HPP_INCLUDED
3 
4 // Copyright (C) 2013 Uni Research AS
5 // This file is licensed under the GNU General Public License v3.0
6 
7 // forward declaration
8 struct UnstructuredGrid;
9 
10 namespace Opm {
11 
59 template <typename T>
60 class RunLenView {
61 protected:
70  int* pos;
71 
76  T* data;
77 
78 public:
91  RunLenView (int num_cols, int* pos_ptr, T* values)
92  // store them locally for later use
93  : num_of_cols (num_cols)
94  , pos (pos_ptr)
95  , data (values) {
96  }
97 
103  RunLenView (const RunLenView& rhs)
104  // copy all fields verbatim
105  : num_of_cols (rhs.num_of_cols)
106  , pos (rhs.pos)
107  , data (rhs.data) {
108  }
109 
116  T* operator [] (int col) const {
117  return &data [pos [col]];
118  }
119 
125  int cols () const {
126  return num_of_cols;
127  }
128 
135  int size (int col) const {
136  return pos [col + 1] - pos [col];
137  }
138 
149  T& last (int col) const {
150  return data [pos [col + 1] - 1];
151  }
152 };
153 
163 template <typename T>
164 struct RunLenData : public RunLenView <T> {
175  RunLenData (int number, int* pos_ptr)
176  // allocate a new vector for the data, containing the needed
177  // number of elements. note that there is only one new
178  // operation is the parameter list, so there is no leakage if
179  // an out-of-memory exception is thrown.
180  : RunLenView <T> (number, pos_ptr, new T [pos_ptr [number]]) {
181  }
182 
184  // this member is initialized with data allocated in our ctor
185  delete [] RunLenView <T>::data;
186  }
187 };
188 
189 // shorthands for most used types
190 typedef const RunLenView <int> rlw_int;
192 
193 // access common run-length encoded matrices in a grid structure
194 rlw_int grid_cell_facetag (const UnstructuredGrid& g);
195 rlw_int grid_cell_faces (const UnstructuredGrid& g);
196 
197 } /* namespace Opm */
198 
199 #endif /* OPM_VERTEQ_RUNLEN_HPP_INCLUDED */
T * operator[](int col) const
Definition: runlen.hpp:116
RunLenData(int number, int *pos_ptr)
Definition: runlen.hpp:175
int cols() const
Definition: runlen.hpp:125
rlw_int grid_cell_faces(const UnstructuredGrid &g)
Definition: opmfwd.hpp:15
int num_of_cols
Definition: runlen.hpp:69
T & last(int col) const
Definition: runlen.hpp:149
const RunLenView< double > rlw_double
Definition: runlen.hpp:191
RunLenView(int num_cols, int *pos_ptr, T *values)
Definition: runlen.hpp:91
T * data
Definition: runlen.hpp:76
int * pos
Definition: runlen.hpp:70
rlw_int grid_cell_facetag(const UnstructuredGrid &g)
Definition: runlen.hpp:164
RunLenView(const RunLenView &rhs)
Definition: runlen.hpp:103
const RunLenView< int > rlw_int
Definition: runlen.hpp:190
~RunLenData()
Definition: runlen.hpp:183
Definition: runlen.hpp:60
int size(int col) const
Definition: runlen.hpp:135