<commit_before>(function(root) {

var Board = root.Board

var helper = root.helper
var setting = root.setting

if (typeof require != 'undefined') {
    Board = require('./board')

    helper = require('./helper')
    setting = require('./setting')
}

var context = typeof module != 'undefined' ? module.exports : (window.gametree = {})

context.new = function() {
    return {
        id: helper.getId(),
        nodes: [],
        subtrees: [],
        current: null,
        parent: null,
        collapsed: false
    }
}

context.clone = function(tree, parent) {
    if (!parent) parent = null

    var c = {
        id: tree.id,
        nodes: [],
        subtrees: [],
        current: tree.current,
        parent: parent,
        collapsed: tree.collapsed
    }

    tree.nodes.forEach(function(node) {
        var cn = {}

        for (var key in node) {
            if (key == 'board') continue

            if (Object.prototype.toString.call(node[key]) == '[object Array]') {
                cn[key] = []

                for (var i = 0; i < node[key].length; i++) {
                    cn[key].push(node[key][i])
                }
            } else {
                cn[key] = node[key]
            }
        }

        c.nodes.push(cn)
    })

    tree.subtrees.forEach(function(subtree) {
        c.subtrees.push(context.clone(subtree, c))
    })

    return c
}

context.getRoot = function(tree) {
    while (tree.parent != null) tree = tree.parent
    return tree
}

context.getPlayerName = function(sign, tree, fallback) {
    tree = context.getRoot(tree)
    var color = sign > 0 ? 'B' : 'W'

    if (tree.nodes.length == 0) return fallback

    var result = ''
    if (('P' + color) in tree.nodes[0]) result = tree.nodes[0]['P' + color][0]
    else if ((color + 'T') in tree.nodes[0]) result = tree.nodes[0][color + 'T'][0]

    return result.trim() == '' ? fallback : result
}

context.navigate = function(tree, index, step) {
    if (index + step >= 0 && index + step < tree.nodes.length) {
        return [tree, index + step]
    } else if (index + step < 0 && tree.parent != null) {
        var prev = tree.parent
        var newstep = index + step + 1

        return context.navigate(prev, prev.nodes.length - 1, newstep)
    } else if (index + step >= tree.nodes.length && tree.current != null) {
        var next = tree.subtrees[tree.current]
        var newstep = index + step - tree.nodes.length

        return context.navigate(next, 0, newstep)
    }

    return null
}

context.makeNodeIterator = function(tree, index) {
    var root = context.getRoot(tree)
    var level = context.getLevel(tree, index, root)
    var sections = context.getSection(root, level)
    var j = sections.map(function(x) { return x[0] }).indexOf(tree)

    return {
        navigate: function(step) {
            if (j + step >= 0 && j + step < sections.length) {
                j = j + step
            } else if (j + step >= sections.length) {
                step = j + step - sections.length
                sections = context.getSection(root, ++level)
                j = 0
                if (sections.length != 0) this.navigate(step)
            } else if (j + step < 0) {
                step = j + step + 1
                sections = context.getSection(root, --level)
                j = sections.length - 1
                if (sections.length != 0) this.navigate(step)
            }
        },
        value: function() {
            return j < sections.length && j >= 0 ? sections[j] : null
        },
        next: function() {
            this.navigate(1)
            return this.value()
        },
        prev: function() {
            this.navigate(-1)
            return this.value()
        }
    }
}

context.splitTree = function(tree, index) {
    if (index < 0 || index >= tree.nodes.length - 1) return tree

    var newnodes = tree.nodes.slice(0, index + 1)
    tree.nodes = tree.nodes.slice(index + 1)

    var newtree = context.new()
    newtree.nodes = newnodes
    newtree.subtrees = [tree]
    newtree.parent = tree.parent
    newtree.current = 0
    tree.parent = newtree

    if (newtree.parent)
        newtree.parent.subtrees[newtree.parent.subtrees.indexOf(tree)] = newtree

    return newtree
}

context.reduceTree = function(tree) {
    if (tree.subtrees.length != 1) return tree

    tree.nodes = tree.nodes.concat(tree.subtrees[0].nodes)
    tree.current = tree.subtrees[0].current
    tree.subtrees = tree.subtrees[0].subtrees

    tree.subtrees.forEach(function(subtree) {
        subtree.parent = tree
    })

    return tree
}

context.getHeight = function(tree) {
    var height = 0

    tree.subtrees.forEach(function(subtree) {
        height = Math.max(context.getHeight(subtree), height)
    })

    return height + tree.nodes.length
}

context.getCurrentHeight = function(tree) {
    var height = tree.nodes.length

    if (tree.current != null)
        height += context.getCurrentHeight(tree.subtrees[tree.current])

    return height
}

context.getLevel = function(tree, index) {
    return index + (tree.parent ? context.getLevel(tree.parent, tree.parent.nodes.length) : 0)
}

context.getSection = function(tree, level) {
    if (level < 0) return []
    if (level < tree.nodes.length) return [[tree, level]]

    var sections = []

    tree.subtrees.forEach(function(subtree) {
        sections = sections.concat(context.getSection(subtree, level - tree.nodes.length))
    })

    return sections
}

context.getSectionWidth = function(y, matrix) {<commit_msg>Add gametree.getWidth()<commit_after>(function(root) {

var Board = root.Board

var helper = root.helper
var setting = root.setting

if (typeof require != 'undefined') {
    Board = require('./board')

    helper = require('./helper')
    setting = require('./setting')
}

var context = typeof module != 'undefined' ? module.exports : (window.gametree = {})

context.new = function() {
    return {
        id: helper.getId(),
        nodes: [],
        subtrees: [],
        current: null,
        parent: null,
        collapsed: false
    }
}

context.clone = function(tree, parent) {
    if (!parent) parent = null

    var c = {
        id: tree.id,
        nodes: [],
        subtrees: [],
        current: tree.current,
        parent: parent,
        collapsed: tree.collapsed
    }

    tree.nodes.forEach(function(node) {
        var cn = {}

        for (var key in node) {
            if (key == 'board') continue

            if (Object.prototype.toString.call(node[key]) == '[object Array]') {
                cn[key] = []

                for (var i = 0; i < node[key].length; i++) {
                    cn[key].push(node[key][i])
                }
            } else {
                cn[key] = node[key]
            }
        }

        c.nodes.push(cn)
    })

    tree.subtrees.forEach(function(subtree) {
        c.subtrees.push(context.clone(subtree, c))
    })

    return c
}

context.getRoot = function(tree) {
    while (tree.parent != null) tree = tree.parent
    return tree
}

context.getPlayerName = function(sign, tree, fallback) {
    tree = context.getRoot(tree)
    var color = sign > 0 ? 'B' : 'W'

    if (tree.nodes.length == 0) return fallback

    var result = ''
    if (('P' + color) in tree.nodes[0]) result = tree.nodes[0]['P' + color][0]
    else if ((color + 'T') in tree.nodes[0]) result = tree.nodes[0][color + 'T'][0]

    return result.trim() == '' ? fallback : result
}

context.navigate = function(tree, index, step) {
    if (index + step >= 0 && index + step < tree.nodes.length) {
        return [tree, index + step]
    } else if (index + step < 0 && tree.parent != null) {
        var prev = tree.parent
        var newstep = index + step + 1

        return context.navigate(prev, prev.nodes.length - 1, newstep)
    } else if (index + step >= tree.nodes.length && tree.current != null) {
        var next = tree.subtrees[tree.current]
        var newstep = index + step - tree.nodes.length

        return context.navigate(next, 0, newstep)
    }

    return null
}

context.makeNodeIterator = function(tree, index) {
    var root = context.getRoot(tree)
    var level = context.getLevel(tree, index, root)
    var sections = context.getSection(root, level)
    var j = sections.map(function(x) { return x[0] }).indexOf(tree)

    return {
        navigate: function(step) {
            if (j + step >= 0 && j + step < sections.length) {
                j = j + step
            } else if (j + step >= sections.length) {
                step = j + step - sections.length
                sections = context.getSection(root, ++level)
                j = 0
                if (sections.length != 0) this.navigate(step)
            } else if (j + step < 0) {
                step = j + step + 1
                sections = context.getSection(root, --level)
                j = sections.length - 1
                if (sections.length != 0) this.navigate(step)
            }
        },
        value: function() {
            return j < sections.length && j >= 0 ? sections[j] : null
        },
        next: function() {
            this.navigate(1)
            return this.value()
        },
        prev: function() {
            this.navigate(-1)
            return this.value()
        }
    }
}

context.splitTree = function(tree, index) {
    if (index < 0 || index >= tree.nodes.length - 1) return tree

    var newnodes = tree.nodes.slice(0, index + 1)
    tree.nodes = tree.nodes.slice(index + 1)

    var newtree = context.new()
    newtree.nodes = newnodes
    newtree.subtrees = [tree]
    newtree.parent = tree.parent
    newtree.current = 0
    tree.parent = newtree

    if (newtree.parent)
        newtree.parent.subtrees[newtree.parent.subtrees.indexOf(tree)] = newtree

    return newtree
}

context.reduceTree = function(tree) {
    if (tree.subtrees.length != 1) return tree

    tree.nodes = tree.nodes.concat(tree.subtrees[0].nodes)
    tree.current = tree.subtrees[0].current
    tree.subtrees = tree.subtrees[0].subtrees

    tree.subtrees.forEach(function(subtree) {
        subtree.parent = tree
    })

    return tree
}

context.getHeight = function(tree) {
    var height = 0

    tree.subtrees.forEach(function(subtree) {
        height = Math.max(context.getHeight(subtree), height)
    })

    return height + tree.nodes.length
}

context.getCurrentHeight = function(tree) {
    var height = tree.nodes.length

    if (tree.current != null)
        height += context.getCurrentHeight(tree.subtrees[tree.current])

    return height
}

context.getWidth = function(tree) {
    var width = 0

    tree.subtrees.forEach(function(subtree) {
        width += context.getWidth(subtree)
    })

    return Math.max(width, 1)
}

context.getLevel = function(tree, index) {
    return index + (tree.parent ? context.getLevel(tree.parent, tree.parent.nodes.length) : 0)
}

context.getSection = function(tree, level) {
    if (level < 0) return []
    if (level < tree.nodes.length) return [[tree, level]]

    var sections = []

    tree.subtrees.forEach(function(subtree) {
        sections = sections.concat(context.getSection(subtree, level - tree.nodes.length))
    })

    return sections
}

context.getSectionWidth = function(y, matrix) {
{'fraction_non_alphanumeric': 0.13662661326212727, 'fraction_numerical': 0.006942590120160214, 'mean_word_length': 2.1237142062830134, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1313628', 'n_tokens_mistral': 3574, 'n_tokens_neox': 3404, 'n_words': 816}