From 7c6fd1d843fe005ca309d5b8de9c40b41599565c Mon Sep 17 00:00:00 2001 From: slowcheet4h Date: Sun, 4 Apr 2021 19:30:18 +0300 Subject: [PATCH] async_loop lifetime (arg) update --- pisi/unitedmeows/meowlib/async/Async.java | 53 +++- .../meowlib/async/BasicTaskPool.java | 2 +- pisi/unitedmeows/meowlib/async/Result.java | 28 +- pisi/unitedmeows/meowlib/etc/IAction.java | 16 +- pisi/unitedmeows/meowlib/etc/MLibSetting.java | 46 +-- pisi/unitedmeows/meowlib/etc/Tuple.java | 56 ++-- pisi/unitedmeows/meowlib/etc/Tuple3.java | 54 ++-- pisi/unitedmeows/meowlib/etc/Tuple4.java | 66 ++-- .../etc/memorable/MemorableElement.java | 14 +- .../meowlib/etc/memorable/MemorableList.java | 50 ++-- .../meowlib/etc/memorable/MemorableTail.java | 162 +++++----- .../meowlib/filesystem/kDirectory.java | 50 ++-- .../unitedmeows/meowlib/filesystem/kFile.java | 276 ++++++++--------- .../meowlib/lists/FlexibleArray.java | 200 ++++++------- pisi/unitedmeows/meowlib/lists/WarnList.java | 196 ++++++------ pisi/unitedmeows/meowlib/lists/kIterator.java | 12 +- pisi/unitedmeows/meowlib/math/MeowMath.java | 282 +++++++++--------- .../meowlib/predefined/STRING.java | 20 +- pisi/unitedmeows/meowlib/thread/kThread.java | 26 +- test/Start.java | 6 +- 20 files changed, 829 insertions(+), 786 deletions(-) diff --git a/pisi/unitedmeows/meowlib/async/Async.java b/pisi/unitedmeows/meowlib/async/Async.java index b41d50c..02c0f0a 100644 --- a/pisi/unitedmeows/meowlib/async/Async.java +++ b/pisi/unitedmeows/meowlib/async/Async.java @@ -10,8 +10,7 @@ import java.util.UUID; public class Async { - - /*TODO: Create ForgettableHashmap and replace this */ + private static HashMap> pointers; static { @@ -64,7 +63,7 @@ public void run() { public static Future async_w(IAsyncAction action, final long after) { - // change this uuid alternative + final CoID pointer = newPointer(); Future future = new Future<>(pointer); @@ -93,11 +92,41 @@ public void run() { } } }; + promise.start(); + + MeowLib.getTaskPool().queue_w(task, repeatDelay); + return promise; + } + + /* same as wloop but starts manually */ + public static Promise async_wloop_w(IAsyncAction action, final long repeatDelay) { + final Promise promise = new Promise(); + Task task = new Task(action) { + @Override + public void run() { + if (promise.isValid()) { + action.start(null); + MeowLib.getTaskPool().queue_w(this, repeatDelay); + } + } + }; MeowLib.getTaskPool().queue_w(task, repeatDelay); return promise; } + public static Promise async_loop(IAsyncAction action, final long repeatDelay, final long lifeTime) { + final Promise promise = async_loop(action, repeatDelay); + async_w((u) -> promise.stop(), lifeTime); + return promise; + } + + public static Promise async_loop_w(IAsyncAction action, final long repeatDelay, final long lifeTime) { + final Promise promise = async_loop_w(action, repeatDelay); + async_w((u) -> promise.stop(), lifeTime + repeatDelay); + return promise; + } + public static Promise async_loop(IAsyncAction action, final long repeatDelay) { final Promise promise = new Promise(); Task task = new Task(action) { @@ -109,6 +138,24 @@ public void run() { } } }; + promise.start(); + + MeowLib.getTaskPool().queue(task); + return promise; + } + + /* same as async_loop but starts manually */ + public static Promise async_loop_w(IAsyncAction action, final long repeatDelay) { + final Promise promise = new Promise(); + Task task = new Task(action) { + @Override + public void run() { + if (promise.isValid()) { + action.start(null); + MeowLib.getTaskPool().queue_w(this, repeatDelay); + } + } + }; MeowLib.getTaskPool().queue(task); return promise; diff --git a/pisi/unitedmeows/meowlib/async/BasicTaskPool.java b/pisi/unitedmeows/meowlib/async/BasicTaskPool.java index 929ee2b..1ab582d 100644 --- a/pisi/unitedmeows/meowlib/async/BasicTaskPool.java +++ b/pisi/unitedmeows/meowlib/async/BasicTaskPool.java @@ -53,7 +53,7 @@ public void workerC() { TaskWorker freeWorker = new TaskWorker(this); taskWorkers.add(freeWorker); freeWorker.startWorker(); - } else if (taskWorkers.size() > 3){ + } else if (taskWorkers.size() > 6){ for (TaskWorker nWorking : nWorkings) { taskWorkers.remove(nWorking); } diff --git a/pisi/unitedmeows/meowlib/async/Result.java b/pisi/unitedmeows/meowlib/async/Result.java index 7f4f1cd..fce00f7 100644 --- a/pisi/unitedmeows/meowlib/async/Result.java +++ b/pisi/unitedmeows/meowlib/async/Result.java @@ -1,14 +1,14 @@ -package pisi.unitedmeows.meowlib.async; - -public class Result { - - protected X value; - - public Result(X val) { - value = val; - } - - public X value() { - return value; - } -} +package pisi.unitedmeows.meowlib.async; + +public class Result { + + protected X value; + + public Result(X val) { + value = val; + } + + public X value() { + return value; + } +} diff --git a/pisi/unitedmeows/meowlib/etc/IAction.java b/pisi/unitedmeows/meowlib/etc/IAction.java index dac59a5..9059c83 100644 --- a/pisi/unitedmeows/meowlib/etc/IAction.java +++ b/pisi/unitedmeows/meowlib/etc/IAction.java @@ -1,8 +1,8 @@ -package pisi.unitedmeows.meowlib.etc; - -import java.util.UUID; - -@FunctionalInterface -public interface IAction { - void run() throws Exception; -} +package pisi.unitedmeows.meowlib.etc; + +import java.util.UUID; + +@FunctionalInterface +public interface IAction { + void run() throws Exception; +} diff --git a/pisi/unitedmeows/meowlib/etc/MLibSetting.java b/pisi/unitedmeows/meowlib/etc/MLibSetting.java index 3bb7d96..a702755 100644 --- a/pisi/unitedmeows/meowlib/etc/MLibSetting.java +++ b/pisi/unitedmeows/meowlib/etc/MLibSetting.java @@ -1,23 +1,23 @@ -package pisi.unitedmeows.meowlib.etc; - - -import java.io.Serializable; - -public class MLibSetting { - - private MLibSettings label; - private X value; - - public MLibSetting(MLibSettings label, X value) { - this.label = label; - this.value = value; - } - - public X getValue() { - return value; - } - - public MLibSettings getLabel() { - return label; - } -} +package pisi.unitedmeows.meowlib.etc; + + +import java.io.Serializable; + +public class MLibSetting { + + private MLibSettings label; + private X value; + + public MLibSetting(MLibSettings label, X value) { + this.label = label; + this.value = value; + } + + public X getValue() { + return value; + } + + public MLibSettings getLabel() { + return label; + } +} diff --git a/pisi/unitedmeows/meowlib/etc/Tuple.java b/pisi/unitedmeows/meowlib/etc/Tuple.java index aed8154..e9b03e0 100644 --- a/pisi/unitedmeows/meowlib/etc/Tuple.java +++ b/pisi/unitedmeows/meowlib/etc/Tuple.java @@ -1,28 +1,28 @@ -package pisi.unitedmeows.meowlib.etc; - -public class Tuple { - - private F first; - private S second; - - public Tuple(F first, S second) { - this.first = first; - this.second = second; - } - - public F getFirst() { - return first; - } - - public S getSecond() { - return second; - } - - public void setFirst(F first) { - this.first = first; - } - - public void setSecond(S second) { - this.second = second; - } -} +package pisi.unitedmeows.meowlib.etc; + +public class Tuple { + + private F first; + private S second; + + public Tuple(F first, S second) { + this.first = first; + this.second = second; + } + + public F getFirst() { + return first; + } + + public S getSecond() { + return second; + } + + public void setFirst(F first) { + this.first = first; + } + + public void setSecond(S second) { + this.second = second; + } +} diff --git a/pisi/unitedmeows/meowlib/etc/Tuple3.java b/pisi/unitedmeows/meowlib/etc/Tuple3.java index 73e75cf..a057ee0 100644 --- a/pisi/unitedmeows/meowlib/etc/Tuple3.java +++ b/pisi/unitedmeows/meowlib/etc/Tuple3.java @@ -1,27 +1,27 @@ -package pisi.unitedmeows.meowlib.etc; - -public class Tuple3 { - - private F first; - private S second; - private T third; - - - public Tuple3(F first, S second, T third) { - this.first = first; - this.second = second; - this.third = third; - } - - public F getFirst() { - return first; - } - - public S getSecond() { - return second; - } - - public T getThird() { - return third; - } -} +package pisi.unitedmeows.meowlib.etc; + +public class Tuple3 { + + private F first; + private S second; + private T third; + + + public Tuple3(F first, S second, T third) { + this.first = first; + this.second = second; + this.third = third; + } + + public F getFirst() { + return first; + } + + public S getSecond() { + return second; + } + + public T getThird() { + return third; + } +} diff --git a/pisi/unitedmeows/meowlib/etc/Tuple4.java b/pisi/unitedmeows/meowlib/etc/Tuple4.java index 859cf2c..a0cb6af 100644 --- a/pisi/unitedmeows/meowlib/etc/Tuple4.java +++ b/pisi/unitedmeows/meowlib/etc/Tuple4.java @@ -1,33 +1,33 @@ -package pisi.unitedmeows.meowlib.etc; - -public class Tuple4 { - - private F first; - private S second; - private T third; - private L last; - - - public Tuple4(F first, S second, T third, L last) { - this.first = first; - this.second = second; - this.third = third; - this.last = last; - } - - public F getFirst() { - return first; - } - - public S getSecond() { - return second; - } - - public T getThird() { - return third; - } - - public L getLast() { - return last; - } -} +package pisi.unitedmeows.meowlib.etc; + +public class Tuple4 { + + private F first; + private S second; + private T third; + private L last; + + + public Tuple4(F first, S second, T third, L last) { + this.first = first; + this.second = second; + this.third = third; + this.last = last; + } + + public F getFirst() { + return first; + } + + public S getSecond() { + return second; + } + + public T getThird() { + return third; + } + + public L getLast() { + return last; + } +} diff --git a/pisi/unitedmeows/meowlib/etc/memorable/MemorableElement.java b/pisi/unitedmeows/meowlib/etc/memorable/MemorableElement.java index 2d9ca86..afd274c 100644 --- a/pisi/unitedmeows/meowlib/etc/memorable/MemorableElement.java +++ b/pisi/unitedmeows/meowlib/etc/memorable/MemorableElement.java @@ -1,7 +1,7 @@ -package pisi.unitedmeows.meowlib.etc.memorable; - -public class MemorableElement { - public int index; - public X element; - public MemorableTail parentTail; -} +package pisi.unitedmeows.meowlib.etc.memorable; + +public class MemorableElement { + public int index; + public X element; + public MemorableTail parentTail; +} diff --git a/pisi/unitedmeows/meowlib/etc/memorable/MemorableList.java b/pisi/unitedmeows/meowlib/etc/memorable/MemorableList.java index 5a6ff83..314f68e 100644 --- a/pisi/unitedmeows/meowlib/etc/memorable/MemorableList.java +++ b/pisi/unitedmeows/meowlib/etc/memorable/MemorableList.java @@ -1,25 +1,25 @@ -package pisi.unitedmeows.meowlib.etc.memorable; - -/* This list is using more ram to use codes faster (index of an element etc.) */ - -public class MemorableList { - private MemorableTail currentTail; - - public MemorableList() { - currentTail = new MemorableTail<>(); - } - public MemorableList(int tailLength) { - currentTail = new MemorableTail<>(tailLength); - } - - public void push(X element) { - currentTail.add(element); - } - public void kick(X element) { - currentTail.remove(element); - } - public void kick(int index) { - currentTail.removeAt(index); - } - -} +package pisi.unitedmeows.meowlib.etc.memorable; + +/* This list is using more ram to use codes faster (index of an element etc.) */ + +public class MemorableList { + private MemorableTail currentTail; + + public MemorableList() { + currentTail = new MemorableTail<>(); + } + public MemorableList(int tailLength) { + currentTail = new MemorableTail<>(tailLength); + } + + public void push(X element) { + currentTail.add(element); + } + public void kick(X element) { + currentTail.remove(element); + } + public void kick(int index) { + currentTail.removeAt(index); + } + +} diff --git a/pisi/unitedmeows/meowlib/etc/memorable/MemorableTail.java b/pisi/unitedmeows/meowlib/etc/memorable/MemorableTail.java index be9077e..b9018c5 100644 --- a/pisi/unitedmeows/meowlib/etc/memorable/MemorableTail.java +++ b/pisi/unitedmeows/meowlib/etc/memorable/MemorableTail.java @@ -1,81 +1,81 @@ -package pisi.unitedmeows.meowlib.etc.memorable; - -public class MemorableTail { - - private MemorableTail down; - - private MemorableElement[] elements; - private int length; - private MemorableElement lastElement; - - public MemorableTail() { - elements = (MemorableElement[]) new Object[length = 5]; - } - public MemorableTail(int length) { - elements = (MemorableElement[]) new Object[this.length = length]; - } - - public MemorableElement[] getElements() { - return elements; - } - - public int getLength() { - return length; - } - - public MemorableTail getDown() { - return down; - } - - public void add(X element) { - if (elements.length > length) { - /* Throw an exception */ - //TODO: Exception - } - boolean needNewChild = lastElement.index + 1 >= length; - MemorableTail tail = lastElement.parentTail; - MemorableElement last = lastElement; - if (needNewChild) { - tail.down = new MemorableTail<>(length); - tail.down.elements[0] = (MemorableElement)element; - tail.down.elements[0].element = element; - tail.down.elements[0].index = 0; - tail.down.elements[0].parentTail = tail.down; - } else { - tail.elements[last.index+1] = (MemorableElement)element; - tail.elements[last.index+1].element = element; - tail.elements[last.index+1].index = last.index+1; - tail.elements[last.index+1].parentTail = tail; - } - } - public void remove(X element) { - removeAt(((MemorableElement)element).index); - } - public void removeAt(int index) { - elements[index].element = null; - int i = index + 1; - for (; i < elements.length && elements[i] != null; i++) { - elements[i - 1].element = elements[i].element; - elements[i - 1].index = i-1; - } - MemorableTail children = down; - boolean lastChild = children != null; - if (!lastChild) { - lastElement = elements[i - 1]; - } - i = 1; - while (children != null && elements[elements.length - 1].element == null && children.elements.length > 0) { - elements[elements.length - 1].element = children.elements[0].element; - elements[elements.length - 1].index = elements.length - 1; - elements[elements.length - 1].parentTail = this; - for (; i < children.elements.length && children.elements[i] != null; i++) { - children.elements[i - 1].element = children.elements[i].element; - children.elements[i - 1].index = i-1; - } - children = children.down; - } - if (lastChild) { - lastElement = elements[i - 1]; - } - } -} +package pisi.unitedmeows.meowlib.etc.memorable; + +public class MemorableTail { + + private MemorableTail down; + + private MemorableElement[] elements; + private int length; + private MemorableElement lastElement; + + public MemorableTail() { + elements = (MemorableElement[]) new Object[length = 5]; + } + public MemorableTail(int length) { + elements = (MemorableElement[]) new Object[this.length = length]; + } + + public MemorableElement[] getElements() { + return elements; + } + + public int getLength() { + return length; + } + + public MemorableTail getDown() { + return down; + } + + public void add(X element) { + if (elements.length > length) { + /* Throw an exception */ + //TODO: Exception + } + boolean needNewChild = lastElement.index + 1 >= length; + MemorableTail tail = lastElement.parentTail; + MemorableElement last = lastElement; + if (needNewChild) { + tail.down = new MemorableTail<>(length); + tail.down.elements[0] = (MemorableElement)element; + tail.down.elements[0].element = element; + tail.down.elements[0].index = 0; + tail.down.elements[0].parentTail = tail.down; + } else { + tail.elements[last.index+1] = (MemorableElement)element; + tail.elements[last.index+1].element = element; + tail.elements[last.index+1].index = last.index+1; + tail.elements[last.index+1].parentTail = tail; + } + } + public void remove(X element) { + removeAt(((MemorableElement)element).index); + } + public void removeAt(int index) { + elements[index].element = null; + int i = index + 1; + for (; i < elements.length && elements[i] != null; i++) { + elements[i - 1].element = elements[i].element; + elements[i - 1].index = i-1; + } + MemorableTail children = down; + boolean lastChild = children != null; + if (!lastChild) { + lastElement = elements[i - 1]; + } + i = 1; + while (children != null && elements[elements.length - 1].element == null && children.elements.length > 0) { + elements[elements.length - 1].element = children.elements[0].element; + elements[elements.length - 1].index = elements.length - 1; + elements[elements.length - 1].parentTail = this; + for (; i < children.elements.length && children.elements[i] != null; i++) { + children.elements[i - 1].element = children.elements[i].element; + children.elements[i - 1].index = i-1; + } + children = children.down; + } + if (lastChild) { + lastElement = elements[i - 1]; + } + } +} diff --git a/pisi/unitedmeows/meowlib/filesystem/kDirectory.java b/pisi/unitedmeows/meowlib/filesystem/kDirectory.java index 989c483..f32e565 100644 --- a/pisi/unitedmeows/meowlib/filesystem/kDirectory.java +++ b/pisi/unitedmeows/meowlib/filesystem/kDirectory.java @@ -1,25 +1,25 @@ -package pisi.unitedmeows.meowlib.filesystem; - -import java.io.File; -import java.util.List; - -public class kDirectory { - - - public static File[] listFiles(String dirName) { - return get(dirName).listFiles(); - } - - public static boolean delete(String dirName) { - return get(dirName).delete(); - } - - public static boolean exists(String dirName) { - return get(dirName).isDirectory(); - } - - - public static File get(String dirName) { - return new File(dirName); - } -} +package pisi.unitedmeows.meowlib.filesystem; + +import java.io.File; +import java.util.List; + +public class kDirectory { + + + public static File[] listFiles(String dirName) { + return get(dirName).listFiles(); + } + + public static boolean delete(String dirName) { + return get(dirName).delete(); + } + + public static boolean exists(String dirName) { + return get(dirName).isDirectory(); + } + + + public static File get(String dirName) { + return new File(dirName); + } +} diff --git a/pisi/unitedmeows/meowlib/filesystem/kFile.java b/pisi/unitedmeows/meowlib/filesystem/kFile.java index 329fa81..2aabbb3 100644 --- a/pisi/unitedmeows/meowlib/filesystem/kFile.java +++ b/pisi/unitedmeows/meowlib/filesystem/kFile.java @@ -1,138 +1,138 @@ -package pisi.unitedmeows.meowlib.filesystem; - -import pisi.unitedmeows.meowlib.etc.Tuple; -import pisi.unitedmeows.meowlib.predefined.STRING; - -import java.io.*; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; - -/* - * life is too short to use - * 'new File(fileName)' - */ -public class kFile { - - public static boolean exists(String fileName) { - return get(fileName).isFile(); - } - - public static Tuple exists_r(String fileName) { - File file = new File(fileName); - return new Tuple<>(file.isFile(), file); - } - - public static List readAllLines(String fileName) { - return readAllLines(get(fileName)); - } - - public static List readAllLines(File file) { - List readContent = new ArrayList<>(); - try { - BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8")); - String str; - while ((str = in.readLine()) != null) - readContent.add(str); - in.close(); - } catch (Exception e) { - e.printStackTrace(); - } - return readContent; - } - - public static String read(String fileName) { - return read(get(fileName)); - } - - public static String read(File file) { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - - try { - StringBuilder sb = new StringBuilder(); - String line = br.readLine(); - - while (line != null) { - sb.append(line); - sb.append(STRING.L_SEPARATOR); - line = br.readLine(); - } - return sb.toString(); - } finally { - br.close(); - } - } catch (IOException e) { - return STRING.EMPTY; - } - } - - /* copied from somewhere idk */ - /** - * faster read for big files - * @param f - * @return - */ - public static String read_f(File f) { - StringBuilder text = new StringBuilder(); - int read, N = 1024 * 1024; - char[] buffer = new char[N]; - - try { - BufferedReader br = new BufferedReader(new FileReader(f)); - - while(true) { - read = br.read(buffer, 0, N); - text.append(new String(buffer, 0, read)); - - if(read < N) { - break; - } - } - br.close(); - } catch(Exception ex) { - ex.printStackTrace(); - } - - return text.toString(); - } - - public static boolean delete(String fileName) { - return get(fileName).delete(); - } - - public static boolean create(String fileName) { - try { - return get(fileName).createNewFile(); - } catch (IOException e) { - return false; - } - } - - public static String extension(File file) { - String extension = ""; - - try { - if (file != null && file.isFile()) { - String name = file.getName(); - extension = name.substring(name.lastIndexOf(STRING.DOT)); - } - } catch (Exception e) { - extension = STRING.EMPTY; - } - - return extension; - } - - public static String extension(String fileName) { - return fileName.substring(fileName.lastIndexOf(STRING.DOT)); - } - - public static File get(String fileName) { - return new File(fileName); - } - -} +package pisi.unitedmeows.meowlib.filesystem; + +import pisi.unitedmeows.meowlib.etc.Tuple; +import pisi.unitedmeows.meowlib.predefined.STRING; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +/* + * life is too short to use + * 'new File(fileName)' + */ +public class kFile { + + public static boolean exists(String fileName) { + return get(fileName).isFile(); + } + + public static Tuple exists_r(String fileName) { + File file = new File(fileName); + return new Tuple<>(file.isFile(), file); + } + + public static List readAllLines(String fileName) { + return readAllLines(get(fileName)); + } + + public static List readAllLines(File file) { + List readContent = new ArrayList<>(); + try { + BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8")); + String str; + while ((str = in.readLine()) != null) + readContent.add(str); + in.close(); + } catch (Exception e) { + e.printStackTrace(); + } + return readContent; + } + + public static String read(String fileName) { + return read(get(fileName)); + } + + public static String read(File file) { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + + try { + StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + + while (line != null) { + sb.append(line); + sb.append(STRING.L_SEPARATOR); + line = br.readLine(); + } + return sb.toString(); + } finally { + br.close(); + } + } catch (IOException e) { + return STRING.EMPTY; + } + } + + /* copied from somewhere idk */ + /** + * faster read for big files + * @param f + * @return + */ + public static String read_f(File f) { + StringBuilder text = new StringBuilder(); + int read, N = 1024 * 1024; + char[] buffer = new char[N]; + + try { + BufferedReader br = new BufferedReader(new FileReader(f)); + + while(true) { + read = br.read(buffer, 0, N); + text.append(new String(buffer, 0, read)); + + if(read < N) { + break; + } + } + br.close(); + } catch(Exception ex) { + ex.printStackTrace(); + } + + return text.toString(); + } + + public static boolean delete(String fileName) { + return get(fileName).delete(); + } + + public static boolean create(String fileName) { + try { + return get(fileName).createNewFile(); + } catch (IOException e) { + return false; + } + } + + public static String extension(File file) { + String extension = ""; + + try { + if (file != null && file.isFile()) { + String name = file.getName(); + extension = name.substring(name.lastIndexOf(STRING.DOT)); + } + } catch (Exception e) { + extension = STRING.EMPTY; + } + + return extension; + } + + public static String extension(String fileName) { + return fileName.substring(fileName.lastIndexOf(STRING.DOT)); + } + + public static File get(String fileName) { + return new File(fileName); + } + +} diff --git a/pisi/unitedmeows/meowlib/lists/FlexibleArray.java b/pisi/unitedmeows/meowlib/lists/FlexibleArray.java index d0eedd8..0edc634 100644 --- a/pisi/unitedmeows/meowlib/lists/FlexibleArray.java +++ b/pisi/unitedmeows/meowlib/lists/FlexibleArray.java @@ -1,100 +1,100 @@ -package pisi.unitedmeows.meowlib.lists; - - -import java.util.Arrays; - -/* dont use it */ -@Deprecated -public class FlexibleArray { - - private X[] array; - private int count; - private int resizeCap; - - public FlexibleArray(int capacity) { - this(capacity, 1); - } - public FlexibleArray(int capacity, int resizeCap) { - array = (X[]) newArray(capacity); - this.resizeCap = resizeCap; - } - - public FlexibleArray() { - this(10); - } - - public void resize(int newCount) { - X[] old = array; - array = (X[]) newArray(newCount); - for (int i = 0; i < newCount && i < old.length; i++) { - array[i] = (X) old[i]; - } - } - - public void add(X element) { - if (++count >= array.length) { - resize(array.length + resizeCap); - } - array[count] = (X) element; - } - - - public boolean remove(X element) { - int index = 0; - boolean found = false; - for (X x : array) { - if (x == element) { - found = true; - break; - } - index++; - } - - if (found && index + 1 != array.length) { - for (int i = index + 1; i < array.length; i++) { - array[index - 1] = (X) array[i]; - } - count--; - } - - if (count + resizeCap <= array.length) { - resize(count + 1); - } - - - return found; - } - - public boolean remove(int index) { - if (index >= array.length || index < 0) { - return false; - } - - if (index + 1 != array.length) { - for (int i = index + 1; i < array.length; i++) { - array[index - 1] = (X) array[i]; - } - count--; - } - - if (count + resizeCap <= array.length) { - resize(count + 1); - } - - return true; - } - - public X[] array() { - return (X[]) array; - } - - - private X[] newArray(int length, X... array) - { - return Arrays.copyOf(array, length); - } - - public X get(int index) { - return array[index]; - } -} +package pisi.unitedmeows.meowlib.lists; + + +import java.util.Arrays; + +/* dont use it */ +@Deprecated +public class FlexibleArray { + + private X[] array; + private int count; + private int resizeCap; + + public FlexibleArray(int capacity) { + this(capacity, 1); + } + public FlexibleArray(int capacity, int resizeCap) { + array = (X[]) newArray(capacity); + this.resizeCap = resizeCap; + } + + public FlexibleArray() { + this(10); + } + + public void resize(int newCount) { + X[] old = array; + array = (X[]) newArray(newCount); + for (int i = 0; i < newCount && i < old.length; i++) { + array[i] = (X) old[i]; + } + } + + public void add(X element) { + if (++count >= array.length) { + resize(array.length + resizeCap); + } + array[count] = (X) element; + } + + + public boolean remove(X element) { + int index = 0; + boolean found = false; + for (X x : array) { + if (x == element) { + found = true; + break; + } + index++; + } + + if (found && index + 1 != array.length) { + for (int i = index + 1; i < array.length; i++) { + array[index - 1] = (X) array[i]; + } + count--; + } + + if (count + resizeCap <= array.length) { + resize(count + 1); + } + + + return found; + } + + public boolean remove(int index) { + if (index >= array.length || index < 0) { + return false; + } + + if (index + 1 != array.length) { + for (int i = index + 1; i < array.length; i++) { + array[index - 1] = (X) array[i]; + } + count--; + } + + if (count + resizeCap <= array.length) { + resize(count + 1); + } + + return true; + } + + public X[] array() { + return (X[]) array; + } + + + private X[] newArray(int length, X... array) + { + return Arrays.copyOf(array, length); + } + + public X get(int index) { + return array[index]; + } +} diff --git a/pisi/unitedmeows/meowlib/lists/WarnList.java b/pisi/unitedmeows/meowlib/lists/WarnList.java index f408454..b03ba4f 100644 --- a/pisi/unitedmeows/meowlib/lists/WarnList.java +++ b/pisi/unitedmeows/meowlib/lists/WarnList.java @@ -1,98 +1,98 @@ -package pisi.unitedmeows.meowlib.lists; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.function.Predicate; -import java.util.function.UnaryOperator; - -public abstract class WarnList extends ArrayList { - - public abstract void onChange(WarnList list); - - @Override - public boolean add(X x) { - boolean result = super.add(x); - onChange(this); - return result; - } - - @Override - public void add(int index, X element) { - super.add(index, element); - onChange(this); - } - - @Override - public boolean remove(Object o) { - boolean result = super.remove(o); - if (result) { - onChange(this); - return true; - } - return false; - } - - @Override - public X remove(int index) { - X result = super.remove(index); - onChange(this); - return result; - } - - @Override - public boolean addAll(Collection c) { - boolean result = super.addAll(c); - if (result) { - onChange(this); - return true; - } - return false; - } - - @Override - public boolean addAll(int index, Collection c) { - boolean result = super.addAll(index, c); - if (result) { - onChange(this); - return true; - } - return false; - } - - - @Override - public X set(int index, X element) { - X result = super.set(index, element); - onChange(this); - return result; - } - - @Override - public boolean removeAll(Collection c) { - boolean result = super.removeAll(c); - if (result) { - onChange(this); - return true; - } - return false; - } - - @Override - public void sort(Comparator c) { - super.sort(c); - onChange(this); - } - - @Override - public boolean removeIf(Predicate filter) { - boolean result = super.removeIf(filter); - if (result) { - onChange(this); - return true; - } - return false; - } - -} +package pisi.unitedmeows.meowlib.lists; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.function.Predicate; +import java.util.function.UnaryOperator; + +public abstract class WarnList extends ArrayList { + + public abstract void onChange(WarnList list); + + @Override + public boolean add(X x) { + boolean result = super.add(x); + onChange(this); + return result; + } + + @Override + public void add(int index, X element) { + super.add(index, element); + onChange(this); + } + + @Override + public boolean remove(Object o) { + boolean result = super.remove(o); + if (result) { + onChange(this); + return true; + } + return false; + } + + @Override + public X remove(int index) { + X result = super.remove(index); + onChange(this); + return result; + } + + @Override + public boolean addAll(Collection c) { + boolean result = super.addAll(c); + if (result) { + onChange(this); + return true; + } + return false; + } + + @Override + public boolean addAll(int index, Collection c) { + boolean result = super.addAll(index, c); + if (result) { + onChange(this); + return true; + } + return false; + } + + + @Override + public X set(int index, X element) { + X result = super.set(index, element); + onChange(this); + return result; + } + + @Override + public boolean removeAll(Collection c) { + boolean result = super.removeAll(c); + if (result) { + onChange(this); + return true; + } + return false; + } + + @Override + public void sort(Comparator c) { + super.sort(c); + onChange(this); + } + + @Override + public boolean removeIf(Predicate filter) { + boolean result = super.removeIf(filter); + if (result) { + onChange(this); + return true; + } + return false; + } + +} diff --git a/pisi/unitedmeows/meowlib/lists/kIterator.java b/pisi/unitedmeows/meowlib/lists/kIterator.java index 90f0c72..567c4d3 100644 --- a/pisi/unitedmeows/meowlib/lists/kIterator.java +++ b/pisi/unitedmeows/meowlib/lists/kIterator.java @@ -1,6 +1,6 @@ -package pisi.unitedmeows.meowlib.lists; - -public abstract class kIterator { - public abstract boolean next(); - public abstract X current(); -} +package pisi.unitedmeows.meowlib.lists; + +public abstract class kIterator { + public abstract boolean next(); + public abstract X current(); +} diff --git a/pisi/unitedmeows/meowlib/math/MeowMath.java b/pisi/unitedmeows/meowlib/math/MeowMath.java index a4c3cfe..4df9a76 100644 --- a/pisi/unitedmeows/meowlib/math/MeowMath.java +++ b/pisi/unitedmeows/meowlib/math/MeowMath.java @@ -1,141 +1,141 @@ - package pisi.unitedmeows.meowlib.math; - -import java.math.BigDecimal; -import java.math.RoundingMode; - -/** - * @author SIN,COS,CEIL,FLOOR,ROUND belongs to Riven (https://jvm-gaming.org/u/Riven) - */ -public class MeowMath { - private static final float BF_SIN_TO_COS; - private static final int BF_SIN_BITS , BF_SIN_MASK , BF_SIN_COUNT; - private static final float BF_radFull , BF_radToIndex; - private static final float[] BF_sinFull; - private static final int BIG_ENOUGH_INT = 16 * 1024; - private static final double BIG_ENOUGH_FLOOR = BIG_ENOUGH_INT; - private static final double BIG_ENOUGH_ROUND = BIG_ENOUGH_INT + 0.5; - private static final double DEG_TO_RAD = 0.0174532925199432957692369076848861271344287188854172545609719144; - static { - BF_SIN_TO_COS = (float) (Math.PI * 0.5f); - BF_SIN_BITS = 12; - BF_SIN_MASK = ~(-1 << BF_SIN_BITS); - BF_SIN_COUNT = BF_SIN_MASK + 1; - BF_radFull = (float) (Math.PI * 2.0); - BF_radToIndex = BF_SIN_COUNT / BF_radFull; - BF_sinFull = new float[BF_SIN_COUNT]; - for (int i = 0; i < BF_SIN_COUNT; i++) { - BF_sinFull[i] = (float) Math.sin((i + Math.min(1, i % (BF_SIN_COUNT / 4)) * 0.5) / BF_SIN_COUNT * BF_radFull); - } - } - - /** - * @deprecated extremely basic hypot - */ - @Deprecated - public static double basicHypot(double x, double y) { - if (Double.isInfinite(x) || Double.isInfinite(y)) return Double.POSITIVE_INFINITY; - if (Double.isNaN(x) || Double.isNaN(y)) return Double.NaN; - return sqrt(x * x + y * y); - } - - public static double hypot(double x, double y) { - if (Double.isInfinite(x) || Double.isInfinite(y)) return Double.POSITIVE_INFINITY; - if (Double.isNaN(x) || Double.isNaN(y)) return Double.NaN; - x = Math.abs(x); - y = Math.abs(y); - if (x < y) { - double d = x; - x = y; - y = d; - } - int xi = Math.getExponent(x); - int yi = Math.getExponent(y); - if (xi > yi + 27) return x; - int bias = 0; - if (xi > 510 || xi < -511) { - bias = xi; - x = Math.scalb(x, -bias); - y = Math.scalb(y, -bias); - } - double z = 0; - if (x > 2 * y) { - double x1 = Double.longBitsToDouble(Double.doubleToLongBits(x) & 0xffffffff00000000L); - double x2 = x - x1; - z = sqrt(x1 * x1 + (y * y + x2 * (x + x1))); - } - else { - double t = 2 * x; - double t1 = Double.longBitsToDouble(Double.doubleToLongBits(t) & 0xffffffff00000000L); - double t2 = t - t1; - double y1 = Double.longBitsToDouble(Double.doubleToLongBits(y) & 0xffffffff00000000L); - double y2 = y - y1; - double x_y = x - y; - z = sqrt(t1 * y1 + (x_y * x_y + (t1 * y2 + t2 * y))); - } - if (bias == 0) { - return z; - } - else { - return Math.scalb(z, bias); - } - } - - public static float sin(float rad) { return BF_sinFull[(int) (rad * BF_radToIndex) & BF_SIN_MASK]; } - - public static float cos(float rad) { return sin(rad + BF_SIN_TO_COS); } - - public static int floor(float x) { return (int) (x + BIG_ENOUGH_FLOOR) - BIG_ENOUGH_INT; } - - public static int round(float x) { return (int) (x + BIG_ENOUGH_ROUND) - BIG_ENOUGH_INT; } - - public static int ceil(float x) { return BIG_ENOUGH_INT - (int) (BIG_ENOUGH_FLOOR - x); } - - public static double map(double val, double mx, double from, double to) { - return Math.min(Math.max(from + (val / mx) * (to - from), from), to); - } - - /** - * @implNote This multiplies the argument with 0.017453292519943295 instead of Math.PI / 180.0 which is slower. - */ - public static double toRadians(double rad) { return rad * DEG_TO_RAD; } - - /** - * @implNote This divides the argument with 0.017453292519943295 instead of Math.PI / 180.0 which is slower. - */ - public static double toDegrees(double deg) { return deg / DEG_TO_RAD; } - - /** - * @see toRadians which is faster (probably) - */ - public static double toRadiansJava(double rad) { return rad * (Math.PI / 180.0); } - - /** - * @see toDegrees which is faster (probably) - */ - public static double toDegreesJava(double deg) { return deg * (180.0 / Math.PI); } - - public static double sqrt(double number) { - double numberIn = Double.longBitsToDouble( - ((Double.doubleToRawLongBits(number) >> 32) + (number > 100000 ? 1072679338 : 1072632448)) << 31); - double accurateNumber1X = (numberIn + number / numberIn) * 0.5; - double accurateNumber2X = (accurateNumber1X + number / accurateNumber1X) * 0.5; - return accurateNumber2X; - } - - public static double roundToPlace(double value, int places) { - if (places < 0) { - return value; - } - else { - BigDecimal bd = new BigDecimal(value); - bd = bd.setScale(places, RoundingMode.HALF_UP); - return bd.doubleValue(); - } - } - - public static int clamp(int num, int min, int max) { return Math.max(min, Math.min(max, num)); } - - public static float clamp(float num, float min, float max) { return Math.max(min, Math.min(max, num)); } - - public static double clamp(double num, double min, double max) { return Math.max(min, Math.min(max, num)); } -} + package pisi.unitedmeows.meowlib.math; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +/** + * @author SIN,COS,CEIL,FLOOR,ROUND belongs to Riven (https://jvm-gaming.org/u/Riven) + */ +public class MeowMath { + private static final float BF_SIN_TO_COS; + private static final int BF_SIN_BITS , BF_SIN_MASK , BF_SIN_COUNT; + private static final float BF_radFull , BF_radToIndex; + private static final float[] BF_sinFull; + private static final int BIG_ENOUGH_INT = 16 * 1024; + private static final double BIG_ENOUGH_FLOOR = BIG_ENOUGH_INT; + private static final double BIG_ENOUGH_ROUND = BIG_ENOUGH_INT + 0.5; + private static final double DEG_TO_RAD = 0.0174532925199432957692369076848861271344287188854172545609719144; + static { + BF_SIN_TO_COS = (float) (Math.PI * 0.5f); + BF_SIN_BITS = 12; + BF_SIN_MASK = ~(-1 << BF_SIN_BITS); + BF_SIN_COUNT = BF_SIN_MASK + 1; + BF_radFull = (float) (Math.PI * 2.0); + BF_radToIndex = BF_SIN_COUNT / BF_radFull; + BF_sinFull = new float[BF_SIN_COUNT]; + for (int i = 0; i < BF_SIN_COUNT; i++) { + BF_sinFull[i] = (float) Math.sin((i + Math.min(1, i % (BF_SIN_COUNT / 4)) * 0.5) / BF_SIN_COUNT * BF_radFull); + } + } + + /** + * @deprecated extremely basic hypot + */ + @Deprecated + public static double basicHypot(double x, double y) { + if (Double.isInfinite(x) || Double.isInfinite(y)) return Double.POSITIVE_INFINITY; + if (Double.isNaN(x) || Double.isNaN(y)) return Double.NaN; + return sqrt(x * x + y * y); + } + + public static double hypot(double x, double y) { + if (Double.isInfinite(x) || Double.isInfinite(y)) return Double.POSITIVE_INFINITY; + if (Double.isNaN(x) || Double.isNaN(y)) return Double.NaN; + x = Math.abs(x); + y = Math.abs(y); + if (x < y) { + double d = x; + x = y; + y = d; + } + int xi = Math.getExponent(x); + int yi = Math.getExponent(y); + if (xi > yi + 27) return x; + int bias = 0; + if (xi > 510 || xi < -511) { + bias = xi; + x = Math.scalb(x, -bias); + y = Math.scalb(y, -bias); + } + double z = 0; + if (x > 2 * y) { + double x1 = Double.longBitsToDouble(Double.doubleToLongBits(x) & 0xffffffff00000000L); + double x2 = x - x1; + z = sqrt(x1 * x1 + (y * y + x2 * (x + x1))); + } + else { + double t = 2 * x; + double t1 = Double.longBitsToDouble(Double.doubleToLongBits(t) & 0xffffffff00000000L); + double t2 = t - t1; + double y1 = Double.longBitsToDouble(Double.doubleToLongBits(y) & 0xffffffff00000000L); + double y2 = y - y1; + double x_y = x - y; + z = sqrt(t1 * y1 + (x_y * x_y + (t1 * y2 + t2 * y))); + } + if (bias == 0) { + return z; + } + else { + return Math.scalb(z, bias); + } + } + + public static float sin(float rad) { return BF_sinFull[(int) (rad * BF_radToIndex) & BF_SIN_MASK]; } + + public static float cos(float rad) { return sin(rad + BF_SIN_TO_COS); } + + public static int floor(float x) { return (int) (x + BIG_ENOUGH_FLOOR) - BIG_ENOUGH_INT; } + + public static int round(float x) { return (int) (x + BIG_ENOUGH_ROUND) - BIG_ENOUGH_INT; } + + public static int ceil(float x) { return BIG_ENOUGH_INT - (int) (BIG_ENOUGH_FLOOR - x); } + + public static double map(double val, double mx, double from, double to) { + return Math.min(Math.max(from + (val / mx) * (to - from), from), to); + } + + /** + * @implNote This multiplies the argument with 0.017453292519943295 instead of Math.PI / 180.0 which is slower. + */ + public static double toRadians(double rad) { return rad * DEG_TO_RAD; } + + /** + * @implNote This divides the argument with 0.017453292519943295 instead of Math.PI / 180.0 which is slower. + */ + public static double toDegrees(double deg) { return deg / DEG_TO_RAD; } + + /** + * @see toRadians which is faster (probably) + */ + public static double toRadiansJava(double rad) { return rad * (Math.PI / 180.0); } + + /** + * @see toDegrees which is faster (probably) + */ + public static double toDegreesJava(double deg) { return deg * (180.0 / Math.PI); } + + public static double sqrt(double number) { + double numberIn = Double.longBitsToDouble( + ((Double.doubleToRawLongBits(number) >> 32) + (number > 100000 ? 1072679338 : 1072632448)) << 31); + double accurateNumber1X = (numberIn + number / numberIn) * 0.5; + double accurateNumber2X = (accurateNumber1X + number / accurateNumber1X) * 0.5; + return accurateNumber2X; + } + + public static double roundToPlace(double value, int places) { + if (places < 0) { + return value; + } + else { + BigDecimal bd = new BigDecimal(value); + bd = bd.setScale(places, RoundingMode.HALF_UP); + return bd.doubleValue(); + } + } + + public static int clamp(int num, int min, int max) { return Math.max(min, Math.min(max, num)); } + + public static float clamp(float num, float min, float max) { return Math.max(min, Math.min(max, num)); } + + public static double clamp(double num, double min, double max) { return Math.max(min, Math.min(max, num)); } +} diff --git a/pisi/unitedmeows/meowlib/predefined/STRING.java b/pisi/unitedmeows/meowlib/predefined/STRING.java index 3371cac..f5ee130 100644 --- a/pisi/unitedmeows/meowlib/predefined/STRING.java +++ b/pisi/unitedmeows/meowlib/predefined/STRING.java @@ -1,10 +1,10 @@ -package pisi.unitedmeows.meowlib.predefined; - -import java.io.File; - -public class STRING { - - public static String EMPTY = ""; - public static String DOT = "."; - public static String L_SEPARATOR = System.lineSeparator(); -} +package pisi.unitedmeows.meowlib.predefined; + +import java.io.File; + +public class STRING { + + public static String EMPTY = ""; + public static String DOT = "."; + public static String L_SEPARATOR = System.lineSeparator(); +} diff --git a/pisi/unitedmeows/meowlib/thread/kThread.java b/pisi/unitedmeows/meowlib/thread/kThread.java index 4945ae4..fc55139 100644 --- a/pisi/unitedmeows/meowlib/thread/kThread.java +++ b/pisi/unitedmeows/meowlib/thread/kThread.java @@ -1,13 +1,13 @@ -package pisi.unitedmeows.meowlib.thread; - -public class kThread { - - public static boolean sleep(long millis) { - try { - Thread.sleep(millis); - return true; - } catch (InterruptedException e) { - return false; - } - } -} +package pisi.unitedmeows.meowlib.thread; + +public class kThread { + + public static boolean sleep(long millis) { + try { + Thread.sleep(millis); + return true; + } catch (InterruptedException e) { + return false; + } + } +} diff --git a/test/Start.java b/test/Start.java index d57a6e0..a838513 100644 --- a/test/Start.java +++ b/test/Start.java @@ -30,11 +30,7 @@ public static void main(String[] args) { async_w((u)-> System.out.println("Usage 3 (this will run after 5000ms)"), 5000); /* defining a variable is not required if you'll not start/stop the loop */ - final Promise promise = async_loop((u)-> System.out.println("Hello World"), 500); - - kThread.sleep(3000); - promise.stop(); - kThread.sleep(1000); + final Promise promise = async_loop((u)-> System.out.println("Hello World"), 500, 3000); }