Sha256: 88cab43c0af449d862bfd14c0b7f861235ec1b14bbfa51194a75792b5a8c39df

Contents?: true

Size: 1.89 KB

Versions: 10

Compression:

Stored size: 1.89 KB

Contents

######################################
# WARNING! Highly expiremental code! #
######################################
# = autoarray.rb
#
# == Copyright (c) 2005 Brian Schröder
#
#   Ruby License
#
#   This module is free software. You may use, modify, and/or redistribute this
#   software under the same terms as Ruby.
#
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#   FOR A PARTICULAR PURPOSE.
#
# == Authors and Contributors
#
# * Brian Schröder
#

# = AutoArray
#
# An Array that automatically expands dimensions as needed.
#
#   a  = Autoarray.new
#   a[1][2][3] = 12
#   a             #=> [nil, [nil, nil, [nil, nil, nil, 12]]]
#   a[2][3][4]    #=> []
#   a             #=> [nil, [nil, nil, [nil, nil, nil, 12]]]
#   a[1][-2][1] = "Negative"
#   a             #=> [nil, [nil, [nil, "Negative"], [nil, nil, nil, 12]]]

class Autoarray < Array

  def initialize(size=0, default=nil, update = nil, update_index = nil)
    super(size, default)
    @update, @update_index = update, update_index
  end

  def [](k)
    if -self.length+1 < k and k < self.length
      super(k)
    else
      Autoarray.new(0, nil, self, k)
    end
  end

  def []=(k, v)
    @update[@update_index] = self if @update and @update_index
    super
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#

=begin test

  require 'test/unit'

  class TC_Autoarray

    def test_001
      a = Autoarray.new
      assert_equal( 12, a[1][2][3] = 12 )
      assert_equal( [nil, [nil, nil, [nil, nil, nil, 12]]], a )
      assert_equal( [], a[2][3][4] )
      assert_equal( [nil, [nil, nil, [nil, nil, nil, 12]]], a )
      assert_equal( "Negative", a[1][-2][1] = "Negative" )
      assert_equal( [nil, [nil, [nil, "Negative"], [nil, nil, nil, 12]]], a )
    end

  end

=end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-1.7.0 lib/facets/more/autoarray.rb
facets-1.7.30 lib/facets/more/autoarray.rb
facets-1.7.38 lib/facets/more/autoarray.rb
facets-1.7.46 lib/facets/more/autoarray.rb
facets-1.8.0 lib/facets/more/autoarray.rb
facets-1.8.20 lib/facets/more/autoarray.rb
facets-1.8.49 lib/facets/more/autoarray.rb
facets-1.8.51 lib/facets/more/autoarray.rb
facets-1.8.54 lib/facets/more/autoarray.rb
facets-1.8.8 lib/facets/more/autoarray.rb