svector.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /*
  2. * Copyright 2002, 2006 Adrian Thurston <thurston@cs.queensu.ca>
  3. */
  4. /* This file is part of Aapl.
  5. *
  6. * Aapl is free software; you can redistribute it and/or modify it under the
  7. * terms of the GNU Lesser General Public License as published by the Free
  8. * Software Foundation; either version 2.1 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * Aapl is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Aapl; if not, write to the Free Software Foundation, Inc., 59
  18. * Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef _AAPL_SVECTOR_H
  21. #define _AAPL_SVECTOR_H
  22. #include <new>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <assert.h>
  26. #include "table.h"
  27. #ifdef AAPL_NAMESPACE
  28. namespace Aapl {
  29. #endif
  30. /**
  31. * \addtogroup vector
  32. * @{
  33. */
  34. /** \class SVector
  35. * \brief Copy-on-write dynamic array.
  36. *
  37. * SVector is a variant of Vector that employs copy-on-write behaviour. The
  38. * SVector copy constructor and = operator make shallow copies. If a vector
  39. * that references shared data is modified with insert, replace, append,
  40. * prepend, setAs or remove, a new copy is made so as not to interfere with
  41. * the shared data. However, shared individual elements may be modified by
  42. * bypassing the SVector interface.
  43. *
  44. * SVector is a dynamic array that can be used to contain complex data
  45. * structures that have constructors and destructors as well as simple types
  46. * such as integers and pointers.
  47. *
  48. * SVector supports inserting, overwriting, and removing single or multiple
  49. * elements at once. Constructors and destructors are called wherever
  50. * appropriate. For example, before an element is overwritten, it's
  51. * destructor is called.
  52. *
  53. * SVector provides automatic resizing of allocated memory as needed and
  54. * offers different allocation schemes for controlling how the automatic
  55. * allocation is done. Two senses of the the length of the data is
  56. * maintained: the amount of raw memory allocated to the vector and the number
  57. * of actual elements in the vector. The various allocation schemes control
  58. * how the allocated space is changed in relation to the number of elements in
  59. * the vector.
  60. */
  61. /*@}*/
  62. /* SVector */
  63. template < class T, class Resize = ResizeExpn > class SVector :
  64. public STable<T>, public Resize
  65. {
  66. private:
  67. typedef STable<T> BaseTable;
  68. public:
  69. /**
  70. * \brief Initialize an empty vector with no space allocated.
  71. *
  72. * If a linear resizer is used, the step defaults to 256 units of T. For a
  73. * runtime vector both up and down allocation schemes default to
  74. * Exponential.
  75. */
  76. SVector() { }
  77. /**
  78. * \brief Create a vector that contains an initial element.
  79. *
  80. * The vector becomes one element in length. The element's copy
  81. * constructor is used to place the value in the vector.
  82. */
  83. SVector(const T &val) { setAs(&val, 1); }
  84. /**
  85. * \brief Create a vector that contains an array of elements.
  86. *
  87. * The vector becomes len elements in length. Copy constructors are used
  88. * to place the new elements in the vector.
  89. */
  90. SVector(const T *val, long len) { setAs(val, len); }
  91. /* Shallow copy. */
  92. SVector( const SVector &v );
  93. /**
  94. * \brief Free all memory used by the vector.
  95. *
  96. * The vector is reset to zero elements. Destructors are called on all
  97. * elements in the vector. The space allocated for the vector is freed.
  98. */
  99. ~SVector() { empty(); }
  100. /* Delete all items. */
  101. void empty();
  102. /**
  103. * \brief Deep copy another vector into this vector.
  104. *
  105. * Copies the entire contents of the other vector into this vector. Any
  106. * existing contents are first deleted. Equivalent to setAs.
  107. */
  108. void deepCopy( const SVector &v ) { setAs(v.data, v.length()); }
  109. /* Perform a shallow copy of another vector. */
  110. SVector &operator=( const SVector &v );
  111. /*@{*/
  112. /**
  113. * \brief Insert one element at position pos.
  114. *
  115. * Elements in the vector from pos onward are shifted one space to the
  116. * right. The copy constructor is used to place the element into this
  117. * vector. If pos is greater than the length of the vector then undefined
  118. * behaviour results. If pos is negative then it is treated as an offset
  119. * relative to the length of the vector.
  120. */
  121. void insert(long pos, const T &val) { insert(pos, &val, 1); }
  122. /* Insert an array of values. */
  123. void insert(long pos, const T *val, long len);
  124. /**
  125. * \brief Insert all the elements from another vector at position pos.
  126. *
  127. * Elements in this vector from pos onward are shifted v.length() spaces
  128. * to the right. The element's copy constructor is used to copy the items
  129. * into this vector. The other vector is left unchanged. If pos is off the
  130. * end of the vector, then undefined behaviour results. If pos is negative
  131. * then it is treated as an offset relative to the length of the vector.
  132. * Equivalent to vector.insert(pos, other.data, other.length()).
  133. */
  134. void insert(long pos, const SVector &v) { insert(pos, v.data, v.length()); }
  135. /* Insert len copies of val into the vector. */
  136. void insertDup(long pos, const T &val, long len);
  137. /**
  138. * \brief Insert one new element using the default constrcutor.
  139. *
  140. * Elements in the vector from pos onward are shifted one space to the right.
  141. * The default constructor is used to init the new element. If pos is greater
  142. * than the length of the vector then undefined behaviour results. If pos is
  143. * negative then it is treated as an offset relative to the length of the
  144. * vector.
  145. */
  146. void insertNew(long pos) { insertNew(pos, 1); }
  147. /* Insert len new items using default constructor. */
  148. void insertNew(long pos, long len);
  149. /*@}*/
  150. /*@{*/
  151. /**
  152. * \brief Remove one element at position pos.
  153. *
  154. * The element's destructor is called. Elements to the right of pos are
  155. * shifted one space to the left to take up the free space. If pos is greater
  156. * than or equal to the length of the vector then undefined behavior results.
  157. * If pos is negative then it is treated as an offset relative to the length
  158. * of the vector.
  159. */
  160. void remove(long pos) { remove(pos, 1); }
  161. /* Delete a number of elements. */
  162. void remove(long pos, long len);
  163. /*@}*/
  164. /*@{*/
  165. /**
  166. * \brief Replace one element at position pos.
  167. *
  168. * If there is an existing element at position pos (if pos is less than the
  169. * length of the vector) then its destructor is called before the space is
  170. * used. The copy constructor is used to place the element into the vector.
  171. * If pos is greater than the length of the vector then undefined behaviour
  172. * results. If pos is negative then it is treated as an offset relative to
  173. * the length of the vector.
  174. */
  175. void replace(long pos, const T &val) { replace(pos, &val, 1); }
  176. /* Replace with an array of values. */
  177. void replace(long pos, const T *val, long len);
  178. /**
  179. * \brief Replace at position pos with all the elements of another vector.
  180. *
  181. * Replace at position pos with all the elements of another vector. The other
  182. * vector is left unchanged. If there are existing elements at the positions
  183. * to be replaced, then destructors are called before the space is used. Copy
  184. * constructors are used to place the elements into this vector. It is
  185. * allowable for the pos and length of the other vector to specify a
  186. * replacement that overwrites existing elements and creates new ones. If pos
  187. * is greater than the length of the vector then undefined behaviour results.
  188. * If pos is negative, then it is treated as an offset relative to the length
  189. * of the vector.
  190. */
  191. void replace(long pos, const SVector &v) { replace(pos, v.data, v.length()); }
  192. /* Replace len items with len copies of val. */
  193. void replaceDup(long pos, const T &val, long len);
  194. /**
  195. * \brief Replace at position pos with one new element.
  196. *
  197. * If there is an existing element at the position to be replaced (pos is
  198. * less than the length of the vector) then the element's destructor is
  199. * called before the space is used. The default constructor is used to
  200. * initialize the new element. If pos is greater than the length of the
  201. * vector then undefined behaviour results. If pos is negative, then it is
  202. * treated as an offset relative to the length of the vector.
  203. */
  204. void replaceNew(long pos) { replaceNew(pos, 1); }
  205. /* Replace len items at pos with newly constructed objects. */
  206. void replaceNew(long pos, long len);
  207. /*@}*/
  208. /*@{*/
  209. /**
  210. * \brief Set the contents of the vector to be val exactly.
  211. *
  212. * The vector becomes one element in length. Destructors are called on any
  213. * existing elements in the vector. The element's copy constructor is used to
  214. * place the val in the vector.
  215. */
  216. void setAs(const T &val) { setAs(&val, 1); }
  217. /* Set to the contents of an array. */
  218. void setAs(const T *val, long len);
  219. /**
  220. * \brief Set the vector to exactly the contents of another vector.
  221. *
  222. * The vector becomes v.length() elements in length. Destructors are called
  223. * on any existing elements. Copy constructors are used to place the new
  224. * elements in the vector.
  225. */
  226. void setAs(const SVector &v) { setAs(v.data, v.length()); }
  227. /* Set as len copies of item. */
  228. void setAsDup(const T &item, long len);
  229. /**
  230. * \brief Set the vector to exactly one new item.
  231. *
  232. * The vector becomes one element in length. Destructors are called on any
  233. * existing elements in the vector. The default constructor is used to
  234. * init the new item.
  235. */
  236. void setAsNew() { setAsNew(1); }
  237. /* Set as newly constructed objects using the default constructor. */
  238. void setAsNew(long len);
  239. /*@}*/
  240. /*@{*/
  241. /**
  242. * \brief Append one elment to the end of the vector.
  243. *
  244. * Copy constructor is used to place the element in the vector.
  245. */
  246. void append(const T &val) { replace(BaseTable::length(), &val, 1); }
  247. /**
  248. * \brief Append len elements to the end of the vector.
  249. *
  250. * Copy constructors are used to place the elements in the vector.
  251. */
  252. void append(const T *val, long len) { replace(BaseTable::length(), val, len); }
  253. /**
  254. * \brief Append the contents of another vector.
  255. *
  256. * The other vector is left unchanged. Copy constructors are used to place
  257. * the elements in the vector.
  258. */
  259. void append(const SVector &v)
  260. { replace(BaseTable::length(), v.data, v.length()); }
  261. /**
  262. * \brief Append len copies of item.
  263. *
  264. * The copy constructor is used to place the item in the vector.
  265. */
  266. void appendDup(const T &item, long len) { replaceDup(BaseTable::length(), item, len); }
  267. /**
  268. * \brief Append a single newly created item.
  269. *
  270. * The new element is initialized with the default constructor.
  271. */
  272. void appendNew() { replaceNew(BaseTable::length(), 1); }
  273. /**
  274. * \brief Append len newly created items.
  275. *
  276. * The new elements are initialized with the default constructor.
  277. */
  278. void appendNew(long len) { replaceNew(BaseTable::length(), len); }
  279. /*@}*/
  280. /*@{*/
  281. /**
  282. * \brief Prepend one elment to the front of the vector.
  283. *
  284. * Copy constructor is used to place the element in the vector.
  285. */
  286. void prepend(const T &val) { insert(0, &val, 1); }
  287. /**
  288. * \brief Prepend len elements to the front of the vector.
  289. *
  290. * Copy constructors are used to place the elements in the vector.
  291. */
  292. void prepend(const T *val, long len) { insert(0, val, len); }
  293. /**
  294. * \brief Prepend the contents of another vector.
  295. *
  296. * The other vector is left unchanged. Copy constructors are used to place
  297. * the elements in the vector.
  298. */
  299. void prepend(const SVector &v) { insert(0, v.data, v.length()); }
  300. /**
  301. * \brief Prepend len copies of item.
  302. *
  303. * The copy constructor is used to place the item in the vector.
  304. */
  305. void prependDup(const T &item, long len) { insertDup(0, item, len); }
  306. /**
  307. * \brief Prepend a single newly created item.
  308. *
  309. * The new element is initialized with the default constructor.
  310. */
  311. void prependNew() { insertNew(0, 1); }
  312. /**
  313. * \brief Prepend len newly created items.
  314. *
  315. * The new elements are initialized with the default constructor.
  316. */
  317. void prependNew(long len) { insertNew(0, len); }
  318. /*@}*/
  319. /* Convenience access. */
  320. T &operator[](int i) const { return BaseTable::data[i]; }
  321. long size() const { return BaseTable::length(); }
  322. /* Various classes for setting the iterator */
  323. struct Iter;
  324. struct IterFirst { IterFirst( const SVector &v ) : v(v) { } const SVector &v; };
  325. struct IterLast { IterLast( const SVector &v ) : v(v) { } const SVector &v; };
  326. struct IterNext { IterNext( const Iter &i ) : i(i) { } const Iter &i; };
  327. struct IterPrev { IterPrev( const Iter &i ) : i(i) { } const Iter &i; };
  328. /**
  329. * \brief Shared Vector Iterator.
  330. * \ingroup iterators
  331. */
  332. struct Iter
  333. {
  334. /* Construct, assign. */
  335. Iter() : ptr(0), ptrBeg(0), ptrEnd(0) { }
  336. /* Construct. */
  337. Iter( const SVector &v );
  338. Iter( const IterFirst &vf );
  339. Iter( const IterLast &vl );
  340. inline Iter( const IterNext &vn );
  341. inline Iter( const IterPrev &vp );
  342. /* Assign. */
  343. Iter &operator=( const SVector &v );
  344. Iter &operator=( const IterFirst &vf );
  345. Iter &operator=( const IterLast &vl );
  346. inline Iter &operator=( const IterNext &vf );
  347. inline Iter &operator=( const IterPrev &vl );
  348. /** \brief Less than end? */
  349. bool lte() const { return ptr != ptrEnd; }
  350. /** \brief At end? */
  351. bool end() const { return ptr == ptrEnd; }
  352. /** \brief Greater than beginning? */
  353. bool gtb() const { return ptr != ptrBeg; }
  354. /** \brief At beginning? */
  355. bool beg() const { return ptr == ptrBeg; }
  356. /** \brief At first element? */
  357. bool first() const { return ptr == ptrBeg+1; }
  358. /** \brief At last element? */
  359. bool last() const { return ptr == ptrEnd-1; }
  360. /* Return the position. */
  361. long pos() const { return ptr - ptrBeg - 1; }
  362. T &operator[](int i) const { return ptr[i]; }
  363. /** \brief Implicit cast to T*. */
  364. operator T*() const { return ptr; }
  365. /** \brief Dereference operator returns T&. */
  366. T &operator *() const { return *ptr; }
  367. /** \brief Arrow operator returns T*. */
  368. T *operator->() const { return ptr; }
  369. /** \brief Move to next item. */
  370. T *operator++() { return ++ptr; }
  371. /** \brief Move to next item. */
  372. T *operator++(int) { return ptr++; }
  373. /** \brief Move to next item. */
  374. T *increment() { return ++ptr; }
  375. /** \brief Move to previous item. */
  376. T *operator--() { return --ptr; }
  377. /** \brief Move to previous item. */
  378. T *operator--(int) { return ptr--; }
  379. /** \brief Move to previous item. */
  380. T *decrement() { return --ptr; }
  381. /** \brief Return the next item. Does not modify this. */
  382. inline IterNext next() const { return IterNext(*this); }
  383. /** \brief Return the previous item. Does not modify this. */
  384. inline IterPrev prev() const { return IterPrev(*this); }
  385. /** \brief The iterator is simply a pointer. */
  386. T *ptr;
  387. /* For testing endpoints. */
  388. T *ptrBeg, *ptrEnd;
  389. };
  390. /** \brief Return first element. */
  391. IterFirst first() { return IterFirst( *this ); }
  392. /** \brief Return last element. */
  393. IterLast last() { return IterLast( *this ); }
  394. protected:
  395. void makeRawSpaceFor(long pos, long len);
  396. void setAsCommon(long len);
  397. long replaceCommon(long pos, long len);
  398. long insertCommon(long pos, long len);
  399. void upResize(long len);
  400. void upResizeDup(long len);
  401. void upResizeFromEmpty(long len);
  402. void downResize(long len);
  403. void downResizeDup(long len);
  404. };
  405. /**
  406. * \brief Perform a shallow copy of the vector.
  407. *
  408. * Takes a reference to the contents of the other vector.
  409. */
  410. template <class T, class Resize> SVector<T, Resize>::
  411. SVector(const SVector<T, Resize> &v)
  412. {
  413. /* Take a reference to other, if any data is allocated. */
  414. if ( v.data == 0 )
  415. BaseTable::data = 0;
  416. else {
  417. /* Get the source header, up the refcount and ref it. */
  418. STabHead *srcHead = ((STabHead*) v.data) - 1;
  419. srcHead->refCount += 1;
  420. BaseTable::data = (T*) (srcHead + 1);
  421. }
  422. }
  423. /**
  424. * \brief Shallow copy another vector into this vector.
  425. *
  426. * Takes a reference to the other vector. The contents of this vector are
  427. * first emptied.
  428. *
  429. * \returns A reference to this.
  430. */
  431. template <class T, class Resize> SVector<T, Resize> &
  432. SVector<T, Resize>:: operator=( const SVector &v )
  433. {
  434. /* First clean out the current contents. */
  435. empty();
  436. /* Take a reference to other, if any data is allocated. */
  437. if ( v.data == 0 )
  438. BaseTable::data = 0;
  439. else {
  440. /* Get the source header, up the refcount and ref it. */
  441. STabHead *srcHead = ((STabHead*) v.data) - 1;
  442. srcHead->refCount += 1;
  443. BaseTable::data = (T*) (srcHead + 1);
  444. }
  445. return *this;
  446. }
  447. /* Init a vector iterator with just a vector. */
  448. template <class T, class Resize> SVector<T, Resize>::
  449. Iter::Iter( const SVector &v )
  450. {
  451. long length;
  452. if ( v.data == 0 || (length=(((STabHead*)v.data)-1)->tabLen) == 0 )
  453. ptr = ptrBeg = ptrEnd = 0;
  454. else {
  455. ptr = v.data;
  456. ptrBeg = v.data-1;
  457. ptrEnd = v.data+length;
  458. }
  459. }
  460. /* Init a vector iterator with the first of a vector. */
  461. template <class T, class Resize> SVector<T, Resize>::
  462. Iter::Iter( const IterFirst &vf )
  463. {
  464. long length;
  465. if ( vf.v.data == 0 || (length=(((STabHead*)vf.v.data)-1)->tabLen) == 0 )
  466. ptr = ptrBeg = ptrEnd = 0;
  467. else {
  468. ptr = vf.v.data;
  469. ptrBeg = vf.v.data-1;
  470. ptrEnd = vf.v.data+length;
  471. }
  472. }
  473. /* Init a vector iterator with the last of a vector. */
  474. template <class T, class Resize> SVector<T, Resize>::
  475. Iter::Iter( const IterLast &vl )
  476. {
  477. long length;
  478. if ( vl.v.data == 0 || (length=(((STabHead*)vl.v.data)-1)->tabLen) == 0 )
  479. ptr = ptrBeg = ptrEnd = 0;
  480. else {
  481. ptr = vl.v.data+length-1;
  482. ptrBeg = vl.v.data-1;
  483. ptrEnd = vl.v.data+length;
  484. }
  485. }
  486. /* Init a vector iterator with the next of some other iterator. */
  487. template <class T, class Resize> SVector<T, Resize>::
  488. Iter::Iter( const IterNext &vn )
  489. :
  490. ptr(vn.i.ptr+1),
  491. ptrBeg(vn.i.ptrBeg),
  492. ptrEnd(vn.i.ptrEnd)
  493. {
  494. }
  495. /* Init a vector iterator with the prev of some other iterator. */
  496. template <class T, class Resize> SVector<T, Resize>::
  497. Iter::Iter( const IterPrev &vp )
  498. :
  499. ptr(vp.i.ptr-1),
  500. ptrBeg(vp.i.ptrBeg),
  501. ptrEnd(vp.i.ptrEnd)
  502. {
  503. }
  504. /* Set a vector iterator with some vector. */
  505. template <class T, class Resize> typename SVector<T, Resize>::Iter &
  506. SVector<T, Resize>::Iter::operator=( const SVector &v )
  507. {
  508. long length;
  509. if ( v.data == 0 || (length=(((STabHead*)v.data)-1)->tabLen) == 0 )
  510. ptr = ptrBeg = ptrEnd = 0;
  511. else {
  512. ptr = v.data;
  513. ptrBeg = v.data-1;
  514. ptrEnd = v.data+length;
  515. }
  516. return *this;
  517. }
  518. /* Set a vector iterator with the first element in a vector. */
  519. template <class T, class Resize> typename SVector<T, Resize>::Iter &
  520. SVector<T, Resize>::Iter::operator=( const IterFirst &vf )
  521. {
  522. long length;
  523. if ( vf.v.data == 0 || (length=(((STabHead*)vf.v.data)-1)->tabLen) == 0 )
  524. ptr = ptrBeg = ptrEnd = 0;
  525. else {
  526. ptr = vf.v.data;
  527. ptrBeg = vf.v.data-1;
  528. ptrEnd = vf.v.data+length;
  529. }
  530. return *this;
  531. }
  532. /* Set a vector iterator with the last element in a vector. */
  533. template <class T, class Resize> typename SVector<T, Resize>::Iter &
  534. SVector<T, Resize>::Iter::operator=( const IterLast &vl )
  535. {
  536. long length;
  537. if ( vl.v.data == 0 || (length=(((STabHead*)vl.v.data)-1)->tabLen) == 0 )
  538. ptr = ptrBeg = ptrEnd = 0;
  539. else {
  540. ptr = vl.v.data+length-1;
  541. ptrBeg = vl.v.data-1;
  542. ptrEnd = vl.v.data+length;
  543. }
  544. return *this;
  545. }
  546. /* Set a vector iterator with the next of some other iterator. */
  547. template <class T, class Resize> typename SVector<T, Resize>::Iter &
  548. SVector<T, Resize>::Iter::operator=( const IterNext &vn )
  549. {
  550. ptr = vn.i.ptr+1;
  551. ptrBeg = vn.i.ptrBeg;
  552. ptrEnd = vn.i.ptrEnd;
  553. return *this;
  554. }
  555. /* Set a vector iterator with the prev of some other iterator. */
  556. template <class T, class Resize> typename SVector<T, Resize>::Iter &
  557. SVector<T, Resize>::Iter::operator=( const IterPrev &vp )
  558. {
  559. ptr = vp.i.ptr-1;
  560. ptrBeg = vp.i.ptrBeg;
  561. ptrEnd = vp.i.ptrEnd;
  562. return *this;
  563. }
  564. /* Up resize the data for len elements using Resize::upResize to tell us the
  565. * new length. Reads and writes allocLen. Does not read or write length.
  566. * Assumes that there is some data allocated already. */
  567. template <class T, class Resize> void SVector<T, Resize>::
  568. upResize(long len)
  569. {
  570. /* Get the current header. */
  571. STabHead *head = ((STabHead*)BaseTable::data) - 1;
  572. /* Ask the resizer what the new length will be. */
  573. long newLen = Resize::upResize(head->allocLen, len);
  574. /* Did the data grow? */
  575. if ( newLen > head->allocLen ) {
  576. head->allocLen = newLen;
  577. /* Table exists already, resize it up. */
  578. head = (STabHead*) realloc( head, sizeof(STabHead) +
  579. sizeof(T) * newLen );
  580. if ( head == 0 )
  581. throw std::bad_alloc();
  582. /* Save the data pointer. */
  583. BaseTable::data = (T*) (head + 1);
  584. }
  585. }
  586. /* Allocates a new buffer for an up resize that requires a duplication of the
  587. * data. Uses Resize::upResize to get the allocation length. Reads and writes
  588. * allocLen. This upResize does write the new length. Assumes that there is
  589. * some data allocated already. */
  590. template <class T, class Resize> void SVector<T, Resize>::
  591. upResizeDup(long len)
  592. {
  593. /* Get the current header. */
  594. STabHead *head = ((STabHead*)BaseTable::data) - 1;
  595. /* Ask the resizer what the new length will be. */
  596. long newLen = Resize::upResize(head->allocLen, len);
  597. /* Dereferencing the existing data, decrement the refcount. */
  598. head->refCount -= 1;
  599. /* Table exists already, resize it up. */
  600. head = (STabHead*) malloc( sizeof(STabHead) + sizeof(T) * newLen );
  601. if ( head == 0 )
  602. throw std::bad_alloc();
  603. head->refCount = 1;
  604. head->allocLen = newLen;
  605. head->tabLen = len;
  606. /* Save the data pointer. */
  607. BaseTable::data = (T*) (head + 1);
  608. }
  609. /* Up resize the data for len elements using Resize::upResize to tell us the
  610. * new length. Reads and writes allocLen. This upresize DOES write length.
  611. * Assumes that no data is allocated. */
  612. template <class T, class Resize> void SVector<T, Resize>::
  613. upResizeFromEmpty(long len)
  614. {
  615. /* There is no table yet. If the len is zero, then there is no need to
  616. * create a table. */
  617. if ( len > 0 ) {
  618. /* Ask the resizer what the new length will be. */
  619. long newLen = Resize::upResize(0, len);
  620. /* If len is greater than zero then we are always allocating the table. */
  621. STabHead *head = (STabHead*) malloc( sizeof(STabHead) +
  622. sizeof(T) * newLen );
  623. if ( head == 0 )
  624. throw std::bad_alloc();
  625. /* Set up the header and save the data pointer. Note that we set the
  626. * length here. This differs from the other upResizes. */
  627. head->refCount = 1;
  628. head->allocLen = newLen;
  629. head->tabLen = len;
  630. BaseTable::data = (T*) (head + 1);
  631. }
  632. }
  633. /* Down resize the data for len elements using Resize::downResize to determine
  634. * the new length. Reads and writes allocLen. Does not read or write length. */
  635. template <class T, class Resize> void SVector<T, Resize>::
  636. downResize(long len)
  637. {
  638. /* If there is already no length, then there is nothing we can do. */
  639. if ( BaseTable::data != 0 ) {
  640. /* Get the current header. */
  641. STabHead *head = ((STabHead*)BaseTable::data) - 1;
  642. /* Ask the resizer what the new length will be. */
  643. long newLen = Resize::downResize( head->allocLen, len );
  644. /* Did the data shrink? */
  645. if ( newLen < head->allocLen ) {
  646. if ( newLen == 0 ) {
  647. /* Simply free the data. */
  648. free( head );
  649. BaseTable::data = 0;
  650. }
  651. else {
  652. /* Save the new allocated length. */
  653. head->allocLen = newLen;
  654. /* Not shrinking to size zero, realloc it to the smaller size. */
  655. head = (STabHead*) realloc( head, sizeof(STabHead) +
  656. sizeof(T) * newLen );
  657. if ( head == 0 )
  658. throw std::bad_alloc();
  659. /* Save the new data ptr. */
  660. BaseTable::data = (T*) (head + 1);
  661. }
  662. }
  663. }
  664. }
  665. /* Allocate a new buffer for a down resize and duplication of the array. The
  666. * new array will be len long and allocation size will be determined using
  667. * Resize::downResize with the old array's allocLen. Does not actually copy
  668. * any data. Reads and writes allocLen and writes the new len. */
  669. template <class T, class Resize> void SVector<T, Resize>::
  670. downResizeDup(long len)
  671. {
  672. /* If there is already no length, then there is nothing we can do. */
  673. if ( BaseTable::data != 0 ) {
  674. /* Get the current header. */
  675. STabHead *head = ((STabHead*)BaseTable::data) - 1;
  676. /* Ask the resizer what the new length will be. */
  677. long newLen = Resize::downResize( head->allocLen, len );
  678. /* Detaching from the existing head, decrement the refcount. */
  679. head->refCount -= 1;
  680. /* Not shrinking to size zero, malloc it to the smaller size. */
  681. head = (STabHead*) malloc( sizeof(STabHead) + sizeof(T) * newLen );
  682. if ( head == 0 )
  683. throw std::bad_alloc();
  684. /* Save the new allocated length. */
  685. head->refCount = 1;
  686. head->allocLen = newLen;
  687. head->tabLen = len;
  688. /* Save the data pointer. */
  689. BaseTable::data = (T*) (head + 1);
  690. }
  691. }
  692. /**
  693. * \brief Free all memory used by the vector.
  694. *
  695. * The vector is reset to zero elements. Destructors are called on all
  696. * elements in the vector. The space allocated for the vector is freed.
  697. */
  698. template <class T, class Resize> void SVector<T, Resize>::
  699. empty()
  700. {
  701. if ( BaseTable::data != 0 ) {
  702. /* Get the header and drop the refcount on the data. */
  703. STabHead *head = ((STabHead*) BaseTable::data) - 1;
  704. head->refCount -= 1;
  705. /* If the refcount just went down to zero nobody else is referencing
  706. * the data. */
  707. if ( head->refCount == 0 ) {
  708. /* Call All destructors. */
  709. T *pos = BaseTable::data;
  710. for ( long i = 0; i < head->tabLen; pos++, i++ )
  711. pos->~T();
  712. /* Free the data space. */
  713. free( head );
  714. }
  715. /* Clear the pointer. */
  716. BaseTable::data = 0;
  717. }
  718. }
  719. /* Prepare for setting the contents of the vector to some array len long.
  720. * Handles reusing the existing space, detaching from a common space or
  721. * growing from zero length automatically. */
  722. template <class T, class Resize> void SVector<T, Resize>::
  723. setAsCommon(long len)
  724. {
  725. if ( BaseTable::data != 0 ) {
  726. /* Get the header. */
  727. STabHead *head = ((STabHead*)BaseTable::data) - 1;
  728. /* If the refCount is one, then we can reuse the space. Otherwise we
  729. * must detach from the referenced data create new space. */
  730. if ( head->refCount == 1 ) {
  731. /* Call All destructors. */
  732. T *pos = BaseTable::data;
  733. for ( long i = 0; i < head->tabLen; pos++, i++ )
  734. pos->~T();
  735. /* Adjust the allocated length. */
  736. if ( len < head->tabLen )
  737. downResize( len );
  738. else if ( len > head->tabLen )
  739. upResize( len );
  740. if ( BaseTable::data != 0 ) {
  741. /* Get the header again and set the length. */
  742. head = ((STabHead*)BaseTable::data) - 1;
  743. head->tabLen = len;
  744. }
  745. }
  746. else {
  747. /* Just detach from the data. */
  748. head->refCount -= 1;
  749. BaseTable::data = 0;
  750. /* Make enough space. This will set the length. */
  751. upResizeFromEmpty( len );
  752. }
  753. }
  754. else {
  755. /* The table is currently empty. Make enough space. This will set the
  756. * length. */
  757. upResizeFromEmpty( len );
  758. }
  759. }
  760. /**
  761. * \brief Set the contents of the vector to be len elements exactly.
  762. *
  763. * The vector becomes len elements in length. Destructors are called on any
  764. * existing elements in the vector. Copy constructors are used to place the
  765. * new elements in the vector.
  766. */
  767. template <class T, class Resize> void SVector<T, Resize>::
  768. setAs(const T *val, long len)
  769. {
  770. /* Common stuff for setting the array to len long. */
  771. setAsCommon( len );
  772. /* Copy data in. */
  773. T *dst = BaseTable::data;
  774. const T *src = val;
  775. for ( long i = 0; i < len; i++, dst++, src++ )
  776. new(dst) T(*src);
  777. }
  778. /**
  779. * \brief Set the vector to len copies of item.
  780. *
  781. * The vector becomes len elements in length. Destructors are called on any
  782. * existing elements in the vector. The element's copy constructor is used to
  783. * copy the item into the vector.
  784. */
  785. template <class T, class Resize> void SVector<T, Resize>::
  786. setAsDup(const T &item, long len)
  787. {
  788. /* Do the common stuff for setting the array to len long. */
  789. setAsCommon( len );
  790. /* Copy item in one spot at a time. */
  791. T *dst = BaseTable::data;
  792. for ( long i = 0; i < len; i++, dst++ )
  793. new(dst) T(item);
  794. }
  795. /**
  796. * \brief Set the vector to exactly len new items.
  797. *
  798. * The vector becomes len elements in length. Destructors are called on any
  799. * existing elements in the vector. Default constructors are used to init the
  800. * new items.
  801. */
  802. template <class T, class Resize> void SVector<T, Resize>::
  803. setAsNew(long len)
  804. {
  805. /* Do the common stuff for setting the array to len long. */
  806. setAsCommon( len );
  807. /* Create items using default constructor. */
  808. T *dst = BaseTable::data;
  809. for ( long i = 0; i < len; i++, dst++ )
  810. new(dst) T();
  811. }
  812. /* Make space in vector for a replacement at pos of len items. Handles reusing
  813. * existing space, detaching or growing from zero space. */
  814. template <class T, class Resize> long SVector<T, Resize>::
  815. replaceCommon(long pos, long len)
  816. {
  817. if ( BaseTable::data != 0 ) {
  818. /* Get the header. */
  819. STabHead *head = ((STabHead*)BaseTable::data) - 1;
  820. /* If we are given a negative position to replace at then treat it as
  821. * a position relative to the length. This doesn't have any meaning
  822. * unless the length is at least one. */
  823. if ( pos < 0 )
  824. pos = head->tabLen + pos;
  825. /* The end is the one past the last item that we want to write to. */
  826. long i, endPos = pos + len;
  827. if ( head->refCount == 1 ) {
  828. /* We can reuse the space. Make sure we have enough space. */
  829. if ( endPos > head->tabLen ) {
  830. upResize( endPos );
  831. /* Get the header again, whose addr may have changed after
  832. * resizing. */
  833. head = ((STabHead*)BaseTable::data) - 1;
  834. /* Delete any objects we need to delete. */
  835. T *item = BaseTable::data + pos;
  836. for ( i = pos; i < head->tabLen; i++, item++ )
  837. item->~T();
  838. /* We are extending the vector, set the new data length. */
  839. head->tabLen = endPos;
  840. }
  841. else {
  842. /* Delete any objects we need to delete. */
  843. T *item = BaseTable::data + pos;
  844. for ( i = pos; i < endPos; i++, item++ )
  845. item->~T();
  846. }
  847. }
  848. else {
  849. /* Use endPos to calc the end of the vector. */
  850. long newLen = endPos;
  851. if ( newLen < head->tabLen )
  852. newLen = head->tabLen;
  853. /* Duplicate and grow up to endPos. This will set the length. */
  854. upResizeDup( newLen );
  855. /* Copy from src up to pos. */
  856. const T *src = (T*) (head + 1);
  857. T *dst = BaseTable::data;
  858. for ( i = 0; i < pos; i++, dst++, src++)
  859. new(dst) T(*src);
  860. /* Copy any items after the replace range. */
  861. for ( i += len, src += len, dst += len;
  862. i < head->tabLen; i++, dst++, src++ )
  863. new(dst) T(*src);
  864. }
  865. }
  866. else {
  867. /* There is no data initially, must grow from zero. This will set the
  868. * new length. */
  869. upResizeFromEmpty( len );
  870. }
  871. return pos;
  872. }
  873. /**
  874. * \brief Replace len elements at position pos.
  875. *
  876. * If there are existing elements at the positions to be replaced, then
  877. * destructors are called before the space is used. Copy constructors are used
  878. * to place the elements into the vector. It is allowable for the pos and
  879. * length to specify a replacement that overwrites existing elements and
  880. * creates new ones. If pos is greater than the length of the vector then
  881. * undefined behaviour results. If pos is negative, then it is treated as an
  882. * offset relative to the length of the vector.
  883. */
  884. template <class T, class Resize> void SVector<T, Resize>::
  885. replace(long pos, const T *val, long len)
  886. {
  887. /* Common work for replacing in the vector. */
  888. pos = replaceCommon( pos, len );
  889. /* Copy data in using copy constructor. */
  890. T *dst = BaseTable::data + pos;
  891. const T *src = val;
  892. for ( long i = 0; i < len; i++, dst++, src++ )
  893. new(dst) T(*src);
  894. }
  895. /**
  896. * \brief Replace at position pos with len copies of an item.
  897. *
  898. * If there are existing elements at the positions to be replaced, then
  899. * destructors are called before the space is used. The copy constructor is
  900. * used to place the element into this vector. It is allowable for the pos and
  901. * length to specify a replacement that overwrites existing elements and
  902. * creates new ones. If pos is greater than the length of the vector then
  903. * undefined behaviour results. If pos is negative, then it is treated as an
  904. * offset relative to the length of the vector.
  905. */
  906. template <class T, class Resize> void SVector<T, Resize>::
  907. replaceDup(long pos, const T &val, long len)
  908. {
  909. /* Common replacement stuff. */
  910. pos = replaceCommon( pos, len );
  911. /* Copy data in using copy constructor. */
  912. T *dst = BaseTable::data + pos;
  913. for ( long i = 0; i < len; i++, dst++ )
  914. new(dst) T(val);
  915. }
  916. /**
  917. * \brief Replace at position pos with len new elements.
  918. *
  919. * If there are existing elements at the positions to be replaced, then
  920. * destructors are called before the space is used. The default constructor is
  921. * used to initialize the new elements. It is allowable for the pos and length
  922. * to specify a replacement that overwrites existing elements and creates new
  923. * ones. If pos is greater than the length of the vector then undefined
  924. * behaviour results. If pos is negative, then it is treated as an offset
  925. * relative to the length of the vector.
  926. */
  927. template <class T, class Resize> void SVector<T, Resize>::
  928. replaceNew(long pos, long len)
  929. {
  930. /* Do the common replacement stuff. */
  931. pos = replaceCommon( pos, len );
  932. /* Copy data in using copy constructor. */
  933. T *dst = BaseTable::data + pos;
  934. for ( long i = 0; i < len; i++, dst++ )
  935. new(dst) T();
  936. }
  937. /**
  938. * \brief Remove len elements at position pos.
  939. *
  940. * Destructor is called on all elements removed. Elements to the right of pos
  941. * are shifted len spaces to the left to take up the free space. If pos is
  942. * greater than or equal to the length of the vector then undefined behavior
  943. * results. If pos is negative then it is treated as an offset relative to the
  944. * length of the vector.
  945. */
  946. template <class T, class Resize> void SVector<T, Resize>::
  947. remove(long pos, long len)
  948. {
  949. /* If there is no data, we can't delete anything anyways. */
  950. if ( BaseTable::data != 0 ) {
  951. /* Get the header. */
  952. STabHead *head = ((STabHead*)BaseTable::data) - 1;
  953. /* If we are given a negative position to remove at then
  954. * treat it as a position relative to the length. */
  955. if ( pos < 0 )
  956. pos = head->tabLen + pos;
  957. /* The first position after the last item deleted. */
  958. long endPos = pos + len;
  959. /* The New data length. */
  960. long i, newLen = head->tabLen - len;
  961. if ( head->refCount == 1 ) {
  962. /* We are the only ones using the data. We can reuse
  963. * the existing space. */
  964. /* The place in the data we are deleting at. */
  965. T *dst = BaseTable::data + pos;
  966. /* Call Destructors. */
  967. T *item = BaseTable::data + pos;
  968. for ( i = 0; i < len; i += 1, item += 1 )
  969. item->~T();
  970. /* Shift data over if necessary. */
  971. long lenToSlideOver = head->tabLen - endPos;
  972. if ( len > 0 && lenToSlideOver > 0 )
  973. memmove(BaseTable::data + pos, dst + len, sizeof(T)*lenToSlideOver);
  974. /* Shrink the data if necessary. */
  975. downResize( newLen );
  976. if ( BaseTable::data != 0 ) {
  977. /* Get the header again (because of the resize) and set the
  978. * new data length. */
  979. head = ((STabHead*)BaseTable::data) - 1;
  980. head->tabLen = newLen;
  981. }
  982. }
  983. else {
  984. /* Must detach from the common data. Just copy the non-deleted
  985. * items from the common data. */
  986. /* Duplicate and grow down to newLen. This will set the length. */
  987. downResizeDup( newLen );
  988. /* Copy over just the non-deleted parts. */
  989. const T *src = (T*) (head + 1);
  990. T *dst = BaseTable::data;
  991. for ( i = 0; i < pos; i++, dst++, src++ )
  992. new(dst) T(*src);
  993. /* ... and the second half. */
  994. for ( i += len, src += len; i < head->tabLen; i++, src++, dst++ )
  995. new(dst) T(*src);
  996. }
  997. }
  998. }
  999. /* Shift over existing data. Handles reusing existing space, detaching or
  1000. * growing from zero space. */
  1001. template <class T, class Resize> long SVector<T, Resize>::
  1002. insertCommon(long pos, long len)
  1003. {
  1004. if ( BaseTable::data != 0 ) {
  1005. /* Get the header. */
  1006. STabHead *head = ((STabHead*)BaseTable::data) - 1;
  1007. /* If we are given a negative position to insert at then treat it as a
  1008. * position relative to the length. This only has meaning if there is
  1009. * existing data. */
  1010. if ( pos < 0 )
  1011. pos = head->tabLen + pos;
  1012. /* Calculate the new length. */
  1013. long i, newLen = head->tabLen + len;
  1014. if ( head->refCount == 1 ) {
  1015. /* Up resize, we are growing. */
  1016. upResize( newLen );
  1017. /* Get the header again, (the addr may have changed after
  1018. * resizing). */
  1019. head = ((STabHead*)BaseTable::data) - 1;
  1020. /* Shift over data at insert spot if needed. */
  1021. if ( len > 0 && pos < head->tabLen ) {
  1022. memmove( BaseTable::data + pos + len, BaseTable::data + pos,
  1023. sizeof(T)*(head->tabLen - pos) );
  1024. }
  1025. /* Grow the length by the len inserted. */
  1026. head->tabLen += len;
  1027. }
  1028. else {
  1029. /* Need to detach from the existing array. Copy over the other
  1030. * parts. This will set the length. */
  1031. upResizeDup( newLen );
  1032. /* Copy over the parts around the insert. */
  1033. const T *src = (T*) (head + 1);
  1034. T *dst = BaseTable::data;
  1035. for ( i = 0; i < pos; i++, dst++, src++ )
  1036. new(dst) T(*src);
  1037. /* ... and the second half. */
  1038. for ( dst += len; i < head->tabLen; i++, src++, dst++ )
  1039. new(dst) T(*src);
  1040. }
  1041. }
  1042. else {
  1043. /* There is no existing data. Start from zero. This will set the
  1044. * length. */
  1045. upResizeFromEmpty( len );
  1046. }
  1047. return pos;
  1048. }
  1049. /**
  1050. * \brief Insert len elements at position pos.
  1051. *
  1052. * Elements in the vector from pos onward are shifted len spaces to the right.
  1053. * The copy constructor is used to place the elements into this vector. If pos
  1054. * is greater than the length of the vector then undefined behaviour results.
  1055. * If pos is negative then it is treated as an offset relative to the length
  1056. * of the vector.
  1057. */
  1058. template <class T, class Resize> void SVector<T, Resize>::
  1059. insert(long pos, const T *val, long len)
  1060. {
  1061. /* Do the common insertion stuff. */
  1062. pos = insertCommon( pos, len );
  1063. /* Copy data in element by element. */
  1064. T *dst = BaseTable::data + pos;
  1065. const T *src = val;
  1066. for ( long i = 0; i < len; i++, dst++, src++ )
  1067. new(dst) T(*src);
  1068. }
  1069. /**
  1070. * \brief Insert len copies of item at position pos.
  1071. *
  1072. * Elements in the vector from pos onward are shifted len spaces to the right.
  1073. * The copy constructor is used to place the element into this vector. If pos
  1074. * is greater than the length of the vector then undefined behaviour results.
  1075. * If pos is negative then it is treated as an offset relative to the length
  1076. * of the vector.
  1077. */
  1078. template <class T, class Resize> void SVector<T, Resize>::
  1079. insertDup(long pos, const T &item, long len)
  1080. {
  1081. /* Do the common insertion stuff. */
  1082. pos = insertCommon( pos, len );
  1083. /* Copy the data item in one at a time. */
  1084. T *dst = BaseTable::data + pos;
  1085. for ( long i = 0; i < len; i++, dst++ )
  1086. new(dst) T(item);
  1087. }
  1088. /**
  1089. * \brief Insert len new elements using the default constructor.
  1090. *
  1091. * Elements in the vector from pos onward are shifted len spaces to the right.
  1092. * Default constructors are used to init the new elements. If pos is off the
  1093. * end of the vector then undefined behaviour results. If pos is negative then
  1094. * it is treated as an offset relative to the length of the vector.
  1095. */
  1096. template <class T, class Resize> void SVector<T, Resize>::
  1097. insertNew(long pos, long len)
  1098. {
  1099. /* Do the common insertion stuff. */
  1100. pos = insertCommon( pos, len );
  1101. /* Init new data with default constructors. */
  1102. T *dst = BaseTable::data + pos;
  1103. for ( long i = 0; i < len; i++, dst++ )
  1104. new(dst) T();
  1105. }
  1106. /* Makes space for len items, Does not init the items in any way. If pos is
  1107. * greater than the length of the vector then undefined behaviour results.
  1108. * Updates the length of the vector. */
  1109. template <class T, class Resize> void SVector<T, Resize>::
  1110. makeRawSpaceFor(long pos, long len)
  1111. {
  1112. if ( BaseTable::data != 0 ) {
  1113. /* Get the header. */
  1114. STabHead *head = ((STabHead*)BaseTable::data) - 1;
  1115. /* Calculate the new length. */
  1116. long i, newLen = head->tabLen + len;
  1117. if ( head->refCount == 1 ) {
  1118. /* Up resize, we are growing. */
  1119. upResize( newLen );
  1120. /* Get the header again, (the addr may have changed after
  1121. * resizing). */
  1122. head = ((STabHead*)BaseTable::data) - 1;
  1123. /* Shift over data at insert spot if needed. */
  1124. if ( len > 0 && pos < head->tabLen ) {
  1125. memmove( BaseTable::data + pos + len, BaseTable::data + pos,
  1126. sizeof(T)*(head->tabLen - pos) );
  1127. }
  1128. /* Grow the length by the len inserted. */
  1129. head->tabLen += len;
  1130. }
  1131. else {
  1132. /* Need to detach from the existing array. Copy over the other
  1133. * parts. This will set the length. */
  1134. upResizeDup( newLen );
  1135. /* Copy over the parts around the insert. */
  1136. const T *src = (T*) (head + 1);
  1137. T *dst = BaseTable::data;
  1138. for ( i = 0; i < pos; i++, dst++, src++ )
  1139. new(dst) T(*src);
  1140. /* ... and the second half. */
  1141. for ( dst += len; i < head->tabLen; i++, src++, dst++ )
  1142. new(dst) T(*src);
  1143. }
  1144. }
  1145. else {
  1146. /* There is no existing data. Start from zero. This will set the
  1147. * length. */
  1148. upResizeFromEmpty( len );
  1149. }
  1150. }
  1151. #ifdef AAPL_NAMESPACE
  1152. }
  1153. #endif
  1154. #endif /* _AAPL_SVECTOR_H */