1
0

tlist.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //PORTABLE
  2. #ifndef _TLIST_H
  3. #define _TLIST_H
  4. #include <bfc/common.h>
  5. #include <bfc/wasabi_std.h>
  6. #include <bfc/named.h>
  7. /*
  8. A generic std::vector wannabe...
  9. NOTE: you must only use this with basic types! constructors and destructors
  10. will not be called.
  11. */
  12. #include <bfc/bfc_assert.h>
  13. #include <bfc/std_mem.h>
  14. #define TLIST_INCREMENT 32
  15. template<class T>
  16. class TList {
  17. public:
  18. TList() {
  19. nitems = 0;
  20. nslots = 0;
  21. items = NULL;
  22. }
  23. virtual ~TList() {
  24. FREE(items);
  25. }
  26. int getNumItems() const { return nitems; };
  27. // CAN'T PROVIDE BOUNDS CHECKING!!! (except by ASSERT() :( )
  28. T enumItem(int n) const {
  29. ASSERT(items != NULL);
  30. ASSERT(n >= 0 && n < nitems);
  31. return items[n];
  32. }
  33. T operator[](int n) const { return enumItem(n); }
  34. T getFirst() const { return enumItem(0); }
  35. T getLast() const { return enumItem(getNumItems()-1); }
  36. T *enumRef(int n) const {
  37. ASSERT(items != NULL);
  38. ASSERT(n >= 0 && n < nitems);
  39. return items+n;
  40. }
  41. T *addItem(const T &item) {
  42. ASSERT(nitems <= nslots);
  43. if (items == NULL) {
  44. nslots = TLIST_INCREMENT;
  45. items = (T *)MALLOC(sizeof(T) * nslots);
  46. nitems = 0;
  47. } else if (nslots == nitems) { // time to expand
  48. T *newitems;
  49. nslots += TLIST_INCREMENT;
  50. newitems = (T *)MALLOC(sizeof(T) * nslots);
  51. MEMCPY(newitems, items, nitems * sizeof(T));
  52. FREE(items);
  53. items = newitems;
  54. }
  55. items[nitems++] = item;
  56. return &items[nitems-1];
  57. }
  58. T *insertItem(const T &item, int pos) { // position to insert
  59. ASSERT(pos >= 0 && pos <= nitems);
  60. T *tmp=addItem(item);
  61. int n=nitems-1;
  62. while (n > pos) {
  63. items[n]=items[n-1];
  64. n--;
  65. }
  66. tmp=items+pos;
  67. *tmp=item;
  68. return tmp;
  69. }
  70. T *replaceItemByPos(const T &item, int pos) {
  71. ASSERT(items != NULL);
  72. ASSERT(nitems != 0);
  73. ASSERT(pos >= 0 && pos < nitems);
  74. items[pos]=item;
  75. return &items[pos];
  76. }
  77. void setItem(int pos, const T &newval) {
  78. if (pos < 0) return;
  79. if (pos >= nitems) return;
  80. items[pos] = newval;
  81. }
  82. int delItem(const T &item) {
  83. int c = 0;
  84. ASSERT(items != NULL);
  85. ASSERT(nitems != 0);
  86. T *src = items;
  87. T *dst = items;
  88. for (int i = 0; i < nitems; i++) {
  89. if (*src != item) {
  90. *dst++ = *src;
  91. c++;
  92. }
  93. src++;
  94. }
  95. nitems -= c;
  96. return c;
  97. }
  98. int delByPos(int pos) {
  99. if (pos < 0 || pos >= nitems) return 0;
  100. --nitems;
  101. if (pos == nitems) return 1; // last one? nothing to copy over
  102. MEMCPY(items+pos, items+(pos+1), sizeof(T)*(nitems-pos)); // CT:not (nitems-(pos+1)) as nitems has been decremented earlier
  103. return 1;
  104. }
  105. void removeAll() {
  106. FREE(items); items = NULL;
  107. nitems = 0;
  108. nslots = 0;
  109. }
  110. void freeAll() {
  111. for (int i = 0; i < nitems; i++)
  112. FREE(items[i]);
  113. removeAll();
  114. }
  115. int getItemPos(const T &it) const {
  116. int n = getNumItems();
  117. for (int i=0;i<n;i++)
  118. if(!MEMCMP(&items[i],&it,sizeof(T))) return i; // CT if (items[i] == it) return i;
  119. return -1;
  120. }
  121. int haveItem(const T &it) const {
  122. return (getItemPos(it) != -1);
  123. }
  124. private:
  125. int nitems, nslots;
  126. T *items;
  127. };
  128. #endif