Commits

Shlomi Fish committed bd1061e

More cleanups and made move_to_string common.

Comments (0)

Files changed (7)

fc-solve/source/dbm_bdb.c

 {
     dbm_t * db = SMALLOC1(db);
 
-    int t_ret, ret;
+    int ret;
 
     if ((ret = db_create(&(db->dbp), NULL, 0)) != 0)
     {
     return;
 
     err:
+    int t_ret;
     if ((t_ret = db->dbp->close(db->dbp, 0)) != 0 && ret == 0)
     {
         ret = t_ret;
     const unsigned char * key_raw
 )
 {
-    dbm_t * db;
-    int ret, t_ret;
     unsigned char dummy[100];
 
-    db = (dbm_t *)store;
+    dbm_t * db = (dbm_t *)store;
     db->key.data = (unsigned char *)key_raw+1;
     db->key.size = key_raw[0];
     db->data.data = dummy;
     db->data.size = sizeof(dummy);
 
+    int ret;
     if ((ret = db->dbp->get(db->dbp, NULL, &(db->key), &(db->data), 0)) == 0)
     {
         return TRUE;
     }
     else
     {
+        int t_ret;
         db->dbp->err(db->dbp, ret, "DB->get");
         if ((t_ret = db->dbp->close(db->dbp, 0)) != 0 && ret == 0)
         {
     unsigned char * parent
 )
 {
-    dbm_t * db;
-    int ret, t_ret;
-
-    db = (dbm_t *)store;
+    dbm_t * db = (dbm_t *)store;
     db->key.data = (unsigned char *)key_raw+1;
     db->key.size = key_raw[0];
     db->data.data = parent+1;
     db->data.size = sizeof(fcs_encoded_state_buffer_t)-1;
 
+    int ret;
     if ((ret = db->dbp->get(db->dbp, NULL, &(db->key), &(db->data), 0)) == 0)
     {
         parent[0] = db->data.size-1;
     else
     {
         db->dbp->err(db->dbp, ret, "DB->get");
-        if ((t_ret = db->dbp->close(db->dbp, 0)) != 0 && ret == 0)
+        if ((int t_ret = db->dbp->close(db->dbp, 0)) != 0 && ret == 0)
         {
             ret = t_ret;
         }
     fcs_pre_cache_t * pre_cache
 )
 {
-    dbm_t * db;
-    dnode_t * node;
-    dict_t * kaz_tree;
-    fcs_pre_cache_key_val_pair_t * kv;
-    DB * dbp;
-    int ret, t_ret;
-
-    db = (dbm_t *)store;
-    kaz_tree = pre_cache->kaz_tree;
-    dbp = db->dbp;
+    dbm_t * const db = (dbm_t *)store;
+    dict_t * const kaz_tree = pre_cache->kaz_tree;
+    DB * const dbp = db->dbp;
 
-    for (node = fc_solve_kaz_tree_first(kaz_tree);
+    for (dnode_t * node = fc_solve_kaz_tree_first(kaz_tree);
             node ;
             node = fc_solve_kaz_tree_next(kaz_tree, node)
             )
     {
-        kv = (fcs_pre_cache_key_val_pair_t *)(node->dict_key);
+        fcs_pre_cache_key_val_pair_t * const kv =
+            (fcs_pre_cache_key_val_pair_t *)(node->dict_key);
 
         db->key.data = kv->key.s+1;
         db->key.size = kv->key.s[0];
         db->data.data = kv->parent.s+1;
         db->data.size = kv->parent.s[0];
-        if ((ret = dbp->put(dbp, NULL, &(db->key), &(db->data), 0)) != 0)
+        if ((int ret = dbp->put(dbp, NULL, &(db->key), &(db->data), 0)) != 0)
         {
             dbp->err(dbp, ret, "DB->put");
-            if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
+            if ((int t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
             {
                 ret = t_ret;
             }
-            exit(ret);
+            exit (ret);
         }
     }
 }
 
 extern void fc_solve_dbm_store_destroy(fcs_dbm_store_t store)
 {
-    dbm_t * db;
-    db = (dbm_t *)store;
-    int ret;
-
-    if ((ret = db->dbp->close(db->dbp, 0)) != 0)
+    dbm_t * const db = (dbm_t *)store;
+    if ((int ret = db->dbp->close(db->dbp, 0)) != 0)
     {
         fprintf(stderr, "DB close failed with ret=%d\n", ret);
         exit(-1);

fc-solve/source/dbm_kaztree.c

 
 void fc_solve_dbm_store_init(fcs_dbm_store_t * store, const char * path, void * * recycle_bin_ptr)
 {
-    dbm_t * db = SMALLOC1(db);
+    dbm_t * const db = SMALLOC1(db);
 
     fc_solve_meta_compact_allocator_init(
         &(db->meta_alloc)
     fcs_dbm_record_t * parent
 )
 {
-    dbm_t * db;
-    fcs_dbm_record_t * to_check;
-    fcs_bool_t ret;
 #ifdef FCS_LIBAVL_STORE_WHOLE_KEYS
     fcs_dbm_record_t record_on_stack;
 #endif
 
-    db = (dbm_t *)store;
+    dbm_t * const db = (dbm_t *)store;
 
+    fcs_dbm_record_t * to_check;
 #ifdef FCS_LIBAVL_STORE_WHOLE_KEYS
     to_check = &record_on_stack;
 #else
     to_check->key = *key;
     to_check->parent = *parent;
 #endif
-    ret = (fc_solve_kaz_tree_alloc_insert(db->kaz_tree, to_check) == NULL);
+    fcs_bool_t ret =
+        (fc_solve_kaz_tree_alloc_insert(db->kaz_tree, to_check) == NULL);
 
 #ifndef FCS_LIBAVL_STORE_WHOLE_KEYS
     if (! ret)
     unsigned char * parent
     )
 {
-    dict_key_t existing;
-
     fcs_dbm_record_t to_check;
     to_check.key = *(const fcs_encoded_state_buffer_t *)key;
 
-    existing = fc_solve_kaz_tree_lookup_value(((dbm_t *)store)->kaz_tree, &to_check);
+    dict_key_t existing = fc_solve_kaz_tree_lookup_value(((dbm_t *)store)->kaz_tree, &to_check);
 
     if (! existing)
     {
     else
     {
 #ifdef FCS_DBM_RECORD_POINTER_REPR
-        fcs_dbm_record_t * p = fcs_dbm_record_get_parent_ptr((fcs_dbm_record_t *)existing);
+        fcs_dbm_record_t * const p = fcs_dbm_record_get_parent_ptr((fcs_dbm_record_t *)existing);
 
         if (p)
         {
 
 extern void fc_solve_dbm_store_destroy(fcs_dbm_store_t store)
 {
-    dbm_t * db;
-    db = (dbm_t *)store;
+    dbm_t * const db = (dbm_t *)store;
 
     fc_solve_kaz_tree_destroy( db->kaz_tree );
 #ifndef FCS_LIBAVL_STORE_WHOLE_KEYS

fc-solve/source/dbm_move_to_string.h

+/* Copyright (c) 2012 Shlomi Fish
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+/*
+ * dbm_procs.h - common procedures for dbm_solver.c and depth_dbm_solver.c.
+ */
+#ifndef FC_SOLVE__DBM_MOVE_TO_STRING_H
+#define FC_SOLVE__DBM_MOVE_TO_STRING_H
+
+#include "inline.h"
+#include "portable_time.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+static const char * move_to_string(unsigned char move, char * const move_buffer)
+{
+    char * s = move_buffer;
+
+    for (int iter=0 ; iter < 2 ; iter++)
+    {
+        int inspect = (move & 0xF);
+        move >>= 4;
+
+        if (inspect < 8)
+        {
+            s += sprintf(s, "Column %d", inspect);
+        }
+        else
+        {
+            inspect -= 8;
+            if (inspect < 4)
+            {
+                s += sprintf(s, "Freecell %d", inspect);
+            }
+            else
+            {
+                inspect -= 4;
+                s += sprintf(s, "Foundation %d", inspect);
+            }
+        }
+        if (iter == 0)
+        {
+            s += sprintf(s, "%s", " -> ");
+        }
+    }
+
+    return move_buffer;
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*  FC_SOLVE__DBM_MOVE_TO_STRING_H */

fc-solve/source/dbm_procs.h

 
 #include "inline.h"
 #include "portable_time.h"
+#include "dbm_move_to_string.h"
 
 #ifdef __cplusplus
 extern "C"
     return;
 }
 
+
 #ifdef FCS_DBM_SINGLE_THREAD
 #define NUM_THREADS() 1
 #else

fc-solve/source/dbm_solver.c

 
 static void * instance_run_solver_thread(void * void_arg)
 {
-    thread_arg_t * arg;
-    enum TERMINATE_REASON should_terminate;
-    fcs_dbm_solver_thread_t * thread;
-    fcs_dbm_solver_instance_t * instance;
     fcs_dbm_queue_item_t physical_item;
-    fcs_dbm_queue_item_t * item, * prev_item;
-    int queue_num_extracted_and_processed;
-    fcs_derived_state_t * derived_list, * derived_list_recycle_bin,
-                        * derived_iter;
-    fcs_compact_allocator_t derived_list_allocator;
-    fc_solve_delta_stater_t * delta_stater;
     fcs_state_keyval_pair_t state;
-    FILE * out_fh;
 #ifdef DEBUG_OUT
     fcs_state_locs_struct_t locs;
 #endif
     fcs_dbm_record_t * token;
-    enum fcs_dbm_variant_type_t local_variant;
     DECLARE_IND_BUF_T(indirect_stacks_buffer)
 
-    arg = (thread_arg_t *)void_arg;
-    thread = arg->thread;
-    instance = thread->instance;
-    delta_stater = thread->delta_stater;
+    thread_arg_t * const arg = (thread_arg_t *)void_arg;
+    fcs_dbm_solver_thread_t * const thread = arg->thread;
+    fcs_dbm_solver_instance_t * const instance = thread->instance;
+    fc_solve_delta_stater_t * const delta_stater = thread->delta_stater;
 
-    prev_item = item = NULL;
-    queue_num_extracted_and_processed = 0;
+    fcs_dbm_queue_item_t * item = NULL, * prev_item = NULL;
+    int queue_num_extracted_and_processed = 0;
 
+    fcs_compact_allocator_t derived_list_allocator;
     fc_solve_compact_allocator_init(&(derived_list_allocator), &(instance->meta_alloc));
-    derived_list_recycle_bin = NULL;
-    derived_list = NULL;
-    out_fh = instance->out_fh;
+    fcs_derived_state_t * derived_list_recycle_bin = NULL;
+    fcs_derived_state_t * derived_list = NULL;
+    FILE * const out_fh = instance->out_fh;
 
-    local_variant = instance->variant;
+    enum fcs_dbm_variant_type_t local_variant = instance->variant;
 
     TRACE0("instance_run_solver_thread start");
 #ifdef DEBUG_OUT
     fc_solve_init_locs(&locs);
 #endif
+    enum TERMINATE_REASON should_terminate;
     while (1)
     {
         /* First of all extract an item. */
         }
 
         /* Encode all the states. */
-        for (derived_iter = derived_list;
+        for (fcs_derived_state_t * derived_iter = derived_list;
                 derived_iter ;
                 derived_iter = derived_iter->next
         )
         /* Now recycle the derived_list */
         while (derived_list)
         {
-#define derived_list_next derived_iter
-            derived_list_next = derived_list->next;
+            fcs_derived_state_t * const derived_list_next = derived_list->next;
             derived_list->next = derived_list_recycle_bin;
             derived_list_recycle_bin = derived_list;
             derived_list = derived_list_next;
-#undef derived_list_next
         }
         /* End handle item. */
         }
 
 #define USER_STATE_SIZE 2000
 
-static const char * move_to_string(unsigned char move, char * move_buffer)
-{
-    int iter, inspect;
-    char * s;
-
-    s = move_buffer;
-
-    for (iter=0 ; iter < 2 ; iter++)
-    {
-        inspect = (move & 0xF);
-        move >>= 4;
-
-        if (inspect < 8)
-        {
-            s += sprintf(s, "Column %d", inspect);
-        }
-        else
-        {
-            inspect -= 8;
-            if (inspect < 4)
-            {
-                s += sprintf(s, "Freecell %d", inspect);
-            }
-            else
-            {
-                inspect -= 4;
-                s += sprintf(s, "Foundation %d", inspect);
-            }
-        }
-        if (iter == 0)
-        {
-            s += sprintf(s, "%s", " -> ");
-        }
-    }
-
-    return move_buffer;
-}
-
 static void populate_instance_with_intermediate_input_line(
     fcs_dbm_solver_instance_t * instance,
     fc_solve_delta_stater_t * delta,

fc-solve/source/depth_dbm_solver.c

 
 #define USER_STATE_SIZE 2000
 
-static const char * move_to_string(unsigned char move, char * move_buffer)
-{
-    int iter, inspect;
-    char * s;
-
-    s = move_buffer;
-
-    for (iter=0 ; iter < 2 ; iter++)
-    {
-        inspect = (move & 0xF);
-        move >>= 4;
-
-        if (inspect < 8)
-        {
-            s += sprintf(s, "Column %d", inspect);
-        }
-        else
-        {
-            inspect -= 8;
-            if (inspect < 4)
-            {
-                s += sprintf(s, "Freecell %d", inspect);
-            }
-            else
-            {
-                inspect -= 4;
-                s += sprintf(s, "Foundation %d", inspect);
-            }
-        }
-        if (iter == 0)
-        {
-            s += sprintf(s, "%s", " -> ");
-        }
-    }
-
-    return move_buffer;
-}
-
 static void instance_run_all_threads(
     fcs_dbm_solver_instance_t * instance,
     fcs_state_keyval_pair_t * init_state,

fc-solve/source/fcc_solver.c

 
 #include "dbm_common.h"
 #include "dbm_solver.h"
+#include "dbm_move_to_string.h"
 
 #include "dbm_lru_cache.h"
 #include "fcc_brfs.h"
 
 #define USER_STATE_SIZE 2000
 
-static const char * move_to_string(unsigned char move, char * move_buffer)
-{
-    int iter, inspect;
-    char * s;
-
-    s = move_buffer;
-
-    for (iter=0 ; iter < 2 ; iter++)
-    {
-        inspect = (move & 0xF);
-        move >>= 4;
-
-        if (inspect < 8)
-        {
-            s += sprintf(s, "Column %d", inspect);
-        }
-        else
-        {
-            inspect -= 8;
-            if (inspect < 4)
-            {
-                s += sprintf(s, "Freecell %d", inspect);
-            }
-            else
-            {
-                inspect -= 4;
-                s += sprintf(s, "Foundation %d", inspect);
-            }
-        }
-        if (iter == 0)
-        {
-            s += sprintf(s, "%s", " -> ");
-        }
-    }
-
-    return move_buffer;
-}
-
 static fcs_dbm_solver_instance_t * global_instance_ptr;
 
 static void command_signal_handler(int signal_num GCC_UNUSED)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.