initial commit

This commit is contained in:
MrLetsplay 2023-11-21 22:52:11 +01:00
commit 3f229dca29
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
11 changed files with 233 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
/bin/
/target/
/.settings/
/.classpath
/dependency-reduced-pom.xml

23
.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ShareClient-Core</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

28
pom.xml Normal file
View File

@ -0,0 +1,28 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.mrletsplay</groupId>
<artifactId>ShareClient-Core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,8 @@
package me.mrletsplay.shareclientcore;
public class ShareClient {
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,22 @@
package me.mrletsplay.shareclientcore.document;
import java.util.ArrayList;
import java.util.List;
public class ArrayCharBag implements CharBag {
private List<Char> chars = new ArrayList<>();
@Override
public void add(Char character) {
int i = 0;
while(Util.comparePositions(chars.get(i).position(), character.position()) < 0) i++;
chars.add(i, character);
}
@Override
public void remove(Char character) {
chars.remove(character);
}
}

View File

@ -0,0 +1,7 @@
package me.mrletsplay.shareclientcore.document;
public record Char(Identifier[] position, int lamport, String value) {
}

View File

@ -0,0 +1,12 @@
package me.mrletsplay.shareclientcore.document;
public interface CharBag {
// public Character find(PositionIdentifier position);
// public Character findBefore(PositionIdentifier position);
// public Character findAfter(PositionIdentifier position);
public void add(Char character);
public void remove(Char character);
}

View File

@ -0,0 +1,10 @@
package me.mrletsplay.shareclientcore.document;
public class Document {
private CharBag charBag;
public Document() {
}
}

View File

@ -0,0 +1,12 @@
package me.mrletsplay.shareclientcore.document;
public record Identifier(int digit, int site) implements Comparable<Identifier> {
@Override
public int compareTo(Identifier o) {
int tmp;
if((tmp = Integer.compare(digit, o.digit)) != 0) return tmp;
return Integer.compare(site, o.site);
}
}

View File

@ -0,0 +1,73 @@
package me.mrletsplay.shareclientcore.document;
import java.util.ArrayList;
import java.util.List;
public class Util {
public static final int BASE = 10;
public static int comparePositions(Identifier[] a, Identifier[] b) {
for(int i = 0; i < Math.min(a.length, b.length); i++) {
int tmp;
if((tmp = a[i].compareTo(b[i])) != 0) return tmp;
}
return Integer.compare(a.length, b.length);
}
public static Identifier[] generatePositionBetween(Identifier[] before, Identifier[] after, int site) {
List<Identifier> newPosition = new ArrayList<>();
for(int i = 0; i < Math.min(before.length, after.length) + 1; i++) {
Identifier c1 = i >= before.length ? new Identifier(0, site) : before[i];
Identifier c2 = i >= after.length ? new Identifier(BASE, site) : after[i];
if(c1.digit() != c2.digit()) {
// TODO: generate delta, then pick a value between
}
if(c1.site() == c2.site()) {
newPosition.add(c1);
continue; // Identifiers are equal, compare next digit
}
if(c1.site() < c2.site()) {
// Anything starting with before will be sorted before after
newPosition.add(new Identifier(BASE, site));
return newPosition.toArray(Identifier[]::new);
}
throw new RuntimeException("Invalid site order");
}
return null;
}
public static void getIncrement(Identifier[] i1, Identifier[] i2, int offset) {
}
/**
* Subtract b from a, where a > b
*/
public static int[] subtract(Identifier[] a, Identifier[] b, int offset) {
int carry = 0;
int[] diff = new int[Math.max(a.length, b.length) - offset];
for(int i = diff.length - 1; i >= 0; i--) {
int idx = i + offset;
int dA = (idx >= a.length ? 0 : a[idx].digit()) - carry;
int dB = idx >= b.length ? 0 : b[idx].digit();
if(dA < dB) {
carry = 1;
diff[i] = dA - dB + BASE;
}else {
carry = 0;
diff[i] = dA - dB;
}
}
return diff;
}
}

View File

@ -0,0 +1,33 @@
package me.mrletsplay.shareclientcore;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
import me.mrletsplay.shareclientcore.document.Identifier;
import me.mrletsplay.shareclientcore.document.Util;
public class DecimalTest {
@Test
public void testSimpleSubtraction() {
Identifier[] a = new Identifier[] { new Identifier(1, 0), new Identifier(2, 0), new Identifier(3, 0) };
Identifier[] b = new Identifier[] { new Identifier(1, 0), new Identifier(1, 0), new Identifier(3, 0) };
assertArrayEquals(Util.subtract(a, b, 0), new int[] { 0, 1, 0 });
}
@Test
public void testCarrySubtraction() {
Identifier[] a = new Identifier[] { new Identifier(2, 0), new Identifier(0, 0), new Identifier(4, 0) };
Identifier[] b = new Identifier[] { new Identifier(1, 0), new Identifier(0, 0), new Identifier(5, 0) };
assertArrayEquals(Util.subtract(a, b, 0), new int[] { 0, 9, 9 });
}
@Test
public void testOffsetSubtraction() {
Identifier[] a = new Identifier[] { new Identifier(1, 0), new Identifier(0, 0), new Identifier(5, 0) };
Identifier[] b = new Identifier[] { new Identifier(1, 0), new Identifier(0, 0), new Identifier(4, 0) };
assertArrayEquals(Util.subtract(a, b, 1), new int[] { 0, 1 });
}
}