comparison oldclasslib/include/array.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
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 * AnaGram, a System for Syntax Directed Programming
3 *
4 * Array Storage Class Definition
5 *
6 * Please note that the entire class is defined in this module. There
7 * is no supplementary code module.
8 *
9 * Copyright 1993 Parsifal Software. All Rights Reserved.
10 *
11 * This software is provided 'as-is', without any express or implied
12 * warranty. In no event will the authors be held liable for any damages
13 * arising from the use of this software.
14 *
15 * Permission is granted to anyone to use this software for any purpose,
16 * including commercial applications, and to alter it and redistribute it
17 * freely, subject to the following restrictions:
18 *
19 * 1. The origin of this software must not be misrepresented; you must not
20 * claim that you wrote the original software. If you use this software
21 * in a product, an acknowledgment in the product documentation would be
22 * appreciated but is not required.
23 * 2. Altered source versions must be plainly marked as such, and must not be
24 * misrepresented as being the original software.
25 * 3. This notice may not be removed or altered from any source distribution.
26 */
27
28 #ifndef ARRAY_H
29 #define ARRAY_H
30
31 #include <assert.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 // some forward decls required by current C++
36 template <class T> class array;
37 template <class T> unsigned size(const array <T> &c);
38
39 template <class T>
40 class array {
41 private:
42 T *base; // pointer to storage
43 unsigned length; // size of array
44 int *use_count; // reference counter
45
46 public:
47
48 // Constructors
49
50 array (const unsigned n) {
51 base = new T[length = n]; // allocate storage
52 memset(base,0,n*sizeof(T)); // clear to zero
53 use_count = new int;
54 *use_count = 1; // init use count
55 }
56 array (const T *src, const unsigned n) {
57 base = new T[length = n]; // allocate storage
58 memmove(base,src, n*sizeof(T)); // copy initial data
59 use_count = new int;
60 *use_count = 1; // init use count
61 }
62 array(const array<T> &a) {
63 *this = a;
64 (*use_count)++; // increment use count
65 }
66 array<T> &operator =(const array<T> &a) {
67 if (--(*use_count) == 0) {
68 delete [] base;
69 delete use_count;
70 }
71 *this = a;
72 (*use_count)++; // increment use count
73 return *this;
74 }
75
76 // Destructor
77 ~array() {
78 if (--(*use_count) == 0) {
79 delete [] base;
80 delete use_count;
81 }
82 }
83
84 // Access to data
85
86 operator T* () const { // Return pointer to data
87 return base;
88 }
89
90 T &operator [](unsigned n) const { // Direct access to data
91 assert(n < length);
92 return base[n];
93 }
94
95 T &operator [](int n) const { // Direct access to data
96 assert(n >= 0 && (unsigned) n < length);
97 return base[n];
98 }
99
100
101 // Size of array
102
103 #ifdef __IBMCPP__
104 friend unsigned size(const array<T> &c);
105 #else
106 friend unsigned size<>(const array<T> &c);
107 #endif
108
109 };
110
111 template <class T>
112 inline unsigned size(const array <T> &c) {
113 return c.length;
114 }
115
116 #endif