indexedlist_test.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @file indexedlist_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <misc/debug.h>
  30. #include <misc/offset.h>
  31. #include <structure/IndexedList.h>
  32. IndexedList il;
  33. struct elem {
  34. int value;
  35. IndexedListNode node;
  36. };
  37. static void elem_insert (struct elem *e, int value, uint64_t index)
  38. {
  39. e->value = value;
  40. IndexedList_InsertAt(&il, &e->node, index);
  41. }
  42. static void remove_at (uint64_t index)
  43. {
  44. IndexedListNode *n = IndexedList_GetAt(&il, index);
  45. struct elem *e = UPPER_OBJECT(n, struct elem, node);
  46. IndexedList_Remove(&il, &e->node);
  47. }
  48. static void print_list (void)
  49. {
  50. for (uint64_t i = 0; i < IndexedList_Count(&il); i++) {
  51. IndexedListNode *n = IndexedList_GetAt(&il, i);
  52. struct elem *e = UPPER_OBJECT(n, struct elem, node);
  53. printf("%d ", e->value);
  54. }
  55. printf("\n");
  56. }
  57. int main (int argc, char *argv[])
  58. {
  59. IndexedList_Init(&il);
  60. struct elem arr[100];
  61. print_list();
  62. elem_insert(&arr[0], 1, 0);
  63. print_list();
  64. elem_insert(&arr[1], 2, 0);
  65. print_list();
  66. elem_insert(&arr[2], 3, 0);
  67. print_list();
  68. elem_insert(&arr[3], 4, 0);
  69. print_list();
  70. elem_insert(&arr[4], 5, 0);
  71. print_list();
  72. elem_insert(&arr[5], 6, 0);
  73. print_list();
  74. elem_insert(&arr[6], 7, 1);
  75. print_list();
  76. remove_at(0);
  77. print_list();
  78. remove_at(5);
  79. print_list();
  80. return 0;
  81. }