table.hh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. //{{{ Table Template
  9. //
  10. // Expanding Array Class
  11. //
  12. //$Id: table.hh,v 1.3 1997/09/29 04:29:52 oliy Exp $
  13. //}}}
  14. #ifndef I4_TABLE_HH
  15. #define I4_TABLE_HH
  16. #include "arch.hh"
  17. #include "error/error.hh"
  18. #include "memory/malloc.hh"
  19. //#define I4_TABLE_NO_GROWTH
  20. template <class T>
  21. class i4_table
  22. {
  23. protected:
  24. T *entry;
  25. w16 used,entries;
  26. #ifndef I4_TABLE_NO_GROWTH
  27. w16 grow;
  28. #endif
  29. public:
  30. int size() const { return used; }
  31. T& operator[](int i) const
  32. //{{{
  33. {
  34. I4_ASSERT(i>=0 && i<used, "table::bad array reference");
  35. return entry[i];
  36. }
  37. //}}}
  38. i4_table(int entries, int grow = 0) : entries(entries), entry(0), used(0)
  39. #ifndef I4_TABLE_NO_GROWTH
  40. ,grow(grow)
  41. #endif
  42. //{{{
  43. {
  44. if (entries>0)
  45. entry = (T*)i4_malloc(sizeof(T)*entries,"table");
  46. I4_ASSERT(entry, "table::can't allocate entries");
  47. }
  48. //}}}
  49. ~i4_table()
  50. //{{{
  51. {
  52. clear();
  53. i4_free(entry);
  54. entries = 0;
  55. }
  56. //}}}
  57. int add(T item,int ref = -1)
  58. //{{{
  59. {
  60. I4_ASSERT(item, "table::bad item add");
  61. if (ref<0)
  62. ref += used+1;
  63. I4_ASSERT(ref>=0 && ref<=used,"table::bad item referenced");
  64. if (used>=entries)
  65. {
  66. #ifndef I4_TABLE_NO_GROWTH
  67. if (grow)
  68. {
  69. entries += grow;
  70. T* new_entry = (T*)i4_realloc(entry, sizeof(T *)*entries, "table");
  71. I4_ASSERT(new_entry, "table::out of memory");
  72. entry = new_entry;
  73. }
  74. else
  75. #endif
  76. I4_ASSERT(0, "table::out of entries");
  77. }
  78. for (int i=used; i>ref; i--)
  79. entry[i] = entry[i-1];
  80. entry[ref] = item;
  81. used++;
  82. return ref;
  83. }
  84. //}}}
  85. int add_table(const i4_table& tab,int ref = -1)
  86. //{{{
  87. {
  88. if (ref<0)
  89. ref += used+1;
  90. I4_ASSERT(ref>=0 && ref<=used,"table::bad item referenced");
  91. if (used+tab.size() >= entries)
  92. {
  93. #ifndef I4_TABLE_NO_GROWTH
  94. if (grow)
  95. {
  96. if (used+tab.size() >= entries+grow)
  97. entries = used+tab.size();
  98. else
  99. entries += grow;
  100. T* new_entry = (T*)i4_realloc(entry, sizeof(T *)*entries, "table");
  101. I4_ASSERT(new_entry, "table::out of memory");
  102. entry = new_entry;
  103. }
  104. else
  105. #endif
  106. I4_ASSERT(0, "table::out of entries");
  107. }
  108. int i;
  109. for (i=used-1; i>ref; i--)
  110. entry[i+tab.size()] = entry[i];
  111. for (i=0; i<tab.size(); i++)
  112. entry[ref+i] = tab.entry[i];
  113. used+=tab.size();
  114. return ref;
  115. }
  116. //}}}
  117. void remove(int ref)
  118. //{{{
  119. {
  120. I4_ASSERT(ref>=0 && ref<used, "table::bad item deletion");
  121. used--;
  122. for (int i=ref; i<used; i++)
  123. entry[i] = entry[i+1];
  124. }
  125. //}}}
  126. void clear()
  127. //{{{
  128. {
  129. used = 0;
  130. }
  131. //}}}
  132. };
  133. #endif
  134. //{{{ Emacs Locals
  135. // Local Variables:
  136. // folded-file: t
  137. // End:
  138. //}}}