Loading…

Persistent Immutable List

A "version history" is often built the easy but wasteful way: copy the whole array/list every time something changes, and keep every copy around for undo. This library implements the classic functional data structure that avoids the copy - a persistent cons list, where every "version" is a lightweight integer handle into an append-only node pool. Pushing a new value onto a version allocates exactly one new node pointing at the version it was built from; the rest of the chain is not touched, copied, or even read. Two versions that share a common history genuinely share the SAME underlying nodes in memory, not merely equal values - the demo proves this with SharedSuffixLength(), which compares node indices directly, and by branching two independent versions off one base and showing the base's node count does not double even though two new versions now depend on it. MQL4 has no reference counting or garbage collector for plain heap objects, and a shared node can be reachable from several live versions at once - no single version can safely free "its" nodes without risking one another version still needs. This library takes the honest approach: all nodes live in one append-only pool and are only ever reclaimed together, via Reset(). There is no per-version memory reclamation, and that trade-off is documented rather than hidden - see NOTES AND LIMITATIONS. Notes and limitations - Values are double. Encode other data as a number (an index into a separate lookup table, for example) if richer payloads are needed - MQL4's lack of generics makes a fully generic node type impractical here. - No per-version deletion: memory is reclaimed only by Reset(), which invalidates every version handle at once. This is a genuine consequence of structural sharing with no reference counting, not an oversight - documented here rather than papered over. - Length()/Get()/ToArray() are O(length), the same cost class as any singly-linked list. Only PushFront/PopFront are O(1); this is not a random-access structure. - Version handles are plain int array indices into an internal pool - they are only meaningful against the CPersistentImmutableList instance that produced them, and become invalid after that instance's Reset().

Script · MT5 · v1.0 · Free

About Persistent Immutable List

Persistent Immutable List is a script for MetaTrader 5. It has 1 setting, such as Undo steps. MQL source code included.

Files in the download: PersistentImmutableList.mq5.mqh, PersistentImmutableList.txt, PersistentImmutableListDemo.ex5, PersistentImmutableListDemo.mq5.

Settings

SettingDefaultWhat it does
Undo steps3how many PopFront/undo steps to demonstrate

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria