STK++ 0.9.13
STK::MemSAllocator< Type_, NzMax_, Size_ > Class Template Reference

memory allocator for sparse Array classes. More...

#include <STK_MemSAllocator.h>

Inheritance diagram for STK::MemSAllocator< Type_, NzMax_, Size_ >:
Inheritance graph

Public Types

enum  { nzmax_ = NzMax_ , size_ = (Size_< (UnknownSize-1)) ? Size_+1 : UnknownSize }
 
typedef Type_ Type
 
typedef hidden::RemoveConst< Type_ >::Type constTypeConst
 
typedef std::pair< int, TypeIndexedValue
 values stored by pair (row/column index, value)
 
typedef Array1D< IndexedValue, NzMax_Allocator
 Type of the base allocator allocating data.
 
typedef Array1D< int, size_PtrIdx
 Type of the base allocator allocating index pointer.
 
typedef TRange< Size_AllocatorRange
 

Public Member Functions

 MemSAllocator ()
 default constructor
 
 MemSAllocator (AllocatorRange const &I)
 constructor with specified dimension
 
 MemSAllocator (MemSAllocator const &A, bool ref=false)
 copy constructor
 
 MemSAllocator (MemSAllocator const &A, Range const &I)
 reference constructor
 
int begin () const
 
int end () const
 
int size () const
 
PtrIdx constptr () const
 
Allocator constidx () const
 
void setPtr (PtrIdx const &ptr)
 
void setIdx (Allocator const &idx)
 
void set (PtrIdx const &ptr, Allocator const &idx)
 
TypeConst getValue (int p_idx, int s_idx) const
 This method allows to get the element (p_idx, s_idx)
 
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)
 
bool isRef () const
 
void setRef (bool ref) const
 Modify the container : can become a reference or the owner of the data.
 
- Public Member Functions inherited from STK::TRef<-1 >
bool isRef () const
 
bool ref () const
 
void exchange (TRef const &T)
 swap this with the container T.
 
void setRef (bool ref) const
 Modify the container : can become a reference or the owner of the data.
 

Protected Attributes

PtrIdx ptr_
 array of pointer
 
Allocator idx_
 array of pair(idx, value)
 

Private Attributes

const Type zero_
 zero value
 

Additional Inherited Members

- Protected Member Functions inherited from STK::TRef<-1 >
 TRef (bool ref)
 Default constructor We have to specify the member ref_.
 
 ~TRef ()
 destructor.
 

Detailed Description

template<typename Type_, int NzMax_, int Size_>
class STK::MemSAllocator< Type_, NzMax_, Size_ >

memory allocator for sparse Array classes.

The data are stored either in the compressed sparse row (CSR) format or compressed sparse column (CSC) format. This Allocator does not assume any orientation and can be used for both kind of storage.

Template Parameters
Type_type of elements stored in this allocator
NzMax_maximal number of element in sparse matrix
Size_size (if known): number of rows (columns)

Definition at line 60 of file STK_MemSAllocator.h.

Member Typedef Documentation

◆ Allocator

template<typename Type_ , int NzMax_, int Size_>
typedef Array1D<IndexedValue, NzMax_> STK::MemSAllocator< Type_, NzMax_, Size_ >::Allocator

Type of the base allocator allocating data.

Can be of fixed size NzMax_

Definition at line 78 of file STK_MemSAllocator.h.

◆ AllocatorRange

template<typename Type_ , int NzMax_, int Size_>
typedef TRange<Size_> STK::MemSAllocator< Type_, NzMax_, Size_ >::AllocatorRange

Definition at line 83 of file STK_MemSAllocator.h.

◆ IndexedValue

template<typename Type_ , int NzMax_, int Size_>
typedef std::pair<int, Type> STK::MemSAllocator< Type_, NzMax_, Size_ >::IndexedValue

values stored by pair (row/column index, value)

Definition at line 72 of file STK_MemSAllocator.h.

◆ PtrIdx

template<typename Type_ , int NzMax_, int Size_>
typedef Array1D<int, size_> STK::MemSAllocator< Type_, NzMax_, Size_ >::PtrIdx

Type of the base allocator allocating index pointer.

Can be of fixed size Size_

Definition at line 80 of file STK_MemSAllocator.h.

◆ Type

template<typename Type_ , int NzMax_, int Size_>
typedef Type_ STK::MemSAllocator< Type_, NzMax_, Size_ >::Type

Definition at line 69 of file STK_MemSAllocator.h.

◆ TypeConst

template<typename Type_ , int NzMax_, int Size_>
typedef hidden::RemoveConst<Type_>::Type const& STK::MemSAllocator< Type_, NzMax_, Size_ >::TypeConst

Definition at line 70 of file STK_MemSAllocator.h.

Member Enumeration Documentation

◆ anonymous enum

template<typename Type_ , int NzMax_, int Size_>
anonymous enum
Enumerator
nzmax_ 
size_ 

Definition at line 63 of file STK_MemSAllocator.h.

64 {
65 nzmax_ = NzMax_,
66 size_ = (Size_< (UnknownSize-1)) ? Size_+1 : UnknownSize
67 };
const int UnknownSize
This value means that an integer is not known at compile-time, and that instead the value is stored i...

Constructor & Destructor Documentation

◆ MemSAllocator() [1/4]

template<typename Type_ , int NzMax_, int Size_>
STK::MemSAllocator< Type_, NzMax_, Size_ >::MemSAllocator ( )
inline

default constructor

Definition at line 86 of file STK_MemSAllocator.h.

86 : IContainerRef(false)
87 , ptr_(incLast(Range()))
88 , idx_()
89 , zero_(0)
90 {
91 ptr_ = baseIdx;
93 }
Allocator idx_
array of pair(idx, value)
const Type zero_
zero value
std::pair< int, Type > IndexedValue
values stored by pair (row/column index, value)
PtrIdx ptr_
array of pointer
TRef<-1 > IContainerRef
Range incLast(TRange< SizeI_ > const &I)
if I=a:b, return a:b+1
Definition STK_Range.h:491
const int baseIdx
base index of the containers created in STK++.
TRange< UnknownSize > Range
Definition STK_Range.h:59
static Type NA()
Adding a Non Available (NA) special number.

References STK::baseIdx, STK::MemSAllocator< Type_, NzMax_, Size_ >::idx_, STK::MemSAllocator< Type_, NzMax_, Size_ >::ptr_, and STK::MemSAllocator< Type_, NzMax_, Size_ >::zero_.

◆ MemSAllocator() [2/4]

template<typename Type_ , int NzMax_, int Size_>
STK::MemSAllocator< Type_, NzMax_, Size_ >::MemSAllocator ( AllocatorRange const I)
inline

constructor with specified dimension

Parameters
Irange of the rows (or columns)

Definition at line 97 of file STK_MemSAllocator.h.

97 : IContainerRef(false)
98 , ptr_(incLast(I))
99 , idx_()
100 , zero_(0)
101 {
102 ptr_ = baseIdx;
104 }

References STK::baseIdx, STK::MemSAllocator< Type_, NzMax_, Size_ >::idx_, STK::MemSAllocator< Type_, NzMax_, Size_ >::ptr_, and STK::MemSAllocator< Type_, NzMax_, Size_ >::zero_.

◆ MemSAllocator() [3/4]

template<typename Type_ , int NzMax_, int Size_>
STK::MemSAllocator< Type_, NzMax_, Size_ >::MemSAllocator ( MemSAllocator< Type_, NzMax_, Size_ > const A,
bool  ref = false 
)
inline

copy constructor

Parameters
Aallocator to copy
reftrue if this copy is just a reference, false otherwise

Definition at line 109 of file STK_MemSAllocator.h.

111 , ptr_(A.ptr_, ref)
112 , idx_(A.idx_, ref)
113 , zero_(A.zero_)
114 {}

◆ MemSAllocator() [4/4]

template<typename Type_ , int NzMax_, int Size_>
STK::MemSAllocator< Type_, NzMax_, Size_ >::MemSAllocator ( MemSAllocator< Type_, NzMax_, Size_ > const A,
Range const I 
)
inline

reference constructor

Parameters
Aallocator to copy
Irange of the rows/columns to reference

Definition at line 119 of file STK_MemSAllocator.h.

120 : IContainerRef(true)
121 , ptr_(A.ptr_, I, true)
122 , idx_(A.idx_, true)
123 , zero_(A.zero_)
124 {}

Member Function Documentation

◆ addValue()

template<typename Type_ , int NzMax_, int Size_>
void STK::MemSAllocator< Type_, NzMax_, Size_ >::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)

Parameters
p_idxindex of the row (respectively column)
s_idxindex of the column (respectively row)
valuevalue to set

Definition at line 195 of file STK_MemSAllocator.h.

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}
Derived & erase(int pos, int n=1)
Delete n elements at the pos index to the container.
Derived & insert(int pos, Type const &v)
STL compatibility: Insert element v at position pos of the Array.

References STK::zero_.

◆ begin()

template<typename Type_ , int NzMax_, int Size_>
int STK::MemSAllocator< Type_, NzMax_, Size_ >::begin ( ) const
inline
Returns
the first index of the data.

Definition at line 128 of file STK_MemSAllocator.h.

128{ return ptr_.begin();}

References STK::IArray1D< Derived >::begin(), and STK::MemSAllocator< Type_, NzMax_, Size_ >::ptr_.

◆ end()

template<typename Type_ , int NzMax_, int Size_>
int STK::MemSAllocator< Type_, NzMax_, Size_ >::end ( ) const
inline
Returns
the ending index of the data

Definition at line 130 of file STK_MemSAllocator.h.

130{ return ptr_.end()-1;}

References STK::IArray1D< Derived >::end(), and STK::MemSAllocator< Type_, NzMax_, Size_ >::ptr_.

◆ getValue()

