<commit_before>/******************************************************************************
 *                    _   _____   __________                                  *
 *                   | | / / _ | / __/_  __/     Visibility                   *
 *                   | |/ / __ |_\ \  / /          Across                     *
 *                   |___/_/ |_/___/ /_/       Space and Time                 *
 *                                                                            *
 * This file is part of VAST. It is subject to the license terms in the       *
 * LICENSE file found in the top-level directory of this distribution and at  *
 * http://vast.io/license. No part of VAST, including this file, may be       *
 * copied, modified, propagated, or distributed except according to the terms *
 * contained in the LICENSE file.                                             *
 ******************************************************************************/

#pragma once

#include <functional>
#include <string>
#include <type_traits>
#include <vector>

#include <caf/detail/type_list.hpp>
#include <caf/error.hpp>
#include <caf/fwd.hpp>
#include <caf/intrusive_ptr.hpp>
#include <caf/make_counted.hpp>
#include <caf/none.hpp>
#include <caf/ref_counted.hpp>
#include <caf/serializer.hpp>
#include <caf/variant.hpp>

#include "vast/aliases.hpp"
#include "vast/attribute.hpp"
#include "vast/expected.hpp"
#include "vast/fwd.hpp"
#include "vast/key.hpp"
#include "vast/offset.hpp"
#include "vast/operator.hpp"
#include "vast/optional.hpp"
#include "vast/time.hpp"

#include "vast/concept/hashable/hash_append.hpp"
#include "vast/concept/hashable/type_erased_hasher.hpp"
#include "vast/concept/hashable/uhash.hpp"
#include "vast/concept/hashable/xxhash.hpp"

#include "vast/detail/assert.hpp"
#include "vast/detail/operators.hpp"
#include "vast/detail/range.hpp"
#include "vast/detail/stack_vector.hpp"

namespace vast {

class address;
class data;
class json;
class pattern;
class port;
class schema;
class subnet;

// -- type hierarchy ----------------------------------------------------------

class abstract_type;
struct none_type;
struct boolean_type;
struct integer_type;
struct count_type;
struct real_type;
struct timespan_type;
struct timestamp_type;
struct string_type;
struct pattern_type;
struct address_type;
struct subnet_type;
struct port_type;
struct enumeration_type;
struct vector_type;
struct set_type;
struct map_type;
struct record_type;
struct alias_type;

/// @relates type
using concrete_types = caf::detail::type_list<
  none_type,
  boolean_type,
  integer_type,
  count_type,
  real_type,
  timespan_type,
  timestamp_type,
  string_type,
  pattern_type,
  address_type,
  subnet_type,
  port_type,
  enumeration_type,
  vector_type,
  set_type,
  map_type,
  record_type,
  alias_type
>;

using type_id_type = int8_t;

constexpr type_id_type invalid_type_id = -1;

template <class T>
constexpr type_id_type type_id() {
  static_assert(caf::detail::tl_contains<concrete_types, T>::value,
                "type IDs only available for concrete types");
  return caf::detail::tl_index_of<concrete_types, T>::value;
}

// -- type ------------------------------------------------------------------

/// @relates type
using type_digest = xxhash64::result_type;

/// @relates type
using type_hasher = type_erased_hasher<type_digest>;

/// @relates type
using abstract_type_ptr = caf::intrusive_ptr<abstract_type>;

/// The sematic representation of data.
class type : detail::totally_ordered<type> {
public:
  // -- construction & assignment ---------------------------------------------

  /// Constructs an invalid type.
  type() = default;

  /// Constructs a type from a concrete instance.
  /// @tparam T a type that derives from @ref abstract_type.
  /// @param x An instance of a type.
  template <
    class T,
    class = std::enable_if_t<caf::detail::tl_contains<concrete_types, T>::value>
  >
  type(T x) : ptr_{caf::make_counted<T>(std::move(x))} {
    // nop
  }

  /// Copy-constructs a type.
  type(const type& x);

  /// Copy-assigns a type.
  type& operator=(const type& x);

  /// Move-constructs a type.
  type(type&&) = default;

  /// Move-assigns a type.
  type& operator=(type&&) = default;

  /// Assigns a type from another instance
  template <class T>
  type& operator=(T x) {
    ptr_ = caf::make_counted<T>(std::move(x));
    return *this;
  }

  // -- modifiers -------------------------------------------------------------

  /// Sets the type name.
  /// @param x The new name of the type.
  /// @returns a new type with name *x*.
  type name(std::string x) const;

  /// Specifies a list of attributes.
  /// @param xs The list of attributes.
  /// @returns A new type with *xs* as attributes.
  type attributes(std::vector<attribute> xs) const;

  // -- inspectors ------------------------------------------------------------

  /// Checks whether a type contains a valid type.
  /// @returns `true` iff the type contains an instantiated type.
  explicit operator bool() const;

  /// @returns the name of the type.
  const std::string& name() const;

  /// @returns The attributes of the type.
  const std::vector<attribute>& attributes() const;

  /// @cond PRIVATE

  abstract_type_ptr ptr() const;

  const abstract_type* raw_ptr() const noexcept;

  const abstract_type* operator->() const noexcept;

