File: vector

package info (click to toggle)
html2text 1.3.2a-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 736 kB
  • ctags: 1,694
  • sloc: cpp: 9,311; yacc: 828; sh: 209; makefile: 144
file content (295 lines) | stat: -rw-r--r-- 9,818 bytes parent folder | download | duplicates (8)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295

/* ------------------------------------------------------------------------- */

/*
 * Copyright (c) 1999
 *      GMRS Software GmbH, Innsbrucker Ring 159, 81669 Munich, Germany.
 *      http://www.gmrs.de
 *      All rights reserved.
 *      Author: Arno Unkrig (arno.unkrig@gmrs.de)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by GMRS Software GmbH.
 * 4. The name of GMRS Software GmbH may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY GMRS SOFTWARE GMBH ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GMRS SOFTWARE GMBH BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

/* ------------------------------------------------------------------------- */

#ifndef __vector_INCLUDED__ /* { */
#define __vector_INCLUDED__

/* ------------------------------------------------------------------------- */

#ident "$Id: vector,v 1.2 1999/11/09 19:55:18 arno Exp $"

#include <new>
#include "../vector_base.h"

/* ------------------------------------------------------------------------- */

template <class value_type>
class vector : public vector_base {

  // Public types

public:
//typedef                  value_type;
//typedef                  allocator_type;
  typedef value_type       &reference;
  typedef const value_type &const_reference;
  typedef value_type       *iterator;
  typedef const value_type *const_iterator;
//typedef                  size_type;     // Inherited from "vector_base"
//typedef                  difference_type;
//typedef                  reverse_iterator;
//typedef                  const_reverse_iterator;

private:
  typedef void (*value_printer)(ostream &, const value_type &);

  // Construct/Copy/Destroy

public:
  explicit vector() : vector_base() {}
  explicit vector(size_type n) :
    vector_base(sizeof(value_type), n, construct_value2) {}
  vector(size_type n, const value_type &value) :
    vector_base(sizeof(value_type), n, construct_value2, (void *) &value) {}
  vector(const vector<value_type> &x) :
    vector_base(
      sizeof(value_type),
      construct_value2,
      (char *) x.begin(),
      (char *) x.end()
    ) {}
  vector(const_iterator from, const_iterator to) :
    vector_base(
      sizeof(value_type),
      construct_value2,
      (char *) from,
      (char *) to
    ) {}
  ~vector() { vector_base::clear(sizeof(value_type), destroy_value2); }
  const vector<value_type> &operator=(const vector<value_type> &x) {
    assign(x.begin(), x.end());
    return *this;
  }
  void assign(const_iterator from, const_iterator to) {
    vector_base::assign(sizeof(value_type), (char *) from, (char *) to);
  }
  void assign(size_type n) {
    vector_base::assign(sizeof(value_type), n);
  }
  void assign(size_type n, const value_type &x) {
    vector_base::assign(sizeof(value_type), n, (void *) &x);
  }
//allocator_type get_allocator() const;

  iterator begin() { return (iterator) p; }
  const_iterator begin() const { return (const_iterator) p; }
  iterator end() { return (iterator) p + size1; }
  const_iterator end() const { return (const_iterator) p + size1; }
//reverse_iterator rbegin();
//const_reverse_iterator rbegin() const;
//reverse_iterator rend();
//const_reverse_iterator rend() const;

  // Capacity

//size_type size() const;        // Inherited from "vector_base".
//size_type max_size() const;    // Inherited from "vector_base".
  void resize(size_type n) { vector_base::resize(sizeof(value_type), n); }
  void resize(size_type n, const value_type &v) {
    vector_base::resize(sizeof(value_type), n, (void *) &v);
  }
//size_type capacity() const;    // Inherited from "vector_base".
//bool empty() const;            // Inherited from "vector_base".
  void reserve(size_type n) { vector_base::reserve(sizeof(value_type), n); }

  // Element access

  reference operator[](size_type i) { return ((iterator) p)[i]; }
  const_reference operator[](size_type i) const {
    return ((const_iterator) p)[i];
  }
  reference at(size_type i) { return ((iterator) p)[i]; }
  const_reference at(size_type i) const { return ((const_iterator) p)[i]; }
  reference front() { return *(iterator) p; }
  const_reference front() const { return *(const_iterator) p; }
  reference back() { return *((iterator) p + size1 - 1); }
  const_reference back() const { return *((const_iterator) p + size1 - 1); }

