Skip to content

Instantly share code, notes, and snippets.

@jvelilla
Created July 7, 2020 17:54
Show Gist options
  • Select an option

  • Save jvelilla/6811e7aa96b71202a13f2d1687ab0cd7 to your computer and use it in GitHub Desktop.

Select an option

Save jvelilla/6811e7aa96b71202a13f2d1687ab0cd7 to your computer and use it in GitHub Desktop.
note
description: "hash_reals application root class"
date: "$Date$"
revision: "$Revision$"
class
APPLICATION
inherit
ARGUMENTS_32
create
make
feature {NONE} -- Initialization
make
-- Run application.
local
timer: DT_STOPWATCH
l_table: REAL_32_HASH_TABLE [STRING_8]
l_table2: HASH_TABLE [STRING_8, REAL_32]
keys: ARRAY [REAL_32]
l_table_64: REAL_64_HASH_TABLE [STRING_8]
keys_64: ARRAY [REAL_64]
do
-- Insert: Keys Large Reals
print ("%NLarge Reals%N")
keys := generate_n_large_keys (Number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
print ("%N%NReals%N")
keys := generate_n_keys (Number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- Insert: Keys: small Reals 0..1
print ("%N%NSmall Reals 0..1 %N")
keys := generate_n_keys_range_0_1 (number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- Insert: KeysNegative Reals
print ("%N%NNegative Reals%N")
keys := generate_n_negative_keys (number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- Insert: Keys NAN Reals
print ("%N%NNAN Reals%N")
keys := generate_n_nan_keys (number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- Insert: Keys Negativity Infinity Reals
print ("%N%NNegative_infinity Reals%N")
keys := generate_n_negative_infinity_keys (number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- Insert: Keys Positive Infinity Reals
print ("%N%NPositive_infinity Reals%N")
keys := generate_n_positive_infinity_keys (number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- Insert: Keys Mixed
print ("%N%NMixed Reals%N")
keys := generate_n_mixed_keys (number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- REAL 64
----------------------------------------------------------------
----------------------------------------------------------------
-- Insert: Keys Large Reals
print ("%NLarge Reals 64%N")
keys_64 := generate_n_large_keys_64 (Number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
print ("%NReals 64%N")
keys_64 := generate_n_keys_64 (Number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
-- Insert: Keys: small Reals 0..1
print ("%N%NSmall Reals 64 0..1%N")
keys_64 := generate_n_keys_range_0_1_64 (number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
-- Insert: KeysNegative Reals
print ("%N%NNegative Reals 64%N")
keys_64 := generate_n_negative_keys_64 (number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
-- Insert: Keys NAN Reals
print ("%N%NNAN Reals 64%N")
keys_64 := generate_n_nan_keys_64 (number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
-- Insert: Keys Negativity Infinity Reals
print ("%N%NNegative_infinity Reals 64%N")
keys_64 := generate_n_negative_infinity_keys_64 (number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
-- Insert: Keys Positive Infinity Reals
print ("%N%NPositive_infinity Reals 64%N")
keys_64 := generate_n_positive_infinity_keys_64 (number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
-- Insert: Keys Mixed
print ("%N%NMixed Reals 64%N")
keys_64 := generate_n_mixed_keys_64 (number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
io.readline
end
insert_on_real_32_hash_table (n: INTEGER; keys: ARRAY [REAL_32])
local
l_table: REAL_32_HASH_TABLE [STRING_8]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
across 1 |..| n as ic1 loop
create l_table.make (Number_of_elements)
create timer.make
timer.start
across 1 |..| Number_of_elements as ic2 loop
l_table.force (generate_token, keys [ic2.item])
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to insert " + number_of_elements.out + " elements in REAL_32_HASH_TABLE [STRING_8], using the media of " + Number_of_iterations.out + " iterations is, " + (l_total_time / Number_of_iterations).truncated_to_integer_64.out + ", milliseconds")
end
search_on_real_32_hash_table (n: INTEGER; keys: ARRAY [REAL_32])
local
l_table: REAL_32_HASH_TABLE [STRING_8]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
create l_table.make (Number_of_elements)
across 1 |..| Number_of_elements as ic2 loop
l_table.force (generate_token, keys [ic2.item])
end
across 1 |..| n as ic loop
create timer.make
timer.start
across 1 |..| Number_of_elements as ic2 loop
if l_table.has_key (keys [ic2.item]) then
-- found
else
-- not found
end
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to search " + number_of_elements.out + " elements in REAL_32_HASH_TABLE [STRING_8], using the media of " + Number_of_iterations.out + " iterations is, " + (l_total_time / Number_of_iterations).truncated_to_integer_64.out + ", milliseconds")
end
insert_on_hash_table (n: INTEGER; keys: ARRAY [REAL_32])
local
l_table: HASH_TABLE [STRING_8, REAL_32]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
across 1 |..| n as ic1 loop
create l_table.make (Number_of_elements)
create timer.make
timer.start
across 1 |..| Number_of_elements as ic2 loop
l_table.force (generate_token, keys [ic2.item])
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to insert " + number_of_elements.out + " elements in HASH_TABLE [STRING_8, REAL_32], using the media of " + Number_of_iterations.out + " iterations is, " + (l_total_time / Number_of_iterations).truncated_to_integer_64.out + ", milliseconds")
end
search_on_hash_table (n: INTEGER; keys: ARRAY [REAL_32])
local
l_table: HASH_TABLE [STRING_8, REAL_32]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
create l_table.make (Number_of_elements)
across 1 |..| Number_of_elements as ic2 loop
l_table.force (generate_token, keys [ic2.item])
end
across 1 |..| n as ic loop
create timer.make
timer.start
across 1 |..| Number_of_elements as ic2 loop
if l_table.has_key (keys [ic2.item]) then
-- found
else
-- not found
end
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to search " + number_of_elements.out + " elements in HASH_TABLE [STRING_8, REAL_32], using the media of " + Number_of_iterations.out + " iterations is, " + (l_total_time / Number_of_iterations).truncated_to_integer_64.out + ", milliseconds")
end
insert_on_real_64_hash_table (n: INTEGER; keys: ARRAY [REAL_64])
local
l_table: REAL_64_HASH_TABLE [STRING_8]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
across 1 |..| n as ic1 loop
create l_table.make (Number_of_elements)
create timer.make
timer.start
across 1 |..| Number_of_elements as ic2 loop
l_table.force (generate_token, keys [ic2.item])
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to insert " + number_of_elements.out + " elements in REAL_64_HASH_TABLE [STRING_8], using the media of " + Number_of_iterations.out + " iterations is, " + (l_total_time / Number_of_iterations).truncated_to_integer_64.out + ", milliseconds")
end
insert_on_hash_table_64 (n: INTEGER; keys: ARRAY [REAL_64])
local
l_table: HASH_TABLE [STRING_8, REAL_64]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
across 1 |..| n as ic1 loop
create l_table.make (Number_of_elements)
create timer.make
timer.start
across 1 |..| Number_of_elements as ic2 loop
l_table.force (generate_token, keys [ic2.item])
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to insert " + number_of_elements.out + " elements in HASH_TABLE [STRING_8, REAL_64], using the media of " + Number_of_iterations.out + " iterations is, " + (l_total_time / Number_of_iterations).truncated_to_integer_64.out + ", milliseconds")
end
search_on_hash_table_64 (n: INTEGER; keys: ARRAY [REAL_64])
local
l_table: HASH_TABLE [STRING_8, REAL_64]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
create l_table.make (Number_of_elements)
across 1 |..| Number_of_elements as ic2 loop
l_table.force (generate_token, keys [ic2.item])
end
across 1 |..| n as ic loop
create timer.make
timer.start
across 1 |..| Number_of_elements as ic2 loop
if l_table.has_key (keys [ic2.item]) then
-- found
else
-- not found
end
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to search " + number_of_elements.out + " elements in HASH_TABLE [STRING_8, REAL_64], using the media of " + Number_of_iterations.out + " iterations is, " + (l_total_time / Number_of_iterations).truncated_to_integer_64.out + ", milliseconds")
end
search_on_real_64_hash_table (n: INTEGER; keys: ARRAY [REAL_64])
local
l_table: REAL_64_HASH_TABLE [STRING_8]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
create l_table.make (Number_of_elements)
across 1 |..| Number_of_elements as ic2 loop
l_table.force (generate_token, keys [ic2.item])
end
across 1 |..| n as ic loop
create timer.make
timer.start
across 1 |..| Number_of_elements as ic2 loop
if l_table.has_key (keys [ic2.item]) then
-- found
else
-- not found
end
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to search " + number_of_elements.out + " elements in REAL_64_HASH_TABLE [STRING_8], using the media of " + Number_of_iterations.out + " iterations is, " + (l_total_time / Number_of_iterations).truncated_to_integer_64.out + ", milliseconds")
end
generate_token: STRING_8
local
random: RANDOM
token_gen: TOKEN_GENERATOR
l_time: TIME
l_seed: INTEGER
do
create random.make
random.set_seed (generate_seed)
random.start
create token_gen
random.forth
Result := token_gen.token + "_" + (random.item \\ Number_of_elements).out
end
generate_n_keys_range_0_1 (n: INTEGER): ARRAY [REAL_32]
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := (random.real_item)
end
end
generate_n_keys_range_0_1_64 (n: INTEGER): ARRAY [REAL_64]
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := (random.double_item)
end
end
generate_n_large_keys (n: INTEGER): ARRAY [REAL_32]
local
random: RANDOM
const: REAL_32
do
const := 3.0
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := ((random.real_item) * const.power (38)).truncated_to_real
end
end
generate_n_large_keys_64 (n: INTEGER): ARRAY [REAL_64]
local
random: RANDOM
const: REAL_64
do
const := 1.0
create random.make
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := (random.double_item) * const.power (308)
end
end
generate_n_keys (n: INTEGER): ARRAY [REAL_32]
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := (random.real_item) * 10000
end
end
generate_n_keys_64 (n: INTEGER): ARRAY [REAL_64]
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := (random.double_item) * 10000
end
end
generate_n_nan_keys (n: INTEGER): ARRAY [REAL_32]
do
create Result.make_filled ({REAL_32}.nan, 1, n)
end
generate_n_nan_keys_64 (n: INTEGER): ARRAY [REAL_64]
do
create Result.make_filled ({REAL_64}.nan, 1, n)
end
generate_n_negative_infinity_keys (n: INTEGER): ARRAY [REAL_32]
do
create Result.make_filled ({REAL_32}.negative_infinity, 1, n)
end
generate_n_negative_infinity_keys_64 (n: INTEGER): ARRAY [REAL_64]
do
create Result.make_filled ({REAL_64}.negative_infinity, 1, n)
end
generate_n_positive_infinity_keys (n: INTEGER): ARRAY [REAL_32]
do
create Result.make_filled ({REAL_32}.positive_infinity, 1, n)
end
generate_n_positive_infinity_keys_64 (n: INTEGER): ARRAY [REAL_64]
do
create Result.make_filled ({REAL_64}.positive_infinity, 1, n)
end
generate_n_negative_keys (n: INTEGER): ARRAY [REAL_32]
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := - (random.item.to_real) * 1000
end
end
generate_n_negative_keys_64 (n: INTEGER): ARRAY [REAL_64]
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := - (random.item.to_double) * 1000
end
end
generate_n_mixed_keys (n: INTEGER): ARRAY [REAL_32]
local
random: RANDOM
val: INTEGER
do
create random.make
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
val := random.item \\ 5
inspect val
when 0 then
random.forth
Result [ic.item] := (random.item.to_real)
when 1 then
Result [ic.item] := {REAL_32}.nan
when 2 then
Result [ic.item] := {REAL_32}.positive_infinity
when 3 then
random.forth
Result [ic.item] := {REAL_32}.negative_infinity
when 4 then
Result [ic.item] := - (random.real_item) * 1000
end
end
end
generate_n_mixed_keys_64 (n: INTEGER): ARRAY [REAL_64]
local
random: RANDOM
val: INTEGER
do
create random.make
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
val := random.item \\ 5
inspect val
when 0 then
random.forth
Result [ic.item] := (random.item.to_double)
when 1 then
Result [ic.item] := {REAL_64}.nan
when 2 then
Result [ic.item] := {REAL_64}.positive_infinity
when 3 then
random.forth
Result [ic.item] := {REAL_64}.negative_infinity
when 4 then
Result [ic.item] := - (random.item.to_double) * 1000
end
end
end
generate_custom_large_reals (n: INTEGER; a_real: REAL_32; steps: REAL_32): ARRAY [REAL_32]
do
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
Result [ic.item] := a_real + steps*ic.item
end
end
feature -- Constants
generate_seed: INTEGER
local
l_time: TIME
do
create l_time.make_now
Result := l_time.hour
Result := Result * 60 + l_time.minute
Result := Result * 60 + l_time.second
Result := Result * 1000 + l_time.milli_second
end
Number_of_elements: INTEGER = 10000
Number_of_iterations: INTEGER = 50
Max_real_number: INTEGER = 10000
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment