gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Array.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 #ifndef ARRAY_H
18 #define ARRAY_H
19 #include <iostream>
20 using namespace std;
21 #include <stdlib.h>
22 
23 template<class ATYPE>
24 class Array {
25  ATYPE *array;
26  unsigned int size;
27 
28  void allocMem(int sizeIn){
29  if (array)
30  deleteMem();
31  //cout<<"allocMem("<<sizeIn<<")"<<endl;
32  array= new ATYPE[sizeIn];
33  if (!array){
34  cerr<<"Couldn't allocate array memory of size "<<size<<endl;
35  exit(-1);
36  }
37  size=sizeIn;
38  }
39 
40  void deleteMem(ATYPE* ptr=NULL){
41  if (ptr=NULL) ptr=array;
42  //cout<<"deleteMem"<<endl;
43  if (ptr){
44  delete [] ptr;
45  ptr=NULL;
46  size=0;
47  }
48  }
49 public:
50  Array(void){
51  size=0;
52  array=NULL;
53  }
54  Array(unsigned int sizeIn){
55  size=0;
56  array=NULL;
57  allocMem(sizeIn);
58  }
59 
60  ~Array(void){
61  deleteMem();
62  }
63 
64  int len(void){return size;}
65 
66  ATYPE& operator[](unsigned int index){
67  if (array==NULL) // handle the case when there are no initialised arrays
68  allocMem(index+1);
69  else if ((index+1)>size){// create a larger array and copy data across
70  ATYPE* oldArray=array;
71  int oldSize=size;
72  allocMem(index+1);
73  for (int i=0;i<oldSize;i++)
74  array[i]=oldArray[i];
75  deleteMem(oldArray);
76  }
77  return array[index];
78  }
79 
80 // friend ostream& operator<<(ostream& c, const Array<ATYPE>& array);
81 
82  void dump(void){
83  for (int i=0;i<len();i++)
84  cout<<array[i]<<'\t';
85  }
86 
87 };
88 
89 //ostream& operator<<(ostream& c, const Array<ATYPE>& array){
90 // for (int i=0;i<array.len();i++)
91  // c<<array[i]<<'\t';
92 // return c;
93 //}
94 #endif //ARRAY_H
size(signal)
~Array(void)
Definition: Array.H:60
Array(unsigned int sizeIn)
Definition: Array.H:54
void dump(void)
Definition: Array.H:82
STL namespace.
void allocMem(int sizeIn)
Definition: Array.H:28
unsigned int size
Definition: Array.H:26
Array(void)
Definition: Array.H:50
int len(void)
Definition: Array.H:64
ATYPE & operator[](unsigned int index)
Definition: Array.H:66
Definition: Array.H:24
ATYPE * array
Definition: Array.H:25
void deleteMem(ATYPE *ptr=NULL)
Definition: Array.H:40
gtkIOStream: /tmp/gtkiostream/include/mffm/possible.future.additions/hash/Array.H Source File
GTK+ IOStream  Beta