Git Product home page Git Product logo

treedoc-java's Introduction

Implementation of TreeDOC data structure in Java

Scientific article describing the data structure: Link

Example usage

package org.treedoc;

import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class IntegrationTest {
	private static final String USER_1 = "user1";
	private static final String USER_2 = "user2";
	private static final String USER_3 = "user3";

	private final TreeDOC<Character, String> treeDOC = TreeDOC.create();

	@Test
	void test() {

		// Document is initially empty
		expectContent(List.of());

		// User 1 inserted first character
		var aPath = treeDOC.insertBetween(null, null, 'A', USER_1);
		expectContent(List.of('A'));

		// User 2 inserted second character before A
		var bPath = treeDOC.insertBetween(null, aPath, 'B', USER_2);
		expectContent(List.of('B', 'A'));

		// Users 1 and 3 concurrently inserted characters between B and A
		var cPath = treeDOC.insertBetween(bPath, aPath, 'C', USER_3);
		var dPath = treeDOC.insertBetween(bPath, aPath, 'D', USER_1);
		expectContent(List.of('B', 'D', 'C', 'A'));

		// Users 1 and 3 concurrently inserted character between D and C, operations was executed in different order
		var ePath = treeDOC.insertBetween(dPath, cPath, 'E', USER_1);
		var fPath = treeDOC.insertBetween(dPath, cPath, 'F', USER_3);

		// Since disambiguator of User 1 is lower than disambiguator of User 2 character E is inserted before F
		expectContent(List.of('B', 'D', 'E', 'F', 'C', 'A'));

		// User 2 deletes character F while User 3 inserts new character between F and C
		treeDOC.delete(fPath);
		var gPath = treeDOC.insertBetween(fPath, cPath, 'G', USER_2);

		expectContent(List.of('B', 'D', 'E', 'G', 'C', 'A'));
	}

	private void expectContent(List<Character> expectedContent) {
		assertThat(treeDOC.getAtoms()).isEqualTo(expectedContent);
	}

}

treedoc-java's People

Contributors

vorobii-vitalii avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.