<commit_before>#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# AUTHOR
#    <NAME> /mig/at/1984/dot/cz/
#
# DESCRIPTION
#    Hash with preserved order and some array-like extensions
#    Public domain.
#
# THANKS
#    <NAME> for his suggestions and fixes of Hash[],
#    merge, to_a, inspect and shift
class Dbt
  class OrderedHash < ::Hash
    attr_accessor :order

    class << self
      def [] *args
        hsh = OrderedHash.new
        if Hash === args[0]
          hsh.replace args[0]
        elsif (args.size % 2) != 0
          raise ArgumentError, "odd number of elements for Hash"
        else
          0.step(args.size - 1, 2) do |a|
            b = a + 1
            hsh[args[a]] = args[b]
          end
        end
        hsh
      end
    end

    def initialize(*a, &b)
      super
      @order = []
    end

    def store_only a, b
      store a, b
    end

    alias orig_store store

    def store a, b
      @order.push a unless has_key? a
      super a, b
    end

    alias []= store

    def == hsh2
      return false if @order != hsh2.order
      super hsh2
    end

    def clear
      @order = []
      super
    end

    def delete key
      @order.delete key
      super
    end

    def each_key
      @order.each { |k| yield k }
      self
    end

    def each_value
      @order.each { |k| yield self[k] }
      self
    end

    def each
      @order.each { |k| yield k, self[k] }
      self
    end

    alias each_pair each

    def delete_if
      @order.clone.each { |k|
        delete k if yield(k)
      }
      self
    end

    def values
      ary = []
      @order.each { |k| ary.push self[k] }
      ary
    end

    def keys
      @order
    end

    def first
      {@order.first => self[@order.first]}
    end

    def last
      {@order.last => self[@order.last]}
    end

    def invert
      hsh2 = Hash.new
      @order.each { |k| hsh2[self[k]] = k }
      hsh2
    end

    def reject &block
      self.dup.delete_if &block
    end

    def reject! &block
      hsh2 = reject &block
      self == hsh2 ? nil : hsh2
    end

    def replace hsh2
      @order = hsh2.keys
      super hsh2
    end

    def shift
      key = @order.first
      key ? [key, delete(key)] : super
    end

    def unshift k, v
      unless self.include? k
        @order.unshift k
        orig_store(k, v)
        true
      else
        false
      end
    end

    def push k, v
      unless self.include? k
        @order.push k
        orig_store(k, v)
        true
      else
        false<commit_msg>Remove warning regarding usage of freestanding &<commit_after>#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# AUTHOR
#    <NAME> /mig/at/1984/dot/cz/
#
# DESCRIPTION
#    Hash with preserved order and some array-like extensions
#    Public domain.
#
# THANKS
#    <NAME> for his suggestions and fixes of Hash[],
#    merge, to_a, inspect and shift
class Dbt
  class OrderedHash < ::Hash
    attr_accessor :order

    class << self
      def [] *args
        hsh = OrderedHash.new
        if Hash === args[0]
          hsh.replace args[0]
        elsif (args.size % 2) != 0
          raise ArgumentError, "odd number of elements for Hash"
        else
          0.step(args.size - 1, 2) do |a|
            b = a + 1
            hsh[args[a]] = args[b]
          end
        end
        hsh
      end
    end

    def initialize(*a, &b)
      super
      @order = []
    end

    def store_only a, b
      store a, b
    end

    alias orig_store store

    def store a, b
      @order.push a unless has_key? a
      super a, b
    end

    alias []= store

    def == hsh2
      return false if @order != hsh2.order
      super hsh2
    end

    def clear
      @order = []
      super
    end

    def delete key
      @order.delete key
      super
    end

    def each_key
      @order.each { |k| yield k }
      self
    end

    def each_value
      @order.each { |k| yield self[k] }
      self
    end

    def each
      @order.each { |k| yield k, self[k] }
      self
    end

    alias each_pair each

    def delete_if
      @order.clone.each { |k|
        delete k if yield(k)
      }
      self
    end

    def values
      ary = []
      @order.each { |k| ary.push self[k] }
      ary
    end

    def keys
      @order
    end

    def first
      {@order.first => self[@order.first]}
    end

    def last
      {@order.last => self[@order.last]}
    end

    def invert
      hsh2 = Hash.new
      @order.each { |k| hsh2[self[k]] = k }
      hsh2
    end

    def reject(&block)
      self.dup.delete_if(&block)
    end

    def reject!(&block)
      hsh2 = reject(&block)
      self == hsh2 ? nil : hsh2
    end

    def replace hsh2
      @order = hsh2.keys
      super hsh2
    end

    def shift
      key = @order.first
      key ? [key, delete(key)] : super
    end

    def unshift k, v
      unless self.include? k
        @order.unshift k
        orig_store(k, v)
        true
      else
        false
      end
    end

    def push k, v
      unless self.include? k
        @order.push k
        orig_store(k, v)
        true
      else
        false
{'fraction_non_alphanumeric': 0.08710573623141037, 'fraction_numerical': 0.009151822193168818, 'mean_word_length': 1.6948480845442537, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '774154', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1934, 'n_words': 685}