table.hh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifndef MK_TABLE_HH
  9. #define MK_TABLE_HH
  10. #include "error.hh"
  11. template <class T>
  12. class table
  13. {
  14. protected:
  15. T *entry;
  16. int used,entries,grow;
  17. public:
  18. int size() const { return used; }
  19. T& operator[](int i) const
  20. {
  21. assert(i>=0 && i<used, "table::bad array reference");
  22. return entry[i];
  23. }
  24. table(int entries, int grow = 0) : entries(entries), grow(grow), entry(0), used(0)
  25. {
  26. if (entries>0)
  27. entry = (T*)malloc(sizeof(T)*entries);
  28. else
  29. entry = (T*)malloc(sizeof(T)*grow);
  30. assert(entry, "table::can't allocate entries");
  31. }
  32. ~table()
  33. {
  34. clear();
  35. free(entry);
  36. entries = 0;
  37. }
  38. int add(T item,int ref = -1)
  39. {
  40. assert(item, "table::bad item add");
  41. if (ref<0)
  42. ref += used+1;
  43. assert(ref>=0 && ref<=used,"table::bad item referenced");
  44. if (used>=entries)
  45. {
  46. if (grow)
  47. {
  48. entries += grow;
  49. T* new_entry = (T*)realloc(entry, sizeof(T *)*entries);
  50. assert(new_entry, "table::out of memory");
  51. entry = new_entry;
  52. }
  53. else
  54. assert(0, "table::out of entries");
  55. }
  56. for (int i=used; i>ref; i--)
  57. entry[i] = entry[i-1];
  58. entry[ref] = item;
  59. used++;
  60. return ref;
  61. }
  62. int add_table(const table& tab,int ref = -1)
  63. {
  64. if (ref<0)
  65. ref += used+1;
  66. assert(ref>=0 && ref<=used,"table::bad item referenced");
  67. if (used+tab.size() >= entries)
  68. {
  69. if (grow)
  70. {
  71. if (used+tab.size() >= entries+grow)
  72. entries = used+tab.size();
  73. else
  74. entries += grow;
  75. T* new_entry = (T*)realloc(entry, sizeof(T *)*entries);
  76. assert(new_entry, "table::out of memory");
  77. entry = new_entry;
  78. }
  79. else
  80. assert(0, "table::out of entries");
  81. }
  82. int i;
  83. for (i=used-1; i>ref; i--)
  84. entry[i+tab.size()] = entry[i];
  85. for (i=0; i<tab.size(); i++)
  86. entry[ref+i] = tab.entry[i];
  87. used+=tab.size();
  88. return ref;
  89. }
  90. void remove(int ref)
  91. {
  92. assert(ref>=0 && ref<used, "table::bad item deletion");
  93. used--;
  94. for (int i=ref; i<used; i++)
  95. entry[i] = entry[i+1];
  96. }
  97. void clear()
  98. {
  99. used = 0;
  100. }
  101. };
  102. class name_table : public table<char *>
  103. {
  104. public:
  105. name_table(int entries = 20, int grow = 100) : table<char*>(entries,grow) {}
  106. name_table(name_table &other) : table<char*>(20,200)
  107. {
  108. add_table(other);
  109. }
  110. name_table& operator=(char *item) { clear(); add(item); return *this; }
  111. name_table& operator=(const name_table &tab) { clear(); add_table(tab); return *this; }
  112. name_table& operator+(char *item) { add(item); return *this; }
  113. name_table& operator+(const name_table &tab) { add_table(tab); return *this; }
  114. name_table& operator+=(char *item) { add(item); return *this; }
  115. name_table& operator+=(const name_table &tab) { add_table(tab); return *this; }
  116. name_table& clear() { table<char*>::clear(); return *this; }
  117. int find(char *item)
  118. {
  119. for (int i=0; i<size(); i++)
  120. if (strcmp(item, entry[i])==0)
  121. return i;
  122. return -1;
  123. }
  124. };
  125. typedef name_table list;
  126. #endif