You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

含10^7个512-bit元素的动态数组版本控制最优数据结构选型

The Best Data Structure for Your Version-Controlled Dynamic Array

First off, let's call out the elephant in the room: storing full copies of your 10^7-element array for every version is a non-starter. Each copy would take ~6.4GB (1e7 * 64 bytes), and you'd burn through storage in no time. For your requirements—space-efficient change logging, and direct access to the final version without replaying all history—persistent arrays (implemented with path copying) are the optimal choice. If you need more flexibility for arbitrary insertions/deletions, a persistent balanced binary search tree works too.

Why Persistent Arrays Work Perfectly for You

Persistent data structures are built to create new versions on modification without copying the entire structure—they share unchanged parts with old versions. Here's how this aligns with your needs:

  • Space efficiency: Every add, delete, or swap operation only requires copying O(log n) nodes (if built on a binary tree structure). For 10^7 elements, log₂(1e7) is roughly 24—so each operation adds a tiny amount of overhead, not gigabytes. That's orders of magnitude better than full version copies.
  • Direct final version access: Each version is represented by a single root pointer. You can use this pointer to access any element in the final version instantly, no need to iterate through historical changes.
  • Supports all your operations:
    • Swaps are just two element modifications, each costing O(log n) space.
    • Appends/deletes from the end can be optimized to O(1) or O(log n) overhead. For arbitrary position inserts/deletes, a persistent balanced BST (like a treap or red-black tree) adapted to act as an ordered array will handle those in O(log n) time and space.

How Path Copying Works for Persistent Arrays

The standard implementation uses a complete binary tree:

  1. Array elements live in the tree's leaf nodes; internal nodes hold pointers to their children.
  2. When you modify an element, you copy every node along the path from the root to that leaf. The new nodes point to unchanged children and the modified leaf.
  3. Each version gets its own root pointer—use that to access the entire version of the array.

Alternative: Chunked Copy-On-Write (COW) Array

If your operations are mostly localized (e.g., modifying elements in the same small section), a chunked COW array is a solid backup:

  • Split the array into fixed-size chunks (say, 1024 elements per chunk—64KB per chunk for your 512-bit elements).
  • When you modify an element, only copy its entire chunk; all other chunks stay shared with the old version.
  • The final version is just a collection of references to these chunks, so you can access it directly.

This is simpler to implement but less efficient if your changes are spread across many chunks, since each chunk copy is larger than the O(log n) nodes in a persistent array.

Final Recommendation

Go with a path-copying persistent array first—it's tailor-made for your space efficiency and direct version access needs. If you need to do frequent arbitrary-position inserts/deletes, switch to a persistent balanced BST structured to act as an ordered array.

内容的提问来源于stack exchange,提问作者Shawn Arthur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:30:08