  // Modifiers

  void push_back(const value_type &v) {
    vector_base::push_back(sizeof(value_type), (void *) &v);
  }
  void pop_back() { vector_base::pop_back(sizeof(value_type)); }
  iterator insert(iterator pos) {
    return (iterator) vector_base::insert(sizeof(value_type), (char *) pos);
  }
  iterator insert(iterator pos, const value_type &v) {
    return (iterator) vector_base::insert(
      sizeof(value_type),
      (char *) pos,
      (void *) &v
    );
  }
  void insert(iterator pos, size_type n, const value_type &v) {
    vector_base::insert(sizeof(value_type), (char *) pos, n, (void *) &v);
  }
  void insert(iterator pos, const_iterator from, const_iterator to) {
    vector_base::insert(
      sizeof(value_type),
      (char *) pos,
      (char *) from, (char *) to
    );
  }
  iterator erase(iterator pos) {
    return (iterator) vector_base::erase(sizeof(value_type), (char *) pos);
  }
  iterator erase(iterator from, iterator to) {
    return (iterator) vector_base::erase(
      sizeof(value_type),
      (char *) from,
      (char *) to
    );
  }
  void swap(vector<value_type> &x) { vector_base::swap(x); }
  void clear() { vector_base::clear(sizeof(value_type)); }

private:

  /*
   * Implement "vector_base"'s pure virtual methods.
   */
  /*virtual*/ void construct_value(void *that) { new(that) value_type; }
  /*virtual*/ void construct_value(void *that, void *v) {
    new(that) value_type(*(const value_type *) v);
  }
  typedef value_type xxxx;
  /*virtual*/ void destruct_value(void *that) {
    ((value_type *) that)->~xxxx();
  }
  /*virtual*/ void assign_value(void *to) {
    *(value_type *) to = value_type();
  }
  /*virtual*/ void assign_value(void *to, void *from) {
    *(value_type *) to = *(value_type *) from;
  }
  /*virtual*/ void print_value(ostream &os, void *v, void *closure) const {
    (*(value_printer *) closure)(os, *(const value_type *) v);
  }

  /*
   * Define some static helpers that are needed during construction and
   * destruction.
   */
  static void construct_value2(void *that) { new(that) value_type; }
  static void construct_value2(void *that, void *v) {
    new(that) value_type(*(const value_type *) v);
  }
  static void destroy_value2(void *that) {
    ((value_type *) that)->~xxxx();
  }

  friend bool operator==(
    const vector<value_type> &x,
    const vector<value_type> &y
  );
  friend bool operator<(
    const vector<value_type> &x,
    const vector<value_type> &y
  );

  void print(ostream &os, value_printer vp) const {
    vector_base::print(sizeof(value_type), os, (void *) &vp);
  }
  friend ostream &operator<<(ostream &, const vector<value_type> &);
};

/* ------------------------------------------------------------------------- */

template<class value_type>
inline bool
vector__value_equals(const value_type *x, const value_type *y)
{
  return *x == *y;
}

template<class value_type>
inline bool
operator==(const vector<value_type> &x, const vector<value_type> &y) {
  if (x.size() != x.size()) return false;
  return x.equals(sizeof(value_type), y, (
    (bool (*)(void *, void *))
    (bool (*)(const value_type *, const value_type *))
    vector__value_equals
  ));
}

/* ------------------------------------------------------------------------- */

template<class value_type>
inline bool
vector__value_less_than(const value_type *x, const value_type *y)
{
  return *x < *y;
}

template<class value_type>
inline bool
operator<(const vector<value_type> &x, const vector<value_type> &y) {
  return x.less_than(sizeof(value_type), y, (
    (bool (*)(void *, void *))
    (bool (*)(const value_type *, const value_type *))
    vector__value_less_than
  ));
}

/* ------------------------------------------------------------------------- */

template<class value_type>
inline void
vector__print_value(ostream &os, const value_type &value)
{
  os << value;
}

template<class value_type>
inline ostream &
operator<<(ostream &os, const vector<value_type> &x)
{
  x.print(os, vector__print_value);
  return os;
}

/* ------------------------------------------------------------------------- */

#endif /* } */

/* ------------------------------------------------------------------------- */