diff --git a/.gradle/7.6/executionHistory/executionHistory.bin b/.gradle/7.6/executionHistory/executionHistory.bin index 343c209..09c1691 100644 Binary files a/.gradle/7.6/executionHistory/executionHistory.bin and b/.gradle/7.6/executionHistory/executionHistory.bin differ diff --git a/.gradle/7.6/executionHistory/executionHistory.lock b/.gradle/7.6/executionHistory/executionHistory.lock index d0ffe0c..52ea210 100644 Binary files a/.gradle/7.6/executionHistory/executionHistory.lock and b/.gradle/7.6/executionHistory/executionHistory.lock differ diff --git a/.gradle/7.6/fileHashes/fileHashes.bin b/.gradle/7.6/fileHashes/fileHashes.bin index 4f88dd8..a412693 100644 Binary files a/.gradle/7.6/fileHashes/fileHashes.bin and b/.gradle/7.6/fileHashes/fileHashes.bin differ diff --git a/.gradle/7.6/fileHashes/fileHashes.lock b/.gradle/7.6/fileHashes/fileHashes.lock index 6f86c5d..4ec62a6 100644 Binary files a/.gradle/7.6/fileHashes/fileHashes.lock and b/.gradle/7.6/fileHashes/fileHashes.lock differ diff --git a/.gradle/7.6/fileHashes/resourceHashesCache.bin b/.gradle/7.6/fileHashes/resourceHashesCache.bin index a9e2146..095f907 100644 Binary files a/.gradle/7.6/fileHashes/resourceHashesCache.bin and b/.gradle/7.6/fileHashes/resourceHashesCache.bin differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 14f1bd5..0f365df 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/file-system.probe b/.gradle/file-system.probe index d6aaf40..4faefd2 100644 Binary files a/.gradle/file-system.probe and b/.gradle/file-system.probe differ diff --git a/README.md b/README.md index 49bd684..18cb16a 100644 --- a/README.md +++ b/README.md @@ -1 +1,23 @@ -# Arithmos \ No newline at end of file +# Arithmos +[![](https://github.com/Arithmos-Algorithms/Arithmos/actions/workflows/build-test.yml/badge.svg)]() [![](https://github.com/Arithmos-Algorithms/Arithmos/actions/workflows/build-deploy.yml/badge.svg)]() + +An algorithmic library for JVM and Android applications powered by Java. + +## Arithmos houses the following utilities: +- [x] Basic Statistics Utilities. +- [x] Number Format Utility. +- [x] Pattern Matching Utility. +- [x] Sorting Utilities. +- [ ] Searching Utilities. +- [ ] BucketCollection (HashTables alternative). +- [x] Hashing Utility. +- [ ] Vector Math Utilities. +- [ ] Physics Utilities. +- [ ] Matrix Utilities. +- [ ] Trigonometric Math Utilities. + +## Current Ongoing tasks: +- [ ] Remastering HashTables. +- [ ] Vector Math library. +- [ ] Matrix library. +- [ ] Trigonometric Math Utilities. diff --git a/arithmos-examples/src/main/java/com/arithmos/examples/TestHashUtils.java b/arithmos-examples/src/main/java/com/arithmos/examples/TestHashUtils.java new file mode 100644 index 0000000..3468c9d --- /dev/null +++ b/arithmos-examples/src/main/java/com/arithmos/examples/TestHashUtils.java @@ -0,0 +1,18 @@ +package com.arithmos.examples; + +import com.arithmos.util.HashingUtils; + +public class TestHashUtils { + public static void main(String[] args) { + final Object key = new Object(); + final Object value = new Object(); + for (int i = 0; i <= 50; i++) { + System.out.println(HashingUtils.modCompress(i, 50)); + } +// System.out.println(HashingUtils.modCompress(key.hashCode() ^ value.hashCode(), 40)); +// +// int number = 0b11111111000000000000000000000000; +// System.out.println("Before spreading: " + number); +// System.out.println("After spreading: " + HashingUtils.spreadMSBtoLSB(number)); + } +} diff --git a/arithmos/src/main/java/com/arithmos/structure/bucket/AbstractMap.java b/arithmos/src/main/java/com/arithmos/structure/bucket/AbstractMap.java new file mode 100644 index 0000000..494ab76 --- /dev/null +++ b/arithmos/src/main/java/com/arithmos/structure/bucket/AbstractMap.java @@ -0,0 +1,72 @@ +package com.arithmos.structure.bucket; + +import com.arithmos.util.HashingUtils; +import java.util.Map; +import java.util.Objects; + +/** + * Houses the basic algorithms for a BucketCollection. + * + * @author pavl_g + * @param + * @param + */ +public abstract class AbstractMap implements Map { + + protected Bucket[] buckets; + protected int size = Const.INITIAL_CAPACITY; + + public AbstractMap() { + + } + + @Override + @SuppressWarnings("unchecked") + public V put(K key, V value) { + /* sanity checks */ + if (buckets == null) { + buckets = new Bucket[size]; + insertNewBucket(key, value); + return null; + } + +// if (exists) { +// +// } + + + return null; + } + + /** + * Only inserts new buckets replacing old ones if they are different, + * if they are the same buckets. + * + * @param key the key of the bucket to calculate the index + * @param value the value to save in the bucket + */ + protected void insertNewBucket(K key, V value) { + int index = HashingUtils.andCompress(key.hashCode(), size); + if (buckets[index] != null && + buckets[index].hashCode() == Objects.hash(key, value, index)) { + return; + } + buckets[index] = new Bucket<>(key, value, index); + } + + /** + * Inserts + * + * @param key + * @param value + */ + protected void insertBucket(K key, V value) { + int index = HashingUtils.andCompress(key.hashCode(), size); + /* Check for a collision and apply a collision criteria */ + insertNewBucket(key, value); + if (buckets[index].key != key && + buckets[index].hashCode() == Objects.hash(key, value, index)) { + + } + } +} diff --git a/arithmos/src/main/java/com/arithmos/structure/bucket/Bucket.java b/arithmos/src/main/java/com/arithmos/structure/bucket/Bucket.java new file mode 100644 index 0000000..5365b3c --- /dev/null +++ b/arithmos/src/main/java/com/arithmos/structure/bucket/Bucket.java @@ -0,0 +1,45 @@ +package com.arithmos.structure.bucket; + +import java.util.Map; +import java.util.Objects; + +class Bucket implements Map.Entry { + + protected K key; + protected V value; + protected int index; + + public Bucket(K key, V value, int index) { + this.key = key; + this.value = value; + this.index = index; + } + + @Override + public K getKey() { + return key; + } + + @Override + public V getValue() { + return value; + } + + @Override + public V setValue(V value) { + return this.value = value; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Bucket bucket = (Bucket) o; + return index == bucket.index && Objects.equals(key, bucket.key) && Objects.equals(value, bucket.value); + } + + @Override + public int hashCode() { + return Objects.hash(key, value, index); + } +} diff --git a/arithmos/src/main/java/com/arithmos/structure/bucket/BucketCollection.java b/arithmos/src/main/java/com/arithmos/structure/bucket/BucketCollection.java new file mode 100644 index 0000000..47a6823 --- /dev/null +++ b/arithmos/src/main/java/com/arithmos/structure/bucket/BucketCollection.java @@ -0,0 +1,4 @@ +package com.arithmos.structure.bucket; + +public class BucketCollection { +} diff --git a/arithmos/src/main/java/com/arithmos/structure/bucket/Const.java b/arithmos/src/main/java/com/arithmos/structure/bucket/Const.java new file mode 100644 index 0000000..241c33c --- /dev/null +++ b/arithmos/src/main/java/com/arithmos/structure/bucket/Const.java @@ -0,0 +1,6 @@ +package com.arithmos.structure.bucket; + +public final class Const { + public static final int INITIAL_CAPACITY = 8; + +} diff --git a/arithmos/src/main/java/com/arithmos/util/HashingUtils.java b/arithmos/src/main/java/com/arithmos/util/HashingUtils.java new file mode 100644 index 0000000..3e78a43 --- /dev/null +++ b/arithmos/src/main/java/com/arithmos/util/HashingUtils.java @@ -0,0 +1,85 @@ +package com.arithmos.util; + +/** + * Houses multiple hashing algorithms out-of-the-box. + * + * @author pavl_g + */ +public final class HashingUtils { + + private HashingUtils() { + } + + /** + * Moves a number of the MSB [nBits] to the LSB. + * + * @param code a hash code to perform shifting on + * @param nBits the number of MSBs (most significant bits) to move to the LSB location + * @return the new hashcode with the re-location applied + */ + public static int shiftToLSB(int code, int nBits) { + return (code << nBits) /* 1) shifts the code to the right by no. of bits + creating some new ZERO bits at the Least significant location */ + | (code >>> (Integer.SIZE - nBits)) /* 2) Moves the MSBs to the LSBs location. 3) ORs the 2 components. */; + } + + + /** + * Moves a number of the LSB [nBits] to the MSB. + * + * @param code a hash code to perform shifting on + * @param nBits the number of LSBs (least significant bits) to move to the MSB location + * @return the new hashcode with the re-location applied + */ + public static int shiftToMSB(int code, int nBits) { + return (code >> nBits) /* 1) shifts the code to the left by no. of bits + creating some new ZERO bits at the Most significant location */ + | (code << (Integer.SIZE - nBits)) /* 2) Moves the LSBs to the MSBs location. 3) ORs the 2 components. */; + } + + /** + * Spreads the most significant 16-bits in a hashcode into + * the least significant locations by (1) shifting the [code] + * by 16-bits to the right and (2) XORing it with the original value. + * + * @param code the original hashcode + * @return a new code with the MSBs spread into the least significant locations + */ + public static int spreadMSBtoLSB(int code) { + return code ^ (code >>> (Integer.SIZE / 2)); + } + + /** + * Spreads the least significant 16-bits in a hashcode into + * the most significant locations by (1) shifting the [code] + * by 16-bits to the left and (2) XORing it with the original value. + * + * @param code the original hashcode + * @return a new code with the LSBs spread into the most significant locations + */ + public static int spreadLSBtoMSB(int code) { + return code ^ (code << (Integer.SIZE / 2)); + } + + /** + * Compresses the hashcode to the size using a modular function. + * + * @param code the hashcode to compress + * @param size the compression value + * @return a new compressed version of the input hashcode + */ + public static int modCompress(int code, int size) { + return code % size; + } + + /** + * Compresses the hashcode to the size using an AND function. + * + * @param code the hashcode to compress + * @param size the compression value + * @return a new compressed version of the input hashcode + */ + public static int andCompress(int code, int size) { + return code & (size - 1); + } +}