Skip to content

Commit 94d071b

Browse files
committed
Updated to latest rust, added take_only Vec method, add factorisation module
1 parent 2029a43 commit 94d071b

File tree

4 files changed

+138
-15
lines changed

4 files changed

+138
-15
lines changed

src/factorisation.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//!
2+
//! factorisation.rs
3+
//!
4+
//! Created by Mitchell Nordine at 05:03AM on May 29, 2014.
5+
//!
6+
//!
7+
8+
use std::num::Int;
9+
use super::modulo;
10+
11+
/// Check if the queried value is a factor of num.
12+
#[inline]
13+
pub fn is_factor<I>(num: I, query: I) -> bool
14+
where I: Int {
15+
modulo(num, query) == Int::zero()
16+
}
17+
18+
/// Check if any of the queried values are a factor of num.
19+
#[inline]
20+
pub fn are_any_factors<I>(num: I, queries: &[I]) -> bool
21+
where I: Int + Copy {
22+
queries.iter().any(|query| is_factor(num, *query))
23+
}
24+
25+
/// Check if all of the queried values are a factor of num.
26+
#[inline]
27+
pub fn are_all_factors<I>(num: I, queries: &[I]) -> bool
28+
where I: Int + Copy {
29+
queries.iter().all(|query| is_factor(num, *query))
30+
}
31+
32+
/// Is the number prime?
33+
#[inline]
34+
pub fn is_prime<I>(num: I) -> bool
35+
where I: Int + Copy {
36+
match get_all_factors(num).len() {
37+
1 | 2 => true,
38+
_ => false
39+
}
40+
}
41+
42+
/// Return the lowest non-one factor.
43+
#[inline]
44+
pub fn lowest_non_one<I>(n: I) -> Option<I>
45+
where I: Int + Copy {
46+
let one: I = Int::one();
47+
let mut i: I = one + one;
48+
while i * i <= n {
49+
if n % i == Int::zero() {
50+
if i > one { return Some(i) }
51+
else if i * i != n {
52+
let n_div_i = n / i;
53+
if n_div_i > one { return Some(n_div_i) }
54+
}
55+
}
56+
i = i + one;
57+
}
58+
None
59+
}
60+
61+
/// Get all factors for 'n'.
62+
#[inline]
63+
pub fn get_all_factors<I>(n: I) -> Vec<I> where I: Int {
64+
let one: I = Int::one();
65+
let mut factors: Vec<I> = vec![one];
66+
let mut i: I = one + one;
67+
while i * i <= n {
68+
if n % i == Int::zero() {
69+
factors.push(i);
70+
if i * i != n { factors.push(n / i); }
71+
}
72+
i = i + one;
73+
}
74+
factors.push(n);
75+
factors.sort_by(|a, b| a.cmp(b));
76+
return factors;
77+
}
78+

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
88
#![feature(if_let)]
99

10+
pub use factorisation::{
11+
is_factor,
12+
are_any_factors,
13+
are_all_factors,
14+
is_prime,
15+
lowest_non_one,
16+
get_all_factors,
17+
};
1018
pub use iter::{
1119
ZipPrev,
1220
};
@@ -22,7 +30,12 @@ pub use math::{
2230
remainder,
2331
wrap,
2432
};
33+
pub use vec::{
34+
TakeOnly,
35+
};
2536

37+
pub mod factorisation;
2638
pub mod iter;
2739
pub mod math;
40+
pub mod vec;
2841

src/math.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,46 @@
66
///
77
88
use std::mem;
9-
use std::num::One;
10-
use std::num::zero;
9+
use std::num::{
10+
Float,
11+
Int,
12+
};
1113

1214
/// Clamp a value to a range.
1315
#[inline]
14-
pub fn clamp<T: Num + Primitive>(val: T, min: T, max: T) -> T {
16+
pub fn clamp<T: PartialOrd>(val: T, min: T, max: T) -> T {
1517
if val < min { min } else { if val > max { max } else { val } }
1618
}
1719

1820
/// Floors value.
1921
#[inline]
20-
pub fn fast_floor<F: Float + FromPrimitive + ToPrimitive>(f: F) -> int {
22+
pub fn fast_floor<F: Float + FromPrimitive + ToPrimitive>(f: F) -> i64 {
2123
return if f > FromPrimitive::from_int(0).unwrap() {
22-
f.to_int().unwrap()
24+
f.to_i64().unwrap()
2325
} else {
24-
f.to_int().unwrap() - One::one()
26+
f.to_i64().unwrap() - Int::one()
2527
}
2628
}
2729

2830
/// Models the CPP fmod function.
2931
#[inline]
30-
pub fn fmod<F: Float + FromPrimitive + ToPrimitive>(numer: F, denom: F) -> F {
32+
pub fn fmod<F: Float>(numer: F, denom: F) -> F {
3133
let rquot: F = (numer / denom).floor();
3234
numer - rquot * denom
3335
}
3436

3537
/// Implementation of grad1 for the ported _slang_library_noise1 method
3638
#[inline]
37-
pub fn grad1(hash: int, x: f32) -> f32 {
38-
let h: int = hash & 15;
39+
pub fn grad1(hash: i64, x: f32) -> f32 {
40+
let h: i64 = hash & 15;
3941
let mut grad: f32 = 1.0f32 + ((h & 7) as f32);
4042
if h & 8 > 0 { grad = (-1.0f32) * grad; }
4143
grad * x
4244
}
4345

4446
/// Check if value is in range.
4547
#[inline]
46-
pub fn in_range<T: Num + Primitive>(val: T, min: T, max: T) -> bool {
48+
pub fn in_range<T: Ord>(val: T, min: T, max: T) -> bool {
4749
val >= min && val <= max
4850
}
4951

@@ -55,8 +57,8 @@ pub fn lerp(start: f32, stop: f32, amt: f32) -> f32 {
5557

5658
/// Map a value from a given range to a new given range.
5759
#[inline]
58-
pub fn map_range<X: Num + Copy + FromPrimitive + ToPrimitive,
59-
Y: Num + Copy + FromPrimitive + ToPrimitive>
60+
pub fn map_range<X: Copy + FromPrimitive + ToPrimitive,
61+
Y: Copy + FromPrimitive + ToPrimitive>
6062
(val: X, in_min: X, in_max: X, out_min: Y, out_max: Y) -> Y {
6163
let (val_f, in_min_f, in_max_f, out_min_f, out_max_f) = (
6264
val.to_f64().unwrap(),
@@ -86,9 +88,9 @@ pub fn remainder<F: Float + FromPrimitive + ToPrimitive>(numer: F, denom: F) ->
8688
#[inline]
8789
pub fn modulo<I: Int>(a: I, b: I) -> I {
8890
match a % b {
89-
r if (r > zero() && b < zero())
90-
|| (r < zero() && b > zero()) => (r + b),
91-
r => r,
91+
r if (r > Int::zero() && b < Int::zero())
92+
|| (r < Int::zero() && b > Int::zero()) => (r + b),
93+
r => r,
9294
}
9395
}
9496

src/vec.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//!
2+
//! vec.rs
3+
//!
4+
//! Created by Mitchell Nordine at 01:25AM on November 18, 2014.
5+
//!
6+
//!
7+
8+
9+
/// Take only the elements that return `true` from the given condition.
10+
pub trait TakeOnly<T> {
11+
/// Take only the elements that return `true` from the given condition.
12+
fn take_only(&mut self, |&T| -> bool) -> Vec<T>;
13+
}
14+
15+
impl<T> TakeOnly<T> for Vec<T> {
16+
fn take_only(&mut self, cond: |&T| -> bool) -> Vec<T> {
17+
let mut vec = Vec::with_capacity(self.len());
18+
let mut i = 0;
19+
while i < self.len() {
20+
let cond_result = cond(&self[i]);
21+
if cond_result {
22+
vec.push(self.remove(i).expect("Tried to take T at `i` - no elem there."));
23+
} else {
24+
i += 1;
25+
}
26+
}
27+
vec
28+
}
29+
}
30+

0 commit comments

Comments
 (0)