Skip to content

Commit 2029a43

Browse files
committed
utils are now successfully compiling, will probably remove linked_list soon
1 parent f716898 commit 2029a43

File tree

5 files changed

+94
-90
lines changed

5 files changed

+94
-90
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/iter.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
//! A collection of custom iterators for iterating things!
77
//!
88
9-
pub use prev_iter::ZipPrev;
9+
pub use self::prev_iter::ZipPrev;
1010

1111
pub mod prev_iter {
1212

1313
/// A trait that produces the previous step's element along
1414
/// with the current step.
15-
pub trait ZipPrev: Iterator {
16-
fn zip_prev(self) -> PairIterator;
15+
pub trait ZipPrev<A, I>: Iterator<A>
16+
where A: Clone, I: Iterator<A> {
17+
fn zip_prev(self) -> PairIterator<A, I>;
1718
}
1819

1920
/// A struct produced by the ZipPrevious iterator.
@@ -26,18 +27,19 @@ pub mod prev_iter {
2627
where A: Clone, I: Iterator<A> {
2728
#[inline]
2829
fn next(&mut self) -> Option<(A, Option<A>)> {
29-
let PairIterator(ref mut part_iter, ref mut maybe_prev) = *self;
30-
if let Some(part) = self.part_iter.next() {
31-
let maybe_prev = self.maybe_prev.clone();
32-
self.maybe_prev = Some(part);
33-
Some(part, maybe_prev)
30+
let PairIterator { ref mut iter, ref mut maybe_prev } = *self;
31+
if let Some(item) = iter.next() {
32+
let maybe_prev_clone = maybe_prev.clone();
33+
*maybe_prev = Some(item.clone());
34+
Some((item, maybe_prev_clone))
3435
} else {
3536
None
3637
}
3738
}
3839
}
3940

40-
impl<A, I> ZipPrev for I where A: Clone, I: Iterator<A> {
41+
impl<A, I> ZipPrev<A, I> for I
42+
where A: Clone, I: Iterator<A> {
4143
fn zip_prev(self) -> PairIterator<A, I> {
4244
PairIterator {
4345
iter: self,

src/lib.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1-
#[test]
2-
fn it_works() {
3-
}
1+
//!
2+
//! mod.rs
3+
//!
4+
//! Created by Mitchell Nordine at 11:25AM on November 07, 2014.
5+
//!
6+
//!
7+
8+
#![feature(if_let)]
9+
10+
pub use iter::{
11+
ZipPrev,
12+
};
13+
pub use math::{
14+
clamp,
15+
fast_floor,
16+
fmod,
17+
grad1,
18+
in_range,
19+
lerp,
20+
map_range,
21+
modulo,
22+
remainder,
23+
wrap,
24+
};
25+
26+
pub mod iter;
27+
pub mod math;
28+

src/math.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,50 @@ use std::mem;
99
use std::num::One;
1010
use std::num::zero;
1111

12-
/// The modulo function.
12+
/// Clamp a value to a range.
1313
#[inline]
14-
pub fn modulo<I: Int>(a: I, b: I) -> I {
15-
match a % b {
16-
r if (r > zero() && b < zero())
17-
|| (r < zero() && b > zero()) => (r + b),
18-
r => r,
14+
pub fn clamp<T: Num + Primitive>(val: T, min: T, max: T) -> T {
15+
if val < min { min } else { if val > max { max } else { val } }
16+
}
17+
18+
/// Floors value.
19+
#[inline]
20+
pub fn fast_floor<F: Float + FromPrimitive + ToPrimitive>(f: F) -> int {
21+
return if f > FromPrimitive::from_int(0).unwrap() {
22+
f.to_int().unwrap()
23+
} else {
24+
f.to_int().unwrap() - One::one()
1925
}
2026
}
2127

28+
/// Models the CPP fmod function.
29+
#[inline]
30+
pub fn fmod<F: Float + FromPrimitive + ToPrimitive>(numer: F, denom: F) -> F {
31+
let rquot: F = (numer / denom).floor();
32+
numer - rquot * denom
33+
}
34+
35+
/// Implementation of grad1 for the ported _slang_library_noise1 method
36+
#[inline]
37+
pub fn grad1(hash: int, x: f32) -> f32 {
38+
let h: int = hash & 15;
39+
let mut grad: f32 = 1.0f32 + ((h & 7) as f32);
40+
if h & 8 > 0 { grad = (-1.0f32) * grad; }
41+
grad * x
42+
}
43+
44+
/// Check if value is in range.
45+
#[inline]
46+
pub fn in_range<T: Num + Primitive>(val: T, min: T, max: T) -> bool {
47+
val >= min && val <= max
48+
}
49+
50+
/// Interpolate from start to stop 'amt' amount.
51+
#[inline]
52+
pub fn lerp(start: f32, stop: f32, amt: f32) -> f32 {
53+
start + (stop - start) * amt
54+
}
55+
2256
/// Map a value from a given range to a new given range.
2357
#[inline]
2458
pub fn map_range<X: Num + Copy + FromPrimitive + ToPrimitive,
@@ -41,22 +75,21 @@ pub fn map_range<X: Num + Copy + FromPrimitive + ToPrimitive,
4175
).unwrap()
4276
}
4377

44-
/// Clamp a value to a range.
45-
#[inline]
46-
pub fn clamp<T: Num + Primitive>(val: T, min: T, max: T) -> T {
47-
if val < min { min } else { if val > max { max } else { val } }
48-
}
49-
50-
/// Check if value is in range.
78+
/// Models the CPP remainder function.
5179
#[inline]
52-
pub fn in_range<T: Num + Primitive>(val: T, min: T, max: T) -> bool {
53-
val >= min && val <= max
80+
pub fn remainder<F: Float + FromPrimitive + ToPrimitive>(numer: F, denom: F) -> F {
81+
let rquot: F = (numer / denom).round();
82+
numer - rquot * denom
5483
}
5584

56-
/// Interpolate from start to stop 'amt' amount.
85+
/// The modulo function.
5786
#[inline]
58-
pub fn lerp(start: f32, stop: f32, amt: f32) -> f32 {
59-
start + (stop - start) * amt
87+
pub fn modulo<I: Int>(a: I, b: I) -> I {
88+
match a % b {
89+
r if (r > zero() && b < zero())
90+
|| (r < zero() && b > zero()) => (r + b),
91+
r => r,
92+
}
6093
}
6194

6295
/// Wrap value to a range.
@@ -68,39 +101,6 @@ pub fn wrap(val: f32, mut from: f32, mut to: f32) -> f32 {
68101
val - cycle * ((val - from) / cycle).floor()
69102
}
70103

71-
/// Floors value.
72-
#[inline]
73-
pub fn fast_floor<F: Float + FromPrimitive + ToPrimitive>(f: F) -> int {
74-
return if f > FromPrimitive::from_int(0).unwrap() {
75-
f.to_int().unwrap()
76-
} else {
77-
f.to_int().unwrap() - One::one()
78-
}
79-
}
80-
81-
/// Models the CPP remainder function.
82-
#[inline]
83-
pub fn remainder<F: Float + FromPrimitive + ToPrimitive>(numer: F, denom: F) -> F {
84-
let rquot: F = (numer / denom).round();
85-
numer - rquot * denom
86-
}
87-
88-
/// Models the CPP fmod function.
89-
#[inline]
90-
pub fn fmod<F: Float + FromPrimitive + ToPrimitive>(numer: F, denom: F) -> F {
91-
let rquot: F = (numer / denom).floor();
92-
numer - rquot * denom
93-
}
94-
95-
/// Implementation of grad1 for the ported _slang_library_noise1 method
96-
#[inline]
97-
pub fn grad1(hash: int, x: f32) -> f32 {
98-
let h: int = hash & 15;
99-
let mut grad: f32 = 1.0f32 + ((h & 7) as f32);
100-
if h & 8 > 0 { grad = (-1.0f32) * grad; }
101-
grad * x
102-
}
103-
104104
/// Implementation of perm for the ported _slang_library_noise1 method
105105
const PERM : [u8, ..512] = [151u8, 160u8, 137u8, 91u8, 90u8, 15u8,
106106
131u8, 13u8, 201u8,95u8,96u8,53u8,194u8,233u8,7u8,225u8,140u8,36u8,103u8,30u8,69u8,142u8,8u8,99u8,37u8,240u8,21u8,10u8,23u8,

src/mod.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)