forked from patmorin/ods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.h
More file actions
101 lines (84 loc) · 1.61 KB
/
array.h
File metadata and controls
101 lines (84 loc) · 1.61 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
95
96
97
98
99
100
101
/*
* array.h
*
* Created on: 2011-11-24
* Author: morin
*/
#ifndef ARRAY_H_
#define ARRAY_H_
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <assert.h>
namespace ods {
/**
* A simple array class that simulates Java's arrays implementation - kind of
* TODO: Make a reference-counted version so that the = operator doesn't have
* to destroy its right-hand side.
*/
template<class T>
class array {
protected:
T *a;
public:
int length;
array(int len);
array(int len, T init);
void fill(T x);
virtual ~array();
array<T>& operator=(array<T> &b) {
if (a != NULL) delete[] a;
a = b.a;
b.a = NULL;
length = b.length;
return *this;
}
T& operator[](int i) {
assert(i >= 0 && i < length);
return a[i];
}
T* operator+(int i) {
return &a[i];
}
void swap(int i, int j) {
T x = a[i];
a[i] = a[j];
a[j] = x;
}
static void copyOfRange(array<T> &a0, array<T> &a, int i, int j);
virtual void reverse();
};
template<class T>
array<T>::array(int len) {
length = len;
a = new T[length];
}
template<class T>
array<T>::array(int len, T init) {
length = len;
a = new T[length];
for (int i = 0; i < length; i++)
a[i] = init;
}
template<class T>
array<T>::~array() {
if (a != NULL) delete[] a;
}
template<class T>
void array<T>::reverse() {
for (int i = 0; i < length/2; i++) {
swap(i, length-i-1);
}
}
template<class T>
void array<T>::copyOfRange(array<T> &a0, array<T> &a, int i, int j) {
array<T> b(j-i);
std::copy(a.a, a.a+j-i, b.a);
a0 = b;
}
template<class T>
void array<T>::fill(T x) {
std::fill(a, a+length, x);
}
} /* namespace ods */
#endif /* ARRAY_H_ */