stacked_list.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include "stacked_list.hpp"
  20. //***************
  21. //* StackedList *
  22. //****************
  23. //--------------------------
  24. // StackedList::erase(NInd)
  25. //--------------------------
  26. void StackedList::erase(NInd i){
  27. NInd p=nodes[i].previous;
  28. NInd n=nodes[i].next;
  29. nodes[p].next=n;
  30. nodes[n].previous=p;
  31. indices[size--]=i;
  32. }
  33. //----------------------------------
  34. // StackedList::fill(NData*,size_t)
  35. //----------------------------------
  36. void
  37. StackedList::fill(NData* array,size_t s){
  38. size=s;
  39. nodes[0].data=0;
  40. nodes[0].previous=s;
  41. nodes[0].next=1;
  42. for(size_t ind=0;ind<s;++ind){
  43. nodes[ind+1].previous=ind;
  44. nodes[ind+1].next=ind+2;
  45. nodes[ind+1].data=array[ind];
  46. }
  47. nodes[s].next=0;
  48. }
  49. //---------------------------
  50. // StackedList::init(size_t)
  51. //--------------------------
  52. void
  53. StackedList::init(size_t s){
  54. clear();
  55. size=s;
  56. nodes[0].previous=s;
  57. nodes[0].next=1;
  58. for(size_t ind=0;ind<s;++ind){
  59. nodes[ind+1].previous=ind;
  60. nodes[ind+1].next=ind+2;
  61. }
  62. nodes[s].next=0;
  63. }
  64. //----------------------------------------
  65. // StackedtList::insert_after(NInd,NData)
  66. //----------------------------------------
  67. NInd
  68. StackedList::insert_after(NInd i,NData data){
  69. // p <-> i <-> n becomes p <-> i <-> j <-> n
  70. NInd j=new_node();
  71. NInd n=nodes[i].next;
  72. nodes[j].previous=i;
  73. nodes[j].next=n;
  74. nodes[j].data=data;
  75. nodes[i].next=j;
  76. nodes[n].previous=j;
  77. return j;
  78. }
  79. //----------------------------------------
  80. // StackedList::insert_before(NInd,Ndata)
  81. //----------------------------------------
  82. NInd
  83. StackedList::insert_before(NInd i,NData data){
  84. //p <-> i <-> n becomes p <-> j <-> i <-> n
  85. NInd j=new_node();
  86. NInd p=nodes[i].previous;
  87. nodes[j].previous=p;
  88. nodes[j].next=i;
  89. nodes[j].data=data;
  90. nodes[i].previous=j;
  91. nodes[p].next=j;
  92. return j;
  93. }
  94. void
  95. StackedList::full_show() const{
  96. NInd ind=0;
  97. size_t i=0;
  98. while(i<10){
  99. cout<<"@"<<ind<<" : "<<nodes[ind].previous<<"<- "<<nodes[ind].data<<" -> "<<nodes[ind].next<<endl;
  100. ind=nodes[ind].next;
  101. if(ind==0) return;
  102. ++i;
  103. }
  104. cout<<"@"<<ind<<" : "<<nodes[ind].previous<<"<- "<<nodes[ind].data<<" -> "<<nodes[ind].next<<endl;
  105. }
  106. //***********************
  107. //* Auxiliary Functions *
  108. //***********************
  109. ostream& operator<<(ostream& os,const StackedList& list){
  110. os<<" Stack =";
  111. for(size_t i=0;i<=list.size;++i) os<<' '<<list.indices[i];
  112. os<<'#';
  113. for(size_t i=list.size+1;i<=20;++i) os<<' '<<list.indices[i];
  114. os<<endl;
  115. if(list.is_empty()) return os<<"()";
  116. NInd ind=list.nodes[0].next;
  117. os<<'('<<list.nodes[ind].data;
  118. size_t i=0;
  119. while((ind=list.nodes[ind].next)!=0){
  120. os<<','<<list.nodes[ind].data;
  121. ++i;
  122. if(i>10) return os<<"...)";
  123. }
  124. return os<<')';
  125. };