<commit_before># depends on: class.rb enumerable.rb tuple.rb kernel.rb

##
# Arrays are ordered, integer-indexed collections of any object.  Array
# indexing starts at 0, as in C or Java.  A negative index is assumed to be
# relative to the end of the array---that is, an index of -1 indicates the
# last element of the array, -2 is the next to last element in the array, and
# so on.
#
# Arrays can be created with the <tt>[]</tt> syntax, or via <tt>Array.new</tt>.

class Array
  def total=(n) ; @total = n ; end
  def tuple=(t) ; @tuple = t ; end
  def start=(s) ; @start = s ; end

  include Enumerable

  # The flow control for many of these methods is
  # pretty evil due to how MRI works. There is
  # also a lot of duplication of code due to very
  # subtle processing differences and, in some
  # cases, to avoid mutual dependency. Apologies.


  # Returns a new Array populated with the given objects
  def self.[](*args)
    new args
  end

  def self.allocate
    ary = super()
    ary.start = 0
    ary.total = 0
    ary.tuple = Tuple.new 8
    ary
  end

  # Creates a new Array. Without arguments, an empty
  # Array is returned. If the only argument is an object
  # that responds to +to_ary+, a copy of that Array is
  # created. Otherwise the first argument is the size
  # of the new Array (default 0.) The second argument
  # may be an object used to fill the Array up to the
  # size given (the same object, not a copy.) Alternatively,
  # a block that takes the numeric index can be given and
  # will be run size times to fill the Array with its
  # result. The block supercedes any object given. If
  # neither is provided, the Array is filled with nil.
  def initialize(*args)
    raise ArgumentError, "Wrong number of arguments, #{args.size} for 2" if args.size > 2

    unless args.empty?
      if args.size == 1 and (args.first.__kind_of__ Array or args.first.respond_to? :to_ary)
        ary = Type.coerce_to args.first, Array, :to_ary

        @tuple = ary.tuple.dup
        @start = ary.start
        @total = ary.size
      else
        count = Type.coerce_to args.first, Fixnum, :to_int
        raise ArgumentError, "Size must be positive" if count < 0
        obj = args[1]

        @total = count
        if block_given?
          @tuple = Tuple.new(count)
          i = 0
          while i < count
            @tuple.put i, yield(i)
            i += 1
          end
        else
          @tuple = Tuple.pattern(count, obj) 
        end
      end
    end

    self
  end

  private :initialize

  # Element reference, returns the element at the given index or
  # a subarray starting from the index continuing for length
  # elements or returns a subarray from range elements. Negative
  # indices count from the end. Returns nil if the index or subarray
  # request cannot be completed. Array#slice is synonymous with #[].
  # Subclasses return instances of themselves.
  def [](arg1, arg2 = nil)
    Ruby.primitive :array_aref

    # Normalise the argument variants
    start_idx, finish_idx, count, simple, is_range = nil, nil, nil, false, false

    if arg1.kind_of? Range
      is_range = true
      start_idx, finish_idx = arg1.begin, arg1.end
    elsif arg2
      start_idx, count = arg1, Type.coerce_to(arg2, Fixnum, :to_int)
      return nil if count < 0       # No need to go further
    else
      start_idx, finish_idx, simple = arg1, arg1, true
    end

    # Convert negative indices
    start_idx = Type.coerce_to start_idx, Fixnum, :to_int
    start_idx += @total if start_idx < 0

    if simple
      return nil if start_idx < 0 or start_idx >= @total
      return @tuple.at(@start + start_idx)
    # ONE past end only, MRI compat
    elsif start_idx == @total
      return self.class.new
    elsif start_idx < 0 or start_idx >= @total
      return nil
    end


    finish_idx = Type.coerce_to finish_idx, Fixnum, :to_int if finish_idx
    finish_idx = (start_idx + count - 1) if count    # For non-ranges

    finish_idx += @total if finish_idx < 0

    finish_idx -= 1 if is_range and arg1.exclude_end?

    # Going past the end is ignored (sort of)
    finish_idx = (@total - 1) if finish_idx >= @total

    if finish_idx < 0
      if is_range
        return self.class.new
      else
        return nil
      end
    elsif finish_idx < start_idx or count == 0
      return self.class.new
    else
      tot = finish_idx - start_idx + 1
      out = self.class.new
      out.tuple = Tuple.new(tot)
      out.total = tot
      out.tuple.copy_from(@tuple, @start + start_idx, tot, 0)
      return out
    end
  end

  alias_method :slice, :[]

  def []=(index, ent, *args)
    Ruby.primitive :array_aset

    ins_length = nil
    if args.size != 0<commit_msg>simplified Array#[] by reducing the number of conditional branches<commit_after># depends on: class.rb enumerable.rb tuple.rb kernel.rb