template<typename Type_ , int NzMax_, int Size_>
MemSAllocator< Type_, NzMax_, Size_ >::TypeConst STK::MemSAllocator< Type_, NzMax_, Size_ >::getValue ( int  p_idx,
int  s_idx 
) const

This method allows to get the element (p_idx, s_idx)

Parameters
p_idxthe index of the row (or column)
s_idxthe index of the column (or row)
Returns
0 if the element is not stored, the value of the element otherwise

Definition at line 182 of file STK_MemSAllocator.h.

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}

References STK::zero_.

◆ idx()

template<typename Type_ , int NzMax_, int Size_>
Allocator const & STK::MemSAllocator< Type_, NzMax_, Size_ >::idx ( ) const
inline
Returns
the vector with the (index,value) pairs columns (or rows)

Definition at line 137 of file STK_MemSAllocator.h.

137{ return idx_;}

References STK::MemSAllocator< Type_, NzMax_, Size_ >::idx_.

Referenced by STK::MemSAllocator< Type_, NzMax_, Size_ >::set(), and STK::MemSAllocator< Type_, NzMax_, Size_ >::setIdx().

◆ isRef()

template<typename Type_ , int NzMax_, int Size_>
bool STK::TRef<-1 >::isRef ( ) const
inline
Returns
true if *this is reference container, false otherwise

Definition at line 81 of file STK_IContainerRef.h.

81{ return ref_;}
bool ref_
Is it a "true" container or a wrapper ? ref_ should be false if this own its own data,...

◆ ptr()

template<typename Type_ , int NzMax_, int Size_>
PtrIdx const & STK::MemSAllocator< Type_, NzMax_, Size_ >::ptr ( ) const
inline
Returns
the vector with the pointers on rows (or columns)

Definition at line 135 of file STK_MemSAllocator.h.

135{ return ptr_;}

References STK::MemSAllocator< Type_, NzMax_, Size_ >::ptr_.

Referenced by STK::MemSAllocator< Type_, NzMax_, Size_ >::set(), and STK::MemSAllocator< Type_, NzMax_, Size_ >::setPtr().

◆ set()

template<typename Type_ , int NzMax_, int Size_>
void STK::MemSAllocator< Type_, NzMax_, Size_ >::set ( PtrIdx const ptr,
Allocator const idx 
)
inline
Parameters
ptrvector with the pointers on rows (or columns)
idxvector with the (index,value) pairs in columns (or rows)

Definition at line 147 of file STK_MemSAllocator.h.

148 { ptr_ = ptr; idx_ = idx;}
Allocator const & idx() const
PtrIdx const & ptr() const

References STK::MemSAllocator< Type_, NzMax_, Size_ >::idx(), STK::MemSAllocator< Type_, NzMax_, Size_ >::idx_, STK::MemSAllocator< Type_, NzMax_, Size_ >::ptr(), and STK::MemSAllocator< Type_, NzMax_, Size_ >::ptr_.

◆ setIdx()

template<typename Type_ , int NzMax_, int Size_>
void STK::MemSAllocator< Type_, NzMax_, Size_ >::setIdx ( Allocator const idx)
inline
Parameters
idxvector with the (index,value) pairs in columns (or rows)

Definition at line 143 of file STK_MemSAllocator.h.

143{ idx_ = idx;}

References STK::MemSAllocator< Type_, NzMax_, Size_ >::idx(), and STK::MemSAllocator< Type_, NzMax_, Size_ >::idx_.

◆ setPtr()

template<typename Type_ , int NzMax_, int Size_>
void STK::MemSAllocator< Type_, NzMax_, Size_ >::setPtr ( PtrIdx const ptr)
inline
Parameters
ptrvector with the pointers on rows (or columns)

Definition at line 141 of file STK_MemSAllocator.h.

141{ ptr_ = ptr;}

References STK::MemSAllocator< Type_, NzMax_, Size_ >::ptr(), and STK::MemSAllocator< Type_, NzMax_, Size_ >::ptr_.

◆ setRef()

template<typename Type_ , int NzMax_, int Size_>
void STK::TRef<-1 >::setRef ( bool  ref) const
inline

Modify the container : can become a reference or the owner of the data.

To use with care if we want to avoid memory leak.

Parameters
ref: false if this own its own data.

Definition at line 92 of file STK_IContainerRef.h.

92{ ref_ = ref;}

◆ size()

template<typename Type_ , int NzMax_, int Size_>
int STK::MemSAllocator< Type_, NzMax_, Size_ >::size ( ) const
inline
Returns
the size of the data

Definition at line 132 of file STK_MemSAllocator.h.

132{ return ptr_.size()-1;}

References STK::MemSAllocator< Type_, NzMax_, Size_ >::ptr_, and STK::IArray1D< Derived >::size().

Member Data Documentation

◆ idx_

◆ ptr_

◆ zero_

template<typename Type_ , int NzMax_, int Size_>
const Type STK::MemSAllocator< Type_, NzMax_, Size_ >::zero_
private

The documentation for this class was generated from the following file: