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