2012-07-02 00:38:27 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of PikoTools
|
|
|
|
* and is distributed under the (new) BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-02-17 16:53:19 +01:00
|
|
|
* Copyright (c) 2012-2021, Tomasz Sowa
|
2012-07-02 00:38:27 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
2012-07-01 02:39:41 +02:00
|
|
|
|
|
|
|
#ifndef headerfile_picotools_membuffer_membuffer
|
|
|
|
#define headerfile_picotools_membuffer_membuffer
|
|
|
|
|
|
|
|
|
2013-11-28 23:21:10 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
2012-07-01 02:39:41 +02:00
|
|
|
namespace PT
|
|
|
|
{
|
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
/*
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
|
2012-09-11 23:41:48 +02:00
|
|
|
stack_size and heap_block_size have to be *greater* than zero
|
2012-07-02 00:38:27 +02:00
|
|
|
*/
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
2012-07-01 02:39:41 +02:00
|
|
|
class MemBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
class iterator
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
public:
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2021-02-17 16:53:19 +01:00
|
|
|
bool operator==(const iterator & i) const;
|
|
|
|
bool operator!=(const iterator & i) const;
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
iterator & operator++(); // prefix ++
|
|
|
|
iterator operator++(int); // postfix ++
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
iterator & operator--(); // prefix --
|
|
|
|
iterator operator--(int); // postfix --
|
2012-07-02 00:38:27 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
item_type & operator*();
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
MemBuffer * mem_buffer;
|
|
|
|
size_t dynamic_array_index;
|
|
|
|
size_t index;
|
|
|
|
|
2012-07-02 00:49:08 +02:00
|
|
|
friend class MemBuffer;
|
2012-07-01 02:39:41 +02:00
|
|
|
};
|
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
class const_iterator
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
public:
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
const_iterator();
|
|
|
|
const_iterator(const const_iterator & i);
|
|
|
|
const_iterator(const iterator & i);
|
|
|
|
const_iterator & operator=(const const_iterator & i);
|
|
|
|
const_iterator & operator=(const iterator & i);
|
2012-07-02 00:38:27 +02:00
|
|
|
|
2021-02-17 16:53:19 +01:00
|
|
|
bool operator==(const const_iterator & i) const;
|
|
|
|
bool operator!=(const const_iterator & i) const;
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
const_iterator & operator++(); // prefix ++
|
|
|
|
const_iterator operator++(int); // postfix ++
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
const_iterator & operator--(); // prefix --
|
|
|
|
const_iterator operator--(int); // postfix --
|
|
|
|
|
|
|
|
item_type operator*();
|
2012-07-02 00:38:27 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
const MemBuffer * mem_buffer;
|
|
|
|
size_t dynamic_array_index;
|
|
|
|
size_t index;
|
|
|
|
|
2012-07-02 00:49:08 +02:00
|
|
|
friend class MemBuffer;
|
2012-07-01 02:39:41 +02:00
|
|
|
};
|
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
|
|
|
|
|
2012-07-01 02:39:41 +02:00
|
|
|
MemBuffer();
|
2012-07-02 00:38:27 +02:00
|
|
|
~MemBuffer();
|
2012-07-03 16:58:18 +02:00
|
|
|
MemBuffer(const MemBuffer<item_type, stack_size, heap_block_size> & arg);
|
|
|
|
MemBuffer & operator=(const MemBuffer<item_type, stack_size, heap_block_size> & arg);
|
|
|
|
|
|
|
|
void append(item_type item);
|
2012-07-10 18:53:26 +02:00
|
|
|
void append(const item_type * item_array, size_t len);
|
2013-11-28 23:21:10 +01:00
|
|
|
|
|
|
|
template<typename in_item_type>
|
|
|
|
void append(const in_item_type * item_array, size_t len);
|
2012-07-03 16:58:18 +02:00
|
|
|
|
|
|
|
template<typename arg_item_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
|
|
|
void append(const MemBuffer<arg_item_type, arg_stack_size, arg_heap_block_size> & arg);
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
size_t size() const;
|
|
|
|
bool empty() const;
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
void reserve(size_t len);
|
|
|
|
size_t capacity() const;
|
|
|
|
void clear(); // frees memory but only to capacity()
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
iterator begin();
|
|
|
|
iterator end();
|
2012-07-02 00:38:27 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
const_iterator begin() const;
|
|
|
|
const_iterator end() const;
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
item_type & operator[](size_t i);
|
|
|
|
const item_type operator[](size_t i) const;
|
2012-07-02 00:38:27 +02:00
|
|
|
|
2012-07-01 02:39:41 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
struct MemArray
|
|
|
|
{
|
|
|
|
size_t size_used;
|
2012-07-03 16:58:18 +02:00
|
|
|
item_type * buf;
|
2012-07-01 02:39:41 +02:00
|
|
|
};
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
item_type stack_array[stack_size];
|
2012-07-02 00:38:27 +02:00
|
|
|
MemArray * dynamic_array; // dynamic array of MemArray descriptors
|
|
|
|
size_t dynamic_array_index; // index of a MemArray to which the last insertion was made
|
|
|
|
// size_t(-1) means the stack_array
|
|
|
|
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
|
2012-07-03 16:58:18 +02:00
|
|
|
size_t size_reserved; // memory reserved by reserve(), it is used by clear()
|
2012-07-02 00:38:27 +02:00
|
|
|
|
2012-10-27 09:45:39 +02:00
|
|
|
// used by ctors
|
|
|
|
void Initialize();
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
void add_dynamic_node();
|
2012-07-01 02:39:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
/*
|
2012-07-03 16:58:18 +02:00
|
|
|
iterator
|
2012-07-02 00:38:27 +02:00
|
|
|
*/
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::iterator &
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::iterator::operator++()
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
if( dynamic_array_index == size_t(-1) )
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
index += 1;
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
if( index >= stack_size )
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
index = 0;
|
|
|
|
dynamic_array_index = 0;
|
2012-07-01 02:39:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
index += 1;
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
if( index >= heap_block_size )
|
|
|
|
{
|
|
|
|
dynamic_array_index += 1;
|
|
|
|
index = 0;
|
2012-07-01 02:39:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::iterator
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::iterator::operator++(int)
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
2012-07-03 16:58:18 +02:00
|
|
|
iterator old(*this);
|
2012-07-02 00:38:27 +02:00
|
|
|
operator++();
|
|
|
|
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::iterator &
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::iterator::operator--()
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::iterator
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::iterator::operator--(int)
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-03 16:58:18 +02:00
|
|
|
iterator old(*this);
|
2012-07-01 02:39:41 +02:00
|
|
|
operator++();
|
|
|
|
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
item_type & MemBuffer<item_type, stack_size, heap_block_size>::iterator::operator*()
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
if( dynamic_array_index == size_t(-1) )
|
|
|
|
{
|
|
|
|
return mem_buffer->stack_array[index];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return mem_buffer->dynamic_array[dynamic_array_index].buf[index];
|
|
|
|
}
|
2012-07-01 02:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
2021-02-17 16:53:19 +01:00
|
|
|
bool MemBuffer<item_type, stack_size, heap_block_size>::iterator::operator==(const iterator & i) const
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
return mem_buffer == i.mem_buffer &&
|
|
|
|
dynamic_array_index == i.dynamic_array_index &&
|
|
|
|
index == i.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
2021-02-17 16:53:19 +01:00
|
|
|
bool MemBuffer<item_type, stack_size, heap_block_size>::iterator::operator!=(const iterator & i) const
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
return mem_buffer != i.mem_buffer ||
|
|
|
|
dynamic_array_index != i.dynamic_array_index ||
|
|
|
|
index != i.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-07-03 16:58:18 +02:00
|
|
|
const_iterator
|
2012-07-02 00:38:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::const_iterator::const_iterator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::const_iterator::const_iterator(const const_iterator & i)
|
|
|
|
{
|
|
|
|
operator=(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::const_iterator::const_iterator(const iterator & i)
|
|
|
|
{
|
|
|
|
operator=(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::const_iterator &
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::const_iterator::operator=(const const_iterator & i)
|
|
|
|
{
|
|
|
|
mem_buffer = i.mem_buffer;
|
|
|
|
dynamic_array_index = i.dynamic_array_index;
|
|
|
|
index = i.index;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::const_iterator &
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::const_iterator::operator=(const iterator & i)
|
|
|
|
{
|
|
|
|
mem_buffer = i.mem_buffer;
|
|
|
|
dynamic_array_index = i.dynamic_array_index;
|
|
|
|
index = i.index;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::const_iterator &
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::const_iterator::operator++()
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
if( dynamic_array_index == size_t(-1) )
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
index += 1;
|
|
|
|
|
|
|
|
if( index >= stack_size )
|
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
dynamic_array_index = 0;
|
|
|
|
}
|
2012-07-01 02:39:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
index += 1;
|
|
|
|
|
|
|
|
if( index >= heap_block_size )
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
dynamic_array_index += 1;
|
|
|
|
index = 0;
|
2012-07-01 02:39:41 +02:00
|
|
|
}
|
2012-07-02 00:38:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::const_iterator
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::const_iterator::operator++(int)
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
2012-07-03 16:58:18 +02:00
|
|
|
const_iterator old(*this);
|
2012-07-02 00:38:27 +02:00
|
|
|
operator++();
|
|
|
|
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::const_iterator &
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::const_iterator::operator--()
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
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;
|
2012-07-01 02:39:41 +02:00
|
|
|
}
|
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::const_iterator
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::const_iterator::operator--(int)
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
2012-07-03 16:58:18 +02:00
|
|
|
const_iterator old(*this);
|
2012-07-02 00:38:27 +02:00
|
|
|
operator++();
|
|
|
|
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
item_type MemBuffer<item_type, stack_size, heap_block_size>::const_iterator::operator*()
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
if( dynamic_array_index == size_t(-1) )
|
|
|
|
{
|
|
|
|
return mem_buffer->stack_array[index];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return mem_buffer->dynamic_array[dynamic_array_index].buf[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
2021-02-17 16:53:19 +01:00
|
|
|
bool MemBuffer<item_type, stack_size, heap_block_size>::const_iterator::operator==(const const_iterator & i) const
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
return mem_buffer == i.mem_buffer &&
|
|
|
|
dynamic_array_index == i.dynamic_array_index &&
|
|
|
|
index == i.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
2021-02-17 16:53:19 +01:00
|
|
|
bool MemBuffer<item_type, stack_size, heap_block_size>::const_iterator::operator!=(const const_iterator & i) const
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
return mem_buffer != i.mem_buffer ||
|
|
|
|
dynamic_array_index != i.dynamic_array_index ||
|
|
|
|
index != i.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
MemBuffer
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
2012-10-27 09:45:39 +02:00
|
|
|
void MemBuffer<item_type, stack_size, heap_block_size>::Initialize()
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-27 09:45:39 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::MemBuffer()
|
|
|
|
{
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::~MemBuffer()
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
if( dynamic_array )
|
|
|
|
{
|
|
|
|
for(size_t i=0 ; i<dynamic_array_used ; ++i)
|
|
|
|
delete [] dynamic_array[i].buf;
|
|
|
|
|
|
|
|
delete [] dynamic_array;
|
|
|
|
}
|
2012-07-01 02:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::MemBuffer(const MemBuffer<item_type, stack_size, heap_block_size> & arg)
|
2012-07-02 22:27:31 +02:00
|
|
|
{
|
2012-10-27 09:45:39 +02:00
|
|
|
Initialize();
|
2012-07-02 22:27:31 +02:00
|
|
|
operator=(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size> &
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::operator=(const MemBuffer<item_type, stack_size, heap_block_size> & arg)
|
2012-07-02 22:27:31 +02:00
|
|
|
{
|
|
|
|
if( size_used > 0 )
|
2012-07-03 16:58:18 +02:00
|
|
|
clear();
|
2012-07-02 22:27:31 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
const_iterator i = arg.begin();
|
2012-07-02 22:27:31 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
for( ; i != arg.end() ; ++i)
|
|
|
|
append(*i);
|
2012-07-02 22:27:31 +02:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
void MemBuffer<item_type, stack_size, heap_block_size>::add_dynamic_node()
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
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;
|
2012-07-03 16:58:18 +02:00
|
|
|
dynamic_array[dynamic_array_used].buf = new item_type[heap_block_size];
|
2012-07-02 00:38:27 +02:00
|
|
|
|
|
|
|
dynamic_array_used += 1;
|
|
|
|
size_allocated += heap_block_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
void MemBuffer<item_type, stack_size, heap_block_size>::append(item_type item)
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
if( size_used < stack_size )
|
|
|
|
{
|
|
|
|
stack_array[size_used] = item;
|
|
|
|
}
|
|
|
|
else
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-02 00:38:27 +02:00
|
|
|
if( dynamic_array_index == size_t(-1) )
|
|
|
|
{
|
|
|
|
dynamic_array_index = 0;
|
|
|
|
|
|
|
|
if( dynamic_array_index >= dynamic_array_used )
|
2012-07-03 16:58:18 +02:00
|
|
|
add_dynamic_node();
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
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 )
|
2012-07-03 16:58:18 +02:00
|
|
|
add_dynamic_node();
|
2012-07-02 00:38:27 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
2012-07-10 18:53:26 +02:00
|
|
|
void MemBuffer<item_type, stack_size, heap_block_size>::append(const item_type * item_array, size_t len)
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
if( size_used + len <= stack_size )
|
|
|
|
{
|
2012-07-01 02:39:41 +02:00
|
|
|
for(size_t i=0 ; i<len ; ++i)
|
|
|
|
stack_array[size_used++] = item_array[i];
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
if( dynamic_array_index != size_t(-1) &&
|
|
|
|
dynamic_array[dynamic_array_index].size_used + len <= heap_block_size )
|
2012-07-01 02:39:41 +02:00
|
|
|
{
|
2012-07-03 16:58:18 +02:00
|
|
|
item_type * buf = dynamic_array[dynamic_array_index].buf;
|
2012-07-02 00:38:27 +02:00
|
|
|
size_t bufsize = dynamic_array[dynamic_array_index].size_used;
|
2012-07-01 02:39:41 +02:00
|
|
|
|
|
|
|
for(size_t i=0 ; i<len ; ++i)
|
|
|
|
buf[bufsize++] = item_array[i];
|
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
dynamic_array[dynamic_array_index].size_used += len;
|
2012-07-01 02:39:41 +02:00
|
|
|
size_used += len;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i=0 ; i<len ; ++i)
|
2012-07-03 16:58:18 +02:00
|
|
|
append(item_array[i]);
|
2012-07-01 02:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-28 23:21:10 +01:00
|
|
|
|
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
template<typename in_item_type>
|
|
|
|
void MemBuffer<item_type, stack_size, heap_block_size>::append(const in_item_type * item_array, size_t len)
|
|
|
|
{
|
|
|
|
for(size_t i=0 ; i<len ; ++i)
|
|
|
|
append(static_cast<item_type>(item_array[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
template<typename arg_item_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
|
|
|
void MemBuffer<item_type, stack_size, heap_block_size>::append(
|
|
|
|
const MemBuffer<arg_item_type, arg_stack_size, arg_heap_block_size> & arg)
|
|
|
|
{
|
|
|
|
typename MemBuffer<arg_item_type, arg_stack_size, arg_heap_block_size>::const_iterator i = arg.begin();
|
|
|
|
|
|
|
|
for( ; i != arg.end() ; ++i)
|
|
|
|
append(static_cast<item_type>(*i));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-01 02:39:41 +02:00
|
|
|
|
2012-07-02 00:38:27 +02:00
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
item_type & MemBuffer<item_type, stack_size, heap_block_size>::operator[](size_t i)
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
const item_type MemBuffer<item_type, stack_size, heap_block_size>::operator[](size_t i) const
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
size_t MemBuffer<item_type, stack_size, heap_block_size>::size() const
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
return size_used;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
bool MemBuffer<item_type, stack_size, heap_block_size>::empty() const
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
return size_used == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::iterator
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::begin()
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
2012-07-03 16:58:18 +02:00
|
|
|
iterator i;
|
2012-07-02 00:38:27 +02:00
|
|
|
|
|
|
|
i.mem_buffer = this;
|
|
|
|
i.dynamic_array_index = size_t(-1);
|
|
|
|
i.index = 0;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::iterator
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::end()
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
2012-07-03 16:58:18 +02:00
|
|
|
iterator i;
|
2012-07-02 00:38:27 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::const_iterator
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::begin() const
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
2012-07-03 16:58:18 +02:00
|
|
|
const_iterator i;
|
2012-07-02 00:38:27 +02:00
|
|
|
|
|
|
|
i.mem_buffer = this;
|
|
|
|
i.dynamic_array_index = size_t(-1);
|
|
|
|
i.index = 0;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
typename MemBuffer<item_type, stack_size, heap_block_size>::const_iterator
|
|
|
|
MemBuffer<item_type, stack_size, heap_block_size>::end() const
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
2012-07-03 16:58:18 +02:00
|
|
|
const_iterator i;
|
2012-07-02 00:38:27 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
void MemBuffer<item_type, stack_size, heap_block_size>::reserve(size_t len)
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
size_reserved = len;
|
|
|
|
|
|
|
|
while( size_allocated < size_reserved )
|
2012-07-03 16:58:18 +02:00
|
|
|
add_dynamic_node();
|
2012-07-02 00:38:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
size_t MemBuffer<item_type, stack_size, heap_block_size>::capacity() const
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
return size_allocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
template<typename item_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
void MemBuffer<item_type, stack_size, heap_block_size>::clear()
|
2012-07-02 00:38:27 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-01 02:39:41 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|