pub fn zigzag_encode_iter<'a, T, I>(
iter: I,
) -> impl Iterator<Item = T::UInt> + 'aExpand description
Creates an iterator that encodes each signed integer from the source iterator.
This function provides an iterator-based API for ZigZag encoding. The values are encoded on-the-fly as the iterator is consumed, without requiring an intermediate buffer.
§Arguments
iter- An iterator that yields references to signed integers
§Returns
An iterator that yields encoded unsigned integers
§Example
use zigzag_rs::{ZigZag, zigzag_encode_iter};
let values = [-10, -1, 0, 1, 10];
let encoded_iter = zigzag_encode_iter::<i32, _>(values.iter());
for (original, encoded) in values.iter().zip(encoded_iter) {
assert_eq!(encoded, i32::zigzag_encode(*original));
}§Advanced Example
use zigzag_rs::{ZigZag, zigzag_encode_iter};
// Filtering and encoding in one pass
let values = [-100, -10, -1, 0, 1, 10, 100];
// Process only positive numbers
let positive_encoded: Vec<u32> = values.iter()
.filter(|&&v| v > 0)
.map(|&v| i32::zigzag_encode(v))
.collect();
assert_eq!(positive_encoded, vec![2, 20, 200]);
// Alternative approach using zigzag_encode_iter
let positive_encoded2: Vec<u32> = zigzag_encode_iter::<i32, _>(
values.iter().filter(|&&v| v > 0)
).collect();
assert_eq!(positive_encoded2, vec![2, 20, 200]);Examples found in repository?
examples/iterator_api.rs (line 14)
3fn main() {
4 println!("ZigZag Iterator-based API Examples");
5 println!("================================\n");
6
7 // Source data
8 let values = [-100, -10, -1, 0, 1, 10, 100];
9 println!("Original values: {:?}", values);
10
11 // Basic encode iterator example
12 println!("\n1. Basic encoding with iterator");
13 println!("----------------------------");
14 let encoded_iter = zigzag_encode_iter::<i32, _>(values.iter());
15
16 // We can display the values as they are generated
17 println!("Encoded values: ");
18 for encoded in encoded_iter {
19 print!("{} ", encoded);
20 }
21 println!();
22
23 // Demonstrate conversion without allocating a buffer
24 println!("\n2. Pairing original and encoded values");
25 println!("----------------------------------");
26 let encoded_iter = zigzag_encode_iter::<i32, _>(values.iter());
27 for (original, encoded) in values.iter().zip(encoded_iter) {
28 println!("{:5} encodes to {:5}", original, encoded);
29 }
30
31 // Decoding example
32 println!("\n3. Iterator-based decoding");
33 println!("------------------------");
34 let encoded = [199u32, 19, 1, 0, 2, 20, 200];
35 println!("Encoded values: {:?}", encoded);
36
37 println!("Decoded values: ");
38 let decoded_iter = zigzag_decode_iter::<i32, _>(encoded.iter());
39 for decoded in decoded_iter {
40 print!("{} ", decoded);
41 }
42 println!();
43
44 // Filtering example
45 println!("\n4. Combining with other iterator operations");
46 println!("---------------------------------------");
47 let values = [-100, -50, -10, -5, -1, 0, 1, 5, 10, 50, 100];
48
49 // Create an iterator that only processes positive values
50 println!("Original values: {:?}", values);
51 println!("Processing only positive values:");
52
53 let filtered_encoded: Vec<u32> = values.iter()
54 .filter(|&&x| x > 0) // Only positive values
55 .map(|&x| i32::zigzag_encode(x)) // Encode them
56 .collect(); // Collect results
57
58 println!("Filtered and encoded: {:?}", filtered_encoded);
59
60 // Memory efficiency example
61 println!("\n5. Memory efficiency comparison");
62 println!("----------------------------");
63
64 // Traditional approach with intermediate allocations
65 let large_range: Vec<i32> = (-1000..1000).collect();
66 println!("Processing {} values", large_range.len());
67
68 println!("Traditional approach (with buffer allocation):");
69 let start = std::time::Instant::now();
70 let mut encoded_buffer = vec![0u32; large_range.len()];
71 i32::zigzag_encode_slice(&large_range, &mut encoded_buffer);
72
73 let mut decoded_buffer = vec![0i32; encoded_buffer.len()];
74 i32::zigzag_decode_slice(&encoded_buffer, &mut decoded_buffer);
75 let duration = start.elapsed();
76 println!("Time with buffers: {:?}", duration);
77
78 // Iterator-based approach
79 println!("Iterator-based approach:");
80 let start = std::time::Instant::now();
81
82 // Process without intermediate allocations
83 let sum: i32 = zigzag_decode_iter::<i32, _>(
84 zigzag_encode_iter::<i32, _>(large_range.iter()).collect::<Vec<_>>().iter()
85 ).sum();
86
87 let duration = start.elapsed();
88 println!("Time with iterators: {:?}", duration);
89 println!("Sum of processed values: {}", sum);
90}