From cbae3665cabd846ba871c366e03b424e4d0985d6 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 9 Feb 2024 10:07:09 -0800 Subject: [PATCH] remove redundant list length variable --- xwords4/common/dllist.c | 18 ++++++++++++++++++ xwords4/common/dllist.h | 3 +++ xwords4/common/knownplyr.c | 22 +++++++--------------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/xwords4/common/dllist.c b/xwords4/common/dllist.c index c34c3d040..acd1d8d4f 100644 --- a/xwords4/common/dllist.c +++ b/xwords4/common/dllist.c @@ -71,6 +71,24 @@ dll_remove( DLHead* list, DLHead* node ) return newHead; } +static ForEachAct +lengthProc(const DLHead* XP_UNUSED(dl1), void* closure) +{ + XP_U16* count = (XP_U16*)closure; + ++*count; + return FEA_OK; +} + +XP_U16 +dll_length( const DLHead* list ) +{ + XP_U16 result = 0; + if ( !!list ) { + dll_map( (DLHead*)list, lengthProc, NULL, &result ); + } + return result; +} + DLHead* dll_map( DLHead* list, DLMapProc mapProc, DLDisposeProc dispProc, void* closure ) diff --git a/xwords4/common/dllist.h b/xwords4/common/dllist.h index 37650e1e4..7365a2c2a 100644 --- a/xwords4/common/dllist.h +++ b/xwords4/common/dllist.h @@ -24,6 +24,8 @@ extern "C" { #endif +#include "xptypes.h" + typedef struct DLHead { struct DLHead* _next; struct DLHead* _prev; @@ -35,6 +37,7 @@ typedef int (*DLCompProc)(const DLHead* dl1, const DLHead* dl2); DLHead* dll_insert( DLHead* list, DLHead* node, DLCompProc proc ); DLHead* dll_remove( DLHead* list, DLHead* node ); +XP_U16 dll_length( const DLHead* list ); DLHead* dll_sort( DLHead* list, DLCompProc proc ); typedef ForEachAct (*DLMapProc)(const DLHead* dl1, void* closure); diff --git a/xwords4/common/knownplyr.c b/xwords4/common/knownplyr.c index 4ee551101..b234f558c 100644 --- a/xwords4/common/knownplyr.c +++ b/xwords4/common/knownplyr.c @@ -35,7 +35,7 @@ typedef struct _KnownPlayer { typedef struct _KPState { KnownPlayer* players; - XP_U16 nPlayers; + // XP_U16 nPlayers; XP_Bool dirty; XP_Bool inUse; } KPState; @@ -51,7 +51,6 @@ loadFromStream( XW_DUtilCtxt* dutil, KPState* state, XWStreamCtxt* stream ) { while ( 0 < stream_getSize( stream ) ) { XP_U32 newestMod = stream_getU32( stream ); - XP_LOGFF( "read newestMod: %d", newestMod ); XP_UCHAR buf[64]; stringFromStreamHere( stream, buf, VSIZE(buf) ); @@ -148,10 +147,9 @@ static void makeUniqueName( const KPState* state, const XP_UCHAR* name, XP_UCHAR newName[], XP_U16 len ) { - XP_U16 nPlayers = state->nPlayers; + XP_U16 nPlayers = dll_length( &state->players->links ); const XP_UCHAR* names[nPlayers]; getPlayersImpl( state, names, &nPlayers ); - XP_ASSERT( nPlayers == state->nPlayers ); for ( int ii = 2; ; ++ii ) { XP_SNPRINTF( newName, len, "%s %d", name, ii ); XP_Bool found = XP_FALSE; @@ -230,9 +228,6 @@ addPlayer( XW_DUtilCtxt* XP_UNUSED_DBG(dutil), KPState* state, AddData ad = {.name = name, .addr = addr, }; dll_map( &state->players->links, addProc, NULL, &ad ); - XP_LOGFF( "withSameDevID: %p; withSameName: %p", - ad.withSameDevID, ad.withSameName ); - XP_UCHAR tmpName[64]; if ( !!ad.withSameDevID ) { /* only one allowed */ XP_Bool isNewer = newestMod > ad.withSameDevID->newestMod; @@ -243,8 +238,6 @@ addPlayer( XW_DUtilCtxt* XP_UNUSED_DBG(dutil), KPState* state, ad.withSameDevID->newestMod, newestMod ); ad.withSameDevID->newestMod = newestMod; changed = XP_TRUE; - } else { - XP_LOGFF( "not newer" ); } state->dirty = changed || state->dirty; } else { @@ -262,9 +255,8 @@ addPlayer( XW_DUtilCtxt* XP_UNUSED_DBG(dutil), KPState* state, = (KnownPlayer*)dll_insert( &state->players->links, &newPlayer->links, compByName ); state->dirty = XP_TRUE; - ++state->nPlayers; } - XP_LOGFF( "nPlayers now: %d", state->nPlayers ); + XP_LOGFF( "nPlayers now: %d", dll_length( &state->players->links ) ); } XP_Bool @@ -302,7 +294,7 @@ XP_Bool kplr_havePlayers( XW_DUtilCtxt* dutil, XWEnv xwe ) { KPState* state = loadState( dutil, xwe ); - XP_Bool result = 0 < state->nPlayers; + XP_Bool result = 0 < dll_length( &state->players->links ); releaseState( dutil, xwe, state ); LOG_RETURNF( "%s", boolToStr(result) ); return result; @@ -327,12 +319,13 @@ static void getPlayersImpl( const KPState* state, const XP_UCHAR** players, XP_U16* nFound ) { - if ( state->nPlayers <= *nFound && !!players ) { + XP_U16 nPlayers = dll_length( &state->players->links ); + if ( nPlayers <= *nFound && !!players ) { GetState gs = { .players = players, .indx = 0, }; DLHead* head = dll_map( &state->players->links, getProc, NULL, &gs ); XP_ASSERT( head == &state->players->links ); } - *nFound = state->nPlayers; + *nFound = nPlayers; } void @@ -479,7 +472,6 @@ delMapProc( const DLHead* dl, void* closure ) DelState* dsp = (DelState*)closure; if ( 0 == XP_STRCMP( kp->name, dsp->name ) ) { result = FEA_REMOVE | FEA_EXIT; - --dsp->state->nPlayers; dsp->state->dirty = XP_TRUE; } return result;