1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
/*
* Copyright (c) 2001 Jani Kajala
*
* Permission to use, copy, modify, distribute and sell this
* software and its documentation for any purpose is hereby
* granted without fee, provided that the above copyright notice
* appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
* Jani Kajala makes no representations about the suitability
* of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*/
#ifndef _DEV_ARRAY_H
#define _DEV_ARRAY_H
namespace dev
{
/** Very simple dynamic array. */
template <class T> class Array
{
public:
/** Creates an empty array. */
Array() :
m_data(0), m_len(0), m_cap(0)
{
}
/** Creates an array of specified size. */
explicit Array( int size ) :
m_data(0), m_len(0), m_cap(0)
{
setSize( size );
}
///
~Array()
{
delete[] m_data;
}
/** Appends an item at the end of the array. */
void add( const T& item )
{
if ( m_len+1 > m_cap )
setCapacity( m_len + 1 );
m_data[m_len++] = item;
}
/** Resizes the array. */
void setSize( int size )
{
if ( size > m_cap )
setCapacity( size );
m_len = size;
}
/** Returns ith item. */
T& operator[]( int i )
{
return m_data[i];
}
/** Returns pointer to the first element in the vector. */
T* begin()
{
return m_data;
}
/** Returns pointer to one beyond the last element in the vector. */
T* end()
{
return m_data + m_len;
}
/** Returns number of items in the array. */
int size() const
{
return m_len;
}
/** Returns ith item. */
const T& operator[]( int i ) const
{
return m_data[i];
}
/** Returns pointer to the first element in the vector. */
const T* begin() const
{
return m_data;
}
/** Returns pointer to one beyond the last element in the vector. */
const T* end() const
{
return m_data + m_len;
}
private:
T* m_data;
int m_len;
int m_cap;
void setCapacity( int cap )
{
++cap;
if ( cap < 8 )
cap = 8;
else if ( cap < m_cap*2 )
cap = m_cap*2;
m_cap = cap;
T* data = new T[cap];
for ( int i = 0 ; i < m_len ; ++i )
data[i] = m_data[i];
delete[] m_data;
m_data = data;
}
};
} // dev
#endif // _DEV_ARRAY_H
|