stacked_list.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * This file is part of Gomu.
  3. *
  4. * Copyright 2016 by Jean Fromentin <jean.fromentin@math.cnrs.fr>
  5. *
  6. * Gomu is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Gomu is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Gomu. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef STACKED_LIST_HPP
  20. #define STACKED_LIST_HPP
  21. #include <iostream>
  22. #include <cstdint>
  23. #include <deque>
  24. #include <stack>
  25. #include <cassert>
  26. using namespace std;
  27. #define SIZE 1024
  28. typedef uint16_t NInd;
  29. typedef int16_t NData;
  30. //**********************
  31. //* Class declarations *
  32. //**********************
  33. //! Node of a StackedList
  34. class StackedListNode{
  35. public:
  36. StackedListNode();
  37. NInd previous,next;
  38. NData data;
  39. };
  40. //! A Both directionnal list structure based on stacks
  41. class StackedList{
  42. public:
  43. private:
  44. //! Fill the current StackedList with ann arry of size s
  45. void fill(NData* array,size_t s);
  46. //Create a new node for the Stacked lisy
  47. //\return index of the new created node
  48. NInd new_node();
  49. public:
  50. //! Size of the stacked list 0 for empty
  51. //! Attention, there is exactly size+1 nodes used in the list
  52. size_t size;
  53. //! An array of Node used or not in the list
  54. StackedListNode nodes[SIZE];
  55. //! Indices of all available nodes
  56. NInd indices[SIZE];
  57. //! The empty constructor
  58. StackedList();
  59. //! Construct a StackedList from an array of size s
  60. StackedList(NData* array,size_t s);
  61. //! Clear the current StackedList
  62. void clear();
  63. //! Erase the node i in the list
  64. void erase(NInd i);
  65. //! Return first data of the list
  66. NData first();
  67. //! Show all information of the nodes
  68. void full_show() const;
  69. //! Init the current StackedList to be of size s
  70. void init(size_t s);
  71. //! Init the current StackedList with an array of size s
  72. void init(NData* array,size_t s);
  73. //! Insert a new node with data before the node i
  74. //! \return index of the newly inserted node
  75. NInd insert_before(NInd i,NData data);
  76. //! Insert a new node with data aftre the node i
  77. //! \return index of the newly inserted node
  78. NInd insert_after(NInd i,NData data);
  79. //! Test if the StackedList is empty
  80. bool is_empty() const;
  81. //! Return last data of the list
  82. NData last();
  83. };
  84. //***********************
  85. //* Auxiliary Functions *
  86. //***********************
  87. ostream& operator<<(ostream& os,const StackedList&);
  88. //**********************
  89. //* Inline definitions *
  90. //**********************
  91. //-------------------
  92. // StackedList::Node
  93. //-------------------
  94. inline
  95. StackedListNode::StackedListNode(){}
  96. //-------------
  97. // StackedList
  98. //-------------
  99. inline
  100. StackedList::StackedList(){
  101. clear();
  102. nodes[0].data=0;
  103. nodes[0].previous=0;
  104. nodes[0].next=0;
  105. }
  106. inline
  107. StackedList::StackedList(NData* array,size_t s){
  108. clear();
  109. fill(array,s);
  110. }
  111. inline void
  112. StackedList::clear(){
  113. size=0;
  114. for(size_t i=0;i<SIZE;++i) indices[i]=i;
  115. nodes[0].previous=0;
  116. nodes[0].next=0;
  117. }
  118. inline void
  119. StackedList::init(NData* array,size_t s){
  120. clear();
  121. fill(array,s);
  122. }
  123. inline bool
  124. StackedList::is_empty() const{
  125. return size==0;
  126. }
  127. inline NData
  128. StackedList::first(){
  129. return nodes[nodes[0].next].data;
  130. }
  131. inline NData
  132. StackedList::last(){
  133. return nodes[nodes[0].previous].data;
  134. }
  135. inline NInd
  136. StackedList::new_node(){
  137. return indices[++size];
  138. }
  139. #endif