<commit_before>package equidistant

import (
	"fmt"
)

// Closed is a grid in [0, 1]^n.
type Closed struct {
	nd uint
}

// NewClosed creates a grid.
func NewClosed(dimensions uint) *Closed {
	return &Closed{dimensions}
}

// Compute returns the nodes corresponding to a set of indices.
func (_ *Closed) Compute(indices []uint64) []float64 {
	nodes := make([]float64, len(indices))
	for i := range nodes {
		level := levelMask & indices[i]
		if level == 0 {
			nodes[i] = 0.5
		} else {
			order := indices[i] >> levelSize
			nodes[i] = float64(order) / float64(uint64(2)<<(level-1))
		}
	}
	return nodes
}

// Children returns the child indices of a set of indices.
func (self *Closed) Children(indices []uint64) []uint64 {
	return closedChildren(indices, self.nd, 0, self.nd)
}

// Index returns the nodal indices of a set of level indices.
func (self *Closed) Index(lindices []uint64) []uint64 {
	return index(lindices, closedIndex, self.nd)
}

func closedChildren(indices []uint64, nd, fd, ld uint) []uint64 {
	nn := uint(len(indices)) / nd

	children := make([]uint64, 2*nn*nd*(ld-fd))

	nc := uint(0)
	push := func(p, d uint, level, order uint64) {
		if level>>levelSize != 0 || order>>orderSize != 0 {
			panic(fmt.Sprintf("the level %d or order %d is too large", level, order))
		}
		copy(children[nc*nd:], indices[p*nd:(p+1)*nd])
		children[nc*nd+d] = level | order<<levelSize
		nc++
	}
<commit_msg>g/equidistant: add ChildrenToward<commit_after>package equidistant

import (
	"fmt"
)

// Closed is a grid in [0, 1]^n.
type Closed struct {
	nd uint
}

// NewClosed creates a grid.
func NewClosed(dimensions uint) *Closed {
	return &Closed{dimensions}
}

// Compute returns the nodes corresponding to a set of indices.
func (_ *Closed) Compute(indices []uint64) []float64 {
	nodes := make([]float64, len(indices))
	for i := range nodes {
		level := levelMask & indices[i]
		if level == 0 {
			nodes[i] = 0.5
		} else {
			order := indices[i] >> levelSize
			nodes[i] = float64(order) / float64(uint64(2)<<(level-1))
		}
	}
	return nodes
}

// Children returns the child indices of a set of indices.
func (self *Closed) Children(indices []uint64) []uint64 {
	return closedChildren(indices, self.nd, 0, self.nd)
}

// ChildrenToward returns the child indices of a set of indices with respect to
// a particular dimension.
func (self *Closed) ChildrenToward(indices []uint64, i uint) []uint64 {
	return closedChildren(indices, self.nd, i, i+1)
}

// Index returns the nodal indices of a set of level indices.
func (self *Closed) Index(lindices []uint64) []uint64 {
	return index(lindices, closedIndex, self.nd)
}

func closedChildren(indices []uint64, nd, fd, ld uint) []uint64 {
	nn := uint(len(indices)) / nd

	children := make([]uint64, 2*nn*nd*(ld-fd))

	nc := uint(0)
	push := func(p, d uint, level, order uint64) {
		if level>>levelSize != 0 || order>>orderSize != 0 {
			panic(fmt.Sprintf("the level %d or order %d is too large", level, order))
		}
		copy(children[nc*nd:], indices[p*nd:(p+1)*nd])
		children[nc*nd+d] = level | order<<levelSize
		nc++
	}

{'fraction_non_alphanumeric': 0.15259210955330943, 'fraction_numerical': 0.028366481904140855, 'mean_word_length': 4.104825291181364, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '5186106', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1063, 'n_words': 335}