home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / GRAPH.ZIP / AI / SRC / NODES / ANDNODE.CPP next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  2.4 KB  |  142 lines

  1. #include "nodes.h"
  2.  
  3. /*                    AONODE_
  4.  
  5.     This constructor just initializes the node, it doesn't create
  6.     room for any possible successors because at this point this
  7.     information is still unknown.
  8.  
  9. */
  10.  
  11. ANDNODE_::ANDNODE_()
  12.     :AONODE_(AND)
  13. {
  14.     n_nodes = 0;
  15.     succlist = NULL;    // succlist is an array of pointers to AONODE_'s
  16. }
  17.  
  18.  
  19.  
  20. /*                      AONODE_
  21.  
  22.     The alternative constructor creates room for n-successors, i.e,
  23.     it declares an array of n-pointers to AONODE_'s and updates
  24.     n_nodes and n_left.
  25.  
  26. */
  27.  
  28. ANDNODE_::ANDNODE_(int num)
  29.     :AONODE_(AND, num)
  30. {
  31.    n_nodes = num;
  32.    if (!(succlist = (AONODE_ **) malloc(num * sizeof(AONODE_ *))))
  33.    {
  34.     puts("Error: out of memory!");
  35.     exit(0);
  36.    }
  37. }
  38.  
  39.  
  40.  
  41. /*            ~ANDNODE_
  42.  
  43.     The destructor deallocates the array of node pointers.
  44.  
  45. */
  46.  
  47. ANDNODE_::~ANDNODE_()
  48. {
  49.     delete [] succlist;   // but don't destroy the nodes themselves!
  50. }
  51.  
  52.  
  53.  
  54. /*                       ADDSUCC
  55.  
  56.     Adds a node to the successor list of this node and increases
  57.     n_nodes and n_left.
  58.  
  59. */
  60.  
  61. void ANDNODE_::addsucc(AONODE_ *node)
  62. {
  63.     if (!(succlist = (AONODE_ **) realloc(succlist, (n_nodes + 1) *
  64.                     sizeof(AONODE_ *))))
  65.     {
  66.     puts("Error: out of memory");
  67.     exit(0);
  68.     }
  69.     succlist[n_nodes] = node;
  70.     n_nodes++;
  71.     incn_left();
  72. }
  73.  
  74.  
  75. /*            SETSUCC
  76.  
  77.     Just like addsucc, this routine adds a node to the successor list of
  78.     this node, except that it is assumed that memory has been allocated
  79.     in the array for the node pointer.
  80.  
  81. */
  82.  
  83. void ANDNODE_::setsucc(int index, AONODE_ *node)
  84. {
  85.     succlist[index] = node;
  86. }
  87.  
  88.  
  89.  
  90. /*            GETN_NODES
  91.  
  92.     Returns the number of successors of this node.
  93.  
  94. */
  95.  
  96. int ANDNODE_::getn_nodes() const
  97. {
  98.     return(n_nodes);
  99. }
  100.  
  101.  
  102.  
  103. /*            GETSUCC
  104.  
  105.     Returns successor n of this node.
  106.  
  107. */
  108.  
  109. AONODE_ *ANDNODE_::getsucc(int index) const
  110. {
  111.     return(succlist[index]);
  112. }
  113.  
  114.  
  115.  
  116. /*                    DISPLAY
  117.  
  118.     This function should never be called since ANDNODE_ only has
  119.     'administrative' value. Still, it needs to be implemented as
  120.     display() is pure virtual in NODE_, from which ANDNODE_ is
  121.     ultimately derived.
  122.  
  123. */
  124.  
  125. void ANDNODE_::display() const
  126. {
  127.     puts("no display() function for class ANDNODE_");
  128. }
  129.  
  130.  
  131.  
  132. /*        EQUAL
  133.  
  134.     Just like display equal() also needs to be implemented.
  135.  
  136. */
  137.  
  138. int ANDNODE_::equal(const VOBJECT_ &) const
  139. {
  140.     return(0);
  141. }
  142.