Update CharBag, Add remote methods in Document

This commit is contained in:
MrLetsplay 2023-11-24 19:53:32 +01:00
parent fb18863ed2
commit 0960890d0d
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
3 changed files with 22 additions and 5 deletions

View File

@ -12,6 +12,7 @@ public class ArrayCharBag implements CharBag {
int i = 0;
// TODO: use binary search
while(i < chars.size() && Util.comparePositions(chars.get(i).position(), character.position()) < 0) i++;
if(i < chars.size() && Util.comparePositions(chars.get(i).position(), character.position()) == 0) return -1;
chars.add(i, character);
return i;
}

View File

@ -2,12 +2,8 @@ package me.mrletsplay.shareclientcore.document;
public interface CharBag {
// public Character find(PositionIdentifier position);
// public Character findBefore(PositionIdentifier position);
// public Character findAfter(PositionIdentifier position);
/**
* Adds a character to the bag and returns the index it was inserted at
* Adds a character to the bag and returns the index it was inserted at, or -1 if it was not inserted because it already exists
* @param character The character to add
* @return The index it was inserted at
*/

View File

@ -48,6 +48,26 @@ public class Document {
}
}
/**
* Inserts a remote change into the document and updates internal parameters accordingly
* @param c The character to insert
* @return The index of the inserted character, or -1 if it was not inserted because it already exists
*/
public int remoteInsert(Char c) {
lamport = Math.max(c.lamport(), lamport) + 1;
return charBag.add(c);
}
/**
* Removes a character from the document and updates internal parameters accordingly
* @param c The character to delete
* @return The index the character was located at, or -1 if it was not contained in the document
*/
public int remoteDelete(Char c) {
lamport = Math.max(c.lamport(), lamport) + 1;
return charBag.remove(c);
}
public CharBag getCharBag() {
return charBag;
}