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.


翻译:迭代器是主流命令式语言(如C++)中遍历和修改容器元素的基础编程抽象。迭代器提供统一的访问机制,隐藏了底层数据结构的实现细节。然而,可变容器的迭代器存在众所周知的缺陷,包括失效、别名化、数据竞争以及细微的副作用。函数式编程语言中使用的不可变数据结构避免了可变性的弊端,但其依赖的编程模型基于递归和高阶组合子,而非迭代。然而,这些组合子并不总能很好地表达某些算法,且递归可能暴露底层数据结构的实现细节。本文提出持久迭代器——一种新的抽象,将命令式语言中熟悉的基于迭代器的编程风格与持久数据结构的语义相协调。持久迭代器在创建时对其底层容器的版本进行快照,从而确保免受失效和别名化问题的影响。迭代器操作基于迭代器持有的容器本地副本进行,实现真正的值语义:变量可重新绑定到新的持久值,而先前版本仍可访问。我们以LibFPP库形式实现了该方法——该C++容器库提供持久向量、映射、集合、字符串及其他抽象,作为标准模板库(Standard Template Library, STL)的持久化对应物。评估表明,LibFPP保留了基于迭代器编程的表达能力,消除了迭代器失效问题,并达到了与STL实现渐近相同的复杂度。我们的设计针对需要持久性和安全性的场景,同时允许开发者保留熟悉的基于迭代器的编程模式。

0
下载
关闭预览

相关内容

人们为了让计算机解决各种棘手的问题,使用编程语言 编写程序代码并通过计算机运算得到最终结果的过程。
《软件定义网络元素与机器代码的形式化验证》
专知会员服务
14+阅读 · 2025年11月18日
机器学习损失函数概述,Loss Functions in Machine Learning
专知会员服务
85+阅读 · 2022年3月19日
【Google】高效Transformer综述,Efficient Transformers: A Survey
专知会员服务
66+阅读 · 2022年3月17日
自编码器导论,26页pdf
专知会员服务
42+阅读 · 2022年1月18日
机器学习的可解释性
专知会员服务
69+阅读 · 2020年12月18日
【Uber AI新论文】持续元学习,Learning to Continually Learn
专知会员服务
37+阅读 · 2020年2月27日
再谈变分自编码器VAE:从贝叶斯观点出发
PaperWeekly
13+阅读 · 2018年4月2日
【干货】深入理解变分自编码器
专知
21+阅读 · 2018年3月22日
【干货】深入理解自编码器(附代码实现)
【干货】一文读懂什么是变分自编码器
专知
12+阅读 · 2018年2月11日
国家自然科学基金
18+阅读 · 2017年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
Arxiv
0+阅读 · 2月26日
VIP会员
最新内容
面向国防作战的最佳自主与蜂群无人机技术
专知会员服务
3+阅读 · 今天8:04
《异构人类团队的协作决策过程混合建模研究》
专知会员服务
3+阅读 · 今天7:59
博士论文 | 面向大模型推理的内存高效算法
专知会员服务
3+阅读 · 7月27日
美空军新型反无人机部队初探
专知会员服务
7+阅读 · 7月27日
《防空交战流程的概率建模研究》
专知会员服务
10+阅读 · 7月27日
ICML 2026 教程 | 数值优化理论还重要吗?
专知会员服务
6+阅读 · 7月26日
ICM 2026 | 陶哲轩:人工智能时代的数学
专知会员服务
9+阅读 · 7月26日
相关VIP内容
《软件定义网络元素与机器代码的形式化验证》
专知会员服务
14+阅读 · 2025年11月18日
机器学习损失函数概述,Loss Functions in Machine Learning
专知会员服务
85+阅读 · 2022年3月19日
【Google】高效Transformer综述,Efficient Transformers: A Survey
专知会员服务
66+阅读 · 2022年3月17日
自编码器导论,26页pdf
专知会员服务
42+阅读 · 2022年1月18日
机器学习的可解释性
专知会员服务
69+阅读 · 2020年12月18日
【Uber AI新论文】持续元学习,Learning to Continually Learn
专知会员服务
37+阅读 · 2020年2月27日
相关基金
国家自然科学基金
18+阅读 · 2017年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
Top
微信扫码咨询专知VIP会员