Skip List Sorted Map
MQL4 has no built-in sorted associative container. The easy way out is a plain linked list or a parallel-array "sorted map" that is really O(n) per operation with extra steps. This library implements an actual skip list: each node's height is decided independently at insert time by a coin flip (RandomLevel()), not derived from the node's position, the current element count, or any other property that would make it a disguised single-level list wearing a taller data structure's name. Search, insert, and remove all walk top-down through however many levels the tallest node currently reached, dropping down a level each time the next hop at the current level would overshoot the target key - exactly the classic skip list algorithm, giving expected O(log n) operations because roughly half of all nodes reach each additional level up. CountAtLevel() is exposed specifically so this can be verified rather than taken on faith: inserting a few hundred keys and printing the per-level node counts shows the expected roughly-halving pattern in the demo's own output. Nodes are real heap-allocated objects (`new`/`delete`) linked by pointer, with a per-node `forward[]` array sized to that node's own random height - not a fixed-size array wasting space on every node for a height only the tallest few ever use. Notes and limitations - Keys are strings, compared with StringCompare() (ordinal, case- sensitive). Store a zero-padded or fixed-width numeric string (e.g. StringFormat("%08d", n)) if numeric ordering is needed. - Values are strings - encode numbers with DoubleToString()/ IntegerToString() and decode with StringToDouble()/ StringToInteger() as needed; this keeps the container generic without MQL4 templates. - SKL_MAX_LEVEL caps height at 16, which comfortably supports well over 60,000 elements before the probability of needing a 17th level becomes practically relevant. Raise the constant for a significantly larger dataset. - Not thread-safe in any sense beyond what a single MQL4 program thread already guarantees - this is a plain in-memory structure with no on-disk persistence of its own (pair it with FileBackedKeyValueStore.mqh, a separate library in this catalogue, if durability is also needed).
Script · MT5 · v1.0 · Free
About Skip List Sorted Map
Skip List Sorted Map is a script for MetaTrader 5. It has 1 setting, such as Key count. MQL source code included.
Files in the download: SkipListSortedMap.mq5.mqh, SkipListSortedMap.txt, SkipListSortedMapDemo.ex5, SkipListSortedMapDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Key count | 500 | how many keys to insert for the level-distribution check |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code