@@ -558,24 +558,24 @@ inline bool arrayXhash(unsigned na, unsigned nh) noexcept {
558558** return the optimal size for the array part.
559559*/
560560static unsigned computesizes (Counters *ct) {
561- unsigned int a = 0 ; /* number of elements smaller than 2^i */
562- unsigned int na = 0 ; /* number of elements to go to array part */
563- unsigned int optimal = 0 ; /* optimal size for array part */
564- /* traverse slices while 'twotoi ' does not overflow and total of array
561+ unsigned int accumulatedCount = 0 ; /* number of elements smaller than 2^i */
562+ unsigned int arrayCount = 0 ; /* number of elements to go to array part */
563+ unsigned int optimalSize = 0 ; /* optimal size for array part */
564+ /* traverse slices while 'powerOfTwo ' does not overflow and total of array
565565 indices still can satisfy 'arrayXhash' against the array size */
566- for (unsigned int i = 0 , twotoi = 1 ; /* 2^i (candidate for optimal size) */
567- twotoi > 0 && arrayXhash (twotoi , ct->na );
568- i++, twotoi *= 2 ) {
569- unsigned nums = ct->nums [i];
570- a += nums ;
571- if (nums > 0 && /* grows array only if it gets more elements... */
572- arrayXhash (twotoi, a )) { /* ...while using "less memory" */
573- optimal = twotoi ; /* optimal size (till now) */
574- na = a ; /* all elements up to 'optimal ' will go to array part */
566+ for (unsigned int i = 0 , powerOfTwo = 1 ; /* 2^i (candidate for optimal size) */
567+ powerOfTwo > 0 && arrayXhash (powerOfTwo , ct->na );
568+ i++, powerOfTwo *= 2 ) {
569+ unsigned elementCount = ct->nums [i];
570+ accumulatedCount += elementCount ;
571+ if (elementCount > 0 && /* grows array only if it gets more elements... */
572+ arrayXhash (powerOfTwo, accumulatedCount )) { /* ...while using "less memory" */
573+ optimalSize = powerOfTwo ; /* optimal size (till now) */
574+ arrayCount = accumulatedCount ; /* all elements up to 'optimalSize ' will go to array part */
575575 }
576576 }
577- ct->na = na ;
578- return optimal ;
577+ ct->na = arrayCount ;
578+ return optimalSize ;
579579}
580580
581581
@@ -598,28 +598,28 @@ static inline int arraykeyisempty (const Table& t, unsigned key) {
598598** Count keys in array part of table 't'.
599599*/
600600static void numusearray (const Table& t, Counters *ct) {
601- unsigned int ause = 0 ; /* summation of 'nums' */
601+ unsigned int arrayUseCount = 0 ; /* summation of 'nums' */
602602 unsigned int i = 1 ; /* index to traverse all array keys */
603603 /* traverse each slice */
604- unsigned int asize = t.arraySize ();
605- for (unsigned int lg = 0 , ttlg = 1 ; lg <= MAXABITS; lg ++, ttlg *= 2 ) { /* 2^lg */
606- unsigned int lc = 0 ; /* counter */
607- unsigned int lim = ttlg ;
608- if (lim > asize ) {
609- lim = asize ; /* adjust upper limit */
610- if (i > lim )
604+ unsigned int arraySize = t.arraySize ();
605+ for (unsigned int logIndex = 0 , powerOfTwo = 1 ; logIndex <= MAXABITS; logIndex ++, powerOfTwo *= 2 ) { /* 2^logIndex */
606+ unsigned int sliceCount = 0 ; /* counter */
607+ unsigned int limit = powerOfTwo ;
608+ if (limit > arraySize ) {
609+ limit = arraySize ; /* adjust upper limit */
610+ if (i > limit )
611611 break ; /* no more elements to count */
612612 }
613- /* count elements in range (2^(lg - 1), 2^lg ] */
614- for (; i <= lim ; i++) {
613+ /* count elements in range (2^(logIndex - 1), 2^logIndex ] */
614+ for (; i <= limit ; i++) {
615615 if (!arraykeyisempty (t, i))
616- lc ++;
616+ sliceCount ++;
617617 }
618- ct->nums [lg ] += lc ;
619- ause += lc ;
618+ ct->nums [logIndex ] += sliceCount ;
619+ arrayUseCount += sliceCount ;
620620 }
621- ct->total += ause ;
622- ct->na += ause ;
621+ ct->total += arrayUseCount ;
622+ ct->na += arrayUseCount ;
623623}
624624
625625
@@ -630,20 +630,20 @@ static void numusearray (const Table& t, Counters *ct) {
630630*/
631631static void numusehash (const Table& t, Counters *ct) {
632632 unsigned i = t.nodeSize ();
633- unsigned total = 0 ;
633+ unsigned totalNodes = 0 ;
634634 while (i--) {
635- const Node *n = &t.getNodeArray ()[i];
636- if (isempty (gval (n ))) {
637- lua_assert (!n ->isKeyNil ()); /* entry was deleted; key cannot be nil */
635+ const Node *node = &t.getNodeArray ()[i];
636+ if (isempty (gval (node ))) {
637+ lua_assert (!node ->isKeyNil ()); /* entry was deleted; key cannot be nil */
638638 ct->deleted = 1 ;
639639 }
640640 else {
641- total ++;
642- if (n ->isKeyInteger ())
643- countint (n ->getKeyIntValue (), ct);
641+ totalNodes ++;
642+ if (node ->isKeyInteger ())
643+ countint (node ->getKeyIntValue (), ct);
644644 }
645645 }
646- ct->total += total ;
646+ ct->total += totalNodes ;
647647}
648648
649649
@@ -885,34 +885,34 @@ static void clearNewSlice (Table& t, unsigned oldasize, unsigned newasize) {
885885** This prevents resize thrashing in insert-delete-insert patterns.
886886** Trade-off: Uses more memory to avoid repeated O(n) rehashing.
887887*/
888- static void rehash (lua_State *L, Table& t, const TValue *ek ) {
889- Counters ct ;
888+ static void rehash (lua_State *L, Table& t, const TValue *extraKey ) {
889+ Counters counters ;
890890 /* reset counts */
891- std::fill_n (ct .nums , MAXABITS + 1 , 0 );
892- ct .na = 0 ;
893- ct .deleted = 0 ;
894- ct .total = 1 ; /* count extra key */
895- if (ttisinteger (ek ))
896- countint (ivalue (ek ), &ct ); /* extra key may go to array */
897- numusehash (t, &ct ); /* count keys in hash part */
898- unsigned asize ; /* optimal size for array part */
899- if (ct .na == 0 ) {
891+ std::fill_n (counters .nums , MAXABITS + 1 , 0 );
892+ counters .na = 0 ;
893+ counters .deleted = 0 ;
894+ counters .total = 1 ; /* count extra key */
895+ if (ttisinteger (extraKey ))
896+ countint (ivalue (extraKey ), &counters ); /* extra key may go to array */
897+ numusehash (t, &counters ); /* count keys in hash part */
898+ unsigned arraySize ; /* optimal size for array part */
899+ if (counters .na == 0 ) {
900900 /* no new keys to enter array part; keep it with the same size */
901- asize = t.arraySize ();
901+ arraySize = t.arraySize ();
902902 }
903903 else { /* compute best size for array part */
904- numusearray (t, &ct ); /* count keys in array part */
905- asize = computesizes (&ct ); /* compute new size for array part */
904+ numusearray (t, &counters ); /* count keys in array part */
905+ arraySize = computesizes (&counters ); /* compute new size for array part */
906906 }
907907 /* all keys not in the array part go to the hash part */
908- unsigned nsize = ct .total - ct .na ; /* size for the hash part */
909- if (ct .deleted ) { /* table has deleted entries? */
908+ unsigned hashSize = counters .total - counters .na ; /* size for the hash part */
909+ if (counters .deleted ) { /* table has deleted entries? */
910910 /* insertion-deletion-insertion: give hash some extra size to
911911 avoid repeated resizings */
912- nsize += nsize >> 2 ;
912+ hashSize += hashSize >> 2 ;
913913 }
914914 /* resize the table to new computed sizes */
915- t.resize (L, asize, nsize );
915+ t.resize (L, arraySize, hashSize );
916916}
917917
918918/*
@@ -924,18 +924,18 @@ static Node *getfreepos (Table& t) {
924924 if (haslastfree (&t)) { /* does it have 'lastfree' information? */
925925 /* look for a spot before 'lastfree', updating 'lastfree' */
926926 while (getlastfree (&t) > t.getNodeArray ()) {
927- Node *free = --getlastfree (&t);
928- if (free ->isKeyNil ())
929- return free ;
927+ Node *freeNode = --getlastfree (&t);
928+ if (freeNode ->isKeyNil ())
929+ return freeNode ;
930930 }
931931 }
932932 else { /* no 'lastfree' information */
933933 unsigned i = t.nodeSize ();
934934 while (i > 0 ) { /* do a linear search */
935935 i--;
936- Node *free = gnode (&t, i);
937- if (free ->isKeyNil ())
938- return free ;
936+ Node *freeNode = gnode (&t, i);
937+ if (freeNode ->isKeyNil ())
938+ return freeNode ;
939939 }
940940 }
941941 return nullptr ; /* could not find a free place */
@@ -952,39 +952,39 @@ static Node *getfreepos (Table& t) {
952952** could not insert key (could not find a free space).
953953*/
954954static int insertkey (Table& t, const TValue *key, TValue *value) {
955- Node *mp = mainpositionTV (t, key);
955+ Node *mainPositionNode = mainpositionTV (t, key);
956956 /* table cannot already contain the key */
957957 lua_assert (isabstkey (getgeneric (t, key, 0 )));
958- if (!isempty (gval (mp )) || t.isDummy ()) { /* main position is taken? */
959- Node *f = getfreepos (t); /* get a free place */
960- if (f == nullptr ) /* cannot find a free place? */
958+ if (!isempty (gval (mainPositionNode )) || t.isDummy ()) { /* main position is taken? */
959+ Node *freeNode = getfreepos (t); /* get a free place */
960+ if (freeNode == nullptr ) /* cannot find a free place? */
961961 return 0 ;
962962 lua_assert (!t.isDummy ());
963- Node *othern = mainpositionfromnode (t, mp );
964- if (othern != mp ) { /* is colliding node out of its main position? */
963+ Node *collidingNode = mainpositionfromnode (t, mainPositionNode );
964+ if (collidingNode != mainPositionNode ) { /* is colliding node out of its main position? */
965965 /* yes; move colliding node into free position */
966- while (othern + gnext (othern ) != mp ) /* find previous */
967- othern += gnext (othern );
968- gnext (othern ) = cast_int (f - othern ); /* rechain to point to 'f ' */
969- *f = *mp ; /* copy colliding node into free pos. (mp ->next also goes) */
970- if (gnext (mp ) != 0 ) {
971- gnext (f ) += cast_int (mp - f ); /* correct 'next' */
972- gnext (mp ) = 0 ; /* now 'mp ' is free */
966+ while (collidingNode + gnext (collidingNode ) != mainPositionNode ) /* find previous */
967+ collidingNode += gnext (collidingNode );
968+ gnext (collidingNode ) = cast_int (freeNode - collidingNode ); /* rechain to point to 'freeNode ' */
969+ *freeNode = *mainPositionNode ; /* copy colliding node into free pos. (mainPositionNode ->next also goes) */
970+ if (gnext (mainPositionNode ) != 0 ) {
971+ gnext (freeNode ) += cast_int (mainPositionNode - freeNode ); /* correct 'next' */
972+ gnext (mainPositionNode ) = 0 ; /* now 'mainPositionNode ' is free */
973973 }
974- setempty (gval (mp ));
974+ setempty (gval (mainPositionNode ));
975975 }
976976 else { /* colliding node is in its own main position */
977977 /* new node will go into free position */
978- if (gnext (mp ) != 0 )
979- gnext (f ) = cast_int ((mp + gnext (mp )) - f ); /* chain new position */
980- else lua_assert (gnext (f ) == 0 );
981- gnext (mp ) = cast_int (f - mp );
982- mp = f ;
978+ if (gnext (mainPositionNode ) != 0 )
979+ gnext (freeNode ) = cast_int ((mainPositionNode + gnext (mainPositionNode )) - freeNode ); /* chain new position */
980+ else lua_assert (gnext (freeNode ) == 0 );
981+ gnext (mainPositionNode ) = cast_int (freeNode - mainPositionNode );
982+ mainPositionNode = freeNode ;
983983 }
984984 }
985- mp ->setKey (key);
986- lua_assert (isempty (gval (mp )));
987- *gval (mp ) = *value;
985+ mainPositionNode ->setKey (key);
986+ lua_assert (isempty (gval (mainPositionNode )));
987+ *gval (mainPositionNode ) = *value;
988988 return 1 ;
989989}
990990
0 commit comments