File: Array.h

package info (click to toggle)
numptyphysics 0.2+svn157-0.3
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 2,596 kB
  • ctags: 2,896
  • sloc: cpp: 18,280; sh: 810; makefile: 205
file content (178 lines) | stat: -rw-r--r-- 3,186 bytes parent folder | download
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * This file is part of NumptyPhysics
 * Copyright (C) 2008 Tim Edmonds
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * 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 for more details.
 *
 */

#ifndef ARRAY_H
#define ARRAY_H

#include "Common.h"
#include <memory.h>

template <typename T>
class Array
{
 public:

  Array( int cap=0 ) : m_data(NULL), m_size(0), m_capacity(0)
  {
    capacity( cap );
  }
  
 Array( int n, const T* d ) : m_data(NULL), m_size(0), m_capacity(0)
  {
    if ( n ) {
      capacity( n );
      memcpy( m_data, d, n * sizeof(T) );
      m_size = n;
    }
  }

  Array( const Array& other ) : m_data(NULL), m_size(0), m_capacity(0)
  {
    if ( other.size() ) {
      capacity( other.size() );
      memcpy( m_data, other.m_data, other.size() * sizeof(T) );
      m_size = other.size();
    }
  }

  ~Array()
  {
    if ( m_data ) {
      free( m_data );
    }
  }

  int size() const
  {
    return m_size;
  }

  void empty()
  {
    m_size = 0;
  }

  T& at( int i )
  {
    ASSERT( i < m_size );
    return m_data[i];
  }

  const T& at( int i ) const
  {
    ASSERT( i < m_size );
    return m_data[i];
  }

  void append( const T& t )
  {
    ensureCapacity( m_size + 1 );
    m_data[ m_size++ ] = t;
  }

  void insert( int i, const T& t )
  {
    if ( i==m_size ) {
      append( t );
    } else {
      ASSERT( i < m_size );
      ensureCapacity( m_size + 1 );
      for ( int j=m_size-1; j>=i; j-- ) {
	m_data[j+1] = m_data[j];
      }
      m_data[ i ] = t;
      m_size++;
    }
  }

  void erase( int i )
  {
    if (i >= 0 ) {
      ASSERT( i < m_size );
      if ( i < m_size-1 ) {
	memmove( m_data+i, m_data+i+1, (m_size-i-1)*sizeof(T) );
      }
      m_size--;
    }
  }

  void trim( int i )
  {
    ASSERT( i < m_size );
    m_size -= i;
  }

  void capacity( int c )
  {
    if ( c >= m_size ) {
      if ( m_capacity ) {
	m_data = (T*)realloc( m_data, c * sizeof(T) );
      } else {
	m_data = (T*)malloc( c * sizeof(T) );
      }
      m_capacity = c;
    }
  }

  int indexOf( const T& t )
  {
    for ( int i=0; i<m_size; i++ ) {
      if ( m_data[i] == t ) {
	return i;
      }
    }
    return -1;
  }

  T& operator[]( int i )
  {
    return at(i);
  }

  const T& operator[]( int i ) const
  {
    return at(i);
  }

  Array<T>& operator=(const Array<T>& other) 
  {
    m_size = 0;
    if ( other.size() ) {
      capacity( other.size() );
      memcpy( m_data, other.m_data, other.size() * sizeof(T) );
      m_size = other.size();
    }
    return *this;
  }

 private:
  void ensureCapacity( int c ) 
  {
    if ( c > m_capacity ) {
      int newc = m_capacity ? m_capacity : 4;
      while ( newc < c  ) {
	newc += newc;
      }
      capacity( newc );
    }
  }

  T* m_data;
  int m_size;
  int m_capacity;
};

#endif //ARRAY_H