<commit_before>#ifndef JSARRAY_H_
#define JSARRAY_H_

#include "object/object.h"
#include "object/jsobject.h"

#include <memory>
#include <vector>

namespace grok { namespace obj {

class JSArray : public JSObject {
public:
  using ObjectPointer = std::shared_ptr<Object>;
  using size_type = typename std::vector<ObjectPointer>::size_type;
  using iterator = typename std::vector<ObjectPointer>::iterator;
  using reverse_iterator 
        = typename std::vector<ObjectPointer>::reverse_iterator;

  JSArray(const std::vector<ObjectPointer> &elements) { elements_ = elements; }

  JSArray() {}

  JSArray(const JSArray &arr) { // create a object by a copy from other object
    elements_ = (arr.elements_);
  }

  JSArray &operator=(const JSArray &arr) { // assign an other array
    elements_ = arr.elements_;
    return (*this);
  }

  ObjectPointer operator[](int i) { // no index checking
    return elements_[i];
  }

  ObjectPointer At(size_t i) {
    if (i >= Size()) {
      return GetProperty(std::to_string(i));
    }
    return elements_[i];
  }

  ObjectPointer At(const std::string &prop)
  {
      int idx = 0;
      // we still try to convert the string into number
      try {
          idx = std::stod(prop);
      } catch (...) {
          return this->GetProperty(prop);
      }
      return this->At(idx);
  }

  size_type Size() const
  { // returns the size of the vector
    return elements_.size();
  }

  ObjectType GetType() const override
  { // returns the type of the javascript object
    return ObjectType::_array;
  }

  bool Erase(size_type idx) { // erases the element
    if (idx < Size())
      elements_[idx];
    elements_[idx] = CreateUndefinedObject();
    return true;
  }

  bool Empty() const { // returns true if the array is empty
    return elements_.empty();
  }

  void Resize(size_type sz)
  {
    elements_.resize(sz, CreateUndefinedObject());
  }

  void Push(const ObjectPointer &obj) { // pushes the element to the last
    elements_.push_back(obj);
  }

  void Pop(const ObjectPointer &obj) { // pops the last element
    elements_.pop_back();
  }

  void Assign(int idx, const ObjectPointer &obj)
  {
    elements_[idx] = obj;
  }

  void Clear() { elements_.clear(); }

  iterator begin()
  {
    return elements_.begin();
  }

  iterator end()
  {
    return elements_.end();
  }

  reverse_iterator rbegin()
  {
    return elements_.rbegin();
  }

  reverse_iterator rend()
  {
    return elements_.rend();
  }

  std::string ToString() const override {<commit_msg>Remove typename from using declaration<commit_after>#ifndef JSARRAY_H_
#define JSARRAY_H_

#include "object/object.h"
#include "object/jsobject.h"

#include <memory>
#include <vector>

namespace grok { namespace obj {

class JSArray : public JSObject {
public:
  using ObjectPointer = std::shared_ptr<Object>;
  using size_type = std::vector<ObjectPointer>::size_type;
  using iterator = std::vector<ObjectPointer>::iterator;
  using reverse_iterator 
        = std::vector<ObjectPointer>::reverse_iterator;

  JSArray(const std::vector<ObjectPointer> &elements) { elements_ = elements; }

  JSArray() {}

  JSArray(const JSArray &arr) { // create a object by a copy from other object
    elements_ = (arr.elements_);
  }

  JSArray &operator=(const JSArray &arr) { // assign an other array
    elements_ = arr.elements_;
    return (*this);
  }

  ObjectPointer operator[](int i) { // no index checking
    return elements_[i];
  }

  ObjectPointer At(size_type i) {
    if (i >= Size()) {
      return GetProperty(std::to_string(i));
    }
    return elements_[i];
  }

  ObjectPointer At(const std::string &prop)
  {
      size_type idx = 0;
      // we still try to convert the string into number
      try {
          idx = std::stod(prop);
      } catch (...) {
          return this->GetProperty(prop);
      }
      return this->At(idx);
  }

  size_type Size() const
  { // returns the size of the vector
    return elements_.size();
  }

  ObjectType GetType() const override
  { // returns the type of the javascript object
    return ObjectType::_array;
  }

  bool Erase(size_type idx) { // erases the element
    if (idx < Size())
      elements_[idx];
    elements_[idx] = CreateUndefinedObject();
    return true;
  }

  bool Empty() const { // returns true if the array is empty
    return elements_.empty();
  }

  void Resize(size_type sz)
  {
    elements_.resize(sz, CreateUndefinedObject());
  }

  void Push(const ObjectPointer &obj) { // pushes the element to the last
    elements_.push_back(obj);
  }

  void Pop(const ObjectPointer &obj) { // pops the last element
    elements_.pop_back();
  }

  void Assign(size_type idx, const ObjectPointer &obj)
  {
    if (idx > Size()) {
      this->AddProperty(std::to_string(idx), obj);
      return;
    }
    elements_[idx] = obj;
  }

  void Clear() { elements_.clear(); }

  iterator begin()
  {
    return elements_.begin();
  }

  iterator end()
  {
    return elements_.end();
  }

  reverse_iterator rbegin()
  {
    return elements_.rbegin();
  }

  reverse_iterator rend()
  {
    return elements_.rend();
  }

  std::string ToString() const override {
{'fraction_non_alphanumeric': 0.1221877424359969, 'fraction_numerical': 0.0003878975950349108, 'mean_word_length': 3.067034700315457, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, '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': '6300058', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1562, 'n_words': 439}