reactdiff

package module
v0.0.0-...-09cff0b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2016 License: MIT Imports: 5 Imported by: 0

README

React Element Diff like Binary Tree Diff in Golang

GitHub license GoDoc Build Status

Features

  • Support React Element Diff (which only run O(n) on Tree diff)
  • Support three diff options.
  • Support Graphviz tree display (Mac OSX only)

What is React Element Diff like Binary Tree Diff

It is a biary tree diff, which support three option:

  • INSERT_MARKUP: Diff and Insert node if current tree don't have this node.
  • MOVE_EXISTING: Move node refer diff tree.
  • REMOVE_NODE: Remove node if found node not exist in diff tree.

The diff rule refer to "React Element Diff" and trying to modify some rul into tree diff.

Limitations

It has some limitation when we trying to apply element dif into tree diff.

  • The tree must be binary tree (could be unsort)
  • The diff most obey the diff option, otherwise diff result will be wrong.
  • It take more memory space and run time to support three diff options.

Install

go get github.com/kkdai/react-diff

Usage


package main

import (
	. "github.com/kkdai/react-diff"
)

func main() {
	nT := NewReactDiffTree(20)
	nT.InsertNote("a", 1)
	nT.InsertNote("b", 2)
	nT.InsertNote("c", 3)
	nT.InsertNote("d", 4)
	nT.InsertNote("f", 6)
	nT.InsertNote("e", 8)

	nT2 := NewReactDiffTree(20)
	nT2.InsertNote("a", 1)
	nT2.InsertNote("b", 2)
	nT2.InsertNote("c", 3)
	nT2.InsertNote("d", 5)
	nT2.InsertNote("h", 7)
	nT2.InsertNote("e", 10)

	nT.DiffTree(nT2, INSERT_MARKUP)
	nT.DisplayGraphvizTree()
}

Benchmark

BenchmarkAdd-4 	 1000000	      1229 ns/op
BenchmarkDel-4 	 5000000	       228 ns/op
BenchmarkGet-4 	   10000	    122375 ns/op
BenchmarkDiff-4	  300000	      4396 ns/op

Inspired

Project52

It is one of my project 52.

License

This package is licensed under MIT license. See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DiffOption

type DiffOption int
const (
	INSERT_MARKUP DiffOption = 1 << iota
	MOVE_EXISTING DiffOption = 1 << iota
	REMOVE_NODE   DiffOption = 1 << iota
)

type ReactDiff

type ReactDiff struct {
	//Major node structure
	NodeList []string

	//Node set target to store all node item in this tree
	//It help to determine if any element is exist in this tree or not
	NodeSet map[string]bool
}

React Diff is a binary unsort tree to represent the concept of React Diff React Diff has optimize tree diff algorithm to optimize original tree diff O(n^3) -> O(n)

func NewReactDiffTree

func NewReactDiffTree(treeSize int) *ReactDiff

New a React Diff Tree with define size The binary tree with basic alignment with array 0-> 1, 2 1-> 3, 4 2-> 5, 6 ....

func (*ReactDiff) Clone

func (r *ReactDiff) Clone() *ReactDiff

Clone current tree to another new one

func (*ReactDiff) DiffTree

func (r *ReactDiff) DiffTree(targetTree *ReactDiff, option DiffOption) bool

Diff Tree will diff with input target tree, if not identical will replace to new one Return true if two tree is identical, false will replace to new one with React Diff Algorithm

func (*ReactDiff) DisplayGraphvizTree

func (r *ReactDiff) DisplayGraphvizTree()

Print out tree structure via Graphviz

func (*ReactDiff) GetNodeIndex

func (r *ReactDiff) GetNodeIndex(searchTarget interface{}) int

Return node index via node value, return -1 if node is not exist

func (*ReactDiff) InsertNode

func (r *ReactDiff) InsertNode(val string, nodeIndex int) bool

Insert node into ReactDiff tree below to Node Index It will return the node index and success or not Note: If parent node not exist, will return false

func (*ReactDiff) RemoveNode

func (r *ReactDiff) RemoveNode(val string) bool

Remove node via node value, return true if node exist and successful delete

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL