/src/rt/util/indexed_list.h

http://github.com/jruderman/rust · C Header · 97 lines · 72 code · 10 blank · 15 comment · 5 complexity · 8fdbf74948691da6855db4540cdadd64 MD5 · raw file

  1. // -*- c++ -*-
  2. #ifndef INDEXED_LIST_H
  3. #define INDEXED_LIST_H
  4. #include <assert.h>
  5. #include "array_list.h"
  6. class indexed_list_object {
  7. public:
  8. int32_t list_index;
  9. };
  10. template<typename T>
  11. class indexed_list_element : public indexed_list_object {
  12. public:
  13. T value;
  14. indexed_list_element(T value) : value(value) {
  15. }
  16. };
  17. /**
  18. * An array list of objects that are aware of their position in the list.
  19. * Normally, objects in this list should derive from the base class
  20. * "indexed_list_object" however because of nasty Rust compiler dependencies
  21. * on the layout of runtime objects we cannot always derive from this
  22. * base class, so instead we just enforce the informal protocol that any
  23. * object inserted in this list must define a "int32_t list_index" member.
  24. */
  25. template<typename T> class indexed_list {
  26. array_list<T*> list;
  27. public:
  28. virtual int32_t append(T *value);
  29. virtual bool pop(T **value);
  30. /**
  31. * Same as pop(), except that it returns NULL if the list is empty.
  32. */
  33. virtual T* pop_value();
  34. virtual size_t length() {
  35. return list.size();
  36. }
  37. virtual bool is_empty() {
  38. return list.is_empty();
  39. }
  40. virtual int32_t remove(T* value);
  41. virtual T * operator[](int32_t index);
  42. virtual ~indexed_list() {}
  43. };
  44. template<typename T> int32_t
  45. indexed_list<T>::append(T *value) {
  46. value->list_index = list.push(value);
  47. return value->list_index;
  48. }
  49. /**
  50. * Swap delete the last object in the list with the specified object.
  51. */
  52. template<typename T> int32_t
  53. indexed_list<T>::remove(T *value) {
  54. assert (value->list_index >= 0);
  55. assert (value->list_index < (int32_t)list.size());
  56. int32_t removeIndex = value->list_index;
  57. T *last = 0;
  58. list.pop(&last);
  59. if (last->list_index == removeIndex) {
  60. last->list_index = -1;
  61. return removeIndex;
  62. } else {
  63. value->list_index = -1;
  64. list[removeIndex] = last;
  65. last->list_index = removeIndex;
  66. return removeIndex;
  67. }
  68. }
  69. template<typename T> bool
  70. indexed_list<T>::pop(T **value) {
  71. return list.pop(value);
  72. }
  73. template<typename T> T*
  74. indexed_list<T>::pop_value() {
  75. T *value = NULL;
  76. if (list.pop(&value)) {
  77. return value;
  78. }
  79. return NULL;
  80. }
  81. template <typename T> T *
  82. indexed_list<T>::operator[](int32_t index) {
  83. T *value = list[index];
  84. assert(value->list_index == index);
  85. return value;
  86. }
  87. #endif /* INDEXED_LIST_H */