Mercurial > ~dholland > hg > ag > index.cgi
comparison anagram/support/agarray.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_H | |
12 #define AGARRAY_H | |
13 | |
14 #include <stdlib.h> | |
15 #include "port.h" | |
16 | |
17 #include "agcontainer.h" | |
18 #include "agref.h" | |
19 #include "agstring.h" | |
20 | |
21 | |
22 template <class T> | |
23 class AgArray | |
24 : public AgIndexedContainer<T> | |
25 , public AgReferenceBase | |
26 { | |
27 public: | |
28 class Kernel : public AgReferenceCount { | |
29 public: | |
30 unsigned length; | |
31 T *data; | |
32 Kernel(); | |
33 Kernel(unsigned n); | |
34 inline void *operator new (size_t n) { return malloc(n); } | |
35 inline void operator delete (void *p) { free(p); } | |
36 | |
37 T &operator [] (const unsigned x); | |
38 const T &operator [] (const unsigned x) const; | |
39 ~Kernel(); | |
40 }; | |
41 Kernel &kernel() const; | |
42 void copy(T *s, unsigned n); | |
43 | |
44 public: | |
45 AgString asString(); | |
46 | |
47 AgArray(); | |
48 inline AgArray(unsigned n) : AgReferenceBase(n ? new Kernel(n) : 0) {} | |
49 AgArray(const AgIndexedContainer<T> &); | |
50 | |
51 void reset() { discardData(); } | |
52 | |
53 inline T &operator [] (const unsigned x) { | |
54 return kernel()[x]; | |
55 } | |
56 | |
57 inline const T &operator [] (const unsigned x) const { | |
58 return kernel()[x]; | |
59 } | |
60 | |
61 inline AgArray<T> &operator = (const AgArray<T> &t) { | |
62 doAssignmentLocks(t); | |
63 return *this; | |
64 } | |
65 | |
66 int operator < (const AgArray<T> &t) const; | |
67 | |
68 inline T *pointer() const { return object!=0? kernel().data : 0; } | |
69 | |
70 inline unsigned size() const { return object!=0? kernel().length : 0; } | |
71 | |
72 inline AgArray(T *s, unsigned n) | |
73 : AgReferenceBase(n ? new Kernel(n) : 0) | |
74 { | |
75 copy(s, n); | |
76 } | |
77 | |
78 inline AgArray(T *s, unsigned k, unsigned n) | |
79 : AgReferenceBase(n ? new Kernel(n) : 0) | |
80 { | |
81 copy(s, k); | |
82 } | |
83 }; | |
84 | |
85 template <class T> | |
86 AgArray<T> makeArray(const T *tp, int n) { | |
87 AgArray<T> result(n); | |
88 while (n--) { | |
89 result[n] = tp[n]; | |
90 } | |
91 return result; | |
92 } | |
93 | |
94 | |
95 #endif /* AGARRAY_H */ |