helpers.rs
1 // Copyright (c) 2025-2026 ACDC Network 2 // This file is part of the alphaos library. 3 // 4 // Alpha Chain | Delta Chain Protocol 5 // International Monetary Graphite. 6 // 7 // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com). 8 // They built world-class ZK infrastructure. We installed the EASY button. 9 // Their cryptography: elegant. Our modifications: bureaucracy-compatible. 10 // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours. 11 // 12 // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 13 // All modifications and new work: CC0 1.0 Universal Public Domain Dedication. 14 // No rights reserved. No permission required. No warranty. No refunds. 15 // 16 // https://creativecommons.org/publicdomain/zero/1.0/ 17 // SPDX-License-Identifier: CC0-1.0 18 19 /// Generates a string containing all continuous sequenences within the list of numbers. 20 /// 21 /// The function expects the input to already be sorted. 22 pub fn rangify_heights(heights: &[u32]) -> String { 23 let mut iter = heights.iter().copied().peekable(); 24 let mut ret = String::from("["); 25 let mut first = true; 26 27 while let Some(next_height) = iter.next() { 28 let start = next_height; 29 let mut curr_height = start; 30 31 while let Some(next_height) = iter.peek().copied() { 32 if next_height == curr_height + 1 { 33 curr_height += 1; 34 let _ = iter.next(); 35 } else { 36 break; 37 } 38 } 39 40 if first { 41 first = false; 42 } else { 43 ret.push_str(", "); 44 } 45 46 if curr_height == start { 47 ret.push_str(&format!("{curr_height}")); 48 } else { 49 ret.push_str(&format!("{start}-{curr_height}")); 50 } 51 } 52 53 ret.push(']'); 54 ret 55 } 56 57 #[cfg(test)] 58 mod tests { 59 use super::rangify_heights; 60 61 #[test] 62 fn test_rangify_heights_empty() { 63 let heights = &[]; 64 65 let rangified = rangify_heights(heights); 66 assert_eq!(rangified, "[]"); 67 } 68 69 #[test] 70 fn test_rangify_heights_single_value() { 71 let heights = &[52425]; 72 73 let rangified = rangify_heights(heights); 74 assert_eq!(rangified, "[52425]"); 75 } 76 77 #[test] 78 fn test_rangify_large_continuous() { 79 let start = 16353; 80 let end = start + 52414; 81 82 let heights: Vec<u32> = (start..=end).collect(); 83 84 let rangified = rangify_heights(&heights); 85 assert_eq!(rangified, format!("[{start}-{end}]")); 86 } 87 88 #[test] 89 fn test_rangify_many_small() { 90 let mut heights = vec![]; 91 let mut expected = vec![]; 92 93 for idx in 0..100 { 94 let start = idx * 4; 95 let mid = idx * 4 + 1; 96 let end = idx * 4 + 2; 97 expected.push(format!("{start}-{end}")); 98 heights.push(start); 99 heights.push(mid); 100 heights.push(end); 101 } 102 103 let rangified = rangify_heights(&heights); 104 let expected = format!("[{}]", expected.join(", ")); 105 106 assert_eq!(rangified, expected); 107 } 108 109 #[test] 110 fn test_rangify_heights_multiple_ranges() { 111 let heights = &[0, 1, 3, 4, 5, 6, 7, 10, 11, 12, 20, 22, 23, 24, 25, 27, 28]; 112 113 let rangified = rangify_heights(heights); 114 assert_eq!(rangified, "[0-1, 3-7, 10-12, 20, 22-25, 27-28]"); 115 } 116 }