1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
//! # iter-num-tools
//!
//! [![Build Status](https://img.shields.io/github/workflow/status/conradludgate/iter-num-tools/coverage/main?style=flat-square)][actions]
//! [![Rust Documentation](https://img.shields.io/crates/v/iter-num-tools?color=blue&label=docs&style=flat-square)][docs.rs]
//! [![Latest Version](https://img.shields.io/crates/d/iter-num-tools?style=flat-square)][crates.io]
//! [![Code Coverage](https://img.shields.io/codecov/c/gh/conradludgate/iter-num-tools?style=flat-square)][codecov]
//!
//! [actions]: https://github.com/conradludgate/iter-num-tools/actions?query=branch%3Amain
//! [crates.io]: https://crates.io/crates/iter_num_tools
//! [docs.rs]: https://docs.rs/iter_num_tools
//! [codecov]: https://codecov.io/gh/conradludgate/iter-num-tools
//!
//! This is a collection if iterator extensions that make heavy use of number properties. Mostly extending on Range.
//!
//! ## LinSpace
//!
//! LinSpace is an iterator over a range with a fixed number of values all evenly spaced.
//!
//! ```rust
//! use iter_num_tools::lin_space;
//!
//! // Count from 1.0 up to and including 5.0, with 5 numbers counted in total
//! let it = lin_space(1.0..=5.0, 5);
//! assert!(it.eq([1.0, 2.0, 3.0, 4.0, 5.0]));
//!
//! // Count from 0.0 up to and excluding 5.0, with 5 numbers counted in total
//! let it = lin_space(0.0..5.0, 5);
//! assert!(it.eq([0.0, 1.0, 2.0, 3.0, 4.0]));
//! ```
//!
//! ## GridSpace
//!
//! GridSpace extends on [LinSpace](#linspace).
//!
//! ```rust
//! use iter_num_tools::grid_space;
//!
//! // count in 2 dimensions (excluding end points),
//! // from 0.0 up to 1.0 in the x direction with 2 even steps,
//! // and 0.0 up to 2.0 in the y direction with 4 even steps
//! let it = grid_space([0.0, 0.0]..[1.0, 2.0], [2, 4]);
//! assert!(it.eq([
//! [0.0, 0.0], [0.5, 0.0],
//! [0.0, 0.5], [0.5, 0.5],
//! [0.0, 1.0], [0.5, 1.0],
//! [0.0, 1.5], [0.5, 1.5],
//! ]));
//!
//! // count in 2 dimensions (including end points),
//! // from 0.0 up to 1.0 in the x direction,
//! // and 0.0 up to 2.0 in the y direction with 3 even steps in all directions
//! let it = grid_space([0.0, 0.0]..=[1.0, 2.0], 3);
//! assert!(it.eq([
//! [0.0, 0.0], [0.5, 0.0], [1.0, 0.0],
//! [0.0, 1.0], [0.5, 1.0], [1.0, 1.0],
//! [0.0, 2.0], [0.5, 2.0], [1.0, 2.0],
//! ]));
//! ```
//!
//! ## Arange
//!
//! Arange is similar to [LinSpace](#linspace), but instead of a fixed amount of steps, it steps by a fixed amount.
//!
//! ```rust
//! use iter_num_tools::arange;
//!
//! let it = arange(0.0..2.0, 0.5);
//! assert!(it.eq([0.0, 0.5, 1.0, 1.5]));
//! ```
//!
//! #### Note
//!
//! There is no inclusive version of arange. Consider the following
//!
//! ```rust,ignore
//! use iter_num_tools::arange;
//! let it = arange(0.0..=2.1, 0.5);
//! ```
//!
//! We would not expect 2.1 to ever be a value that the iterator will ever meet, but the range suggests it should be included. Therefore, no RangeInclusive implementation is provided.
//!
//! ## ArangeGrid
//!
//! ArangeGrid is the same as [GridSpace](#gridspace) but for [Arange](#arange) instead of [LinSpace](#linspace).
//!
//! ```rust
//! use iter_num_tools::arange_grid;
//!
//! // count in 2 dimensions,
//! // from 0.0 up to 1.0 in the x direction,
//! // and 0.0 up to 2.0 in the y direction,
//! // stepping by 0.5 each time
//! let it = arange_grid([0.0, 0.0]..[1.0, 2.0], 0.5);
//! assert!(it.eq([
//! [0.0, 0.0], [0.5, 0.0],
//! [0.0, 0.5], [0.5, 0.5],
//! [0.0, 1.0], [0.5, 1.0],
//! [0.0, 1.5], [0.5, 1.5],
//! ]));
//!
//! // count in 2 dimensions,
//! // from 0.0 up to 1.0 in the x direction stepping by 0.5 each time,
//! // and 0.0 up to 2.0 in the y direction stepping by 1.0 each time
//! let it = arange_grid([0.0, 0.0]..[1.0, 2.0], [0.5, 1.0]);
//! assert!(it.eq([
//! [0.0, 0.0], [0.5, 0.0],
//! [0.0, 1.0], [0.5, 1.0],
//! ]));
//! ```
//!
//! ## LogSpace
//!
//! LogSpace is similar to [LinSpace](#linspace), but instead of evenly spaced linear steps, it has evenly spaced logarithmic steps.
//!
//! ```rust
//! use iter_num_tools::log_space;
//! use itertools::zip_eq;
//!
//! // From 1.0 up to and including 1000.0, taking 4 logarithmic steps
//! let it = log_space(1.0..=1000.0, 4);
//! let expected: [f64; 4] = [1.0, 10.0, 100.0, 1000.0];
//!
//! assert!(zip_eq(it, expected).all(|(x, y)| (x-y).abs() < 1e-10));
//!
//! // From 1.0 up to 1000.0, taking 3 logarithmic steps
//! let it = log_space(1.0..1000.0, 3);
//! let expected: [f64; 3] = [1.0, 10.0, 100.0];
//!
//! assert!(zip_eq(it, expected).all(|(x, y)| (x-y).abs() < 1e-10));
//! ```
#![warn(missing_docs)]
#![cfg_attr(feature = "trusted_len", feature(trusted_len))]
#![cfg_attr(feature = "iter_advance_by", feature(iter_advance_by))]
#![cfg_attr(not(test), no_std)]
#[cfg(test)]
#[macro_use]
extern crate pretty_assertions;
mod arange;
mod arange_grid;
mod gridspace;
mod gridstep;
mod linspace;
mod logspace;
mod space;
mod step;
pub use arange::{arange, Arange, IntoArange, ToArange};
pub use arange_grid::{arange_grid, ArangeGrid, IntoArangeGrid, ToArangeGrid};
pub use gridspace::{grid_space, GridSpace, IntoGridSpace, ToGridSpace};
pub use gridstep::{grid_step, GridStep, IntoGridStep, ToGridStep};
pub use linspace::{lin_space, IntoLinSpace, LinSpace, ToLinSpace};
pub use logspace::{log_space, IntoLogSpace, LogSpace, ToLogSpace};
#[cfg(test)]
#[track_caller]
pub fn check_double_ended_iter<T: PartialEq + core::fmt::Debug, const N: usize>(
i: impl DoubleEndedIterator<Item = T> + Clone,
mut expected: [T; N],
) {
let actual = i.clone().collect::<Vec<_>>();
assert_eq!(actual, expected);
let actual = i.rev().collect::<Vec<_>>();
expected.reverse();
assert_eq!(actual, expected);
}
// pub trait Array<const N: usize> {
// type Item;
// }
// impl<T, const N: usize> Array<N> for [T; N] {
// type Item = T;
// }