home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / SRC / PROPAGAT.CPP < prev    next >
C/C++ Source or Header  |  1996-07-08  |  5KB  |  166 lines

  1. /****************************************************************************
  2.     $Id: propagat.cpp 501.0 1995/03/07 12:26:20 RON Exp $
  3.  
  4.     Copyright (c) 1991-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:     Ron van der Wal
  8.  
  9.     Implementation of the TLPropagator class. The TLPropagator class
  10.     embodies the various constraint propagation algorithms.
  11.  
  12.     $Log: propagat.cpp $
  13.     Revision 501.0  1995/03/07 12:26:20  RON
  14.     Updated for TLX 5.01
  15.     Revision 1.11  1995/01/31 16:30:22  RON
  16.     Update for release 012
  17.     Added partial support for SunPro C++ compiler
  18.     Revision 1.10  1995/01/06  15:58:10  ron
  19.     Corrected Revision keyword
  20.  
  21.     Revision 1.9  1994/11/16  15:42:08  ron
  22.     Added module info; rearranged #include directives
  23.  
  24.     Revision 1.8  1994/10/10  16:53:33  ron
  25.     Changed to <tlx\solve\csp.h>
  26.  
  27.     Revision 1.7  1994/10/07  17:03:26  ron
  28.     Changed Unregister...() to Deregister...()
  29.  
  30.     Revision 1.6  1994/09/28  14:20:21  ron
  31.     Removed Macintosh-style #include references
  32.  
  33.     Revision 1.5  1994/09/27  20:22:45  ron
  34.     Changed path separator from / to \
  35.  
  36.     Revision 1.4  1994/09/26  15:45:13  ron
  37.     Implemented function to allow nesting of propagators
  38.  
  39.     Revision 1.3  1994/09/13  10:13:41  ron
  40.     Implemented support to remember the first constraint that failed
  41.     during a constraint propagation.
  42.  
  43.     Revision 1.2  1994/09/06  14:09:15  ron
  44.     Adapted to changes in CSP framework
  45.  
  46.     Revision 1.1  1994/08/16  18:13:08  ron
  47.     Initial revision
  48.  
  49. ****************************************************************************/
  50.  
  51. #include <tlx\501\_build.h>
  52.  
  53. TLX_MODULE_INFO("$Revision: 501.0 $");
  54.  
  55. #include <iostream.h>
  56. #include <tlx\501\solve\csp.h>
  57.  
  58. /*-------------------------------------------------------------------------*/
  59.     TLPropagator::TLPropagator()
  60.  
  61. /*  Constructor. Initializes the propagator.
  62. ---------------------------------------------------------------------------*/
  63. {
  64.     mPrevProp = 0;
  65.     mFailed   = 0;
  66.     ClearStats();
  67. }
  68.  
  69. /*-------------------------------------------------------------------------*/
  70.     TLPropagator::~TLPropagator()
  71.  
  72. /*  Destructor. Deregisters the propagator, in case it was still registered.
  73. ---------------------------------------------------------------------------*/
  74. {
  75.     DeregisterPropagator();
  76. }
  77.  
  78. /*-------------------------------------------------------------------------*/
  79.     void TLPropagator::ClearStats()
  80.  
  81. /*  Clears the statistics that are kept by the propagator.
  82. ---------------------------------------------------------------------------*/
  83. {
  84.     mStats.mCheckCount = 0;
  85.     mStats.mMaxPending = 0;
  86. }
  87.  
  88. /*-------------------------------------------------------------------------*/
  89.     bool TLPropagator::IsActive() const
  90.  
  91. /*  Returns true iff the propagator is currently registered with the
  92.     TLVariable class as the active (topmost) propagator.
  93. ---------------------------------------------------------------------------*/
  94. {
  95.     return this == TLVariable::sPropagator;
  96. }
  97.  
  98. /*-------------------------------------------------------------------------*/
  99.     bool TLPropagator::IsRegistered() const
  100.  
  101. /*  Returns true iff the propagator is currently registered with the
  102.     TLVariable class as a propagator (not necessarily the topmost).
  103. ---------------------------------------------------------------------------*/
  104. {
  105.     for (TLPropagator *prop = TLVariable::sPropagator; prop;
  106.      prop = prop->mPrevProp)
  107.     {
  108.     if (prop == this)
  109.         return true;
  110.     }
  111.     return false;
  112. }
  113.  
  114. /*-------------------------------------------------------------------------*/
  115.     void TLPropagator::RegisterPropagator()
  116.  
  117. /*  Registers the current propagator as the active propagator. It is an
  118.     error to register a propagator while another one is still registered.
  119. ---------------------------------------------------------------------------*/
  120. {
  121.     TLX_ASSERT(!IsRegistered());
  122.     mPrevProp = TLVariable::sPropagator;
  123.     TLVariable::sPropagator = this;
  124.     TLX_ASSERT(IsRegistered());
  125. }
  126.  
  127. /*-------------------------------------------------------------------------*/
  128.     void TLPropagator::SetFailedConstraint(TLConstraint *aConstraint)
  129.  
  130. /*  Stores the given constraint pointer (which may be 0) as the constraint
  131.     that failed during constraint propagation.
  132. ---------------------------------------------------------------------------*/
  133. {
  134.     mFailed = aConstraint;
  135. }
  136.  
  137. /*-------------------------------------------------------------------------*/
  138.     void TLPropagator::DeregisterPropagator()
  139.  
  140. /*  Deregisters the current propagator with the TLVariable class. It is
  141.     an error to all this routine if the current propagator isn't the
  142.     registered one.
  143. ---------------------------------------------------------------------------*/
  144. {
  145.     if (this == TLVariable::sPropagator)
  146.     {
  147.     // Topmost; just replace by previous monitor
  148.  
  149.     TLVariable::sPropagator = mPrevProp;
  150.     mPrevProp         = 0;
  151.     }
  152.     else
  153.     {
  154.         for (TLPropagator *prop = TLVariable::sPropagator; prop;
  155.          prop = prop->mPrevProp)
  156.     {
  157.         if (prop->mPrevProp == this)
  158.         {
  159.         prop->mPrevProp = mPrevProp;
  160.         mPrevProp     = 0;
  161.         break;
  162.         }
  163.     }
  164.     }
  165. }
  166.