comparison anagram/support/agarray-imp.h @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children 607e3be6bad8
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /**********************************************************
2
3 The AnaGram Class Library
4
5 The AgArray Class
6 Copyright 1997-1999 Parsifal Software. All Rights Reserved.
7 See the file COPYING for license and usage terms.
8
9 ***********************************************************/
10
11 #ifndef AGARRAY_IMP_H
12 #define AGARRAY_IMP_H
13
14 template <class T>
15 AgArray<T>::AgArray()
16 : AgReferenceBase()
17 {
18 //LOGSECTION("AgArray<T>::AgArray");
19 }
20
21
22 template <class T>
23 T &AgArray<T>::Kernel::operator [] (const unsigned x) {
24 //LOGSECTION("AgArray::Kernel::operator []");
25 //LOGV(x) LCV(length);
26 assert(x < length);
27 return data[x];
28 }
29
30 template <class T>
31 const T &AgArray<T>::Kernel::operator [] (const unsigned x) const {
32 //LOGSECTION("AgArray::Kernel::operator [] const");
33 //LOGV(x) LCV(length);
34 assert(x < length);
35 return data[x];
36 }
37
38 template <class T>
39 AgArray<T>::Kernel::Kernel() : length(0), data(0) {
40 // nothing
41 }
42
43 template <class T>
44 AgArray<T>::Kernel::Kernel(unsigned n) : length(n), data(new T[n]) {
45 //arrayBytesAllocated += n*sizeof(T);
46 //arraysAllocated++;
47 //LOGV(arrayBytesAllocated);
48 }
49
50 template <class T>
51 AgArray<T>::Kernel::~Kernel() {
52 //LOGSECTION("AgArray::Kernel::~Kernel");
53 if (data == 0) {
54 return;
55 }
56 delete [] data;
57 //arrayBytesFreed += length*sizeof(T);
58 //arraysFreed++;
59 }
60
61 template <class T>
62 typename AgArray<T>::Kernel &AgArray<T>::kernel() const {
63 //LOGSECTION("AgArray::kernel");
64 assert(object != 0);
65 return (Kernel &) (*object);
66 }
67
68 template <class T>
69 AgString AgArray<T>::asString() {
70 //LOGSECTION("AgArray::asString");
71 if (object) {
72 Kernel &k = kernel();
73 return AgString::format("%d @ %p", k.length, k.data);
74 }
75 else {
76 return AgString("Nothing here");
77 }
78 }
79
80
81 template <class T>
82 AgArray<T>::AgArray(const AgIndexedContainer<T> &c)
83 : AgReferenceBase(c.size() ? new Kernel(c.size()) : 0)
84 {
85 //LOGSECTION("AgArray::AgArray(AgIndexedArray)");
86 //LOGV(c.size());
87 int n = c.size();
88 for (int i = 0; i < n; i++) {
89 (*this)[i] = c[i];
90 }
91 }
92
93 template <class T>
94 void AgArray<T>::copy(T *s, unsigned n) {
95 T *data = kernel().data;
96 assert(n <= kernel().length);
97 while (n--) {
98 data[n] = s[n];
99 }
100 }
101
102 template <class T>
103 int AgArray<T>::operator < (const AgArray<T> &t) const {
104 //LOGSECTION("AgArray<T>::operator <");
105 int n = min(size(), t.size());
106 int i = 0;
107 while (i < n) {
108 if ((*this)[i] < t[i]) {
109 return 1;
110 }
111 if (t[i] < (*this)[i]) {
112 return 0;
113 }
114 i++;
115 }
116 return size() < t.size();
117 }
118
119
120 #endif /* AGARRAY_IMP_H */