arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2604.14072cs.PL
收藏

持久迭代器与值语义

Persistent Iterators with Value Semantics

National University of Singapore(新加坡国立大学)

Yihe Li, Gregory J. Duck

AI总结:

本文提出持久迭代器,结合了命令式语言的迭代风格与持久数据结构的语义,通过快照容器版本确保安全,实现真正的值语义,提升表达力并消除迭代器失效问题。

摘要:

迭代器是主流命令式语言如C++中遍历和修改容器元素的基本抽象,提供统一的访问机制,但 mutable 容器的迭代器面临无效化、别名、数据竞赛和隐含副作用等危险。函数式语言中的不可变数据结构避免了突变的陷阱,但依赖递归和高阶组合器而非迭代。然而,这些组合器不总是适合表达某些算法,递归会暴露底层数据结构的实现细节。本文提出持久迭代器,一种新的抽象,协调了命令式语言熟悉的迭代器编程风格与持久数据结构的语义。持久迭代器在创建时快照其底层容器的版本,确保安全,避免无效化和别名。迭代器操作在迭代器本地的容器副本上操作,提供真正的值语义:变量可以重新绑定到新的持久值,而之前的版本仍可访问。我们以LibFPP形式实现该方法,一个C++容器库提供持久向量、映射、集合、字符串等抽象作为标准模板库(STL)的持久对应物。评估显示,LibFPP保持了迭代器编程的表达力,消除了迭代器失效,并实现了与STL实现相媲美的渐近复杂度。我们的设计针对需要持久性和安全性的用例,同时允许开发者保留熟悉的迭代器编程模式。

英文摘要:

Iterators are a fundamental programming abstraction for traversing and modifying elements in containers in mainstream imperative languages such as C++. Iterators provide a uniform access mechanism that hides low-level implementation details of the underlying data structure. However, iterators over mutable containers suffer from well-known hazards including invalidation, aliasing, data races, and subtle side effects. Immutable data structures, as used in functional programming languages, avoid the pitfalls of mutation but rely on a very different programming model based on recursion and higher-order combinators rather than iteration. However, these combinators are not always well-suited to expressing certain algorithms, and recursion can expose implementation details of the underlying data structure. In this paper, we propose persistent iterators -- a new abstraction that reconciles the familiar iterator-based programming style of imperative languages with the semantics of persistent data structures. A persistent iterator snapshots the version of its underlying container at creation, ensuring safety against invalidation and aliasing. Iterator operations operate on the iterator-local copy of the container, giving true value semantics: variables can be rebound to new persistent values while previous versions remain accessible. We implement our approach in the form of LibFPP -- a C++ container library providing persistent vectors, maps, sets, strings, and other abstractions as persistent counterparts to the Standard Template Library (STL). Our evaluation shows that LibFPP retains the expressiveness of iterator-based programming, eliminates iterator-invalidation, and achieves asymptotic complexities comparable to STL implementations. Our design targets use cases where persistence and safety are desired, while allowing developers to retain familiar iterator-based programming patterns.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