Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
al1-ce committed May 11, 2024
1 parent 84d4693 commit 58464ec
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 72 deletions.
5 changes: 2 additions & 3 deletions core/sily/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ bool isOneOf(T)(T val, T[] vals ...) {
}

/**
Fills array with values `val` up to `size` if it's not 0
Fills array with values `val`
Params:
arr = Array to fill
val = Values to fill with
Returns: Filled array
*/
T[] fill(T)(T[] arr, T val){

arr = arr.dup;
T[] arr = new T[](arr.length);

for (int i = 0; i < arr.length; i ++) {
arr[i] = val;
Expand Down
11 changes: 5 additions & 6 deletions core/sily/file.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ Performs chmod +x on file
Params:
name = Path to file
*/
void chmodpx(string name) {
name = name.buildAbsolutePath();
void chmodpx(string path) {
path = path.buildAbsolutePath();

if (!name.exists) {
if (!path.exists) {
return;
}

name.setAttributes(name.getAttributes | octal!700);
path.setAttributes(path.getAttributes | octal!700);
}

/**
Expand All @@ -36,8 +36,7 @@ string readFile(string path) {
path = path.buildAbsolutePath();

if (!path.isFile) {
writefln("ERROR: Unable to find file at '%s'.", path);
throw new FileException("Unable to read file.");
throw new FileException("Unable to find file at '%s'.", path);
}

return readText(path);
Expand Down
11 changes: 7 additions & 4 deletions core/sily/path.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ string buildRelativePath(string p) {
/**
Returns array of files/dirs from path
Params:
pathname = Path to dir
path = Path to dir
Returns:
*/
string[] listdir(string pathname) {
string[] listdir(string path, bool listDirs = true, bool listFiles = true) {
import std.algorithm;
import std.array;
import std.file;
import std.path;

return std.file.dirEntries(pathname, SpanMode.shallow)
.filter!(a => a.isFile)
path = buildAbsolutePath(path);
if (!exists(path) || !isDir(path)) return [];

return std.file.dirEntries(path, SpanMode.shallow)
.filter!(a => listFiles ? true : a.isFile || listDirs ? true : a.isDir)
.map!((return a) => baseName(a.name))
.array;
}
24 changes: 14 additions & 10 deletions core/sily/random.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Random number generation utils
module sily.random;

import std.math;
import std.random;
import std.random: StdRandom = Random, unpredictableSeed;
import std.algorithm.comparison;
import std.conv: to;
import std.datetime: Clock;
Expand All @@ -34,15 +34,15 @@ randf(4, 20);
rand!ulong();
---
+/
struct RNG {
struct Random {
private uint _seed = defaultSeed;
private Random _rnd;
private ulong _calls;
private StdRandom _rnd;
private size_t _calls;

/// Creates RNG with set seed
this(uint p_seed) {
_seed = p_seed;
_rnd = Random(_seed);
_rnd = StdRandom(_seed);
}

/// Randomizes seed
Expand All @@ -56,13 +56,14 @@ struct RNG {
@property public void seed(uint p_seed) {
_seed = p_seed;
_rnd.seed(_seed);
_calls = 0;
}

/// Returns current seed
@property public uint seed() { return _seed; }

/// Alias to std.random default seed
public static alias defaultSeed = Random.defaultSeed;
public static alias defaultSeed = StdRandom.defaultSeed;
/// Alias to std.random unpredictable seed
public static alias randomSeed = unpredictableSeed;

Expand All @@ -76,7 +77,7 @@ struct RNG {
alias randi = rand!int;
/// Ditto
alias randl = rand!long;

template rand(T) {
static if(isFloatingPoint!T) {
/// Returns random value between 0 and T.max or custom min and max
Expand Down Expand Up @@ -109,9 +110,12 @@ struct RNG {
}

/// Skips current random value
public void skip() {
_rnd.popFront();
_calls ++;
public void skip(size_t amount = 1) {
while (amount > 0) {
_rnd.popFront();
++_calls;
--amount;
}
}

/// Skips N amount of random values
Expand Down

0 comments on commit 58464ec

Please sign in to comment.