<commit_before>package system

import (
	"errors"
	"strconv"
	"strings"

	"github.com/goesd/format/tgff"
)

// LoadTGFF constructs a platform and an application based on a TGFF file.
func LoadTGFF(path string) (Platform, Application, error) {
	p, a := Platform{}, Application{}

	r, err := tgff.ParseFile(path)
	if err != nil {
		return p, a, err
	}

	p, err = loadPlatform(r.Tables)
	if err != nil {
		return p, a, err
	}

	a, err = loadApplication(r.Graphs)
	if err != nil {
		return p, a, err
	}

	return p, a, nil
}

func loadPlatform(tables []tgff.Table) (Platform, error) {
	p := Platform{}

	size := len(tables)

	if size == 0 {
		return p, errors.New("need at least one table")
	}

	p.Cores = make([]Core, size)

	var err error

	for _, table := range tables {
		i := table.Number

		if i >= uint32(size) {
			return p, errors.New("unknown table indexing scheme")
		}

		p.Cores[i], err = loadCore(table)

		if err != nil {
			return p, err
		}
	}

	rows := len(p.Cores[0].Time)

	for i := 1; i < size; i++ {
		if rows != len(p.Cores[i].Time) {
			return p, errors.New("inconsistent table data")
		}
	}

	return p, nil
}

func loadApplication(graphs []tgff.Graph) (Application, error) {
	a := Application{}

	if len(graphs) != 1 {
		return a, errors.New("need exactly one task graph")
	}

	size := uint32(len(graphs[0].Tasks))

	a.Tasks = make([]Task, size)

	for _, task := range graphs[0].Tasks {
		i, err := extractTaskID(task.Name, size)

		if err != nil {
			return a, err
		}

		a.Tasks[i].ID = i
		a.Tasks[i].Type = task.Type
	}

	for _, arc := range graphs[0].Arcs {
		i, err := extractTaskID(arc.From, size)

		if err != nil {
			return a, err
		}

		j, err := extractTaskID(arc.To, size)

		if err != nil {
			return a, err
		}

		a.Tasks[i].Children = append(a.Tasks[i].Children, &a.Tasks[j])
	}

	return a, nil
}

func loadCore(table tgff.Table) (Core, error) {
	c := Core{
		ID: table.Number,
	}

	var tycol, tmcol, pwcol *tgff.Column

	for i := range table.Columns {
		col := &table.Columns[i]

		name := strings.ToLower(col.Name)<commit_msg>Return pointers instead of values from LoadTGFF<commit_after>package system

import (
	"errors"
	"strconv"
	"strings"

	"github.com/goesd/format/tgff"
)

// LoadTGFF constructs a platform and an application based on a TGFF file.
func LoadTGFF(path string) (*Platform, *Application, error) {
	r, err := tgff.ParseFile(path)
	if err != nil {
		return nil, nil, err
	}

	p, err := loadPlatform(r.Tables)
	if err != nil {
		return nil, nil, err
	}

	a, err := loadApplication(r.Graphs)
	if err != nil {
		return nil, nil, err
	}

	return p, a, nil
}

func loadPlatform(tables []tgff.Table) (*Platform, error) {
	size := len(tables)

	if size == 0 {
		return nil, errors.New("need at least one table")
	}

	p := &Platform{
		Cores: make([]Core, size),
	}

	var err error

	for _, table := range tables {
		i := table.Number

		if i >= uint32(size) {
			return nil, errors.New("unknown table indexing scheme")
		}

		p.Cores[i], err = loadCore(table)

		if err != nil {
			return nil, err
		}
	}

	rows := len(p.Cores[0].Time)

	for i := 1; i < size; i++ {
		if rows != len(p.Cores[i].Time) {
			return nil, errors.New("inconsistent table data")
		}
	}

	return p, nil
}

func loadApplication(graphs []tgff.Graph) (*Application, error) {
	if len(graphs) != 1 {
		return nil, errors.New("need exactly one task graph")
	}

	size := uint32(len(graphs[0].Tasks))

	a := &Application{
		Tasks: make([]Task, size),
	}

	for _, task := range graphs[0].Tasks {
		i, err := extractTaskID(task.Name, size)

		if err != nil {
			return nil, err
		}

		a.Tasks[i].ID = i
		a.Tasks[i].Type = task.Type
	}

	for _, arc := range graphs[0].Arcs {
		i, err := extractTaskID(arc.From, size)

		if err != nil {
			return nil, err
		}

		j, err := extractTaskID(arc.To, size)

		if err != nil {
			return nil, err
		}

		a.Tasks[i].Children = append(a.Tasks[i].Children, &a.Tasks[j])
	}

	return a, nil
}

func loadCore(table tgff.Table) (Core, error) {
	c := Core{
		ID: table.Number,
	}

	var tycol, tmcol, pwcol *tgff.Column

	for i := range table.Columns {
		col := &table.Columns[i]

		name := strings.ToLower(col.Name)
{'fraction_non_alphanumeric': 0.158097068716963, 'fraction_numerical': 0.005285920230658337, 'mean_word_length': 3.1299603174603177, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4960690', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1572, 'n_words': 447}