# Ruby Treasures 0.1
# Copyright (C) 2001 Paul Brannan <paul@atdesk.com>
# 
# You may distribute this software under the same terms as Ruby (see the file
# COPYING that was distributed with this library).
# 
require 'extended_array'
require 'sorted_hash'

$random_containers = Hash.new

def generate_random_array(size, type=Array)
  $random_containers[type] ||= Hash.new
  if not $random_containers[type][size] then
    a = type.new
    size.times do |i|
      if rand() > 0.9 then
        a.push(rand(100))
      else
        a.push(nil)
      end
    end
    a.freeze
    $random_containers[type][size] = a
  end
  $random_containers[type][size] # return
end

def generate_random_hash(size, type=Hash)
  $random_containers[type] ||= Hash.new
  if not $random_containers[type][size] then
    a = type.new
    size.times do |i|
      if rand() > 0.1 then
        a[rand(size*10)] = rand(100)
      else
        a[rand(size*10)] = nil
      end
    end
    a.freeze
    $random_containers[type][size] = a
  end
  $random_containers[type][size] # return
end

def generate_random_container(size, type)
  if type == Array or type == ExtendedArray then
    generate_random_array(size, type)
  elsif type == Hash or type == SortedHash then
    generate_random_hash(size, type)
  else
    raise TypeError, "Type #{type} not supported"
  end
end

