| Crates.io | citer |
| lib.rs | citer |
| version | 0.1.16 |
| created_at | 2024-06-25 15:30:25.763318+00 |
| updated_at | 2025-12-22 05:00:35.717162+00 |
| description | citer |
| homepage | https://github.com/js0-site/rust/tree/main/citer |
| repository | https://github.com/js0-site/rust.git |
| max_upload_size | |
| id | 1283422 |
| size | 56,846 |
CIter is a lightweight, zero-copy circular iterator library for Rust that enables efficient traversal of slices from any starting position. The iterator wraps around the slice boundaries, providing seamless circular access to data with index information without memory allocation or copying.
rand feature&[T]use citer::CIter;
let data = [1, 2, 3, 4, 5];
let iter = CIter::new(&data, 2); // Start from index 2
let result: Vec<_> = iter.collect();
// Result: [(2, &3), (3, &4), (4, &5), (0, &1), (1, &2)]
let data = [10, 20, 30];
let iter = CIter::new(&data, 2);
println!("Current position: {}", iter.pos()); // Output: 1
// Requires "rand" feature
let data = [1, 2, 3, 4, 5];
let iter = CIter::rand(&data);
let result: Vec<_> = iter.collect();
// Result: Random permutation starting from random position with indices
let data = [1, 2, 3];
let mut iter = CIter::new(&data, 1);
assert_eq!(iter.next(), Some((1, &2)));
assert_eq!(iter.next(), Some((2, &3)));
assert_eq!(iter.next(), Some((0, &1)));
assert_eq!(iter.next(), None); // Iterator exhausted
let data = [1, 2, 3, 4, 5];
let iter = CIter::new(&data, 2);
let values: Vec<&i32> = iter.map(|(_, &value)| value).collect();
// Result: [3, 4, 5, 1, 2]
CIter<'a, T>Main circular iterator struct with lifetime parameter 'a and generic type T.
idx: usize - Current index positionli: &'a [T] - Reference to the slice dataed: usize - Number of elements already visitednew(li: &'a [T], pos: usize) -> Self
pos(&self) -> usize
idx - 1 for non-zero indices, 0 for zero indexrand(li: &'a [T]) -> Self (requires rand feature)
Implements standard Iterator trait:
type Item = (usize, &'a T) - Returns tuple of (index, reference to value)fn next(&mut self) -> Option<Self::Item>The library follows a minimalist approach focusing on performance and safety:
graph TD
A[Slice Input] --> B[CIter::new]
B --> C[Position Calculation]
C --> D[Iterator Creation]
D --> E[next Call]
E --> F{Elements Visited < Length}
F -->|Yes| G[Calculate Index with Modulo]
G --> H[Return (index, value) Tuple]
F -->|No| I[Return None]
H --> J[Increment Counters]
J --> E
I --> K[Iterator Exhausted]
CIter::new() initializes iterator stateIterator::next() implements core circular logic with modulo arithmeticrand feature provides randomized starting positionsaok (0.1.18) - Result handling utilitiesrand (0.9.2) - Random starting position supportaok (0.1.18) - Test result handlinglog (0.4.29) - Logging infrastructureloginit (0.1.18) - Log initializationstatic_init (1.0.4) - Static initializationlog (0.1.43) - Structured logginglog_init (0.1.34) - Local log initialization utilitiesciter/
├── src/
│ └── lib.rs # Core CIter implementation
├── tests/
│ └── main.rs # Integration tests
├── readme/
│ ├── en.md # English documentation
│ └── zh.md # Chinese documentation
├── Cargo.toml # Project configuration
└── test.sh # Test execution script
CIter struct: Main iterator implementation with position and element trackingCircular iterators have deep roots in computer science, log back to early work on circular buffers in the 1960s. The concept gained prominence with Donald Knuth's "The Art of Computer Programming," where circular data structures were explored as fundamental algorithmic building blocks.
In systems programming, circular iterators became essential for implementing ring buffers, round-robin schedulers, and audio processing pipelines. The zero-copy approach pioneered in languages like C found new expression in Rust's ownership system, enabling memory-safe circular iteration without runtime overhead.
CIter's unique approach of preserving index information alongside values reflects modern needs in data processing, where maintaining context about element positions is crucial for algorithms like circular convolution, periodic data analysis, and rotating window operations. The modulo arithmetic approach used in CIter reflects decades of optimization in circular addressing, commonly found in digital signal processing and embedded systems where efficient wraparound behavior is critical for real-time performance.
This project is an open-source component of js0.site ⋅ Refactoring the Internet Plan.
We are redefining the development paradigm of the Internet in a componentized way. Welcome to follow us:
CIter 是 Rust 语言的轻量级零拷贝循环迭代器库,支持从任意起始位置高效遍历切片。迭代器在切片边界处自动环绕,提供无缝的循环数据访问,同时保留索引信息,无需内存分配或数据拷贝。
rand 特性支持随机起始位置&[T]use citer::CIter;
let data = [1, 2, 3, 4, 5];
let iter = CIter::new(&data, 2); // 从索引 2 开始
let result: Vec<_> = iter.collect();
// 结果: [(2, &3), (3, &4), (4, &5), (0, &1), (1, &2)]
let data = [10, 20, 30];
let iter = CIter::new(&data, 2);
println!("当前位置: {}", iter.pos()); // 输出: 1
// 需要启用 "rand" 特性
let data = [1, 2, 3, 4, 5];
let iter = CIter::rand(&data);
let result: Vec<_> = iter.collect();
// 结果: 从随机位置开始的随机排列,包含索引信息
let data = [1, 2, 3];
let mut iter = CIter::new(&data, 1);
assert_eq!(iter.next(), Some((1, &2)));
assert_eq!(iter.next(), Some((2, &3)));
assert_eq!(iter.next(), Some((0, &1)));
assert_eq!(iter.next(), None); // 迭代器耗尽
let data = [1, 2, 3, 4, 5];
let iter = CIter::new(&data, 2);
let values: Vec<&i32> = iter.map(|(_, &value)| value).collect();
// 结果: [3, 4, 5, 1, 2]
CIter<'a, T>主要循环迭代器结构体,包含生命周期参数 'a 和泛型类型 T。
idx: usize - 当前索引位置li: &'a [T] - 切片数据的引用ed: usize - 已访问元素数量new(li: &'a [T], pos: usize) -> Self
pos(&self) -> usize
idx - 1,零索引返回 0rand(li: &'a [T]) -> Self (需要 rand 特性)
实现标准 Iterator 特征:
type Item = (usize, &'a T) - 返回 (索引, 值的引用) 元组fn next(&mut self) -> Option<Self::Item>库采用极简主义方法,专注于性能和安全性:
graph TD
A[切片输入] --> B[CIter::new]
B --> C[位置计算]
C --> D[迭代器创建]
D --> E[next 调用]
E --> F{已访问元素 < 长度}
F -->|是| G[通过模运算计算索引]
G --> H[返回 (索引, 值) 元组]
F -->|否| I[返回 None]
H --> J[递增计数器]
J --> E
I --> K[迭代器耗尽]
CIter::new() 初始化迭代器状态Iterator::next() 实现核心循环逻辑和模运算rand 特性提供随机起始位置aok (0.1.18) - 结果处理工具rand (0.9.2) - 随机起始位置支持aok (0.1.18) - 测试结果处理log (0.4.29) - 日志基础设施loginit (0.1.18) - 日志初始化static_init (1.0.4) - 静态初始化log (0.1.43) - 结构化日志log_init (0.1.34) - 本地日志初始化工具citer/
├── src/
│ └── lib.rs # 核心 CIter 实现
├── tests/
│ └── main.rs # 集成测试
├── readme/
│ ├── en.md # 英文文档
│ └── zh.md # 中文文档
├── Cargo.toml # 项目配置
└── test.sh # 测试执行脚本
CIter 结构体: 主要迭代器实现,包含位置和元素跟踪循环迭代器在计算机科学中有着深厚的历史根源,可追溯到 1960 年代早期的循环缓冲区工作。这个概念在 Donald Knuth 的《计算机程序设计艺术》中得到了重视,其中循环数据结构被探索为基础算法构建块。
在系统编程中,循环迭代器成为实现环形缓冲区、轮询调度器和音频处理管道的关键技术。C 语言中开创的零拷贝方法在 Rust 的所有权系统中找到了新的表达方式,实现了内存安全的循环迭代,且无运行时开销。
CIter 在保留值的同时保留索引信息的独特方法反映了现代数据处理的需求,在循环卷积、周期性数据分析和旋转窗口操作等算法中,维护元素位置的上下文信息至关重要。CIter 中使用的模运算方法反映了循环寻址数十年的优化历程,这种方法在数字信号处理和嵌入式系统中很常见,在这些领域中,高效的环绕行为对实时性能至关重要。
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: