Add support for displaying level names and authors
[numtypysics.git] / Array.h
blob85940451331052e3f98155065ce21a3c9bbc9d0d
1 /*
2 * This file is part of NumptyPhysics
3 * Copyright (C) 2008 Tim Edmonds
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
17 #ifndef ARRAY_H
18 #define ARRAY_H
20 #include "Common.h"
21 #include <memory.h>
23 template <typename T>
24 class Array
26 public:
28 Array( int cap=0 ) : m_data(NULL), m_size(0), m_capacity(0)
30 capacity( cap );
33 Array( int n, const T* d ) : m_data(NULL), m_size(0), m_capacity(0)
35 if ( n ) {
36 capacity( n );
37 memcpy( m_data, d, n * sizeof(T) );
38 m_size = n;
42 Array( const Array& other ) : m_data(NULL), m_size(0), m_capacity(0)
44 if ( other.size() ) {
45 capacity( other.size() );
46 memcpy( m_data, other.m_data, other.size() * sizeof(T) );
47 m_size = other.size();
51 ~Array()
53 if ( m_data ) {
54 free( m_data );
58 int size() const
60 return m_size;
63 void empty()
65 m_size = 0;
68 T& at( int i )
70 ASSERT( i < m_size );
71 return m_data[i];
74 const T& at( int i ) const
76 ASSERT( i < m_size );
77 return m_data[i];
80 void append( const T& t )
82 ensureCapacity( m_size + 1 );
83 m_data[ m_size++ ] = t;
86 void insert( int i, const T& t )
88 if ( i==m_size ) {
89 append( t );
90 } else {
91 ASSERT( i < m_size );
92 ensureCapacity( m_size + 1 );
93 for ( int j=m_size-1; j>=i; j-- ) {
94 m_data[j+1] = m_data[j];
96 m_data[ i ] = t;
97 m_size++;
101 void erase( int i )
103 ASSERT( i < m_size );
104 if ( i < m_size-1 ) {
105 memcpy( m_data+i, m_data+i+1, (m_size-i-1)*sizeof(T) );
107 m_size--;
110 void trim( int i )
112 ASSERT( i < m_size );
113 m_size -= i;
116 void capacity( int c )
118 if ( c >= m_size ) {
119 if ( m_capacity ) {
120 m_data = (T*)realloc( m_data, c * sizeof(T) );
121 } else {
122 m_data = (T*)malloc( c * sizeof(T) );
124 m_capacity = c;
128 int indexOf( const T& t )
130 for ( int i=0; i<m_size; i++ ) {
131 if ( m_data[i] == t ) {
132 return i;
135 return -1;
138 T& operator[]( int i )
140 return at(i);
143 const T& operator[]( int i ) const
145 return at(i);
148 Array<T>& operator=(const Array<T>& other)
150 m_size = 0;
151 if ( other.size() ) {
152 capacity( other.size() );
153 memcpy( m_data, other.m_data, other.size() * sizeof(T) );
154 m_size = other.size();
156 return *this;
159 private:
160 void ensureCapacity( int c )
162 if ( c > m_capacity ) {
163 int newc = m_capacity ? m_capacity : 4;
164 while ( newc < c ) {
165 newc += newc;
167 capacity( newc );
171 T* m_data;
172 int m_size;
173 int m_capacity;
176 #endif //ARRAY_H