Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / nsgmls / NCVector.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: NCVector.h /main/1 1996/07/29 16:58:13 cde-hp $ */
24 // Copyright (c) 1994, 1996 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef NCVector_INCLUDED
28 #define NCVector_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 NCVector {
43 public:
44   typedef size_t size_type;
45   typedef T *iterator;
46   typedef const T *const_iterator;
47   NCVector() : ptr_(0), size_(0), alloc_(0) { }
48   NCVector(size_t n) : ptr_(0), size_(0), alloc_(0) { append(n); }
49   ~NCVector();
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   void swap(NCVector<T> &);
57   void clear() { erase(ptr_, ptr_ + size_); }
58   size_t size() const { return size_; }
59   T &operator[](size_t i) { return ptr_[i]; }
60   const T &operator[](size_t i) const { return ptr_[i]; }
61   iterator begin() { return ptr_; }
62   const_iterator begin() const { return ptr_; }
63   T &back() { return ptr_[size_ - 1]; }
64   const T &back() const { return ptr_[size_ - 1]; }
65   void reserve(size_t n) {  if (n > alloc_) reserve1(n); }
66   iterator erase(const_iterator, const_iterator);
67 private:
68   void append(size_t);
69   void reserve1(size_t);
70   
71   size_t size_;
72   T *ptr_;
73   size_t alloc_;                // allocated size
74 };
75
76 #ifdef SP_NAMESPACE
77 }
78 #endif
79
80 #endif /* not NCVector_INCLUDED */
81
82 #ifdef SP_DEFINE_TEMPLATES
83 #include "NCVector.C"
84 #endif