STK++ 0.9.13
STK_MemSAllocator.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2018 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/* Project: stkpp::Arrays
26 * created on: Apr 13, 2018
27 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
28 **/
29
36#ifndef STK_MEMSALLOCATOR_H
37#define STK_MEMSALLOCATOR_H
38
39#include "../STK_Array1D.h"
40
41
42namespace STK
43{
44// forward declaration
45template< typename Type_, int NzMax_ = UnknownSize, int Size_ = UnknownSize>
46class MemSAllocator;
47
59template< typename Type_, int NzMax_, int Size_>
61{
62 public:
63 enum
64 {
67 };
68
69 typedef Type_ Type;
72 typedef std::pair<int, Type> IndexedValue;
73
76
81
82 //typedef TRange<NzMax_> AllocatorRange;
84
98 , ptr_(incLast(I))
99 , idx_()
100 , zero_(0)
101 {
102 ptr_ = baseIdx;
104 }
109 MemSAllocator( MemSAllocator const& A, bool ref =false)
111 , ptr_(A.ptr_, ref)
112 , idx_(A.idx_, ref)
113 , zero_(A.zero_)
114 {}
121 , ptr_(A.ptr_, I, true)
122 , idx_(A.idx_, true)
123 , zero_(A.zero_)
124 {}
125
126 // getters
128 inline int begin() const { return ptr_.begin();}
130 inline int end() const { return ptr_.end()-1;}
132 inline int size() const { return ptr_.size()-1;}
133
135 inline PtrIdx const& ptr() const { return ptr_;}
137 inline Allocator const& idx() const { return idx_;}
138
139 // setters
141 inline void setPtr(PtrIdx const& ptr) { ptr_ = ptr;}
143 inline void setIdx(Allocator const& idx) { idx_ = idx;}
147 inline void set(PtrIdx const& ptr, Allocator const& idx)
148 { ptr_ = ptr; idx_ = idx;}
149
150 // manipulator
156 TypeConst getValue(int p_idx, int s_idx) const;
162 void addValue(int p_idx, int s_idx, Type const& value);
163
164 protected:
169
170 private:
172 const Type zero_;
173};
174
175
176/* This method allows to get the element (p_idx, s_idx)
177 * @param p_idx the index of the row (or column)
178 * @param s_idx the index of the column (or row)
179 * @return 0 if the element is not stored, the value of the element otherwise
180 **/
181template< typename Type_, int NzMax_, int Size_>
183{
184 for (int t=ptr_[p_idx]; t<ptr_[p_idx+1]; ++t)
185 { if (idx_[t].first == s_idx) return idx_[t].second;}
186 return zero_;
187}
188
189/* This method allows to overwrite or insert an element to the position (p_idx, s_idx)
190 * @param p_idx index of the row (respectively column)
191 * @param s_idx index of the column (respectively row)
192 * @param value value to set
193 **/
194template< typename Type_, int NzMax_, int Size_>
196{
197 // loop over already entries in this row/column
198 for (int t=ptr_[p_idx]; t<ptr_[p_idx+1]; ++t)
199 {
200 if (idx_[t].first == s_idx) // there is an existing stored value for this entry
201 {
202 if (value != zero_) { idx_[t].second = value;}
203 else // value to enter is zero
204 {
205 idx_.erase(t);
206 for (int pt = p_idx+1; pt<ptr_.end(); ++pt) { --ptr_[pt];}
207 }
208 return;
209 }
210 else if (idx_[t].first > s_idx) // value is not yet an entry, so add it
211 {
212 if (value == zero_) return; // value is zero, there is nothing to do
213 // Otherwise insert it
214 idx_.insert(t, IndexedValue(s_idx, value));
215 for (int pt = p_idx+1; pt<ptr_.end(); ++pt) { ++ptr_[pt];}
216 return;
217 }
218 }
219 // No entry in this row/column, if value is zero there is nothing to do
220 if (value == zero_) return;
221 // otherwise add it taking care of empty rows/columns
222 idx_.insert( (ptr_[p_idx] == ptr_[p_idx+1]) ? ptr_[p_idx] : ptr_[p_idx]+1, IndexedValue(s_idx, value));
223 for (int pt = p_idx+1; pt<ptr_.end(); ++pt) { ++ptr_[pt];}
224}
225
226} // namespace STK
227
228#endif /* STK_MEMSALLOCATOR_H */
memory allocator for sparse Array classes.
void addValue(int p_idx, int s_idx, Type const &value)
This method allows to overwrite or insert an element to the position (p_idx, s_idx)
Allocator const & idx() const
Allocator idx_
array of pair(idx, value)
MemSAllocator()
default constructor
const Type zero_
zero value
TypeConst getValue(int p_idx, int s_idx) const
This method allows to get the element (p_idx, s_idx)
void setIdx(Allocator const &idx)
Array1D< IndexedValue, NzMax_ > Allocator
Type of the base allocator allocating data.
std::pair< int, Type > IndexedValue
values stored by pair (row/column index, value)
MemSAllocator(AllocatorRange const &I)
constructor with specified dimension
void setPtr(PtrIdx const &ptr)
TRange< Size_ > AllocatorRange
MemSAllocator(MemSAllocator const &A, Range const &I)
reference constructor
void set(PtrIdx const &ptr, Allocator const &idx)
MemSAllocator(MemSAllocator const &A, bool ref=false)
copy constructor
PtrIdx const & ptr() const
Array1D< int, size_ > PtrIdx
Type of the base allocator allocating index pointer.
PtrIdx ptr_
array of pointer
hidden::RemoveConst< Type_ >::Type const & TypeConst
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
@ zero_
0 value
Definition STK_Binary.h:49
Range incLast(TRange< SizeI_ > const &I)
if I=a:b, return a:b+1
Definition STK_Range.h:491
const int UnknownSize
This value means that an integer is not known at compile-time, and that instead the value is stored i...
const int baseIdx
base index of the containers created in STK++.
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
void setRef(bool ref) const
Modify the container : can become a reference or the owner of the data.
bool isRef() const