1
0

ptr_list.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "mem_block.h"
  2. class ptr_list
  3. {
  4. private:
  5. mem_block_t<void *> data;
  6. int count;
  7. protected:
  8. void * get_raw_ptr() {return data.get_ptr();}
  9. public:
  10. ptr_list() {count=0;}
  11. int get_count() const {return count;}
  12. void * get_item(int n) const
  13. {
  14. if (n>=0 && n<count) return data[n];
  15. else return 0;
  16. }
  17. int add_item(void * ptr)//returns index
  18. {
  19. count++;
  20. data.check_size(count);
  21. data[count-1] = ptr;
  22. return count-1;
  23. }
  24. int find_item(void * ptr)//returns index, -1 if not found
  25. {
  26. int n;
  27. for(n=0;n<count;n++)
  28. {
  29. if (data[n] == ptr) return n;
  30. }
  31. return -1;
  32. }
  33. bool have_item(void * ptr) {return find_item(ptr)>=0;}
  34. void remove_item(void * ptr)
  35. {
  36. int idx = find_item(ptr);
  37. if (idx>=0) remove_by_idx(idx);
  38. }
  39. void * remove_by_idx(int idx)
  40. {
  41. void * ptr = 0;
  42. if (idx>=0 && idx<count)
  43. {
  44. ptr = data[idx];
  45. int n;
  46. count--;
  47. for(n=idx;n<count;n++)
  48. {
  49. data[n] = data[n+1];
  50. }
  51. }
  52. return ptr;
  53. }
  54. void remove_all()
  55. {
  56. count=0;
  57. }
  58. int insert_item(void * ptr,int idx) //returns index
  59. {
  60. if (idx>count || idx<0) idx = count;
  61. count++;
  62. data.check_size(count);
  63. int n;
  64. for(n=count-1;n>idx;n--)
  65. {
  66. data[n]=data[n-1];
  67. }
  68. data[idx] = ptr;
  69. return idx;
  70. }
  71. void * operator[](int idx) const {return get_item(idx);}
  72. };
  73. template<class T>
  74. class ptr_list_t : protected ptr_list
  75. {
  76. public:
  77. int get_count() const {return ptr_list::get_count();}
  78. T * get_item(int n) const {return static_cast<T*>(ptr_list::get_item(n));}
  79. int add_item(T * ptr) {return ptr_list::add_item(static_cast<void*>(ptr));}
  80. int find_item(T * ptr) {return ptr_list::find_item(static_cast<void*>(ptr));}
  81. bool have_item(T * ptr) {return ptr_list::have_item(static_cast<void*>(ptr));}
  82. void remove_item(T * ptr) {ptr_list::remove_item(static_cast<void*>(ptr));}
  83. T * remove_by_idx(int idx) {return static_cast<T*>(ptr_list::remove_by_idx(idx));}
  84. void remove_all() {ptr_list::remove_all();}
  85. void * operator[](int idx) const {return get_item(idx);}
  86. int insert_item(int idx,T* ptr) {return ptr_list::insert_item(idx,static_cast<void*>(ptr));}
  87. void delete_item(T * ptr)
  88. {
  89. remove_item(ptr);
  90. delete ptr;
  91. }
  92. void delete_by_idx(int idx)
  93. {
  94. T * ptr = remove_by_idx(idx);
  95. if (ptr) delete ptr;
  96. }
  97. void delete_all()
  98. {
  99. int n,max=get_count();
  100. for(n=0;n<max;n++)
  101. {
  102. T * ptr = get_item(n);
  103. if (ptr) delete ptr;
  104. }
  105. remove_all();
  106. }
  107. void sort(int (__cdecl *compare )(const T ** elem1, const T** elem2 ) )
  108. {
  109. qsort(get_raw_ptr(),get_count(),sizeof(void*),(int (__cdecl *)(const void *, const void *) )compare);
  110. }
  111. };