2
0

nstate.pas 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. {
  2. Copyright (c) 1998-2002 by Daniel Mantione
  3. This unit contains support routines for the state tracker
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nstate;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses cclasses,node;
  21. type Tstate_entry=class(Tlinkedlistitem)
  22. what:Tnode;
  23. value:Tnode;
  24. constructor create(w,v:Tnode);
  25. end;
  26. Tstate_storage=class
  27. storage:Tlinkedlist;
  28. constructor create;
  29. procedure store_fact(w,v:Tnode);
  30. function find_fact(what:Tnode):Tnode;
  31. procedure delete_fact(what:Tnode);
  32. end;
  33. var aktstate:Tstate_storage;
  34. implementation
  35. constructor Tstate_entry.create(w,v:Tnode);
  36. begin
  37. inherited create;
  38. what:=w;
  39. value:=v;
  40. end;
  41. constructor Tstate_storage.create;
  42. begin
  43. storage:=Tlinkedlist.create;
  44. end;
  45. procedure Tstate_storage.store_fact(w,v:Tnode);
  46. var se:Tstate_entry;
  47. begin
  48. { writeln('fact:');
  49. writenode(w);
  50. writeln('=');
  51. writenode(v);}
  52. se:=Tstate_entry(storage.first);
  53. while assigned(se) do
  54. begin
  55. if se.what.isequal(w) then
  56. begin
  57. storage.remove(se);
  58. se.destroy;
  59. break;
  60. end;
  61. se:=Tstate_entry(se.next);
  62. end;
  63. se:=Tstate_entry.create(w,v);
  64. storage.concat(se);
  65. end;
  66. function Tstate_storage.find_fact(what:Tnode):Tnode;
  67. var se:Tstate_entry;
  68. begin
  69. find_fact:=nil;
  70. se:=storage.first as Tstate_entry;
  71. while assigned(se) do
  72. begin
  73. if se.what.isequal(what) then
  74. begin
  75. find_fact:=se.value;
  76. break;
  77. end;
  78. se:=se.next as Tstate_entry;
  79. end;
  80. end;
  81. procedure Tstate_storage.delete_fact(what:Tnode);
  82. var se:Tstate_entry;
  83. begin
  84. se:=storage.first as Tstate_entry;
  85. while assigned(se) do
  86. begin
  87. if se.what.isequal(what) then
  88. begin
  89. storage.remove(se);
  90. se.destroy;
  91. break;
  92. end;
  93. se:=se.next as Tstate_entry;
  94. end;
  95. end;
  96. end.