itemlist.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. ** Copyright (C) 2003-2006 Nullsoft, Inc.
  3. **
  4. ** This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held
  5. ** liable for any damages arising from the use of this software.
  6. **
  7. ** Permission is granted to anyone to use this software for any purpose, including commercial applications, and to
  8. ** alter it and redistribute it freely, subject to the following restrictions:
  9. **
  10. ** 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  11. ** If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  12. **
  13. ** 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  14. **
  15. ** 3. This notice may not be removed or altered from any source distribution.
  16. **
  17. */
  18. #include <windows.h>
  19. #include "itemlist.h"
  20. C_ItemList::C_ItemList()
  21. {
  22. }
  23. C_ItemList::~C_ItemList()
  24. {
  25. if (m_list) ::free(m_list);
  26. }
  27. void *C_ItemList::Add(void *i)
  28. {
  29. // check if we have enough space to add an element
  30. if (!m_list || alloc_size == 0 || current_index >= alloc_size - 1)//|| !(m_size&31))
  31. {
  32. alloc_size += MEMORY_STEP;
  33. void **new_m_list=(void**)::realloc(m_list,sizeof(void*)*(alloc_size));
  34. if (new_m_list)
  35. m_list = new_m_list;
  36. else
  37. {
  38. m_list=NULL;
  39. return NULL;
  40. }
  41. }
  42. // add the element and increase the index
  43. m_list[current_index]=i;
  44. current_index++;
  45. return i;
  46. }
  47. void C_ItemList::Set(int w, void *newv)
  48. {
  49. if (w >= 0 && w < current_index)
  50. {
  51. m_list[w]=newv;
  52. }
  53. }
  54. void *C_ItemList::Get(int w) const
  55. {
  56. if (w >= 0 && w < current_index)
  57. {
  58. return m_list[w];
  59. }
  60. return NULL;
  61. }
  62. void C_ItemList::Del(int idx)
  63. {
  64. if (m_list && idx >= 0 && idx < current_index)
  65. {
  66. current_index--;
  67. if (idx != current_index)
  68. {
  69. ::memcpy(m_list + idx, m_list + idx + 1, sizeof(void*) * (current_index - idx));
  70. }
  71. if (!(current_index &31)&& current_index) // resize down
  72. {
  73. void** new_m_list=(void**)::realloc(m_list,sizeof(void*)* current_index);
  74. if (new_m_list) m_list = new_m_list;
  75. else
  76. {
  77. new_m_list=(void**)::malloc(sizeof(void*)* current_index);
  78. if (new_m_list)
  79. {
  80. memcpy(new_m_list, m_list, sizeof(void*)* current_index);
  81. free(m_list);
  82. m_list = new_m_list;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. void *C_ItemList::Insert(void *i, int pos)
  89. {
  90. if (!m_list || !(current_index &31))
  91. {
  92. void** new_m_list=(void**)::realloc(m_list,sizeof(void*)*(current_index +32));
  93. if (new_m_list)
  94. m_list = new_m_list;
  95. else
  96. {
  97. new_m_list=(void**)::malloc(sizeof(void*)*(current_index +32));
  98. if (new_m_list)
  99. {
  100. memcpy(new_m_list, m_list, sizeof(void*)* current_index);
  101. free(m_list);
  102. m_list = new_m_list;
  103. }
  104. else
  105. return i;
  106. }
  107. }
  108. current_index++;
  109. for ( int j = current_index - 1; j > pos; j-- )
  110. m_list[ j ] = m_list[ j - 1 ];
  111. m_list[ pos ] = i;
  112. return i;
  113. }
  114. void C_ItemList::for_all(void (*for_all_func)(void *))
  115. {
  116. if (m_list)
  117. {
  118. for (int i=0;i< current_index;i++)
  119. {
  120. for_all_func(m_list[i]);
  121. }
  122. }
  123. }
  124. void C_ItemList::for_all_ctx(void (*for_all_func)(void *, void *), void *ctx)
  125. {
  126. if (m_list)
  127. {
  128. for (int i=0;i< current_index;i++)
  129. {
  130. for_all_func(m_list[i], ctx);
  131. }
  132. }
  133. }