The ChainCon Software (Release 0.03)
extarray.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// An extendible array.
6 ///
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // Copyright (C) 2009-2016 by Pawel Pilarczyk.
10 //
11 // This file is part of my research software package. This is free software:
12 // you can redistribute it and/or modify it under the terms of the GNU
13 // General Public License as published by the Free Software Foundation,
14 // either version 3 of the License, or (at your option) any later version.
15 //
16 // This software is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this software; see the file "license.txt". If not,
23 // please, see <http://www.gnu.org/licenses/>.
24 
25 // Started on March 24, 2009. Last revision: June 15, 2012.
26 
27 
28 #ifndef _CHAINCON_EXTARRAY_H_
29 #define _CHAINCON_EXTARRAY_H_
30 
31 
32 // include selected header files from the CHomP library
33 #include "chomp/system/config.h"
34 
35 
36 // --------------------------------------------------
37 // -------------- an extendible array ---------------
38 // --------------------------------------------------
39 
40 /// A simple extendible array that extends automatically when needed.
41 template <class ElemT>
42 class extarray
43 {
44 public:
45  /// Default constructor.
46  extarray ();
47 
48  /// Destructor.
49  ~extarray ();
50 
51  /// Copy constructor.
52  extarray (const extarray<ElemT> &other);
53 
54  /// Assignment operator.
56 
57  /// Accesses a given element, and extends the array if necessary.
58  ElemT &operator [] (int_t n);
59 
60  /// Accesses a given element without modifying the array.
61  const ElemT &operator [] (int_t n) const;
62 
63  /// Swaps contents with another extensible array.
64  void swap (extarray<ElemT> &other);
65 
66 private:
67  /// The length of the allocated array.
68  int_t len;
69 
70  /// The array of elements.
71  ElemT *arr;
72 
73 }; /* class extarray */
74 
75 // --------------------------------------------------
76 
77 template <class ElemT>
79  len (0), arr (0)
80 {
81  return;
82 } /* extarray::extarray */
83 
84 template <class ElemT>
86 {
87  if (arr)
88  delete [] arr;
89  return;
90 } /* extarray::~extarray */
91 
92 template <class ElemT>
94  len (other. len), arr (other. len ? new ElemT [other. len] : 0)
95 {
96  for (int_t i = 0; i < len; ++ i)
97  arr [i] = other. arr [i];
98  return;
99 } /* extarray::extarray */
100 
101 template <class ElemT>
103  (const extarray<ElemT> &other)
104 {
105  if (arr)
106  delete [] arr;
107  len = other. len;
108  if (len)
109  {
110  arr = new ElemT [len];
111  for (int_t i = 0; i < len; ++ i)
112  arr [i] = other. arr [i];
113  }
114  else
115  {
116  arr = 0;
117  }
118  return *this;
119 } /* extarray::operator = */
120 
121 template <class ElemT>
122 inline ElemT &extarray<ElemT>::operator [] (int_t n)
123 {
124  if (n < 0)
125  throw "Negative offset requested for an extensible array.";
126  if (!len)
127  {
128  len = n + 1;
129  arr = new ElemT [len];
130  }
131  else if (len <= n)
132  {
133  int_t lenNew = len << 1;
134  if (lenNew <= n)
135  lenNew = n + 1;
136  ElemT *arrNew = new ElemT [lenNew];
137  for (int_t i = 0; i < len; ++ i)
138  arrNew [i] = arr [i];
139  delete [] arr;
140  arr = arrNew;
141  len = lenNew;
142  }
143  return arr [n];
144 } /* extarray::operator [] */
145 
146 template <class ElemT>
147 inline const ElemT &extarray<ElemT>::operator [] (int_t n) const
148 {
149  if (n < 0)
150  throw "Negative offset while reading an extensible array.";
151  if (n >= len)
152  throw "Offset too large while reading an extensible array.";
153  return arr [n];
154 } /* extarray::operator [] */
155 
156 template <class ElemT>
158 {
159  int_t lenTemp = len;
160  len = other. len;
161  other. len = lenTemp;
162  ElemT *arrTemp = arr;
163  arr = other. arr;
164  other. arr = arrTemp;
165  return;
166 } /* extarray::swap */
167 
168 
169 #endif // _CHAINCON_EXTARRAY_H_
170 
A simple extendible array that extends automatically when needed.
Definition: extarray.h:42
int_t len
The length of the allocated array.
Definition: extarray.h:68
ElemT & operator[](int_t n)
Accesses a given element, and extends the array if necessary.
Definition: extarray.h:122
void swap(extarray< ElemT > &other)
Swaps contents with another extensible array.
Definition: extarray.h:157
ElemT * arr
The array of elements.
Definition: extarray.h:71
~extarray()
Destructor.
Definition: extarray.h:85
extarray()
Default constructor.
Definition: extarray.h:78
extarray< ElemT > & operator=(const extarray< ElemT > &other)
Assignment operator.
Definition: extarray.h:103