JustAllTheSame

YOU CAN RUN QUICKLY """Array#all?(nil)"""

Installation

Add this line to your application's Gemfile:

gem 'just_all_the_same', github: "uproad/just_all_the_same"

And then execute:

$ bundle

Or install it yourself as:

$ gem install specific_install
$ gem install specific_install [email protected]:uproad/just_all_the_same.git master

Usage

require 'just_all_the_same'

ary = [nil, nil, nil]

ary.all_nil? #=> true
# same ary.all?(nil)

Algorithm (gem is written by Clang)

  1. A long size array.
ary = [nil]*10000
  1. Check first item is nil? in Clang.
return false unless ary[0].nil?
  1. Split array to half and half.

IMPORTANT!

 forward_ary = ary[0...ary.size/2]
backward_ary = ary[ary.size/2..ary.size/2*2] # If size is odd, to under even.
  1. If odd, check last item is nil.
return false unless ary.size.odd? && ary[-1].nil?
  1. Check same forward_ary and backward_ary with C function 'memcmp'.

IMPORTANT!!!

return false if memcmp(forward_ary.ptr, backward_ary.ptr, forward_ary.size)
  1. Split forward_ary to half and half.
         ary = forward_ary
 forward_ary = ary[0...ary.size/2]
backward_ary = ary[ary.size/2..ary.size/2*2] # If size is odd, to under even.
  1. Loop to 4

Algorithm written by data

[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
                      split

[nil, nil, nil, nil, nil] [nil, nil, nil, nil, nil]
                      memcmp


[nil, nil, nil, nil, nil]
       split

[nil, nil] [nil, nil] [nil]
       memcmp          nil?


[nil, nil]
  split

[nil] [nil]
  memcmp

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/just_all_the_same.

License

The gem is available as open source under the terms of the MIT License.