lowdash 0.5.3

A Lodash inspired utility library to manipulate array and object for Rust
Documentation
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use std::hash::{Hash, Hasher};
use std::{
    any::TypeId,
    sync::atomic::{AtomicU64, Ordering},
    time::SystemTime,
};

#[derive(Clone, Debug)]
pub struct Float(pub f64);

impl PartialEq for Float {
    fn eq(&self, other: &Self) -> bool {
        self.0.to_bits() == other.0.to_bits()
    }
}

impl Eq for Float {}

impl Hash for Float {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.to_bits().hash(state);
    }
}

/// Determines if the collection contains floating-point numbers (`f32` or `f64`).
///
/// # Arguments
/// * `collection` - A slice of items.
///
/// # Returns
/// * `true` if all elements in the collection are of type `f32` or `f64`, otherwise `false`.
#[allow(dead_code)]
pub fn is_collection_float(collection: &[Box<dyn std::any::Any>]) -> bool {
    collection.iter().all(|item| {
        let type_id = item.type_id();
        type_id == TypeId::of::<f32>() || type_id == TypeId::of::<f64>()
    })
}

/// Determines if the given generic is floating-point numbers (`f32` or `f64`).
///
/// # Returns
///
/// * `bool` - `true` if given generic is floating-point numbers, else `false`.
#[allow(dead_code)]
pub fn is_floats<T: 'static>() -> bool {
    TypeId::of::<T>() == TypeId::of::<f32>() || TypeId::of::<T>() == TypeId::of::<f64>()
}

/// Returns a pseudo-random index from the collection.
///
/// # Arguments
/// * `n` - The upper bound of the random index (exclusive).
///
/// # Returns
/// * `usize` - A pseudo-random index from 0 to n-1.
#[allow(dead_code)]
pub fn random_usize(maximum: usize) -> usize {
    static COUNTER: AtomicU64 = AtomicU64::new(0);

    if maximum == 0 {
        return 0;
    }

    let nanos = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos() as u64;

    let pid = std::process::id() as u64;

    let tid_str = format!("{:?}", std::thread::current().id());
    let tid_hash = tid_str
        .bytes()
        .fold(0u64, |acc, b| acc.wrapping_add(b as u64));

    // Increment the global counter atomically
    let counter = COUNTER.fetch_add(1, Ordering::Relaxed);

    // Combine entropy sources with prime multipliers and the counter for better distribution
    let mixed = nanos
        .wrapping_mul(0x517cc1b727220a95) // Prime multiplier
        .wrapping_add(pid)
        .wrapping_mul(0x2545f4914f6cdd1d) // Another prime
        ^ tid_hash
        ^ counter;

    // Calculate the random index within bounds
    (mixed % (maximum as u64)) as usize
}

/// Returns a pseudo-random index from the collection using a seed.
///
/// # Arguments
/// * `n` - The upper bound of the random index (exclusive).
/// * `seed` - The seed value for the random number generator.
///
/// # Returns
/// * `usize` - A pseudo-random index from 0 to n-1.
#[allow(dead_code)]
pub fn random_usize_with_seed(n: usize, seed: u64) -> usize {
    static COUNTER: AtomicU64 = AtomicU64::new(0);

    if n == 0 {
        return 0;
    }

    let pid = std::process::id() as u64;
    let tid_hash = format!("{:?}", std::thread::current().id())
        .bytes()
        .fold(0u64, |acc, b| acc.wrapping_add(b as u64));

    // Increment the global counter atomically
    let counter = COUNTER.fetch_add(1, Ordering::Relaxed);

    // Combine entropy sources with prime multipliers and the counter for better distribution
    let mixed = seed
        .wrapping_mul(0x517cc1b727220a95) // Prime multiplier
        .wrapping_add(pid)
        .wrapping_mul(0x2545f4914f6cdd1d) // Another prime
        ^ tid_hash
        ^ counter;

    // Calculate the random index within bounds
    (mixed % (n as u64)) as usize
}

/// Calculates the ceiling of the base-2 logarithm of a number.
///
/// # Arguments
///
/// * `n` - The number to calculate the ceiling log2 for.
///
/// # Returns
///
/// * The smallest integer greater than or equal to log2(n).
#[allow(dead_code)]
pub fn ceil_log2(n: usize) -> usize {
    if n <= 1 {
        return 1;
    }
    let mut bits = 0;
    let mut value = n - 1;
    while value > 0 {
        value >>= 1;
        bits += 1;
    }
    bits
}

