Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

FCSStateStorage.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include "FCSStateStorage.h"
00012 #include "AVLTree.h"
00013 #include "GLIBTree.h"
00014 #include "RedBlackTree.h"
00015 #include "AVLRedBlackTree.h"
00016 #include "SearchAlgorithms.h"
00017 #include "FCHelpingAlgorithms.h"
00018 #include "HashAlgorithms.h"
00019 #include "FCState.h"
00020 #include "FCSIndirectStateWithLocations.h"
00021 
00022 AFCSGenericStateStorage::~AFCSGenericStateStorage()
00023 {
00024 }
00025 
00026 FCTreeStateStorage::FCTreeStateStorage()
00027 {
00028     m_Tree = NULL;
00029 }
00030 
00031 FCTreeStateStorage::~FCTreeStateStorage()
00032 {
00033     if (m_Tree != NULL)
00034     {
00035         m_Tree->DeleteTree();
00036         delete m_Tree;
00037     }
00038 }
00039 
00040 bool FCTreeStateStorage::CheckAndInsert(FCSStateWithLocations** ExistingState, FCSStateWithLocations* NewState)
00041 {
00042     bool IsInsert;
00043     (*ExistingState) = m_Tree->Search(NewState, &IsInsert);
00044     return IsInsert;
00045 }
00046 
00047 FCAVLTreeStateStorage::FCAVLTreeStateStorage(ACompareNodesAlgorithm<FCSStateWithLocations, void>* Compare)
00048 {
00049     m_Tree = new AVLTree<FCSStateWithLocations, void>(Compare);
00050 }
00051 
00052 FCAVLTreeStateStorage::~FCAVLTreeStateStorage()
00053 {
00054 }
00055 
00056 FCAVLRedBlackTreeStateStorage::FCAVLRedBlackTreeStateStorage(ACompareNodesAlgorithm<FCSStateWithLocations, void>* Compare)
00057 {
00058     m_Tree = new AVLRedBlackTree<FCSStateWithLocations, void>(Compare);
00059 }
00060 
00061 FCAVLRedBlackTreeStateStorage::~FCAVLRedBlackTreeStateStorage()
00062 {
00063 }
00064 
00065 FCRedBlackTreeStateStorage::FCRedBlackTreeStateStorage(ACompareNodesAlgorithm<FCSStateWithLocations, void>* Compare)
00066 {
00067     m_Tree = new RedBlackTree<FCSStateWithLocations, void>(Compare);
00068 }
00069 
00070 FCRedBlackTreeStateStorage::~FCRedBlackTreeStateStorage()
00071 {
00072 }
00073 
00074 FCGLIBTreeStateStorage::FCGLIBTreeStateStorage(ACompareNodesAlgorithm<FCSStateWithLocations, void>* Compare)
00075 {
00076     m_Tree = new GLIBTree<FCSStateWithLocations, void>(Compare);
00077 }
00078 
00079 FCGLIBTreeStateStorage::~FCGLIBTreeStateStorage()
00080 {
00081 }
00082 
00083 FCHashStateStorage::FCHashStateStorage()
00084 {
00085     m_Hash = NULL;
00086 }
00087 
00088 FCHashStateStorage::~FCHashStateStorage()
00089 {
00090     if (m_Hash != NULL)
00091     {
00092         m_Hash->DeleteItems();
00093         delete m_Hash;
00094     }
00095 }
00096 
00097 bool FCHashStateStorage::CheckAndInsert(FCSStateWithLocations** ExistingState, FCSStateWithLocations* NewState)
00098 {
00099     *ExistingState = m_Hash->Insert(NewState, true);
00100 
00101     if (*ExistingState == NULL)
00102     {
00103         // The new state was found.  Already inserted
00104         return true;
00105     }
00106 
00107     return false;
00108 }
00109 
00110 FCGLIBHashStateStorage::FCGLIBHashStateStorage(int SizeWanted, ACompareNodesAlgorithm<FCSStateWithLocations, void>* Compare, 
00111                                      AHashAlgorithm<FCSStateWithLocations>* Hash)
00112 {
00113     m_Hash = new GLIBHash<FCSStateWithLocations, void>(SizeWanted, Compare, Hash);
00114 }
00115 
00116 FCGLIBHashStateStorage::~FCGLIBHashStateStorage()
00117 {
00118 }
00119 
00120 FCInternalHashStateStorage::FCInternalHashStateStorage(int SizeWanted, ACompareNodesAlgorithm<FCSStateWithLocations, void>* CompareAlgorithm, 
00121                                              AHashAlgorithm<FCSStateWithLocations>* Hash)
00122 {
00123     m_Hash = new FCInternalHash<FCSStateWithLocations, void>(SizeWanted, CompareAlgorithm, Hash);
00124 }
00125 
00126 FCInternalHashStateStorage::~FCInternalHashStateStorage()
00127 {
00128 }
00129 
00130 FCIndirectStateStorage::FCIndirectStateStorage()
00131 {
00132     m_NumberOfPreviousStatesMargin = 0;
00133     m_NumberOfIndirectPreviousStates = 0;
00134     m_MaxNumberOfIndirectPreviousStates = PREV_STATES_GROW_BY;
00135     m_IndirectPreviousStates = new FCSIndirectStateWithLocations<FCSStateWithLocations>*[m_MaxNumberOfIndirectPreviousStates];
00136     for (int a = 0;a<m_MaxNumberOfIndirectPreviousStates;a++)
00137         m_IndirectPreviousStates[a] = NULL;
00138 }
00139 
00140 FCIndirectStateStorage::~FCIndirectStateStorage()
00141 {
00142     delete [] m_IndirectPreviousStates;
00143 }
00144 
00145 /*
00146 bool FCIndirectStateStorage::CheckAndInsert(FCSStateWithLocations** ExistingState, FCSStateWithLocations* NewState)
00147 {
00148     FCSIndirectStateWithLocations<FCSStateWithLocations>** PosPtr;
00149     bool Found;
00150 
00151     //Try to see if the state is found in m_IndirectPreviousStates
00152     if ((PosPtr = BSearch<FCSIndirectStateWithLocations<FCSStateWithLocations>*>((FCSIndirectStateWithLocations<FCSStateWithLocations>**)&NewState, 
00153                                                         m_IndirectPreviousStates,
00154                                                         m_NumberOfIndirectPreviousStates, 
00155                                                         &m_Compare)) == NULL)
00156     {
00157         //It isn't in the PreviousStates, but maybe it's in the sort margin
00158         PosPtr = FreecellSolverBSearch<FCSIndirectStateWithLocations<FCSStateWithLocations>*>((FCSIndirectStateWithLocations<FCSStateWithLocations>**)&NewState, 
00159                                                                 m_IndirectPreviousStatesMargin,
00160                                                                 m_NumberOfPreviousStatesMargin, &m_Compare,
00161                                                                 &Found);
00162 
00163         if (Found)
00164         {
00165             *ExistingState = *PosPtr;
00166             return false;
00167         }
00168 
00169         //Insert the state into its corresponding place in the sort margin
00170         memmove((PosPtr+1), PosPtr, sizeof(FCSIndirectStateWithLocations<FCSStateWithLocations>*) * 
00171                     (m_NumberOfPreviousStatesMargin - (PosPtr - m_IndirectPreviousStatesMargin)));
00172         *PosPtr = (FCSIndirectStateWithLocations<FCSStateWithLocations>*)NewState;
00173 
00174         m_NumberOfPreviousStatesMargin++;
00175 
00176         if (m_NumberOfPreviousStatesMargin >= PREV_STATES_SORT_MARGIN)
00177         {
00178             //The sort margin is full, let's combine it with the main array
00179             if (m_NumberOfIndirectPreviousStates + m_NumberOfPreviousStatesMargin > 
00180                 m_MaxNumberOfIndirectPreviousStates)
00181             {
00182                 while (m_NumberOfIndirectPreviousStates + m_NumberOfPreviousStatesMargin > 
00183                         m_MaxNumberOfIndirectPreviousStates)
00184                 {
00185                     m_MaxNumberOfIndirectPreviousStates += PREV_STATES_GROW_BY;
00186                 }
00187 
00188                 Realloc<FCSIndirectStateWithLocations<FCSStateWithLocations>*>(&m_IndirectPreviousStates,
00189                                     m_NumberOfIndirectPreviousStates+1, m_MaxNumberOfIndirectPreviousStates);
00190             }
00191 
00192             SFOMergeLargeAndSmallSortedArrays<FCSIndirectStateWithLocations<FCSStateWithLocations>*>(
00193                     m_IndirectPreviousStates, m_NumberOfIndirectPreviousStates,
00194                     m_IndirectPreviousStatesMargin, m_NumberOfPreviousStatesMargin,
00195                     &m_Compare);
00196 
00197             m_NumberOfIndirectPreviousStates += m_NumberOfPreviousStatesMargin;
00198             m_NumberOfPreviousStatesMargin = 0;
00199         }
00200 
00201         return true;
00202     }
00203 
00204     *ExistingState = *PosPtr;
00205     return false;
00206 }
00207 */
00208 
00209 
00210 bool FCIndirectStateStorage::CheckAndInsert(FCSStateWithLocations** ExistingState, FCSStateWithLocations* NewState)
00211 {
00212     FCSIndirectStateWithLocations<FCSStateWithLocations>** PosPtr;
00213     bool Found;
00214 
00215     //Try to see if the state is found in m_IndirectPreviousStates
00216     PosPtr = FreecellSolverBSearch<FCSIndirectStateWithLocations<FCSStateWithLocations>*>((FCSIndirectStateWithLocations<FCSStateWithLocations>**)&NewState,
00217                                                         m_IndirectPreviousStates,
00218                                                         m_NumberOfIndirectPreviousStates, 
00219                                                         &m_Compare, &Found);
00220 
00221     if (!Found)
00222     {
00223         //It isn't in the PreviousStates, but maybe it's in the sort margin
00224         PosPtr = FreecellSolverBSearch<FCSIndirectStateWithLocations<FCSStateWithLocations>*>((FCSIndirectStateWithLocations<FCSStateWithLocations>**)&NewState, 
00225                                                                 m_IndirectPreviousStatesMargin,
00226                                                                 m_NumberOfPreviousStatesMargin, &m_Compare,
00227                                                                 &Found);
00228 
00229         if (Found)
00230         {
00231             *ExistingState = *PosPtr;
00232             return false;
00233         }
00234 
00235         //Insert the state into its corresponding place in the sort margin
00236         memmove((PosPtr+1), PosPtr, sizeof(FCSIndirectStateWithLocations<FCSStateWithLocations>*) * 
00237                     (m_NumberOfPreviousStatesMargin - (PosPtr - m_IndirectPreviousStatesMargin)));
00238 
00239         *PosPtr = (FCSIndirectStateWithLocations<FCSStateWithLocations>*)NewState;
00240 
00241         m_NumberOfPreviousStatesMargin++;
00242 
00243         if (m_NumberOfPreviousStatesMargin >= PREV_STATES_SORT_MARGIN)
00244         {
00245             //The sort margin is full, let's combine it with the main array
00246             if (m_NumberOfIndirectPreviousStates + m_NumberOfPreviousStatesMargin > 
00247                 m_MaxNumberOfIndirectPreviousStates)
00248             {
00249                 while (m_NumberOfIndirectPreviousStates + m_NumberOfPreviousStatesMargin > 
00250                         m_MaxNumberOfIndirectPreviousStates)
00251                 {
00252                     m_MaxNumberOfIndirectPreviousStates += PREV_STATES_GROW_BY;
00253                 }
00254 
00255                 Realloc<FCSIndirectStateWithLocations<FCSStateWithLocations>*>(&m_IndirectPreviousStates,
00256                                     m_NumberOfIndirectPreviousStates, m_MaxNumberOfIndirectPreviousStates);
00257             }
00258 
00259             SFOMergeLargeAndSmallSortedArrays<FCSIndirectStateWithLocations<FCSStateWithLocations>*>(
00260                     m_IndirectPreviousStates, m_NumberOfIndirectPreviousStates,
00261                     m_IndirectPreviousStatesMargin, m_NumberOfPreviousStatesMargin,
00262                     &m_Compare);
00263 
00264             m_NumberOfIndirectPreviousStates += m_NumberOfPreviousStatesMargin;
00265             m_NumberOfPreviousStatesMargin = 0;
00266         }
00267 
00268         return true;
00269     }
00270 
00271     *ExistingState = *PosPtr;
00272     return false;
00273 }
00274 

Generated on Sat Nov 5 11:20:15 2005 for Cpp Freecell Solver by  doxygen 1.4.4