<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 "memory_usage_stuff.h"
#include "value_type.h"
#include "typed_cells.h"
#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/util/traits.h>
#include <vector>
#include <memory>

namespace vespalib::eval {

class Tensor;

/**
 * An abstract Value.
 **/
struct Value {
    using UP = std::unique_ptr<Value>;
    using CREF = std::reference_wrapper<const Value>;
    virtual const ValueType &type() const = 0;
    virtual ~Value() {}

// ---- new interface enabling separation of values and operations
    // Root lookup structure for mapping labels to dense subspace indexes
    struct Index {

        // A view able to look up dense subspace indexes from labels
        // specifying a partial address for the dimensions given to
        // create_view. A view is re-usable. Lookups are performed by
        // calling the lookup function and lookup results are
        // extracted using the next_result function.
        struct View {

            // look up dense subspace indexes from labels specifying a
            // partial address for the dimensions given to
            // create_view. Results from the lookup is extracted using
            // the next_result function.
            virtual void lookup(ConstArrayRef<const vespalib::stringref*> addr) = 0;

            // Extract the next result (if any) from the previous
            // lookup into the given partial address and index. Only
            // the labels for the dimensions NOT specified in
            // create_view will be extracted here.
            virtual bool next_result(ConstArrayRef<vespalib::stringref*> addr_out, size_t &idx_out) = 0;

            virtual ~View() {}
        };

        // total number of mappings (equal to the number of dense subspaces)
        virtual size_t size() const = 0;

        // create a view able to look up dense subspaces based on
        // labels from a subset of the mapped dimensions.
        virtual std::unique_ptr<View> create_view(const std::vector<size_t> &dims) const = 0;

        virtual ~Index() {}
    };
    virtual TypedCells cells() const = 0;
    virtual const Index &index() const = 0;
// --- end of new interface

    virtual MemoryUsage get_memory_usage() const = 0;

// --- old interface that may be (partially) removed in the future
    virtual bool is_double() const { return type().is_double(); }
    virtual bool is_tensor() const { return type().is_tensor(); }
    virtual double as_double() const;
    bool as_bool() const { return (as_double() != 0.0); }
    virtual const Tensor *as_tensor() const { return nullptr; }
// --- end of old interface
};

/**
 * Common index for values without any mapped dimensions.
 **/
class TrivialIndex : public Value::Index {
private:
    TrivialIndex();
    static TrivialIndex _index;
public:
    static const TrivialIndex &get() { return _index; }
    size_t size() const override;
    std::unique_ptr<View> create_view(const std::vector<size_t> &dims) const override;
};

template <typename T>
class ScalarValue final : public Value
{
private:
    T _value;
    static ValueType _type;
public:
    ScalarValue(T value) : _value(value) {}
    TypedCells cells() const final override { return TypedCells(ConstArrayRef<T>(&_value, 1)); }
    const Index &index() const final override { return TrivialIndex::get(); }
    MemoryUsage get_memory_usage() const final override { return self_memory_usage<ScalarValue<T>>(); }
    bool is_double() const final override { return std::is_same_v<T,double>; }
    double as_double() const final override { return _value; }
    const ValueType &type() const final override { return _type; }
    static const ValueType &shared_type() { return _type; }
};
<commit_msg>Add explicit instantiation declaration for ScalarValue<double> and ScalarValue<float>.<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 "memory_usage_stuff.h"
#include "value_type.h"
#include "typed_cells.h"
#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/util/traits.h>
#include <vector>
#include <memory>

namespace vespalib::eval {

class Tensor;

/**
 * An abstract Value.
 **/
struct Value {
    using UP = std::unique_ptr<Value>;
    using CREF = std::reference_wrapper<const Value>;
    virtual const ValueType &type() const = 0;
    virtual ~Value() {}

// ---- new interface enabling separation of values and operations
    // Root lookup structure for mapping labels to dense subspace indexes
    struct Index {

        // A view able to look up dense subspace indexes from labels
        // specifying a partial address for the dimensions given to
        // create_view. A view is re-usable. Lookups are performed by
        // calling the lookup function and lookup results are
        // extracted using the next_result function.
        struct View {

            // look up dense subspace indexes from labels specifying a
            // partial address for the dimensions given to
            // create_view. Results from the lookup is extracted using
            // the next_result function.
            virtual void lookup(ConstArrayRef<const vespalib::stringref*> addr) = 0;

            // Extract the next result (if any) from the previous
            // lookup into the given partial address and index. Only
            // the labels for the dimensions NOT specified in
            // create_view will be extracted here.
            virtual bool next_result(ConstArrayRef<vespalib::stringref*> addr_out, size_t &idx_out) = 0;

            virtual ~View() {}
        };

        // total number of mappings (equal to the number of dense subspaces)
        virtual size_t size() const = 0;

        // create a view able to look up dense subspaces based on
        // labels from a subset of the mapped dimensions.
        virtual std::unique_ptr<View> create_view(const std::vector<size_t> &dims) const = 0;

        virtual ~Index() {}
    };
    virtual TypedCells cells() const = 0;
    virtual const Index &index() const = 0;
// --- end of new interface

    virtual MemoryUsage get_memory_usage() const = 0;

// --- old interface that may be (partially) removed in the future
    virtual bool is_double() const { return type().is_double(); }
    virtual bool is_tensor() const { return type().is_tensor(); }
    virtual double as_double() const;
    bool as_bool() const { return (as_double() != 0.0); }
    virtual const Tensor *as_tensor() const { return nullptr; }
// --- end of old interface
};

/**
 * Common index for values without any mapped dimensions.
 **/
class TrivialIndex : public Value::Index {
private:
    TrivialIndex();
    static TrivialIndex _index;
public:
    static const TrivialIndex &get() { return _index; }
    size_t size() const override;
    std::unique_ptr<View> create_view(const std::vector<size_t> &dims) const override;
};

template <typename T>
class ScalarValue final : public Value
{
private:
    T _value;
    static ValueType _type;
public:
    ScalarValue(T value) : _value(value) {}
    TypedCells cells() const final override { return TypedCells(ConstArrayRef<T>(&_value, 1)); }
    const Index &index() const final override { return TrivialIndex::get(); }
    MemoryUsage get_memory_usage() const final override { return self_memory_usage<ScalarValue<T>>(); }
    bool is_double() const final override { return std::is_same_v<T,double>; }
    double as_double() const final override { return _value; }
    const ValueType &type() const final override { return _type; }
    static const ValueType &shared_type() { return _type; }
};

extern template class ScalarValue<double>;
extern template class ScalarValue<float>;

{'fraction_non_alphanumeric': 0.09663492063492063, 'fraction_numerical': 0.004317460317460317, 'mean_word_length': 3.0723888314374355, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '4801271', 'n_tokens_mistral': 2190, 'n_tokens_neox': 2095, 'n_words': 829}