<commit_before>/* This is free and unencumbered software released into the public domain. */

#ifndef POSIXXX_MAPPED_FILE_H
#define POSIXXX_MAPPED_FILE_H

#ifndef __cplusplus
#error "<posix++/mapped_file.h> requires a C++ compiler"
#endif

////////////////////////////////////////////////////////////////////////////////

#include "file.h"
#include "memory_mapping.h"

#include <cstring> /* for std::strlen() */
#include <string>  /* for std::string */

namespace posix {
  class mapped_file;
  class appendable_mapped_file;
}

////////////////////////////////////////////////////////////////////////////////

/**
 * A memory-mapped file for random access.
 */
class posix::mapped_file : public posix::file {
protected:
  memory_mapping _mapping;

public:
  static mapped_file open(const pathname& pathname, int flags, mode mode = 0);

  static mapped_file open(const directory& directory, const pathname& pathname, int flags, mode mode = 0);

  /**
   * Constructor.
   */
  mapped_file(int dirfd, const char* pathname, int flags, mode mode = 0);
};

////////////////////////////////////////////////////////////////////////////////

/**
 * A memory-mapped file for append-only writes.
 */
class posix::appendable_mapped_file : public posix::mapped_file {
public:
  static appendable_mapped_file open(const pathname& pathname, int flags, mode mode = 0);

  static appendable_mapped_file open(const directory& directory, const pathname& pathname, int flags, mode mode = 0);

  /**
   * Constructor.
   */
  using mapped_file::mapped_file;

  /**
   * @throws posix::runtime_error if an error occurs
   */
  template<class T>
  std::size_t append(const T& data) {
    const char* const c_str = data.c_str();
    return append(c_str, std::strlen(c_str));
  }<commit_msg>Implemented posix::mapped_file#data() and #operator[]().<commit_after>/* This is free and unencumbered software released into the public domain. */

#ifndef POSIXXX_MAPPED_FILE_H
#define POSIXXX_MAPPED_FILE_H

#ifndef __cplusplus
#error "<posix++/mapped_file.h> requires a C++ compiler"
#endif

////////////////////////////////////////////////////////////////////////////////

#include "file.h"
#include "memory_mapping.h"

#include <cstring> /* for std::strlen() */
#include <string>  /* for std::string */

namespace posix {
  class mapped_file;
  class appendable_mapped_file;
}

////////////////////////////////////////////////////////////////////////////////

/**
 * A memory-mapped file for random access.
 */
class posix::mapped_file : public posix::file {
protected:
  memory_mapping _mapping;

public:
  static mapped_file open(const pathname& pathname, int flags, mode mode = 0);

  static mapped_file open(const directory& directory, const pathname& pathname, int flags, mode mode = 0);

  /**
   * Constructor.
   */
  mapped_file(int dirfd, const char* pathname, int flags, mode mode = 0);

  /**
   * Returns a pointer to the mapped memory.
   */
  template <typename T>
  T* data(const std::size_t offset = 0) noexcept {
    return _mapping.data<T>(offset);
  }

  /**
   * Returns a pointer to the mapped memory.
   */
  template <typename T>
  const T* data(const std::size_t offset = 0) const noexcept {
    return _mapping.data<T>(offset);
  }

  /**
   * Returns a pointer to the mapped memory.
   */
  std::uint8_t* data(const std::size_t offset = 0) noexcept {
    return _mapping.data(offset);
  }

  /**
   * Returns a pointer to the mapped memory.
   */
  const std::uint8_t* data(const std::size_t offset = 0) const noexcept {
    return _mapping.data(offset);
  }

  /**
   * Returns the byte at the given offset.
   */
  std::uint8_t operator[](const std::size_t offset) const noexcept {
    return _mapping[offset];
  }
};

////////////////////////////////////////////////////////////////////////////////

/**
 * A memory-mapped file for append-only writes.
 */
class posix::appendable_mapped_file : public posix::mapped_file {
public:
  static appendable_mapped_file open(const pathname& pathname, int flags, mode mode = 0);

  static appendable_mapped_file open(const directory& directory, const pathname& pathname, int flags, mode mode = 0);

  /**
   * Constructor.
   */
  using mapped_file::mapped_file;

  /**
   * @throws posix::runtime_error if an error occurs
   */
  template<class T>
  std::size_t append(const T& data) {
    const char* const c_str = data.c_str();
    return append(c_str, std::strlen(c_str));
  }
{'fraction_non_alphanumeric': 0.2163689803300927, 'fraction_numerical': 0.003843545105132263, 'mean_word_length': 2.432117920868891, 'pattern_counts': {'":': 0, '<': 15, '<?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': '4119370', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1317, 'n_words': 384}