-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArray.cpp
More file actions
94 lines (77 loc) · 2.02 KB
/
Array.cpp
File metadata and controls
94 lines (77 loc) · 2.02 KB
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
/*
* Array.cc --
* Extensible array implementation
*
*/
#pragma once
#ifndef _Array_cc_
#define _Array_cc_
#endif
#ifndef lint
static char Array_Copyright[] = "Copyright (c) 1995-2005 SRI International. All Rights Reserved.";
static char Array_RcsId[] = "@(#)$Header: /home/srilm/CVS/srilm/dstruct/src/Array.cc,v 1.11 2005/07/17 22:19:12 stolcke Exp $";
#endif
#include "stdafx.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "Array.h"
#undef INSTANTIATE_ARRAY
#define INSTANTIATE_ARRAY(DataT) \
template class Array<DataT>
/*
* extend the size of an Array to accomodate size elements
* Note we want to zero-initialize the data elements by default,
* so we call their initializers with argument 0. This means
* that all data type used as arguments to the Array template
* need to provide an initializer that accepts 0 as a single argument.
*/
template <class DataT>
void
Array<DataT>::alloc(unsigned int size)
{
unsigned int newSize = size + 1 + alloc_size/2;
DataT *newData = new DataT[newSize];
assert(newData != 0);
#ifdef ZERO_INITIALIZE
memset(newData, 0, newSize * sizeof(DataT));
#endif
for (unsigned i = 0; i < alloc_size; i++) {
newData[i] = _data[i];
}
delete [] _data;
_data = newData;
alloc_size = newSize;
}
template <class DataT>
void
Array<DataT>::memStats(MemStats &stats) const
{
stats.total += _size * sizeof(_data[0]);
stats.wasted += (alloc_size - _size) * sizeof(_data[0]);
}
template <class DataT>
Array<DataT> &
Array<DataT>::operator= (const Array<DataT> &other)
{
#ifdef DEBUG
cerr << "warning: Array::operator= called\n";
#endif
if (&other == this) {
return *this;
}
delete [] _data;
_base = other._base;
_size = other._size;
alloc_size = other.alloc_size;
_data = new DataT[alloc_size];
assert(_data != 0);
#ifdef ZERO_INITIALIZE
memset(_data, 0, alloc_size * sizeof(DataT));
#endif
for (unsigned i = 0; i < alloc_size; i++) {
_data[i] = other._data[i];
}
return *this;
}
/* _Array_cc_ */