Skip to content

feat: implement pvector<T> persistent vector with O(log n) access via AVL order-statistic tree (Issue #186)#187

Merged
netkeep80 merged 9 commits intonetkeep80:mainfrom
konard:issue-186-4a9c4abda809
Mar 12, 2026
Merged

feat: implement pvector<T> persistent vector with O(log n) access via AVL order-statistic tree (Issue #186)#187
netkeep80 merged 9 commits intonetkeep80:mainfrom
konard:issue-186-4a9c4abda809

Conversation

@konard
Copy link
Copy Markdown
Contributor

@konard konard commented Mar 11, 2026

Implements pvector<T> — a persistent sequential container (vector) for the persistent address space (PAP) as requested in Issue #186.

Key features

  • O(log n) random access via at(i) — uses an AVL order-statistic tree indexed by element position.
  • O(1) size() — reads weight of root node (total subtree size).
  • O(log n) push_back / pop_back / front / back — insert/remove rightmost node with AVL rebalancing.
  • In-order iterator — traversal in element order via standard BST in-order walk.
  • Nodes are not permanently locked — can be freed after removal.

Implementation

Each element is stored in a separate PAP block. The built-in TreeNode fields are used for the AVL tree:

  • weighttotal subtree size (self + all descendants). Enables O(log n) position lookup.
  • left_offset / right_offset → left/right children (smaller/larger indices).
  • parent_offset → parent node.
  • avl_height → subtree height for AVL balance.

The _avl_find_by_index(p, i) function navigates O(log n) using subtree sizes:

  • If i < left.weight → go left
  • If i == left.weight → found
  • If i > left.weight → go right with i -= left.weight + 1

Custom _rotate_left / _rotate_right and _rebalance_up maintain both height (AVL balance) and weight (subtree sizes) on every structural change.

Changed files

  • include/pmm/pvector.h — New AVL order-statistic tree implementation (replaces linked-list)
  • include/pmm/persist_memory_manager.h — Added Mgr::pvector<T> type alias
  • tests/test_issue186_pvector.cpp — 14 test cases (added I186-N: AVL invariant verification)
  • tests/CMakeLists.txt — Added test registration
  • docs/api_reference.md — API documentation
  • README.md — Updated with pvector section
  • scripts/generate-single-headers.sh — Include pvector in single-header generation
  • single_include/pmm/pmm.h — Regenerated single-header
  • changelog.d/20260311_120000_pvector.md — Changelog fragment

Test plan

All 53 existing tests pass, plus 14 new pvector tests:

  • push_back, at, size, empty, front, back, pop_back, clear, reset, iterators
  • Block locking verification
  • Stress test (100 elements with indexed access)
  • Struct type support
  • I186-N: AVL tree invariants — verifies weight fields are correct at every node, balance factor |bf|≤1, and tree height is O(log n)

Fixes #186

konard and others added 2 commits March 11, 2026 22:46
Adding .gitkeep for PR creation (default mode).
This file will be removed when the task is complete.

Issue: netkeep80#186
…80#186)

Add pvector<T, ManagerT> - a persistent sequential container that stores
elements in PAP blocks linked via TreeNode fields to form a doubly-linked
list with O(1) push_back operations.

Key features:
- O(1) push_back(), front(), back(), pop_back() with tail pointer
- O(n) at(i) access via linear traversal from head
- Iterator support for range-based for loops
- clear() method to remove all elements and free memory
- Nodes are NOT permanently locked (can be freed)

Includes:
- include/pmm/pvector.h - implementation
- Mgr::pvector<T> type alias in PersistMemoryManager
- Comprehensive test suite (test_issue186_pvector.cpp)
- Documentation in README.md and docs/api_reference.md
- Updated single-header generation script

Fixes netkeep80#186

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@konard konard changed the title [WIP] Реализовать pvector feat: implement pvector<T> persistent vector container (Issue #186) Mar 11, 2026
konard and others added 3 commits March 11, 2026 22:53
Updated pmm.h and pmm_no_comments.h to include the new pvector<T>
persistent vector container.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@konard konard marked this pull request as ready for review March 11, 2026 23:01
@konard
Copy link
Copy Markdown
Contributor Author

konard commented Mar 11, 2026

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $8.969752
  • Calculated by Anthropic: $5.589500 USD
  • Difference: $-3.380252 (-37.69%)
    📎 Log file uploaded as Gist (2085KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard
Copy link
Copy Markdown
Contributor Author

konard commented Mar 11, 2026

✅ Ready to merge

This pull request is now ready to be merged:

  • All CI checks have passed
  • No merge conflicts
  • No pending changes

Monitored by hive-mind with --auto-restart-until-mergeable flag

@netkeep80
Copy link
Copy Markdown
Owner

Используй встроенные avl-деревья в менеджер ПАП, что бы ускорить доступ к произвольному элементу вектора с O(n) до O(log n)

@konard konard marked this pull request as draft March 12, 2026 09:43
@konard
Copy link
Copy Markdown
Contributor Author

konard commented Mar 12, 2026

🤖 AI Work Session Started

Starting automated work session at 2026-03-12T09:43:42.553Z

The PR has been converted to draft mode while work is in progress.

This comment marks the beginning of an AI work session. Please wait for the session to finish, and provide your feedback.

…tic tree

Use the built-in AVL tree in the PAP manager to index pvector elements by
position, achieving O(log n) random access as requested in the issue feedback.

Implementation changes:
- Replace linked-list structure with an AVL order-statistic tree.
- The `weight` field of each TreeNode stores the total subtree size (self +
  all descendants), enabling O(log n) index-based lookup (_avl_find_by_index).
- Custom _rotate_left/_rotate_right and _rebalance_up maintain both `height`
  (for AVL balance) and `weight` (for subtree sizes) on every structural change.
- push_back inserts as rightmost node in O(log n).
- pop_back removes the rightmost node in O(log n).
- size() is now O(1) (reads weight of root).
- front()/back() are O(log n) leftmost/rightmost traversals.
- Iterator uses in-order (left→node→right) traversal, preserving order.
- Added test I186-N to verify AVL invariants: correct weight fields, |bf|<=1
  at every node, and tree height bounded by O(log n).

Fixes netkeep80#186

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@konard konard changed the title feat: implement pvector<T> persistent vector container (Issue #186) feat: implement pvector<T> persistent vector with O(log n) access via AVL order-statistic tree (Issue #186) Mar 12, 2026
konard and others added 2 commits March 12, 2026 09:55
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…nts)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@konard konard marked this pull request as ready for review March 12, 2026 10:04
@konard
Copy link
Copy Markdown
Contributor Author

konard commented Mar 12, 2026

AI Work Session Completed

Implemented the requested improvement: O(log n) random access via AVL order-statistic tree.

Changes made

The pvector now uses the built-in AVL tree infrastructure of the PAP manager instead of a linked list.

Key design:

  • Each node's weight field stores its total subtree size (self + all descendants)
  • at(i) navigates in O(log n) using subtree sizes: compare i against left.weight, go left/right, adjust index
  • size() is now O(1) — reads weight of root
  • push_back inserts as rightmost node, O(log n)
  • pop_back removes rightmost node, O(log n)
  • Custom _rotate_left/_rotate_right and _rebalance_up maintain both avl_height (for AVL balance) and weight (for subtree sizes) on every structural change

New test I186-N verifies AVL invariants at every node after 100 push_back + 30 pop_back operations:

  • weight == 1 + left.weight + right.weight at every node
  • Balance factor |bf| ≤ 1 at every node
  • Tree height ≤ O(log n) bound

All 53 existing tests + 14 pvector tests pass. All CI checks pass.

@konard
Copy link
Copy Markdown
Contributor Author

konard commented Mar 12, 2026

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $3.074593
  • Calculated by Anthropic: $2.391127 USD
  • Difference: $-0.683466 (-22.23%)
    📎 Log file uploaded as Gist (1571KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard
Copy link
Copy Markdown
Contributor Author

konard commented Mar 12, 2026

✅ Ready to merge

This pull request is now ready to be merged:

  • All CI checks have passed
  • No merge conflicts
  • No pending changes

Monitored by hive-mind with --auto-restart-until-mergeable flag

@netkeep80 netkeep80 merged commit 7d4cdea into netkeep80:main Mar 12, 2026
13 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.

Реализовать pvector

2 participants