##
# Arrays are ordered, integer-indexed collections of any object.  Array
# indexing starts at 0, as in C or Java.  A negative index is assumed to be
# relative to the end of the array---that is, an index of -1 indicates the
# last element of the array, -2 is the next to last element in the array, and
# so on.
#
# Arrays can be created with the <tt>[]</tt> syntax, or via <tt>Array.new</tt>.

class Array
  def total=(n) ; @total = n ; end
  def tuple=(t) ; @tuple = t ; end
  def start=(s) ; @start = s ; end

  include Enumerable

  # The flow control for many of these methods is
  # pretty evil due to how MRI works. There is
  # also a lot of duplication of code due to very
  # subtle processing differences and, in some
  # cases, to avoid mutual dependency. Apologies.


  # Returns a new Array populated with the given objects
  def self.[](*args)
    new args
  end

  def self.allocate
    ary = super()
    ary.start = 0
    ary.total = 0
    ary.tuple = Tuple.new 8
    ary
  end

  # Creates a new Array. Without arguments, an empty
  # Array is returned. If the only argument is an object
  # that responds to +to_ary+, a copy of that Array is
  # created. Otherwise the first argument is the size
  # of the new Array (default 0.) The second argument
  # may be an object used to fill the Array up to the
  # size given (the same object, not a copy.) Alternatively,
  # a block that takes the numeric index can be given and
  # will be run size times to fill the Array with its
  # result. The block supercedes any object given. If
  # neither is provided, the Array is filled with nil.
  def initialize(*args)
    raise ArgumentError, "Wrong number of arguments, #{args.size} for 2" if args.size > 2

    unless args.empty?
      if args.size == 1 and (args.first.__kind_of__ Array or args.first.respond_to? :to_ary)
        ary = Type.coerce_to args.first, Array, :to_ary

        @tuple = ary.tuple.dup
        @start = ary.start
        @total = ary.size
      else
        count = Type.coerce_to args.first, Fixnum, :to_int
        raise ArgumentError, "Size must be positive" if count < 0
        obj = args[1]

        @total = count
        if block_given?
          @tuple = Tuple.new(count)
          i = 0
          while i < count
            @tuple.put i, yield(i)
            i += 1
          end
        else
          @tuple = Tuple.pattern(count, obj) 
        end
      end
    end

    self
  end

  private :initialize

  # Element reference, returns the element at the given index or
  # a subarray starting from the index continuing for length
  # elements or returns a subarray from range elements. Negative
  # indices count from the end. Returns nil if the index or subarray
  # request cannot be completed. Array#slice is synonymous with #[].
  # Subclasses return instances of themselves.
  def [](arg1, arg2 = nil)
    Ruby.primitive :array_aref

    if arg1.kind_of? Range
      start_idx = Type.coerce_to arg1.begin, Fixnum, :to_int
    else
      start_idx = Type.coerce_to arg1, Fixnum, :to_int
      if arg2
        count = Type.coerce_to arg2, Fixnum, :to_int
        return nil if count < 0       # No need to go further
      else # repeat prim case after coercing with to_int
        # Convert negative indices
        start_idx += @total if start_idx < 0
        return nil if start_idx < 0 or start_idx >= @total
        return @tuple.at(@start + start_idx)
      end
    end

    # Convert negative indices
    start_idx += @total if start_idx < 0

    if start_idx < 0 or start_idx >= @total
      # ONE past end only, MRI compat
      if start_idx == @total
        return self.class.new
      else
        return nil
      end
    end

    if count
      return self.class.new if count == 0
      finish_idx = start_idx + count - 1
    else # from a range
      finish_idx = Type.coerce_to arg1.end, Fixnum, :to_int
      finish_idx += @total if finish_idx < 0
      finish_idx -= 1 if arg1.exclude_end?
    end

    if finish_idx < start_idx
      return self.class.new
    else
      # Going past the end is ignored (sort of)
      finish_idx = (@total - 1) if finish_idx >= @total

      tot = finish_idx - start_idx + 1
      out = self.class.new
      out.tuple = Tuple.new(tot)
      out.total = tot
      out.tuple.copy_from(@tuple, @start + start_idx, tot, 0)
      return out
    end
  end

  alias_method :slice, :[]

  def []=(index, ent, *args)
    Ruby.primitive :array_aset

    ins_length = nil
    if args.size != 0
{'fraction_non_alphanumeric': 0.07888258589318206, 'fraction_numerical': 0.007920368190088836, 'mean_word_length': 2.415204678362573, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 19, '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': '2093934', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2808, 'n_words': 1180}