2 * This file is part of NumptyPhysics
3 * Copyright (C) 2008 Tim Edmonds
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.
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.
28 Array( int cap
=0 ) : m_data(NULL
), m_size(0), m_capacity(0)
33 Array( int n
, const T
* d
) : m_data(NULL
), m_size(0), m_capacity(0)
37 memcpy( m_data
, d
, n
* sizeof(T
) );
42 Array( const Array
& other
) : m_data(NULL
), m_size(0), m_capacity(0)
45 capacity( other
.size() );
46 memcpy( m_data
, other
.m_data
, other
.size() * sizeof(T
) );
47 m_size
= other
.size();
74 const T
& at( int i
) const
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
)
92 ensureCapacity( m_size
+ 1 );
93 for ( int j
=m_size
-1; j
>=i
; j
-- ) {
94 m_data
[j
+1] = m_data
[j
];
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
) );
112 ASSERT( i
< m_size
);
116 void capacity( int c
)
120 m_data
= (T
*)realloc( m_data
, c
* sizeof(T
) );
122 m_data
= (T
*)malloc( c
* sizeof(T
) );
128 int indexOf( const T
& t
)
130 for ( int i
=0; i
<m_size
; i
++ ) {
131 if ( m_data
[i
] == t
) {
138 T
& operator[]( int i
)
143 const T
& operator[]( int i
) const
148 Array
<T
>& operator=(const Array
<T
>& other
)
151 if ( other
.size() ) {
152 capacity( other
.size() );
153 memcpy( m_data
, other
.m_data
, other
.size() * sizeof(T
) );
154 m_size
= other
.size();
160 void ensureCapacity( int c
)
162 if ( c
> m_capacity
) {
163 int newc
= m_capacity
? m_capacity
: 4;