/// Generates a pseudo-random `u64` number using entropy sources.
///
/// Combines system time, process ID, and thread ID to generate randomness.
///
/// # Returns
/// * A pseudo-random `u64` number.
#[allow(dead_code)]
pub fn random_u64() -> u64 {
    static COUNTER: AtomicU64 = AtomicU64::new(0);

    let nanos = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos() as u64;

    let pid = std::process::id() as u64;

    let tid_str = format!("{:?}", std::thread::current().id());
    let tid_hash = tid_str
        .bytes()
        .fold(0u64, |acc, b| acc.wrapping_add(b as u64));

    // Increment the global counter atomically
    let counter = COUNTER.fetch_add(1, Ordering::Relaxed);

    // Combine entropy sources with prime multipliers and the counter for better distribution
    let mixed = nanos
        .wrapping_mul(0x517cc1b727220a95) // Prime multiplier
        .wrapping_add(pid)
        .wrapping_mul(0x2545f4914f6cdd1d) // Another prime
        ^ tid_hash
        ^ counter;

    // Add additional randomness using a simple linear congruential generator (LCG)
    mixed.wrapping_mul(6364136223846793005).wrapping_add(1)
}

/// Lowercase letters charset.
#[allow(dead_code)]
pub const LOWERCASE_LETTERS_CHARSET: &[char] = &[
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
    't', 'u', 'v', 'w', 'x', 'y', 'z',
];