  /// @pre `ptr() != nullptr`<commit_msg>Remove no longer valid precondition<commit_after>/******************************************************************************
 *                    _   _____   __________                                  *
 *                   | | / / _ | / __/_  __/     Visibility                   *
 *                   | |/ / __ |_\ \  / /          Across                     *
 *                   |___/_/ |_/___/ /_/       Space and Time                 *
 *                                                                            *
 * This file is part of VAST. It is subject to the license terms in the       *
 * LICENSE file found in the top-level directory of this distribution and at  *
 * http://vast.io/license. No part of VAST, including this file, may be       *
 * copied, modified, propagated, or distributed except according to the terms *
 * contained in the LICENSE file.                                             *
 ******************************************************************************/

#pragma once

#include <functional>
#include <string>
#include <type_traits>
#include <vector>

#include <caf/detail/type_list.hpp>
#include <caf/error.hpp>
#include <caf/fwd.hpp>
#include <caf/intrusive_ptr.hpp>
#include <caf/make_counted.hpp>
#include <caf/none.hpp>
#include <caf/ref_counted.hpp>
#include <caf/serializer.hpp>
#include <caf/variant.hpp>

#include "vast/aliases.hpp"
#include "vast/attribute.hpp"
#include "vast/expected.hpp"
#include "vast/fwd.hpp"
#include "vast/key.hpp"
#include "vast/offset.hpp"
#include "vast/operator.hpp"
#include "vast/optional.hpp"
#include "vast/time.hpp"

#include "vast/concept/hashable/hash_append.hpp"
#include "vast/concept/hashable/type_erased_hasher.hpp"
#include "vast/concept/hashable/uhash.hpp"
#include "vast/concept/hashable/xxhash.hpp"

#include "vast/detail/assert.hpp"
#include "vast/detail/operators.hpp"
#include "vast/detail/range.hpp"
#include "vast/detail/stack_vector.hpp"

namespace vast {

class address;
class data;
class json;
class pattern;
class port;
class schema;
class subnet;

// -- type hierarchy ----------------------------------------------------------

class abstract_type;
struct none_type;
struct boolean_type;
struct integer_type;
struct count_type;
struct real_type;
struct timespan_type;
struct timestamp_type;
struct string_type;
struct pattern_type;
struct address_type;
struct subnet_type;
struct port_type;
struct enumeration_type;
struct vector_type;
struct set_type;
struct map_type;
struct record_type;
struct alias_type;

/// @relates type
using concrete_types = caf::detail::type_list<
  none_type,
  boolean_type,
  integer_type,
  count_type,
  real_type,
  timespan_type,
  timestamp_type,
  string_type,
  pattern_type,
  address_type,
  subnet_type,
  port_type,
  enumeration_type,
  vector_type,
  set_type,
  map_type,
  record_type,
  alias_type
>;

using type_id_type = int8_t;

constexpr type_id_type invalid_type_id = -1;

template <class T>
constexpr type_id_type type_id() {
  static_assert(caf::detail::tl_contains<concrete_types, T>::value,
                "type IDs only available for concrete types");
  return caf::detail::tl_index_of<concrete_types, T>::value;
}

// -- type ------------------------------------------------------------------

/// @relates type
using type_digest = xxhash64::result_type;

/// @relates type
using type_hasher = type_erased_hasher<type_digest>;

/// @relates type
using abstract_type_ptr = caf::intrusive_ptr<abstract_type>;

/// The sematic representation of data.
class type : detail::totally_ordered<type> {
public:
  // -- construction & assignment ---------------------------------------------

  /// Constructs an invalid type.
  type() = default;

  /// Constructs a type from a concrete instance.
  /// @tparam T a type that derives from @ref abstract_type.
  /// @param x An instance of a type.
  template <
    class T,
    class = std::enable_if_t<caf::detail::tl_contains<concrete_types, T>::value>
  >
  type(T x) : ptr_{caf::make_counted<T>(std::move(x))} {
    // nop
  }

  /// Copy-constructs a type.
  type(const type& x);

  /// Copy-assigns a type.
  type& operator=(const type& x);

  /// Move-constructs a type.
  type(type&&) = default;

  /// Move-assigns a type.
  type& operator=(type&&) = default;

  /// Assigns a type from another instance
  template <class T>
  type& operator=(T x) {
    ptr_ = caf::make_counted<T>(std::move(x));
    return *this;
  }

  // -- modifiers -------------------------------------------------------------

  /// Sets the type name.
  /// @param x The new name of the type.
  /// @returns a new type with name *x*.
  type name(std::string x) const;

  /// Specifies a list of attributes.
  /// @param xs The list of attributes.
  /// @returns A new type with *xs* as attributes.
  type attributes(std::vector<attribute> xs) const;

  // -- inspectors ------------------------------------------------------------

  /// Checks whether a type contains a valid type.
  /// @returns `true` iff the type contains an instantiated type.
  explicit operator bool() const;

  /// @returns the name of the type.
  const std::string& name() const;

  /// @returns The attributes of the type.
  const std::vector<attribute>& attributes() const;

  /// @cond PRIVATE

  abstract_type_ptr ptr() const;

  const abstract_type* raw_ptr() const noexcept;

  const abstract_type* operator->() const noexcept;

{'fraction_non_alphanumeric': 0.19360798078699426, 'fraction_numerical': 0.0007389617587289858, 'mean_word_length': 2.4208530805687203, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3671239', 'n_tokens_mistral': 3307, 'n_tokens_neox': 3171, 'n_words': 874}