Commits

Shlomi Fish committed 16beda2

Convert to fcs_make_card.

Comments (0)

Files changed (7)

fc-solve/source/DONE.txt

 
 * Code a generic tests grouping.
 
+* Convert the card initializers to an fcs_make_card constructor instead
+of two separate set_rank and set_suit calls.
+

fc-solve/source/TODO.txt

 Non-pressing
 ------------
 
-* Convert the card initializers to an fcs_make_card constructor instead
-of two separate set_rank and set_suit calls.
-
 * See if GCC_INLINE-ing the functions inside fcs_hash.c will yield
 any benefit.
 

fc-solve/source/card.c

  * */
 fcs_card_t fc_solve_card_user2perl(const char * str)
 {
-    fcs_card_t card = fc_solve_empty_card;
-
 #ifndef FCS_WITHOUT_CARD_FLIPPING
     fcs_card_set_flipped(card, fcs_u2p_flipped_status(str));
 #endif
-    fcs_card_set_rank(card, fc_solve_u2p_rank(str));
-    fcs_card_set_suit(card, fc_solve_u2p_suit(str));
-
-    return card;
+    const int rank = fc_solve_u2p_rank(str);
+    const int suit = fc_solve_u2p_suit(str);
+    return fcs_make_card(rank, suit);
 }
 
 

fc-solve/source/delta_states.c

 
             for ( i = 0 ; i < num_cards_in_seq ; i++)
             {
-                fcs_card_t new_card;
-                int suit_bit;
-
-                new_card = fc_solve_empty_card;
-                suit_bit = fc_solve_bit_reader_read(bit_r, 1);
-
-                fcs_card_set_rank(new_card, fcs_card_rank(last_card)-1);
-                fcs_card_set_suit(new_card,
+                int suit_bit = fc_solve_bit_reader_read(bit_r, 1);
+                fcs_card_t new_card = fcs_make_card(
+                    fcs_card_rank(last_card)-1,
                     ((suit_bit << 1) |
-                        ((fcs_card_suit(last_card) & 0x1) ^ 0x1)
+                     ((fcs_card_suit(last_card) & 0x1) ^ 0x1)
                     )
                 );
 

fc-solve/source/delta_states_debondt.c

 
                 if (IS_BAKERS_DOZEN())
                 {
-                    fcs_card_t card = fc_solve_empty_card;
-                    fcs_card_set_rank(card, rank);
-                    fcs_card_set_suit(card, suit_idx);
+                    fcs_card_t card = fcs_make_card(rank, suit_idx);
 
                     if (self->bakers_dozen_topmost_cards_lookup[card >> 3] & (1 << (card & (8-1))))
                     {
     {
         for (int suit_idx = 0 ; suit_idx < NUM_SUITS ; suit_idx++)
         {
-            fcs_card_t card = fc_solve_empty_card;
-
-            fcs_card_set_rank(card, RANK_KING);
-            fcs_card_set_suit(card, suit_idx);
+            fcs_card_t card = fcs_make_card(RANK_KING, suit_idx);
 
             if (! IS_IN_FOUNDATIONS(card))
             {
         {
             for (int suit_idx = 0 ; suit_idx < NUM_SUITS ; suit_idx++)
             {
-                fcs_card_t card = fc_solve_empty_card;
-                fcs_card_set_rank(card, rank);
-                fcs_card_set_suit(card, suit_idx);
+                fcs_card_t card = fcs_make_card(rank, suit_idx);
 
                 if (IS_BAKERS_DOZEN())
                 {

fc-solve/source/state.h

 #define FCS_MAX_NUM_SCANS_BUCKETS 4
 #endif
 
+/* How many ranks there are - Ace to King == 13. */
+#define FCS_MAX_RANK 13
+
 #define FCS_CHAR_BIT_SIZE_LOG2 3
 #define MAX_NUM_SCANS (FCS_MAX_NUM_SCANS_BUCKETS * (sizeof(unsigned char) * 8))
 
 
 typedef int fcs_locs_t;
 
+static GCC_INLINE fcs_card_t fcs_make_card(const int rank, const int suit)
+{
+    fcs_card_t ret = { .rank = rank, .suit = suit, .flags = 0 };
+
+    return ret;
+}
+
 #define fcs_state_get_col(state, col_idx) \
     (&((state).stacks[(col_idx)]))
 
  * INDIRECT_STACK_STATES */
 #if defined(COMPACT_STATES) || defined(INDIRECT_STACK_STATES)
 
+static GCC_INLINE fcs_card_t fcs_make_card(const int rank, const int suit)
+{
+    return ( (((fcs_card_t)rank) << 2) | ((fcs_card_t)suit) );
+}
+
 #define fcs_col_len(col) \
     ( ((col)[0]) )
 
 
     /* Now check if there are extra or missing cards */
 
-    for(d=0;d<4;d++)
+    for(int suit_idx = 0; suit_idx < 4; suit_idx ++)
     {
-        for(c=1;c<=13;c++)
+        for (int rank = 1; rank <= FCS_MAX_RANK ; rank++)
         {
-            if (cards[d][c] != decks_num)
+            if (cards[suit_idx][rank] != decks_num)
             {
-                *misplaced_card = fc_solve_empty_card;
-                fcs_card_set_suit(*misplaced_card, d);
-                fcs_card_set_rank(*misplaced_card, c);
-                return ((cards[d][c] < decks_num)
+                *misplaced_card = fcs_make_card(rank, suit_idx);
+                return ((cards[suit_idx][rank] < decks_num)
                     ? FCS_STATE_VALIDITY__MISSING_CARD
                     : FCS_STATE_VALIDITY__EXTRA_CARD
                     )

fc-solve/source/t/delta-states-test.c

 
 static fcs_card_t make_card(int rank, int suit)
 {
-    fcs_card_t card;
-
-    card = fc_solve_empty_card;
-
-    fcs_card_set_rank(card, rank);
-    fcs_card_set_suit(card, suit);
-
-    return card;
+    return fcs_make_card(rank, suit);
 }
 
 static int test_encode_and_decode(fc_solve_delta_stater_t * delta, fcs_state_keyval_pair_t * state, const char * expected_str, const char * blurb)