Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / nsgmls / Vector.C
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.C /main/3 1996/08/17 08:15:28 mgreess $ */
24 // Copyright (c) 1994, 1996 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef Vector_DEF_INCLUDED
28 #define Vector_DEF_INCLUDED 1
29
30 #include <stddef.h>
31 #include <string.h>
32
33 #ifdef SP_QUAL_TEMPLATE_DTOR_BROKEN
34 #define DTOR(T) ~T
35 #else
36 #define DTOR(T) T::~T
37 #endif
38
39 #ifdef SP_NAMESPACE
40 namespace SP_NAMESPACE {
41 #endif
42
43 template<class T>
44 Vector<T>::~Vector()
45 {
46   if (ptr_) {
47     erase(ptr_, ptr_ + size_);
48     ::operator delete((void *)ptr_);
49   }
50 }
51
52 template<class T>
53 Vector<T>::Vector(const Vector<T> &v)
54 : ptr_(0), size_(0), alloc_(0)
55 {
56   insert(ptr_ + size_, v.ptr_, v.ptr_ + v.size_);
57 }
58
59 template<class T>
60 Vector<T>::Vector(size_t n, const T &t)
61 : ptr_(0), size_(0), alloc_(0)
62 {
63   insert(ptr_ + size_, n, t);
64 }
65
66 template<class T>
67 Vector<T> &Vector<T>::operator=(const Vector<T> &v)
68 {
69   if (&v != this) {
70     size_t n = v.size_;
71     if (n > size_) {
72       n = size_;
73       insert(ptr_ + size_, v.ptr_ + size_, v.ptr_ + v.size_);
74     }
75     else if (n < size_)
76       erase(ptr_ + n, ptr_ + size_);
77     while (n-- > 0)
78       ptr_[n] = v.ptr_[n];
79   }
80   return *this;
81 }
82
83 template<class T>
84 void Vector<T>::assign(size_t n, const T &t)
85 {
86   size_t sz = n;
87   if (n > size_) {
88     sz = size_;
89     insert(ptr_ + size_, n - size_, t);
90   }
91   else if (n < size_)
92     erase(ptr_ + n, ptr_ + size_);
93   while (sz-- > 0)
94     ptr_[sz] = t;
95 }
96
97 template<class T>
98 void Vector<T>::insert(const T *p, size_t n, const T &t)
99 {
100   size_t i = p - ptr_;
101   reserve(size_ + n);
102   if (i != size_)
103     memmove(ptr_ + i + n, ptr_ + i, (size_ - i)*sizeof(T));
104   size_ += n;
105   for (T *pp = ptr_ + i; n-- > 0; pp++)
106     (void)new (pp) T(t);
107 }
108
109 template<class T>
110 void Vector<T>::insert(const T *p, const T *q1, const T *q2)
111 {
112   size_t i = p - ptr_;
113   size_t n = q2 - q1;
114   reserve(size_ + n);
115   if (i != size_)
116     memmove(ptr_ + i + n, ptr_ + i, (size_ - i)*sizeof(T));
117   size_ += n;
118   for (T *pp = ptr_ + i; q1 != q2; q1++, pp++)
119     (void)new (pp) T(*q1);
120 }
121
122 template<class T>
123 void Vector<T>::swap(Vector<T> &v)
124 {
125   {
126     T *tem = ptr_;
127     ptr_ = v.ptr_;
128     v.ptr_ = tem;
129   }
130   {
131     size_t tem = size_;
132     size_ = v.size_;
133     v.size_ = tem;
134   }
135   {
136     size_t tem = alloc_;
137     alloc_ = v.alloc_;
138     v.alloc_ = tem;
139   }
140 }
141
142 template<class T>
143 void Vector<T>::append(size_t n)
144 {
145   reserve(size_ + n);
146   while (n-- > 0)
147     (void)new (ptr_ + size_++) T;
148 }
149
150 template<class T>
151 T *Vector<T>::erase(const T *p1, const T *p2)
152 {
153 #if !defined(SP_TEMPLATE_DESTRUCTOR_COMPILER_BUG)
154   for (const T *p = p1; p != p2; p++)
155     p->~T();
156 #endif
157   if (p2 != ptr_ + size_)
158     memmove((T *)p1, p2, ((const T *)(ptr_ + size_) - p2)*sizeof(T));
159   size_ -= p2 - p1;
160   return (T *)p1;
161 }
162
163 template<class T>
164 void Vector<T>::reserve1(size_t size)
165 {
166   alloc_ *= 2;
167   if (size > alloc_)
168     alloc_ += size;
169   void *p = ::operator new(alloc_*sizeof(T));
170   if (ptr_) {
171     memcpy(p, ptr_, size_*sizeof(T));
172     ::operator delete((void *)ptr_);
173   }
174   ptr_ = (T *)p;
175 }
176
177 #ifdef SP_NAMESPACE
178 }
179 #endif
180
181 #endif /* not Vector_DEF_INCLUDED */