-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathG_Array.cpp
123 lines (106 loc) · 3.84 KB
/
G_Array.cpp
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
///////////////////////////////////////////////////////////////////////////////
// Title : G_Array
// Date : March 20, 2002
// Author : Kristina Klinkner
// Description : creates an array of pointers to elements, which
// can be any size that will fit into memory.
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2002 Kristina Klinkner
// This file is part of CSSR
//
// CSSR 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 2 of the License, or
// (at your option) any later version.
//
// CSSR 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.
//
// You should have received a copy of the GNU General Public License
// along with CSSR; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//////////////////////////////////////////////////////////////////////////////
#include "G_Array.h"
////////////////////////////////////////////////////////////////
//Function: G_Array::Grow
//Purpose: updates size of array
//In parameter: new array size
////////////////////////////////////////////////////////////////
void G_Array::Grow(int newsize)
{
//check if index is valid
ArrayElem** newBuffer = NULL;
newBuffer = new ArrayElem*[newsize];
if (newBuffer == NULL)
{
cerr << "Out of memory." << endl;
exit(1);
}
//copy array contents into newBuffer
for (int i=0;i<=m_size;i++)
{
newBuffer[i] = m_G_ArrayList[i];
}
delete [] m_G_ArrayList;
m_G_ArrayList = NULL;
//point the array at the newly created buffer
m_G_ArrayList = newBuffer;
m_maxsize = newsize;
}
/////////////////////////////////////////////////////////////
//Function: G_Array::Insert
//Purpose: adds new element to the growable array
//In parameter: string, array of ints and length of
// array of ints
/////////////////////////////////////////////////////////////
void G_Array::Insert(char string[], int counts[], int length)
{
ArrayElem* temp = new ArrayElem;
temp->setString(string);
temp->setCounts(counts, length);
if (Full())
Grow( m_maxsize + G_INCREMENT);
m_G_ArrayList[m_size] = temp;
m_size++;
}
////////////////////////////////////////////////////////////
//Function: G_Array::~G_Array
//Purpose: destructor for G_Array
////////////////////////////////////////////////////////////
G_Array::~G_Array()
{
if(m_G_ArrayList)
{
for(int i =0; i< m_size; i++)
delete m_G_ArrayList[i];
delete[] m_G_ArrayList;
m_G_ArrayList = NULL;
}
}
/////////////////////////////////////////////////////////////
//Function: ArrayElem::setCounts
//Purpose: set m_counts to specified value
//In parameters: the array with values and the length of the
// array
////////////////////////////////////////////////////////////
void ArrayElem::setCounts(int counts[], int length)
{
m_counts = new int[length];
for(int i = 0; i < length; i++)
m_counts[i] = counts[i];
}
/////////////////////////////////////////////////////////////
//Function: ArrayElem::setString
//Purpose: set m_string to specified value
//In parameters: the string of desired value
////////////////////////////////////////////////////////////
void ArrayElem::setString(char string[])
{
m_string = new char[strlen(string)+ 1];
strcpy(m_string, string);
}