tb_list.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #include "tb_list.h"
  6. #include "tb_core.h"
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <memory.h>
  10. namespace tb {
  11. bool TBListBackend::Add(void *data)
  12. {
  13. if (!GrowIfNeeded())
  14. return false;
  15. m_data->list[m_data->num] = data;
  16. m_data->num++;
  17. return true;
  18. }
  19. bool TBListBackend::Add(void *data, int index)
  20. {
  21. assert(index >= 0 && index <= GetNumItems());
  22. if (!GrowIfNeeded())
  23. return false;
  24. if (index < m_data->num)
  25. memmove(&m_data->list[index + 1], &m_data->list[index], (m_data->num - index) * sizeof(void*));
  26. m_data->list[index] = data;
  27. m_data->num++;
  28. return true;
  29. }
  30. void TBListBackend::Set(void *data, int index)
  31. {
  32. assert(index >= 0 && index < GetNumItems());
  33. m_data->list[index] = data;
  34. }
  35. void *TBListBackend::RemoveFast(int index)
  36. {
  37. assert(index >= 0 && index < GetNumItems());
  38. void *data = m_data->list[index];
  39. m_data->list[index] = m_data->list[m_data->num - 1];
  40. m_data->num--;
  41. return data;
  42. }
  43. void *TBListBackend::Remove(int index)
  44. {
  45. assert(index >= 0 && index < GetNumItems());
  46. void *data = m_data->list[index];
  47. if(index < m_data->num - 1)
  48. memmove(&m_data->list[index], &m_data->list[index + 1], (m_data->num - index - 1) * sizeof(void*));
  49. m_data->num--;
  50. return data;
  51. }
  52. void TBListBackend::RemoveAll()
  53. {
  54. free(m_data);
  55. m_data = nullptr;
  56. }
  57. void TBListBackend::Swap(int index1, int index2)
  58. {
  59. assert(index1 >= 0 && index1 < GetNumItems());
  60. assert(index2 >= 0 && index2 < GetNumItems());
  61. void *tmp = m_data->list[index1];
  62. m_data->list[index1] = m_data->list[index2];
  63. m_data->list[index2] = tmp;
  64. }
  65. int TBListBackend::Find(void *data) const
  66. {
  67. int num = GetNumItems();
  68. for(int i = 0; i < num; i++)
  69. {
  70. if (Get(i) == data)
  71. return i;
  72. }
  73. return -1;
  74. }
  75. void *TBListBackend::Get(int index) const
  76. {
  77. assert(index >= 0 && index < GetNumItems());
  78. return m_data->list[index];
  79. }
  80. bool TBListBackend::Reserve(int new_capacity)
  81. {
  82. assert(new_capacity > 0);
  83. if (new_capacity > GetCapacity())
  84. {
  85. int num = GetNumItems();
  86. if (char *new_data = (char *) realloc(m_data, sizeof(TBLIST_DATA) + sizeof(void *) * (new_capacity)))
  87. {
  88. m_data = (TBLIST_DATA *) new_data;
  89. m_data->num = num;
  90. m_data->capacity = new_capacity;
  91. m_data->list = (void**) (new_data + sizeof(TBLIST_DATA));
  92. return true;
  93. }
  94. return false;
  95. }
  96. return true;
  97. }
  98. bool TBListBackend::GrowIfNeeded()
  99. {
  100. int capacity = GetCapacity();
  101. if (GetNumItems() == capacity)
  102. return Reserve(CLAMP(4, capacity * 2, 1024));
  103. return true;
  104. }
  105. }; // namespace tb