From 3e6bdbcf5704993d24f1bcc9002134889f4f8e75 Mon Sep 17 00:00:00 2001 From: pavl_g Date: Sun, 6 Aug 2023 21:32:03 +0300 Subject: [PATCH 1/2] BucketCollection: base classes and HashingUtils --- .../7.6/executionHistory/executionHistory.bin | Bin 64771 -> 92257 bytes .../executionHistory/executionHistory.lock | Bin 17 -> 17 bytes .gradle/7.6/fileHashes/fileHashes.bin | Bin 22297 -> 22697 bytes .gradle/7.6/fileHashes/fileHashes.lock | Bin 17 -> 17 bytes .../7.6/fileHashes/resourceHashesCache.bin | Bin 20061 -> 21727 bytes .../buildOutputCleanup.lock | Bin 17 -> 17 bytes .gradle/file-system.probe | Bin 8 -> 8 bytes README.md | 23 ++++- .../com/arithmos/examples/TestHashUtils.java | 18 ++++ .../structure/bucket/AbstractMap.java | 72 +++++++++++++++ .../com/arithmos/structure/bucket/Bucket.java | 45 ++++++++++ .../structure/bucket/BucketCollection.java | 4 + .../com/arithmos/structure/bucket/Const.java | 6 ++ .../java/com/arithmos/util/HashingUtils.java | 85 ++++++++++++++++++ 14 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 arithmos-examples/src/main/java/com/arithmos/examples/TestHashUtils.java create mode 100644 arithmos/src/main/java/com/arithmos/structure/bucket/AbstractMap.java create mode 100644 arithmos/src/main/java/com/arithmos/structure/bucket/Bucket.java create mode 100644 arithmos/src/main/java/com/arithmos/structure/bucket/BucketCollection.java create mode 100644 arithmos/src/main/java/com/arithmos/structure/bucket/Const.java create mode 100644 arithmos/src/main/java/com/arithmos/util/HashingUtils.java diff --git a/.gradle/7.6/executionHistory/executionHistory.bin b/.gradle/7.6/executionHistory/executionHistory.bin index 343c209d7fd15645e5f7e6a6d2067b6fc8265c45..09c16918169ba7bd1b30cf9e8e8edafb90055a86 100644 GIT binary patch delta 3607 zcmcgt2~<;88h)4XSORDSWKl!{f+E#~gb=Md6|^0wMO@fiKwd%u34}lv0y%Cd2nkq< z*Gdakj!Uh!V8tiG6jX2I`AV82Ioqc%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sWPOb0dlT=TYt5!lqiW%Dkvs<5N>^d0hf8)@UHFLm?U zFWyGN8mW);mUlUb26y!o&ldFUeT+5craEub$sF`DmL+}wF6`n@7$CZj4j*^V&5k^j zGWf;N%7fM_&k#d!f3>AYqYg+X(al-8;T1KSx z7n>BUv690~WEm23!AV&|hxiAThf5`c4;*Rkj_bV*etLUzoSnV>2OIBe+#^nEJjJ$; z$;lbg>%WE#aZ2UUVq4b6Mp)Mz$Sm448EbrZZhm}hKyrtb);|(_7iakBemOI-O8LA| znX4?`w@%ohX(w&(NUI7vcob{=v<#V0ucKMMcc#{-tCJv~g5Yn<1tPU%{9x1LFTjoKvp!yZ%y+f{h`EUc#SJ^XErfN}LxSA#GYU`M{;{(x(T7fk}9WF}7 zUNGfGr7?XuQ6exMtGtdLjSNh9F?-6IiuD@>IAs%4DPw=7ckIiyd&#*)G)7qGh_WlI z7`YFtg^ZvNH`4R&2HRz_hp`4kPsVG-fprV#xb7E*@(=z%No@Vf4;*;5=A+t8fNRy6 zfeqRDIiyK0Z|oRIt2&iaCfCFowOfNpQR*)v!uNU z+^%z>64&XVyuDe?aKN&=C^+iqt=-1h+=Yeab3&WbjZTN|!kvCs7>+gCXFiBabIfYq z9&*#N;PDCNJBP{wd&3uIO5Du#mzfst?-6jbH4+AEEJ0r20i?f?J) delta 149 zcmcb=lJV{w#tkMCf_g0m2l-BB#Q89Qfmie7Sc!niMi7KpzBajT5MWLH^($xCHDC$q`vPxh6Qn7md_a`HVOTU8#YU*2=_ oHu>d~OBEz0?*-zw3KBg3p@4ybC2^zT5Alr#JA^koddRQ=0F{R|&j0`b diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 14f1bd5a608b35c670a1af6ff7561dcb9bf96b24..0f365dfa32cc0251d1adb795f351e90b05ad8f8a 100644 GIT binary patch literal 17 VcmZRMZQnOLZ@=(61~6a@1^_n>1j7IT literal 17 UcmZRMZQnOLZ@=(61_%fQ05=K*zyJUM diff --git a/.gradle/file-system.probe b/.gradle/file-system.probe index d6aaf402caa3e96c20ca4065bdaaa5eeceedad00..4faefd2666f7e1a43622191ded4a759797cd0d0d 100644 GIT binary patch literal 8 PcmZQzVC+1e9$pCm2r~kS literal 8 PcmZQzVC>w>Dp(Hy244Z) diff --git a/README.md b/README.md index 49bd684..1d2bba3 100644 --- a/README.md +++ b/README.md @@ -1 +1,22 @@ -# Arithmos \ No newline at end of file +# Arithmos + +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); + } +} From 53c82950389412b689e298e8b378b91ed255102b Mon Sep 17 00:00:00 2001 From: pavl_g <60224159+Scrappers-glitch@users.noreply.github.com> Date: Sun, 6 Aug 2023 21:42:01 +0300 Subject: [PATCH 2/2] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1d2bba3..18cb16a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -# Arithmos +# 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.