home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 02 Useful Techniques / 08 Zarozinski / src / ffllapi / RuleArray.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-16  |  4.6 KB  |  253 lines

  1.  
  2. //
  3. // File:    RuleArray.cpp
  4. //
  5. // Purpose:    Implementation of the RuleArray class.  This class represents
  6. //            the rules associated with a set of variables.
  7. //
  8. // Copyright ⌐ 1999-2001 Louder Than A Bomb! Software
  9. //
  10. // This file is part of the FFLL (Free Fuzzy Logic Library) project (http://ffll.sourceforge.net)
  11. // It is released under the BSD license, see http://ffll.sourceforge.net/license.txt for the full text.
  12. //
  13.  
  14. #include "RuleArray.h"
  15.  
  16. #ifdef _DEBUG  
  17. #undef THIS_FILE
  18. static char THIS_FILE[]=__FILE__;
  19. #endif
  20.  
  21. //
  22. // Function: RuleArray()
  23. // 
  24. // Purpose:    default constructor 
  25. // 
  26. // Arguments:
  27. //
  28. //        FuzzyModelBase* _parent - model that these rules belong to
  29. // 
  30. // Returns:
  31. //
  32. //        nothing
  33. // 
  34. // Author:    Michael Zarozinski    
  35. // Date:    6/99
  36. // 
  37. // Modification History
  38. // Author        Date        Modification
  39. // ------        ----        ------------
  40. //
  41. //
  42. RuleArray::RuleArray(FuzzyModelBase* _parent) :  FFLLBase(_parent)
  43. {
  44.     rules = NULL; 
  45.     max = 0;
  46.  
  47. }; // end RuleArray::RuleArray()
  48.  
  49.  
  50. //
  51. // Function: ~RuleArray()
  52. // 
  53. // Purpose:    default destructor 
  54. // 
  55. // Arguments:
  56. //
  57. //         none
  58. //
  59. // Returns:
  60. //
  61. //        nothing
  62. // 
  63. // Author:    Michael Zarozinski    
  64. // Date:    6/99
  65. // 
  66. // Modification History
  67. // Author        Date        Modification
  68. // ------        ----        ------------
  69. //
  70. //
  71. RuleArray::~RuleArray()
  72. {
  73.     free_memory();
  74. };
  75.  
  76. //
  77. // Function:    alloc()
  78. // 
  79. // Purpose:        This function allocates memory for the rule array
  80. // 
  81. // Arguments:
  82. //
  83. //            int    size    -    how many rules to allocate space for
  84. // 
  85. // Returns:
  86. //
  87. //            0 - success
  88. //            non-zero - failure
  89. //
  90. // Author:    Michael Zarozinski
  91. // Date:    7/8/99
  92. // 
  93. // Modification History
  94. // Author        Date        Modification
  95. // ------        ----        ------------
  96. //
  97. int RuleArray::alloc(int size) 
  98. {
  99.  
  100.     // if we have rules, free the memory
  101.     if (rules != NULL)
  102.         {
  103.         free_memory();
  104.         }
  105.  
  106.     // 0 size is OK - we may not have any terms yet
  107.     if (size <= 0)
  108.         {
  109.         max = 0;
  110.         return 0;
  111.         } 
  112.  
  113.     rules = new RuleArrayType[size]; 
  114.  
  115.     if (rules == NULL)
  116.         {
  117.         assert(rules != NULL);
  118.         set_msg_text(ERR_ALLOC_MEM);
  119.         return -1;
  120.         }
  121.  
  122.     // set the size of the array
  123.     max = size;
  124.  
  125.     // init the memory...
  126.     clear();
  127.  
  128.     return 0;
  129.  
  130. } // end RuleArray::alloc()
  131.  
  132. //
  133. // Function:    set()
  134. // 
  135. // Purpose:        This function sets the rules array equal to the array passed in.
  136. // 
  137. // Arguments:
  138. //
  139. //            RuleArrayType*    source    -    source data to copy
  140. //            int                size    -    how many rules to copy.  NOTE this is NOT the total size
  141. //                                        of the memory, it's the number of elements in the rules array
  142. // 
  143. // Returns:
  144. //
  145. //            0 - success
  146. //            non-zero - failure
  147. //
  148. // Author:    Michael Zarozinski
  149. // Date:    7/8/99
  150. // 
  151. // Modification History
  152. // Author        Date        Modification
  153. // ------        ----        ------------
  154. //
  155.  
  156. int RuleArray::set(RuleArrayType* source, int size) 
  157. {
  158.  
  159.     // allocate the rules (this deletes any existing memory)
  160.     alloc(size);
  161.  
  162.     // copy the rules passed in
  163.     memcpy(rules, source, sizeof(RuleArrayType) * size);
  164.      
  165.     return 0;
  166.  
  167. } // end RuleArray::set()
  168.  
  169.  
  170. //
  171. // Function:    no_rules()
  172. // 
  173. // Purpose:        This function tells us if we have any rules that have been
  174. //                defined for this model.
  175. // 
  176. // Arguments:
  177. //
  178. //            none
  179. // 
  180. // Returns:
  181. //
  182. //            true - if no rules are defined
  183. //            false - if we have at least one rule
  184. //
  185. // Author:    Michael Zarozinski
  186. // Date:    7/8/99
  187. // 
  188. // Modification History
  189. // Author        Date        Modification
  190. // ------        ----        ------------
  191. //
  192. bool RuleArray::no_rules() const 
  193. {
  194.     if (rules == NULL)
  195.         return true;
  196.  
  197.     // loop through and find out if there are ANY rules - we need this
  198.     // so we don't ask stuipid questions like "this will delete all rules" when
  199.     // there are NO rules
  200.     for (int i = 0; i < max; i++)
  201.         {
  202.         if (rules[i] != NO_RULE)
  203.             return  false;
  204.         }
  205.  
  206.     return true;
  207.     
  208. }; // end RuleArray::no_rules()
  209.  
  210. /////////////////////////////////////////////////////////////////////
  211. ////////// Trivial Functions That Don't Require Headers /////////////
  212. /////////////////////////////////////////////////////////////////////
  213.  
  214. void RuleArray::clear()
  215. {
  216.     memset(rules, NO_RULE, max * sizeof(RuleArrayType));
  217.  
  218. }; // end RuleArray::clear()
  219.  
  220. void RuleArray::free_memory()
  221. {
  222.     if (rules != NULL) 
  223.         delete[] rules;
  224.  
  225.     max = 0; 
  226.     rules = NULL;
  227. };
  228.  
  229. RuleArrayType RuleArray::get_rule(int index) const 
  230. {
  231.     assert(index < max);
  232.  
  233.     return(rules[index]);
  234. }; 
  235.  
  236. void RuleArray::add_rule(int index, RuleArrayType output_set)
  237. {
  238.     assert(index < max);
  239.  
  240.     rules[index] = (RuleArrayType)(output_set);
  241. }; 
  242.  
  243. int RuleArray::get_max() const
  244. {
  245.     return max;
  246. };
  247.  
  248. void RuleArray::remove_rule(int index)
  249. {
  250.     assert(index < max);
  251.  
  252.     rules[index] = NO_RULE;
  253. };