Commit 062e9d3c authored by Duong Tat Dat's avatar Duong Tat Dat
Browse files

chore: add next node change

parent 1f46055f
Loading
Loading
Loading
Loading
+52 −20
Original line number Diff line number Diff line
@@ -25,27 +25,48 @@ function remove(memory: Memory, srcAddr: string, index: number) {
  src.children.splice(index, 1)
}

// function increaseHeight(
//   memory: Memory,
//   srcAddr: string,
//   leftAddr: string,
//   rightAddr: string,
//   key: number
// ) {
//   const src = getInstance(memory, srcAddr)
//   const left = getInstance(memory, leftAddr)
//   const right = getInstance(memory, rightAddr)

//   left.keys = src.keys.slice(0)
//   left.children = src.children.slice(0)
//   left.sibling = src.sibling

//   left.orphan = false
//   right.orphan = false

//   src.keys = [key]
//   src.children = [leftAddr, rightAddr]
//   src.sibling = null
// }

function increaseHeight(
  memory: Memory,
  srcAddr: string,
  leftAddr: string,
  rootAddr: string,
  rightAddr: string,
  key: number
) {
  const src = getInstance(memory, srcAddr)
  const left = getInstance(memory, leftAddr)
  const root = getInstance(memory, rootAddr)
  const right = getInstance(memory, rightAddr)

  left.keys = src.keys.slice(0)
  left.children = src.children.slice(0)
  left.sibling = src.sibling

  left.orphan = false
  right.orphan = false

  src.keys = [key]
  src.children = [leftAddr, rightAddr]
  src.sibling = null
  root.keys = [key]
  root.children = [leftAddr, rightAddr]
  root.sibling = null

  return rootAddr
}

function split(
@@ -127,7 +148,8 @@ function transformLog(line: string) {
  if (line.includes("LATCH")) {
    return matchAndInvoke(
      ["acquire", "attempt", "release"] as const,
      (method, [addr]) => (memory) =>
      (method, [addr]) =>
        (memory) =>
          setLatch(memory, address(addr), method !== "release")
    )
  }
@@ -239,25 +261,34 @@ interface MutableMemoryFunc {
  (memory: Memory): void | string
}

interface PlaybackItem {
  memory: Memory
  root: string
}

export class Playback {
  index = -1
  private root: string = ""
  private map: Record<number, Memory | null> = { [-1]: {} }
  private map: Record<number, PlaybackItem> = {
    [-1]: {
      memory: {},
      root: "",
    },
  }
  readonly instructions: [string, MutableMemoryFunc | null][]

  constructor(lines: string[]) {
    this.instructions = lines.map((line) => [line, transformLog(line)])
  }

  private getReelState() {
  private getRealState() {
    const [line, exec] = this.instructions[this.index] ?? []

    return {
      line,
      memory: this.map[this.index] ?? {},
      memory: this.map[this.index]?.memory ?? {},
      root: this.map[this.index]?.root ?? "",
      index: this.index,
      validCommand: exec != null,
      root: this.root,
    }
  }

@@ -265,13 +296,14 @@ export class Playback {
    if (this.index <= 0) return null
    this.index -= 1

    return this.getReelState()
    return this.getRealState()
  }

  next(): PlaybackState | null {
    if (this.index >= this.instructions.length - 1) return null
    this.index += 1

    console.log("next")
    const [, exec] = this.instructions[this.index] ?? []

    const prevMemory = this.map[this.index - 1]
@@ -280,17 +312,17 @@ export class Playback {

      if (exec) {
        currMemory = JSON.parse(JSON.stringify(prevMemory))
        const root = exec(currMemory)
        const root = exec(currMemory.memory)

        if (typeof root === "string") {
          this.root = root
          currMemory.root = root
        }
      }

      this.map[this.index] = currMemory
    }

    return this.getReelState()
    return this.getRealState()
  }
}