Rewrote our array class.

This commit is contained in:
n-a-c-h 2015-06-16 01:59:41 +03:00
parent e083c00982
commit 3b850ac295
3 changed files with 46 additions and 41 deletions

View File

@ -1,40 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Sindre Aam<EFBFBD>s *
* aamas@stud.ntnu.no *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License version 2 as *
* published by the Free Software Foundation. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License version 2 for more details. *
* *
* You should have received a copy of the GNU General Public License *
* version 2 along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef ARRAY_H
#define ARRAY_H
#include <cstddef>
template<typename T>
class Array {
T *a;
std::size_t sz;
Array(const Array &ar);
public:
Array(const std::size_t size = 0) : a(size ? new T[size] : 0), sz(size) {}
~Array() { delete []a; }
void reset(const std::size_t size) { delete []a; a = size ? new T[size] : 0; sz = size; }
std::size_t size() const { return sz; }
operator T*() { return a; }
operator const T*() const { return a; }
};
#endif

View File

@ -19,7 +19,7 @@
#ifndef RINGBUFFER_H
#define RINGBUFFER_H
#include "Array.h"
#include "array.h"
#include <cstddef>
#include <algorithm>
#include <cstring>

45
src/common/array.h Normal file
View File

@ -0,0 +1,45 @@
#ifndef ARRAY_H
#define ARRAY_H
#include <iterator>
#include <cstddef>
template<typename T>
class Array
{
public:
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T &reference;
typedef const T &const_reference;
typedef T *pointer;
typedef const T *const_pointer;
typedef T *iterator;
typedef const T *const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
private:
pointer m_p;
size_type m_size;
public:
Array(size_type size = 0) : m_p(size ? new value_type[size] : 0), m_size(size) {}
void reset(size_t size)
{
delete[] this->m_p;
this->m_p = size ? new value_type[size] : 0;
this->m_size = size;
}
size_type size() const { return(this->m_size); }
operator pointer() { return(this->m_p); }
operator const_pointer() const { return(this->m_p); }
///TODO: Add more functions from here: http://en.cppreference.com/w/cpp/container/array
};
#endif