Link with C++ linker
[oweals/cde.git] / cde / programs / nsgmls / Vector.h
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: Vector.h /main/1 1996/07/29 17:08:00 cde-hp $ */
24 // Copyright (c) 1994, 1996 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef Vector_INCLUDED
28 #define Vector_INCLUDED 1
29
30 #include <stddef.h>
31 #include "xnew.h"
32
33 // This offers a subset of the interface offered by the standard C++
34 // vector class as defined in the Jan 96 WP.
35 // Code in SP currently assumes that size_type is size_t.
36
37 #ifdef SP_NAMESPACE
38 namespace SP_NAMESPACE {
39 #endif
40
41 template<class T>
42 class Vector {
43 public:
44   typedef size_t size_type;
45   typedef T *iterator;
46   typedef const T *const_iterator;
47   Vector() : ptr_(0), size_(0), alloc_(0) { }
48   Vector(size_t n) : ptr_(0), size_(0), alloc_(0) { append(n); }
49   ~Vector();
50   void resize(size_t n) {
51     if (n < size_)
52       erase(ptr_ + n, ptr_ + size_);
53     else if (n > size_)
54       append(n - size_);
55   }
56   Vector(size_t, const T &);
57   Vector(const Vector<T> &);
58   Vector<T> &operator=(const Vector<T> &);
59   void assign(size_t, const T &);
60   void push_back(const T &t) {
61     reserve(size_ + 1);
62     (void)new (ptr_ + size_++) T(t);
63   }
64   void insert(const_iterator p, size_t n, const T &t);
65   void insert(const_iterator p, const_iterator q1, const_iterator q2);
66   void swap(Vector<T> &);
67   void clear() { erase(ptr_, ptr_ + size_); }
68   size_t size() const { return size_; }
69   T &operator[](size_t i) { return ptr_[i]; }
70   const T &operator[](size_t i) const { return ptr_[i]; }
71   iterator begin() { return ptr_; }
72   const_iterator begin() const { return ptr_; }
73   T &back() { return ptr_[size_ - 1]; }
74   const T &back() const { return ptr_[size_ - 1]; }
75   void reserve(size_t n) {  if (n > alloc_) reserve1(n); }
76   iterator erase(const_iterator, const_iterator);
77 private:
78   void append(size_t);
79   void reserve1(size_t);
80   
81   size_t size_;
82   T *ptr_;
83   size_t alloc_;                // allocated size
84 };
85
86 #ifdef SP_NAMESPACE
87 }
88 #endif
89
90 #endif /* not Vector_INCLUDED */
91
92 #ifdef SP_DEFINE_TEMPLATES
93 #include "Vector.C"
94 #endif