36#ifndef STK_MEMALLOCATOR_H
37#define STK_MEMALLOCATOR_H
40#include "../STK_IContainerRef.h"
72 p_data =
new Type_[range.size()];
73 p_data -= range.begin();
79 template<
int OtherSize>
83 return p_data + range.begin() -
I.begin();
99 p_data+=range.
begin();
110 p_data =
new Type_[range.
size()];
111 p_data -= range.
begin();
118 template<
int OtherSize>
125 p_data =
free(p_data, range);
141 static Type_*
memcpy(Type_*
p, Type_* q,
size_t size)
142 {
return static_cast<Type_*
>(std::memcpy(
p , q,
sizeof(Type_)*size));}
144 static Type_*
memmove(Type_*
p, Type_* q,
size_t size)
145 {
return static_cast<Type_*
>(std::memmove(
p, q,
sizeof(Type_)*size));}
152 static Type_*
memcpy(Type_*
p, Type_* q,
size_t size)
154 for (
size_t k=0; k<size; k++) {
p[k] = q[k];}
157 static Type_*
memmove(Type_*
p, Type_* q,
size_t size)
159 if (size == 0)
return p;
160 if (
p<q) {
for (
size_t k=0; k<size; k++) {
p[k] = q[k];}}
161 else {
for (
size_t k=size-1; k==0; --k) {
p[k] = q[k];}}
179template<
typename Type_,
int Size_>
212 template<
int OtherSize_>
218 template<
int OtherSize_>
253 for(
int pos=
begin(); pos <
end(); ++pos)
261#ifdef STK_BOUNDS_CHECK
275#ifdef STK_BOUNDS_CHECK
300#ifdef STK_BOUNDS_CHECK
312 template<
int OtherSize>
324 template<
int OtherSize>
332 template<
int OtherSize_,
int RangeSize_>
337 template<
int RangeSize_>
358 template<
int OtherSize_>
411template<
typename Type,
int Size_>
419template<
typename Type,
int Size_>
427template<
typename Type,
int Size_>
440template<
typename Type,
int Size_>
443 , p_data_(ref ? T.p_data(): 0)
459template<
typename Type,
int Size_>
460template<
int OtherSize_>
463 , p_data_(ref ? T.p_data(): 0)
475template<
typename Type,
int Size_>
476template<
int OtherSize_>
479 , p_data_(ref ? T.p_data() : 0)
485 for(
int i=
I.begin();
i<
I.end(); ++
i)
495template<
typename Type,
int Size_>
506template<
typename Type,
int Size_>
512template<
typename Type,
int Size_>
519template<
typename Type,
int Size_>
522 std::swap(p_data_, T.p_data_);
523 std::swap(range_, T.range_);
531template<
typename Type,
int Size_>
536 memcpy(begin(), T, range());
541template<
typename Type,
int Size_>
542template<
int OtherSize_>
545 for (
int pos=
I.begin(); pos <
I.end(); ++pos) { p_data_[pos] = value;}
557template<
typename Type,
int Size_>
560 if (
this == &T)
return *
this;
562 setPtr(T.p_data_, T.range_, T.isRef());
566template<
typename Type,
int Size_>
570 if (first == begin())
return *
this;
575 shiftPtr(first - begin());
579template<
typename Type,
int Size_>
580template<
int OtherSize>
585 if ((range_ ==
I)&&(p_data_)&&(!isRef()))
return;
590 p_data_ = MemHandler::free(p_data_, range_);
591 p_data_ = MemHandler::malloc(p_data_,
I);
592 setPtr(p_data_,
I,
false);
594 catch (std::bad_alloc
const&
error)
601template<
typename Type,
int Size_>
602template<
int OtherSize>
607 if ((range_ ==
I)&&(p_data_)&&(!isRef()))
return;
612 Type*
p = MemHandler::malloc(p_data_,
I);
618 p_data_ = MemHandler::free(p_data_, range_);
621 catch (std::bad_alloc
const&
error)
625template<
typename Type,
int Size_>
630 p_data_ = MemHandler::free(p_data_, range_);
635template<
typename Type,
int Size_>
636template<
int OtherSize_,
int RangeSize_>
639 if (range.size() <= 0)
return;
640#ifdef STK_BOUNDS_CHECK
643 if (!T.range().isContaining(range))
646 MemChooser::memcpy(p_data_+pos, T.p_data()+range.begin(), range.size());
649template<
typename Type,
int Size_>
652 if ((size <= 0)||(start == pos))
return;
653#ifdef STK_BOUNDS_CHECK
658 if (!range_.isContaining(
Range(start,size)))
661 MemChooser::memmove( p_data_+pos, p_data_+ start, size);
664template<
typename Type,
int Size_>
665template<
int RangeSize_>
668 if ((range.size() <= 0)||(range.begin() == pos))
return;
669#ifdef STK_BOUNDS_CHECK
674 if (!range_.isContaining(range))
677 MemChooser::memmove( p_data_+pos, p_data_+range.begin(), range.size());
In this file we define for the template class Arithmetic for handling the greatest,...
In this file we define the standard Exceptions of the STK++ library.
In this file we define the Runtime Type Identification (RTTI) classes.
In this file we define the macro and utilities function used throughout the stk++ library.
#define STKOUT_OF_RANGE_1ARG(Where, Arg, Error)
#define STKRUNTIME_ERROR_1ARG(Where, Arg, Error)
#define STKOUT_OF_RANGE_2ARG(Where, Arg1, Arg2, Error)
In this file we define the class Range, a range of Index denoting a sub-vector region.
In this file we define the fundamental type String.
String const & error() const
get the last error message.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
int begin() const
get the first index of the TRange.
Index sub-vector region: Specialization when the size is unknown.
int end() const
get the ending index of the TRange.
int size() const
get the size of the TRange (the number of elements).
Range inf(TRange< SizeI_ > const &I, TRange< SizeJ_ > const &J)
compute inf(I,J).
const int UnknownSize
This value means that an integer is not known at compile-time, and that instead the value is stored i...
The namespace STK is the main domain space of the Statistical ToolKit project.
TRange< UnknownSize > Range
template base class for all Allocator classes.
void setValue(int pos, TypeConst value)
This method allows to set a value to the position pos.
~MemAllocator()
destructor.
MemAllocator(Type *const &q, Range const &I, bool ref)
Wrapper or copy constructor.
void memmove(int pos, TRange< RangeSize_ > const &range)
function moving a part of the allocator.
AllocatorRange range_
Range of the data.
Type & elt(int pos)
Get the element number pos.
hidden::RemoveConst< Type >::Type const & TypeConst
Type *const & p_data() const
Get constant pointer on data.
void free()
function for main ptr memory deallocation.
hidden::MemChooser< isNumeric_, Type_ > MemChooser
void exchange(MemAllocator &T)
exchange this with T.
MemAllocator(AllocatorRange const &I, Type const &value)
constructor with specified Range and initial value
void forcedFree()
function for main ptr memory deallocation.
void setRef(bool ref) const
Modify the container : can become a reference or the owner of the data.
void memcpy(int pos, MemAllocator< Type, OtherSize_ > const &T, TRange< RangeSize_ > const &range)
function copying a part of allocator T in this.
void malloc(TRange< OtherSize > const &I)
main method for memory allocation.
void memmove(int pos, int begin, int size)
function moving a part of the allocator.
void setPtr(Type *p_data, Range const &range, bool ref)
Set address and range of allocated data.
MemAllocator & shift(int first)
shift the first index of the data to first.
void realloc(TRange< OtherSize > const &I)
function for main ptr memory reallocation.
AllocatorRange const & range() const
MemAllocator(AllocatorRange const &I)
constructor with specified Range
void swap(int pos1, int pos2)
swap two elements of the Allocator.
TRange< Size_ > AllocatorRange
MemAllocator()
Default constructor.
MemAllocator(MemAllocator< Type, OtherSize_ > const &T, AllocatorRange const &I, bool ref=true)
Copy constructor.
void setValue(TypeConst value)
This method allows to set a value to the allocator.
MemAllocator(MemAllocator const &T, bool ref=false)
Copy constructor.
MemAllocator & assign(TRange< OtherSize_ > const &I, Type const &value)
assign a value to allocator.
TypeConst elt(int pos) const
Get the const element number pos.
MemAllocator & assign(MemAllocator const &T)
copy the Allocator T by value.
MemAllocator & move(MemAllocator const &T)
move the Allocator T to this.
MemAllocator(Type *const &q, int size, bool ref)
Wrapper or copy constructor: second form.
hidden::MemHandler< Type_, Size_ > MemHandler
Type * p_data_
Main pointer on the data.
void shiftPtr(int inc)
move main pointer on data
MemAllocator(MemAllocator< Type, OtherSize_ > const &T, bool ref=false)
Copy constructor.
TypeConst operator[](int i) const
void setRef(bool ref) const
Modify the container : can become a reference or the owner of the data.
void exchange(TRef const &T)
swap this with the container T.
static Type_ * memcpy(Type_ *p, Type_ *q, size_t size)
static Type_ * memmove(Type_ *p, Type_ *q, size_t size)
static Type_ * memcpy(Type_ *p, Type_ *q, size_t size)
static Type_ * memmove(Type_ *p, Type_ *q, size_t size)
static Type_ * malloc(Type_ *p_data, AllocatorRange const &range)
p_data should not point on existing data.
TRange< UnknownSize > AllocatorRange
static Type_ * realloc(Type_ *p_data, AllocatorRange const &range, TRange< OtherSize > const &I)
realloc main pointer and copy existing data.
static Type_ * free(Type_ *p_data, AllocatorRange const &range)
shift data pointer and delete allocated memory
Helper class allowing to free memory (or not)
TRange< Size_ > AllocatorRange
static Type_ * realloc(Type_ *p_data, AllocatorRange const &range, TRange< OtherSize > const &I)
realloc main pointer and copy existing data.
static Type_ * free(Type_ *p_data, AllocatorRange const &range)
do nothing for fixed size allocators !
static Type_ * malloc(Type_ *p_data, AllocatorRange const &range)
malloc memory.