/// Uppercase letters charset.
#[allow(dead_code)]
pub const UPPERCASE_LETTERS_CHARSET: &[char] = &[
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
    'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];

/// Letters charset (both lowercase and uppercase).
#[allow(dead_code)]
pub const LETTERS_CHARSET: &[char] = &[
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
    't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
    'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];

/// Numbers charset.
#[allow(dead_code)]
pub const NUMBERS_CHARSET: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

/// Alphanumeric charset (letters and numbers).
#[allow(dead_code)]
pub const ALPHANUMERIC_CHARSET: &[char] = &[
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
    't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
    'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4',
    '5', '6', '7', '8', '9',
];

/// Special characters charset.
#[allow(dead_code)]
pub const SPECIAL_CHARSET: &[char] = &[
    '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '=', '[', ']', '{', '}', '|',
    ';', '\'', ':', '"', ',', '.', '/', '<', '>', '?',
];

/// All characters charset (alphanumeric and special characters).
#[allow(dead_code)]
pub const ALL_CHARSET: &[char] = &[
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
    't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
    'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4',
    '5', '6', '7', '8', '9', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '=',
    '[', ']', '{', '}', '|', ';', '\'', ':', '"', ',', '.', '/', '<', '>', '?',
];

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::*;

    #[test]
    fn test_is_collection_float_with_float() {
        let collection: Vec<Box<dyn std::any::Any>> =
            vec![Box::new(1.0f64), Box::new(2.0f64), Box::new(3.0f64)];
        let result = is_collection_float(&collection);
        assert!(result);
    }

    #[test]
    fn test_is_collection_float_with_integer() {
        let collection: Vec<Box<dyn std::any::Any>> = vec![Box::new(1), Box::new(2), Box::new(3)];
        let result = is_collection_float(&collection);
        assert!(!result);
    }

    #[test]
    fn test_is_collection_float_with_mixed_types() {
        let collection: Vec<Box<dyn std::any::Any>> =
            vec![Box::new(1.0f64), Box::new(2), Box::new(3.0f64)];
        let result = is_collection_float(&collection);
        assert!(!result);
    }

    #[test]
    fn test_is_floats_with_f32() {
        let result = is_floats::<f32>();
        assert!(result);
    }

    #[test]
    fn test_is_floats_with_f64() {
        let result = is_floats::<f64>();
        assert!(result);
    }

    #[test]
    fn test_is_floats_with_other_type() {
        let result = is_floats::<i32>();
        assert!(!result);
    }

    #[test]
    fn test_random_usize_uniqueness() {
        let n = 100;
        let iterations = 1000;
        let mut results = HashSet::new();

        for _ in 0..iterations {
            let index = random_usize(n);
            assert!(index < n, "random_usize({}) returned {}", n, index);
            results.insert(index);
        }

        // Expect a good distribution; not all unique, but no immediate duplicates in sequence
        assert!(!results.is_empty(), "Random index set should not be empty");
    }

    #[test]
    fn test_random_usize_range() {
        let n = 50;
        for _ in 0..1000 {
            let index = random_usize(n);
            assert!(index < n, "random_usize({}) returned {}", n, index);
        }
    }

    #[test]
    fn test_random_usize_zero() {
        let n = 0;
        let index = random_usize(n);
        assert_eq!(index, 0, "random_usize(0) should return 0, got {}", index);
    }

    #[test]
    fn test_random_usize_variety() {
        let n = 10;
        let mut previous = random_usize(n);
        for _ in 0..100 {
            let current = random_usize(n);
            // It's possible to get the same index; ensure not always the same
            if current != previous {
                return;
            }
            previous = current;
        }
        panic!("random_usize should produce varied results");
    }

    #[test]
    fn test_random_usize_with_seed_uniqueness() {
        let n = 100;
        let seed = 42;
        let iterations = 1000;
        let mut results = HashSet::new();

        for _ in 0..iterations {
            let index = random_usize_with_seed(n, seed);
            assert!(
                index < n,
                "random_usize_with_seed({}, {}) returned {}",
                n,
                seed,
                index
            );
            results.insert(index);
        }

        // Expect a good distribution; not all unique
        assert!(
            results.len() > 50,
            "Random index set does not have enough unique values"
        );
    }

    #[test]
    fn test_random_usize_with_seed_range() {
        let n = 50;
        let seed = 12345;
        for _ in 0..1000 {
            let index = random_usize_with_seed(n, seed);
            assert!(
                index < n,
                "random_usize_with_seed({}, {}) returned {}",
                n,
                seed,
                index
            );
        }
    }

    #[test]
    fn test_random_usize_with_seed_zero() {
        let n = 0;
        let seed = 42;
        let index = random_usize_with_seed(n, seed);
        assert_eq!(
            index, 0,
            "random_usize_with_seed({}, {}) should return 0, got {}",
            n, seed, index
        );
    }

    #[test]
    fn test_random_usize_with_seed_variety() {
        let n = 10;
        let seed = 999;
        let mut previous = random_usize_with_seed(n, seed);
        for _ in 0..100 {
            let current = random_usize_with_seed(n, seed);
            // It's possible to get the same index; ensure not always the same
            if current != previous {
                return;
            }
            previous = current;
        }
        panic!("random_usize_with_seed should produce varied results");
    }

    #[test]
    fn test_lowercase_letters_charset() {
        assert_eq!(LOWERCASE_LETTERS_CHARSET.len(), 26);
        for c in LOWERCASE_LETTERS_CHARSET {
            assert!(*c >= 'a' && *c <= 'z');
        }
    }

    #[test]
    fn test_uppercase_letters_charset() {
        assert_eq!(UPPERCASE_LETTERS_CHARSET.len(), 26);
        for c in UPPERCASE_LETTERS_CHARSET {
            assert!(*c >= 'A' && *c <= 'Z');
        }
    }

    #[test]
    fn test_letters_charset() {
        assert_eq!(LETTERS_CHARSET.len(), 52);
        for c in LETTERS_CHARSET {
            assert!((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z'));
        }
    }

    #[test]
    fn test_numbers_charset() {
        assert_eq!(NUMBERS_CHARSET.len(), 10);
        for c in NUMBERS_CHARSET {
            assert!(*c >= '0' && *c <= '9');
        }
    }

    #[test]
    fn test_alphanumeric_charset() {
        assert_eq!(ALPHANUMERIC_CHARSET.len(), 62);
        for c in ALPHANUMERIC_CHARSET {
            assert!(
                (*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z') || (*c >= '0' && *c <= '9')
            );
        }
    }

    #[test]
    fn test_special_charset() {
        let expected_special_chars: Vec<char> = vec![
            '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '=', '[', ']', '{',
            '}', '|', ';', '\'', ':', '"', ',', '.', '/', '<', '>', '?',
        ];
        assert_eq!(SPECIAL_CHARSET.len(), expected_special_chars.len());
        for c in SPECIAL_CHARSET {
            assert!(expected_special_chars.contains(c));
        }
    }

    #[test]
    fn test_ceil_log2() {
        // Test cases: (input, expected_output)
        let test_cases = [
            (0, 1),
            (1, 1),
            (2, 1),
            (3, 2),
            (4, 2),
            (5, 3),
            (7, 3),
            (8, 3),
            (9, 4),
            (16, 4),
            (17, 5),
            (31, 5),
            (32, 5),
            (33, 6),
            (64, 6),
            (65, 7),
            (127, 7),
            (128, 7),
            (129, 8),
        ];

        for &(input, expected) in &test_cases {
            assert_eq!(
                ceil_log2(input),
                expected,
                "ceil_log2({}) should be {}",
                input,
                expected
            );
        }
    }

    #[test]
    fn test_random_u64_uniqueness() {
        let mut results = HashSet::new();
        let iterations = 1000;

        for i in 0..iterations {
            let rand_val = random_u64();
            // Assuming a high chance of uniqueness, we check for duplicates
            assert!(
                results.insert(rand_val),
                "Duplicate value found: {} in iteration {}",
                rand_val,
                i
            );
        }
    }

    #[test]
    fn test_random_u64_range() {
        for _ in 0..1000 {
            let rand_val = random_u64();
            // Since u64 can be any value, we just check it's a valid u64
            // Not much to assert here, but ensure no panics
            let _ = rand_val;
        }
    }
}