<commit_before>// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "dense_tensor.h"

namespace vespalib::tensor {

/**
 * Class for building a dense tensor by inserting cell values directly into underlying array of cells.
 */
class DirectDenseTensorBuilder
{
public:
    using Cells = DenseTensor::Cells;
    using Address = DenseTensor::Address;

private:
    eval::ValueType _type;
    Cells _cells;

    static size_t calculateCellAddress(const Address &address, const eval::ValueType &type) {
        size_t result = 0;
        for (size_t i = 0; i < address.size(); ++i) {
            result *= type.dimensions()[i].size;
            result += address[i];
        }
        return result;
    }
public:
    DirectDenseTensorBuilder(const eval::ValueType &type_in);
    ~DirectDenseTensorBuilder();
    void insertCell(const Address &address, double cellValue) {
        _cells[calculateCellAddress(address, _type)] = cellValue;
    }
    Tensor::UP build();
};
<commit_msg>Allow for building index on the outside.<commit_after>// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "dense_tensor.h"

namespace vespalib::tensor {

/**
 * Class for building a dense tensor by inserting cell values directly into underlying array of cells.
 */
class DirectDenseTensorBuilder
{
public:
    using Cells = DenseTensor::Cells;
    using Address = DenseTensor::Address;

private:
    eval::ValueType _type;
    Cells _cells;

    static size_t calculateCellAddress(const Address &address, const eval::ValueType &type) {
        size_t result = 0;
        for (size_t i = 0; i < address.size(); ++i) {
            result *= type.dimensions()[i].size;
            result += address[i];
        }
        return result;
    }
public:
    DirectDenseTensorBuilder(const eval::ValueType &type_in);
    ~DirectDenseTensorBuilder();
    void insertCell(const Address &address, double cellValue) {
        insertCell(calculateCellAddress(address, _type), cellValue);
    }
    void insertCell(size_t index, double cellValue) {
        _cells[index] = cellValue;
    }
    Tensor::UP build();
};

{'fraction_non_alphanumeric': 0.10075121520106053, 'fraction_numerical': 0.007070260715863898, 'mean_word_length': 3.3288718929254304, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4822574', 'n_tokens_mistral': 670, 'n_tokens_neox': 633, 'n_words': 205}