Skip to content

Keep int elements findable after removeFirst() on hash sets - #54

Merged
delacry merged 1 commit into
0.1.xfrom
fix/hash-set-remove-first
Oct 9, 2026
Merged

delacry merged 1 commit into
0.1.xfrom
fix/hash-set-remove-first

Conversation

@delacry

@delacry delacry commented Oct 9, 2026

Copy link
Copy Markdown
Member

HashElementStore keys each element by KeyHasher::hashSetKey(), which for an int is the int itself. removeFirst() came from AbstractElementStore and used array_shift(), which renumbers integer keys, so the remaining ints ended up under the wrong keys:

$set = mutableSetOf([5, 7, 9]);
$set->removeFirst();
$set->contains(7);        // before: false, after: true
$set->add(7)->toArray();  // before: [7, 9, 7], after: [7, 9]

The same happened on immutable sets, since ImmutableSet::removeFirst() calls the store on a clone. Sets of strings weren't affected because array_shift() keeps string keys.

HashElementStore now overrides removeFirst() and unsets the first key, as it already did for removeIf(), sort(), reverse() and shuffle(). ArrayIndexStore keeps using array_shift(), since lists need the renumbering.

Tests: removeFirst() on an int set, then contains() and add() (failed on all four set types before).

@codecov

codecov Bot commented Oct 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@delacry
delacry merged commit 995469a into 0.1.x Oct 9, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant