added: some work in MemBuffer
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@422 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
parent
62f0624539
commit
4d968f5a85
|
@ -1,113 +1,184 @@
|
||||||
|
/*
|
||||||
|
* This file is a part of PikoTools
|
||||||
|
* and is distributed under the (new) BSD licence.
|
||||||
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Tomasz Sowa
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||||
|
* project may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||||
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef headerfile_picotools_membuffer_membuffer
|
#ifndef headerfile_picotools_membuffer_membuffer
|
||||||
#define headerfile_picotools_membuffer_membuffer
|
#define headerfile_picotools_membuffer_membuffer
|
||||||
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace PT
|
namespace PT
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
template<class ItemType, size_t stack_size>
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
class MemBuffer
|
class MemBuffer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
class Iterator
|
class Iterator
|
||||||
{
|
{
|
||||||
size_t vector_index;
|
public:
|
||||||
size_t array_index;
|
|
||||||
|
|
||||||
bool operator==(const Iterator & i);
|
bool operator==(const Iterator & i);
|
||||||
bool operator!=(const Iterator & i);
|
bool operator!=(const Iterator & i);
|
||||||
bool operator<(const Iterator & i);
|
|
||||||
|
|
||||||
Iterator & operator++(); // prefix ++
|
Iterator & operator++(); // prefix ++
|
||||||
Iterator operator++(int); // postfix ++
|
Iterator operator++(int); // postfix ++
|
||||||
|
|
||||||
|
Iterator & operator--(); // prefix --
|
||||||
|
Iterator operator--(int); // postfix --
|
||||||
|
|
||||||
ItemType & operator*();
|
ItemType & operator*();
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
MemBuffer * mem_buffer;
|
||||||
|
size_t dynamic_array_index;
|
||||||
|
size_t index;
|
||||||
|
|
||||||
|
friend MemBuffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class ConstIterator
|
class ConstIterator
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
bool operator==(const ConstIterator & i);
|
||||||
|
bool operator!=(const ConstIterator & i);
|
||||||
|
|
||||||
|
ConstIterator & operator++(); // prefix ++
|
||||||
|
ConstIterator operator++(int); // postfix ++
|
||||||
|
|
||||||
|
ConstIterator & operator--(); // prefix --
|
||||||
|
ConstIterator operator--(int); // postfix --
|
||||||
|
|
||||||
|
ItemType operator*();
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
const MemBuffer * mem_buffer;
|
||||||
|
size_t dynamic_array_index;
|
||||||
|
size_t index;
|
||||||
|
|
||||||
|
friend MemBuffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
MemBuffer();
|
MemBuffer();
|
||||||
|
~MemBuffer();
|
||||||
|
//MemBuffer(const MemBuffer<ItemType, stack_size, heap_block_size> & arg);
|
||||||
|
//MemBuffer & operator=(const MemBuffer<ItemType, stack_size, heap_block_size> & arg);
|
||||||
|
|
||||||
void Append(ItemType item);
|
void Append(ItemType item);
|
||||||
void Append(ItemType * item_array, size_t len);
|
void Append(ItemType * item_array, size_t len);
|
||||||
void Size() const;
|
size_t Size() const;
|
||||||
void Empty() const;
|
bool Empty() const;
|
||||||
|
|
||||||
void Reserve();
|
void Reserve(size_t len);
|
||||||
void Capacity();
|
size_t Capacity() const;
|
||||||
void SetMinCapacity();
|
void Clear(); // frees memory but only to Capacity()
|
||||||
void Clear(); // frees memory but only to SetMinCapacity()
|
|
||||||
|
|
||||||
Iterator Begin();
|
Iterator Begin();
|
||||||
ConstIterator Begin() const;
|
|
||||||
|
|
||||||
Iterator End();
|
Iterator End();
|
||||||
|
|
||||||
|
ConstIterator Begin() const;
|
||||||
ConstIterator End() const;
|
ConstIterator End() const;
|
||||||
|
|
||||||
// may it's a better to have a static size of dynamic_array items
|
ItemType & operator[](size_t i);
|
||||||
// so we can have following operators working in O(1)
|
const ItemType operator[](size_t i) const;
|
||||||
//ItemType & operator[](size_t i);
|
|
||||||
//const ItemType operator[](size_t i) const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
struct MemArray
|
struct MemArray
|
||||||
{
|
{
|
||||||
size_t size;
|
|
||||||
size_t size_used;
|
size_t size_used;
|
||||||
ItemType * buf;
|
ItemType * buf;
|
||||||
};
|
};
|
||||||
|
|
||||||
ItemType stack_array[stack_size];
|
ItemType stack_array[stack_size];
|
||||||
std::vector<MemArray> dynamic_array;
|
MemArray * dynamic_array; // dynamic array of MemArray descriptors
|
||||||
size_t size_used; // the size of all items
|
size_t dynamic_array_index; // index of a MemArray to which the last insertion was made
|
||||||
size_t reserve_size; // how many memory is reserved
|
// size_t(-1) means the stack_array
|
||||||
size_t last_allocated_list; // size of the last allocated vector
|
size_t dynamic_array_used; // how many MemArray-s have been inited in dynamic_array
|
||||||
|
size_t dynamic_array_size; // the size of the dynamic_array
|
||||||
|
|
||||||
|
size_t size_used; // the size of all valid items
|
||||||
|
size_t size_allocated; // how many memory is reserved
|
||||||
|
size_t size_reserved; // memory reserved by Reserve(), it is used by Clear()
|
||||||
|
|
||||||
|
void AddDynamicNode();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Iterator
|
||||||
|
*/
|
||||||
|
|
||||||
template<class ItemType, size_t stack_size>
|
|
||||||
MemBuffer<ItemType>::Iterator & MemBuffer<ItemType>::Iterator::operator++()
|
|
||||||
{
|
|
||||||
if( vector_index == size_t(-1) )
|
|
||||||
{
|
|
||||||
array_index += 1;
|
|
||||||
|
|
||||||
if( array_index >= stack_size )
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
typename MemBuffer<ItemType, stack_size, heap_block_size>::Iterator &
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::Iterator::operator++()
|
||||||
{
|
{
|
||||||
array_index = 0;
|
if( dynamic_array_index == size_t(-1) )
|
||||||
vector_index = 0;
|
{
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
if( index >= stack_size )
|
||||||
|
{
|
||||||
|
index = 0;
|
||||||
|
dynamic_array_index = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// if not it means that this iterator is pointing at End()
|
index += 1;
|
||||||
if( vector_index < dynamic_array.size() && array_index < dynamic_array.back().size_used )
|
|
||||||
{
|
|
||||||
array_index += 1;
|
|
||||||
|
|
||||||
if( array_index >= dynamic_array.back().size )
|
if( index >= heap_block_size )
|
||||||
{
|
{
|
||||||
array_index = 0;
|
dynamic_array_index += 1;
|
||||||
vector_index += 1;
|
index = 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,8 +186,9 @@ return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class ItemType, size_t stack_size>
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
MemBuffer<ItemType>::Iterator MemBuffer<ItemType>::Iterator::operator++(int)
|
typename MemBuffer<ItemType, stack_size, heap_block_size>::Iterator
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::Iterator::operator++(int)
|
||||||
{
|
{
|
||||||
Iterator old(*this);
|
Iterator old(*this);
|
||||||
operator++();
|
operator++();
|
||||||
|
@ -126,69 +198,303 @@ return old;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<class ItemType, size_t stack_size>
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
void MemBuffer<ItemType>::MemBuffer()
|
typename MemBuffer<ItemType, stack_size, heap_block_size>::Iterator &
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::Iterator::operator--()
|
||||||
{
|
{
|
||||||
size_used = 0;
|
if( index == 0 )
|
||||||
list_size = 0;
|
{
|
||||||
reserve_size = stack_size;
|
dynamic_array_index -= 1;
|
||||||
last_allocated_list = 512; // minimum 1
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if( dynamic_array_index == size_t(-1) )
|
||||||
template<class ItemType, size_t stack_size>
|
index = stack_size - 1;
|
||||||
void MemBuffer<ItemType>::Append(ItemType item)
|
else
|
||||||
{
|
index = heap_block_size - 1;
|
||||||
if( size_used < stack_size )
|
|
||||||
{
|
|
||||||
stack_array[size_used];
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if( dynamic_array.empty() || dynamic_array.back().size_used >= dynamic_array.back().size )
|
index -= 1;
|
||||||
{
|
|
||||||
last_allocated_list = last_allocated_list * 2;
|
|
||||||
dynamic_array.push_back(MemArray());
|
|
||||||
dynamic_array.back().size = last_allocated_list;
|
|
||||||
dynamic_array.back().size_used = 0;
|
|
||||||
dynamic_array.back().buf = new ItemType[last_allocated_list];
|
|
||||||
reserve_size += last_allocated_list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dynamic_array.back().buf[dynamic_array.back().size_used] = item;
|
return *this;
|
||||||
dynamic_array.back().size_used += 1;
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
typename MemBuffer<ItemType, stack_size, heap_block_size>::Iterator
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::Iterator::operator--(int)
|
||||||
|
{
|
||||||
|
Iterator old(*this);
|
||||||
|
operator++();
|
||||||
|
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
ItemType & MemBuffer<ItemType, stack_size, heap_block_size>::Iterator::operator*()
|
||||||
|
{
|
||||||
|
if( dynamic_array_index == size_t(-1) )
|
||||||
|
{
|
||||||
|
return mem_buffer->stack_array[index];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return mem_buffer->dynamic_array[dynamic_array_index].buf[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
bool MemBuffer<ItemType, stack_size, heap_block_size>::Iterator::operator==(const Iterator & i)
|
||||||
|
{
|
||||||
|
return mem_buffer == i.mem_buffer &&
|
||||||
|
dynamic_array_index == i.dynamic_array_index &&
|
||||||
|
index == i.index;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
bool MemBuffer<ItemType, stack_size, heap_block_size>::Iterator::operator!=(const Iterator & i)
|
||||||
|
{
|
||||||
|
return mem_buffer != i.mem_buffer ||
|
||||||
|
dynamic_array_index != i.dynamic_array_index ||
|
||||||
|
index != i.index;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
ConstIterator
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
typename MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator &
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator::operator++()
|
||||||
|
{
|
||||||
|
if( dynamic_array_index == size_t(-1) )
|
||||||
|
{
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
if( index >= stack_size )
|
||||||
|
{
|
||||||
|
index = 0;
|
||||||
|
dynamic_array_index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
if( index >= heap_block_size )
|
||||||
|
{
|
||||||
|
dynamic_array_index += 1;
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
typename MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator::operator++(int)
|
||||||
|
{
|
||||||
|
ConstIterator old(*this);
|
||||||
|
operator++();
|
||||||
|
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
typename MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator &
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator::operator--()
|
||||||
|
{
|
||||||
|
if( index == 0 )
|
||||||
|
{
|
||||||
|
dynamic_array_index -= 1;
|
||||||
|
|
||||||
|
if( dynamic_array_index == size_t(-1) )
|
||||||
|
index = stack_size - 1;
|
||||||
|
else
|
||||||
|
index = heap_block_size - 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
index -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
typename MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator::operator--(int)
|
||||||
|
{
|
||||||
|
ConstIterator old(*this);
|
||||||
|
operator++();
|
||||||
|
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
ItemType MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator::operator*()
|
||||||
|
{
|
||||||
|
if( dynamic_array_index == size_t(-1) )
|
||||||
|
{
|
||||||
|
return mem_buffer->stack_array[index];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return mem_buffer->dynamic_array[dynamic_array_index].buf[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
bool MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator::operator==(const ConstIterator & i)
|
||||||
|
{
|
||||||
|
return mem_buffer == i.mem_buffer &&
|
||||||
|
dynamic_array_index == i.dynamic_array_index &&
|
||||||
|
index == i.index;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
bool MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator::operator!=(const ConstIterator & i)
|
||||||
|
{
|
||||||
|
return mem_buffer != i.mem_buffer ||
|
||||||
|
dynamic_array_index != i.dynamic_array_index ||
|
||||||
|
index != i.index;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
MemBuffer
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::MemBuffer()
|
||||||
|
{
|
||||||
|
size_reserved = 0;
|
||||||
|
size_used = 0;
|
||||||
|
size_allocated = stack_size;
|
||||||
|
dynamic_array = 0;
|
||||||
|
dynamic_array_index = size_t(-1);
|
||||||
|
dynamic_array_used = 0;
|
||||||
|
dynamic_array_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::~MemBuffer()
|
||||||
|
{
|
||||||
|
if( dynamic_array )
|
||||||
|
{
|
||||||
|
for(size_t i=0 ; i<dynamic_array_used ; ++i)
|
||||||
|
delete [] dynamic_array[i].buf;
|
||||||
|
|
||||||
|
delete [] dynamic_array;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
void MemBuffer<ItemType, stack_size, heap_block_size>::AddDynamicNode()
|
||||||
|
{
|
||||||
|
if( dynamic_array_used >= dynamic_array_size )
|
||||||
|
{
|
||||||
|
// reallocating
|
||||||
|
dynamic_array_size += 2; // 64;
|
||||||
|
MemArray * new_array = new MemArray[dynamic_array_size];
|
||||||
|
|
||||||
|
for(size_t i=0 ; i<dynamic_array_used ; ++i)
|
||||||
|
new_array[i] = dynamic_array[i];
|
||||||
|
|
||||||
|
delete [] dynamic_array;
|
||||||
|
dynamic_array = new_array;
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic_array[dynamic_array_used].size_used = 0;
|
||||||
|
dynamic_array[dynamic_array_used].buf = new ItemType[heap_block_size];
|
||||||
|
|
||||||
|
dynamic_array_used += 1;
|
||||||
|
size_allocated += heap_block_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
void MemBuffer<ItemType, stack_size, heap_block_size>::Append(ItemType item)
|
||||||
|
{
|
||||||
|
if( size_used < stack_size )
|
||||||
|
{
|
||||||
|
stack_array[size_used] = item;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if( dynamic_array_index == size_t(-1) )
|
||||||
|
{
|
||||||
|
dynamic_array_index = 0;
|
||||||
|
|
||||||
|
if( dynamic_array_index >= dynamic_array_used )
|
||||||
|
AddDynamicNode();
|
||||||
|
|
||||||
|
dynamic_array[dynamic_array_index].size_used = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if( dynamic_array[dynamic_array_index].size_used >= heap_block_size )
|
||||||
|
{
|
||||||
|
dynamic_array_index += 1;
|
||||||
|
|
||||||
|
if( dynamic_array_index >= dynamic_array_used )
|
||||||
|
AddDynamicNode();
|
||||||
|
|
||||||
|
dynamic_array[dynamic_array_index].size_used = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic_array[dynamic_array_index].buf[dynamic_array[dynamic_array_index].size_used] = item;
|
||||||
|
dynamic_array[dynamic_array_index].size_used += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_used += 1;
|
size_used += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class ItemType, size_t stack_size>
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
void MemBuffer<ItemType>::Append(ItemType * item_array, size_t len)
|
void MemBuffer<ItemType, stack_size, heap_block_size>::Append(ItemType * item_array, size_t len)
|
||||||
{
|
{
|
||||||
// in the future we can add a test
|
if( size_used + len <= stack_size )
|
||||||
// if the array can be placed directly in the last item from std::list
|
|
||||||
// the we put it all
|
|
||||||
|
|
||||||
if( size_used < stack_size && size_used + len <= stack_size )
|
|
||||||
{
|
{
|
||||||
// may memcpy in the future? need some tests
|
|
||||||
|
|
||||||
for(size_t i=0 ; i<len ; ++i)
|
for(size_t i=0 ; i<len ; ++i)
|
||||||
stack_array[size_used++] = item_array[i];
|
stack_array[size_used++] = item_array[i];
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( !dynamic_array.empty() && dynamic_array.back().size_used + len <= dynamic_array.back().size )
|
if( dynamic_array_index != size_t(-1) &&
|
||||||
|
dynamic_array[dynamic_array_index].size_used + len <= heap_block_size )
|
||||||
{
|
{
|
||||||
ItemType * buf = dynamic_array.back().buf;
|
ItemType * buf = dynamic_array[dynamic_array_index].buf;
|
||||||
size_t bufsize = dynamic_array.back().size_used;
|
size_t bufsize = dynamic_array[dynamic_array_index].size_used;
|
||||||
|
|
||||||
for(size_t i=0 ; i<len ; ++i)
|
for(size_t i=0 ; i<len ; ++i)
|
||||||
buf[bufsize++] = item_array[i];
|
buf[bufsize++] = item_array[i];
|
||||||
|
|
||||||
dynamic_array.back().size_used += len;
|
dynamic_array[dynamic_array_index].size_used += len;
|
||||||
size_used += len;
|
size_used += len;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -201,6 +507,199 @@ void MemBuffer<ItemType>::Append(ItemType * item_array, size_t len)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
ItemType & MemBuffer<ItemType, stack_size, heap_block_size>::operator[](size_t i)
|
||||||
|
{
|
||||||
|
if( i < stack_size )
|
||||||
|
{
|
||||||
|
return stack_array[i];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
i -= stack_size;
|
||||||
|
size_t index = i / heap_block_size;
|
||||||
|
size_t offset = i % heap_block_size;
|
||||||
|
|
||||||
|
return dynamic_array[index].buf[offset];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
const ItemType MemBuffer<ItemType, stack_size, heap_block_size>::operator[](size_t i) const
|
||||||
|
{
|
||||||
|
if( i < stack_size )
|
||||||
|
{
|
||||||
|
return stack_array[i];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
i -= stack_size;
|
||||||
|
size_t index = i / heap_block_size;
|
||||||
|
size_t offset = i % heap_block_size;
|
||||||
|
|
||||||
|
return dynamic_array[index].buf[offset];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
size_t MemBuffer<ItemType, stack_size, heap_block_size>::Size() const
|
||||||
|
{
|
||||||
|
return size_used;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
bool MemBuffer<ItemType, stack_size, heap_block_size>::Empty() const
|
||||||
|
{
|
||||||
|
return size_used == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
typename MemBuffer<ItemType, stack_size, heap_block_size>::Iterator
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::Begin()
|
||||||
|
{
|
||||||
|
Iterator i;
|
||||||
|
|
||||||
|
i.mem_buffer = this;
|
||||||
|
i.dynamic_array_index = size_t(-1);
|
||||||
|
i.index = 0;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
typename MemBuffer<ItemType, stack_size, heap_block_size>::Iterator
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::End()
|
||||||
|
{
|
||||||
|
Iterator i;
|
||||||
|
|
||||||
|
i.mem_buffer = this;
|
||||||
|
|
||||||
|
if( size_used <= stack_size )
|
||||||
|
{
|
||||||
|
i.dynamic_array_index = size_t(-1);
|
||||||
|
i.index = size_used;
|
||||||
|
|
||||||
|
if( i.index >= stack_size )
|
||||||
|
{
|
||||||
|
i.dynamic_array_index = 0;
|
||||||
|
i.index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
i.dynamic_array_index = dynamic_array_index;
|
||||||
|
i.index = dynamic_array[dynamic_array_index].size_used;
|
||||||
|
|
||||||
|
if( i.index >= heap_block_size )
|
||||||
|
{
|
||||||
|
i.dynamic_array_index += 1;
|
||||||
|
i.index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
typename MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::Begin() const
|
||||||
|
{
|
||||||
|
ConstIterator i;
|
||||||
|
|
||||||
|
i.mem_buffer = this;
|
||||||
|
i.dynamic_array_index = size_t(-1);
|
||||||
|
i.index = 0;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
typename MemBuffer<ItemType, stack_size, heap_block_size>::ConstIterator
|
||||||
|
MemBuffer<ItemType, stack_size, heap_block_size>::End() const
|
||||||
|
{
|
||||||
|
ConstIterator i;
|
||||||
|
|
||||||
|
i.mem_buffer = this;
|
||||||
|
|
||||||
|
if( size_used <= stack_size )
|
||||||
|
{
|
||||||
|
i.dynamic_array_index = size_t(-1);
|
||||||
|
i.index = size_used;
|
||||||
|
|
||||||
|
if( i.index >= stack_size )
|
||||||
|
{
|
||||||
|
i.dynamic_array_index = 0;
|
||||||
|
i.index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
i.dynamic_array_index = dynamic_array_index;
|
||||||
|
i.index = dynamic_array[dynamic_array_index].size_used;
|
||||||
|
|
||||||
|
if( i.index >= heap_block_size )
|
||||||
|
{
|
||||||
|
i.dynamic_array_index += 1;
|
||||||
|
i.index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
void MemBuffer<ItemType, stack_size, heap_block_size>::Reserve(size_t len)
|
||||||
|
{
|
||||||
|
size_reserved = len;
|
||||||
|
|
||||||
|
while( size_allocated < size_reserved )
|
||||||
|
AddDynamicNode();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
size_t MemBuffer<ItemType, stack_size, heap_block_size>::Capacity() const
|
||||||
|
{
|
||||||
|
return size_allocated;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class ItemType, size_t stack_size, size_t heap_block_size>
|
||||||
|
void MemBuffer<ItemType, stack_size, heap_block_size>::Clear()
|
||||||
|
{
|
||||||
|
size_t index = 0;
|
||||||
|
|
||||||
|
if( size_reserved > stack_size )
|
||||||
|
{
|
||||||
|
index = (size_reserved - stack_size) / heap_block_size + 1;
|
||||||
|
size_t old_index = index;
|
||||||
|
|
||||||
|
for(; index < dynamic_array_used ; ++index)
|
||||||
|
{
|
||||||
|
size_allocated -= heap_block_size;
|
||||||
|
delete [] dynamic_array[index].buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic_array_used = old_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_used = 0;
|
||||||
|
dynamic_array_index = size_t(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue