FEAT: Implement SigmaMap with Insertion Order Preservation #2283
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The current
ContextExtensionimplementation uses Scala's defaultscala.collection.Map, which only preserves insertion order for up to 4 elements (using specializedMap1throughMap4implementations). For larger maps, the traversal order is not guaranteed and may change across Scala versions, which is problematic for consensus-critical code where insertion order matters.Solution
Implemented
SigmaMap[V], a new immutable map data structure specifically designed for Byte keys that:Vector[Byte]to maintain insertion order and aMap[Byte, V]for O(1) lookupsImplementation Details
Core Data Structure:
insertionOrder: Vector[Byte]- maintains the order keys were insertedvalues: Map[Byte, V]- provides fast lookupsKey Operations:
updated(key, value): Adds/updates entries while preserving insertion orderremoved(key): Removes entries while maintaining order of remaining elementsget(key): O(1) lookupiterator: Returns elements in insertion orderfilter: Filters while preserving orderTesting
Comprehensive test suite (
SigmaMapSpec) includes:scala.collection.Mapbehavior for >4 elementsFiles Changed
Added:
ergo-core/src/main/scala/org/ergoplatform/utils/SigmaMap.scala- Main implementationergo-core/src/test/scala/org/ergoplatform/utils/SigmaMapSpec.scala- Comprehensive testsTesting Commands