Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 60 additions & 26 deletions bflibcpp/src/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
#include <stdbool.h>
#include <stdint.h>
#include <initializer_list>
#include <utility>
#include <iostream>
#include "access.hpp"
#include "vector.hpp"
#include <string.h>
#include "exception.hpp"
#include "swap.hpp"

extern "C" {
#include <bflibc/bfmath.h>
}

namespace BF {

/**
Expand All @@ -26,8 +31,14 @@ namespace BF {
*
* Objects stored in array are assumed to be owned by owner of array
* object
*
* blockSize:
* block size of memory allocation
*
* each reallocation where count > capacity, will always
* allocate memory in blocks of blockSize
*/
template <typename T, typename S = long>
template <typename T, typename S = long, S blockSize = 2 << 3>
class Array : public Vector<T,S> {
public:
virtual const char * className() const {
Expand All @@ -39,6 +50,9 @@ class Array : public Vector<T,S> {
this->_count = 0;
this->_callback = Array::comparisonDefault;
this->_releasecb = NULL;

this->_capacity = 0;
this->allocate(*this, blockSize);
}

/**
Expand Down Expand Up @@ -75,7 +89,8 @@ class Array : public Vector<T,S> {
}
}

this->deallocate(this->_address);
//this->deallocate(this->_address);
this->deallocate(*this);
this->_address = 0;
this->_count = 0;
}
Expand Down Expand Up @@ -180,7 +195,6 @@ class Array : public Vector<T,S> {
return res;
}


/**
* Prints the array from the first element to the last
*/
Expand Down Expand Up @@ -210,19 +224,18 @@ class Array : public Vector<T,S> {
/**
* Copies content from arr to us
*/
void copyFromArray(const Array<T,S> * arr) {
void copyFromArray(const Array<T,S,blockSize> * arr) {
this->removeAll();
this->_address = (T *) this->allocate(arr->count());
this->allocate(*this, arr->count());
this->_count = arr->count();
memcpy(this->_address, arr->address(), this->_count);
}

/**
* copies content of arr to the end of ours
*/
void append(const Array<T,S> & arr) {
this->_address = this->reallocate(this->_address, this->_count + arr._count);
//memcpy(&this->_address[this->_count], &arr._address[0], arr._count);
void append(const Array<T,S,blockSize> & arr) {
this->reallocate(*this, this->_count, this->_count + arr._count);
for (int i = this->_count; i < this->_count + arr._count; i++) {
this->_address[i] = arr._address[i - this->_count];
}
Expand All @@ -233,7 +246,7 @@ class Array : public Vector<T,S> {
* Adds object at the end of the array
*/
int add(T obj) {
this->_address = this->reallocate(this->_address, this->_count + 1);
this->reallocate(*this, this->_count, this->_count + 1);
if (this->_address == NULL) {
this->_count = 0;
return -3;
Expand Down Expand Up @@ -290,45 +303,60 @@ class Array : public Vector<T,S> {
* adjusts address memory to size
*/
void adjustMemorySize(S size) {
this->reallocate(*this, this->_count, size);
this->_count = size;
this->_address = this->reallocate(this->_address, this->_count);
}

/**
* Returns address of array
*/
T * address() const { return this->_address; }
T * address() const {
// capacity may be > 0 and _address may be allocated
if (this->_count == 0) return NULL;
return this->_address;
}

private:

/**
* uses malloc to allocate mem
*/
static T * allocate(S size) {
return (T *) malloc(sizeof(T) * size);
static void allocate(Array<T,S,blockSize> & array, S size) {
if (size > array._capacity) {
array._capacity = size;
array._address = (T *) new T[size];
}
}

/**
* returns modified `addr` with `newsize`
*/
static T * reallocate(T * addr, S newsize) {
return (T *) realloc(addr, sizeof(T) * newsize);
static void reallocate(Array<T,S,blockSize> & array, S oldsize, S newsize) {
if (newsize < array._capacity) {
return;
}

S adjustedNewSize = (((newsize / blockSize) + 1) * blockSize);
array._capacity = adjustedNewSize;
T * res = new T[adjustedNewSize];
memcpy(res, array._address, sizeof(T) * oldsize);
memset(array._address, 0, sizeof(T) * oldsize);

delete[] array._address;
array._address = res;

return;
}

/**
* Derived must make sure this follows the standard established
* by allocate()
*/
static void deallocate(T * value) {
free((void *) value);
static void deallocate(Array<T,S,blockSize> & array) {
delete[] array._address;
array._capacity = 0;
}

/**
* Copies values from array
*/
void saveArray(const T * array, S size) {
this->removeAll();
this->_address = (T *) this->allocate(size);
this->allocate(*this, size);
this->_count = size;

if (this->_address) {
Expand All @@ -347,7 +375,7 @@ class Array : public Vector<T,S> {
typename std::initializer_list<T>::iterator itr;

this->_count = list.size();
this->_address = (T *) this->allocate(this->_count);
this->allocate(*this, this->_count);

if (this->_address) {
S i = 0;
Expand All @@ -368,6 +396,12 @@ class Array : public Vector<T,S> {
/// Holds size of _address
S _count;

/**
* will hold a certain amount of reserved space for the
* array elements. we will ask for more memory when _count > _capacity
*/
S _capacity;

/**
* How we compare each item in the array
*/
Expand All @@ -390,7 +424,7 @@ class Array : public Vector<T,S> {
/**
* Copies the string content from arr to us
*/
virtual Array<T,S> & operator=(const Array<T,S> & arr) {
virtual Array<T,S,blockSize> & operator=(const Array<T,S,blockSize> & arr) {
this->copyFromArray(&arr);
return *this;
}
Expand Down
2 changes: 1 addition & 1 deletion bflibcpp/src/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Data::Data(const Data & in) : Data(in.size(), (unsigned char *) in.buffer()) { }

Data::~Data() { }

Data::Data(const size_t size, const unsigned char * data) : Array<unsigned char, size_t>() {
Data::Data(const size_t size, const unsigned char * data) : Array<unsigned char, size_t, 2<<5>() {
this->alloc(size, data);
}

Expand Down
2 changes: 1 addition & 1 deletion bflibcpp/src/data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace BF {
class String;
class URL;

class Data : public Array<unsigned char, size_t> {
class Data : public Array<unsigned char, size_t, 2<<5> {
public:
virtual const char * className() const;

Expand Down
2 changes: 1 addition & 1 deletion bflibcpp/src/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace BF {

template<template <typename...> class T> struct Sort;
template<typename T> struct Sort;

/**
* Linked List implementation
Expand Down
Loading