Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
resizable.h
Go to the documentation of this file.
1 #include <cstdlib>
2 template < class ITEM >
3 class Resizable
4 {
5  unsigned int num;
6  unsigned int alloc;
7  ITEM *q;
8 public: Resizable()
9  {
10  num = 0;
11  alloc = 16;
12  q = (ITEM*) malloc( sizeof (ITEM)*16 );
13  }
15  {
16  num = c.num;
17  alloc = c.alloc;
18  q = (ITEM*) malloc( sizeof (ITEM)*alloc );
19  memcpy( q, c.q, sizeof (ITEM)*num );
20  }
22  {
23  free( q );
24  q = NULL;
25  }
26  void assert_free( unsigned int n )
27  {
28  while (n+num > alloc) {
29  alloc *= 2;
30  q = (ITEM*) realloc( q, sizeof (ITEM)*alloc );
31  }
32  }
33  void push_back_nocheck( const ITEM &a )
34  {
35  q[num] = a;
36  num++;
37  }
38  void inc_num( unsigned int n )
39  {
40  num += n;
41  }
42  void push_back( const ITEM &a )
43  {
44  if (alloc < num+1) {
45  alloc *= 2;
46  q = (ITEM*) realloc( q, sizeof (ITEM)*alloc );
47  }
48  q[num] = a;
49  num++;
50  }
51  void push_back3( const ITEM aa[3] )
52  {
53  if (alloc < num+3) {
54  alloc *= 2;
55  q = (ITEM*) realloc( q, sizeof (ITEM)*alloc );
56  }
57  q[num] = aa[0];
58  q[num+1] = aa[1];
59  q[num+2] = aa[2];
60  num += 3;
61  }
62  void push_back( const ITEM &aa, const ITEM &bb, const ITEM &cc )
63  {
64  if (alloc < num+3) {
65  alloc *= 2;
66  q = (ITEM*) realloc( q, sizeof (ITEM)*alloc );
67  }
68  q[num] = aa;
69  q[num+1] = bb;
70  q[num+2] = cc;
71  num += 3;
72  }
73  void push_backN( const ITEM *aa, const unsigned int N )
74  {
75  while (alloc < num+N) {
76  alloc *= 2;
77  q = (ITEM*) realloc( q, sizeof (ITEM)*alloc );
78  }
79  for (unsigned int i = 0; i < N; i++)
80  q[num+i] = aa[i];
81  num += N;
82  }
83  ITEM * begin()
84  {
85  return q;
86  }
87  ITEM * end()
88  {
89  return q+num;
90  }
91  void clear()
92  {
93  num = 0;
94  }
95  ITEM& back()
96  {
97  return q[num-1];
98  }
99  ITEM& pop_back()
100  {
101  num--;
102  return q[num];
103  }
104  unsigned int size()
105  {
106  return num;
107  }
108 };
109