Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .gradle/7.6/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.6/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.6/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.6/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/7.6/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/file-system.probe
Binary file not shown.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
# Arithmos
# <img src="https://github.com/Arithmos-Algorithms/Arithmos/assets/60224159/7de3af64-28eb-41d6-b9b0-28df8f61071b" width=60 height=60/> 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.
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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 <K>
* @param <V>
*/
public abstract class AbstractMap<K, V> implements Map<K, V> {

protected Bucket<K, V>[] 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)) {

}
}
}
45 changes: 45 additions & 0 deletions arithmos/src/main/java/com/arithmos/structure/bucket/Bucket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.arithmos.structure.bucket;

import java.util.Map;
import java.util.Objects;

class Bucket<K, V> implements Map.Entry<K, V> {

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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.arithmos.structure.bucket;

public class BucketCollection {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.arithmos.structure.bucket;

public final class Const {
public static final int INITIAL_CAPACITY = 8;

}
85 changes: 85 additions & 0 deletions arithmos/src/main/java/com/arithmos/util/HashingUtils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}