Class: Kibuvits_krl171bt4_htoper_t1

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/kibuvits_ruby_library_krl171bt4_.rb

Overview

The class Kibuvits_krl171bt4_htoper_t1 is a namespace for general, simplistic, functions that read their operands from hashtables or store the result to hashtables. The methods of the Kibuvits_krl171bt4_htoper_t1 somewhat resemble operands that operate on hashtable values. The main idea is to abstract away the fetching of values from hashtables and the storing of computation results back to the hashtables.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKibuvits_krl171bt4_htoper_t1

Returns a new instance of Kibuvits_krl171bt4_htoper_t1.



9154
9155
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 9154

def initialize
end

Class Method Details

.ht_clone_with_shared_references(ht_orig) ⇒ Object

ht_clone_with_shared_references



9340
9341
9342
9343
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 9340

def Kibuvits_krl171bt4_htoper_t1.ht_clone_with_shared_references(ht_orig)
   ht_out=Kibuvits_krl171bt4_htoper_t1.instance.ht_clone_with_shared_references(ht_orig)
   return ht_out
end

.insert_to_ht_without_overwriting_any_key_t1(ht_in, s_key, x_value, b_all_keys_will_contain_numeration_rpefix, i_minimum_amount_of_digits, s_suffix_of_the_prefix) ⇒ Object



9388
9389
9390
9391
9392
9393
9394
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 9388

def Kibuvits_krl171bt4_htoper_t1.insert_to_ht_without_overwriting_any_key_t1(
   ht_in,s_key,x_value, b_all_keys_will_contain_numeration_rpefix,
   i_minimum_amount_of_digits, s_suffix_of_the_prefix)
   Kibuvits_krl171bt4_htoper_t1.instance.insert_to_ht_without_overwriting_any_key_t1(
   ht_in,s_key,x_value,b_all_keys_will_contain_numeration_rpefix,
   i_minimum_amount_of_digits,s_suffix_of_the_prefix)
end

.plus(ht, s_key, x_value_to_add, a_binding = nil) ⇒ Object

plus



9268
9269
9270
9271
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 9268

def Kibuvits_krl171bt4_htoper_t1.plus(ht,s_key,x_value_to_add,a_binding=nil)
   x_sum=Kibuvits_krl171bt4_htoper_t1.instance.plus(ht,s_key,x_value_to_add,a_binding)
   return x_sum
end

.run_in_htspace(ht, a_binding = nil, &block) ⇒ Object

run_in_htspace



9235
9236
9237
9238
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 9235

def Kibuvits_krl171bt4_htoper_t1.run_in_htspace(ht,a_binding=nil,&block)
   x_out=Kibuvits_krl171bt4_htoper_t1.instance.run_in_htspace(ht,a_binding,&block)
   return x_out
end

.x_getset_semisparse_var(ht, s_varname, x_var_default_value) ⇒ Object

x_getset_semisparse_var



9294
9295
9296
9297
9298
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 9294

def Kibuvits_krl171bt4_htoper_t1.x_getset_semisparse_var(ht,s_varname,x_var_default_value)
   x_out=Kibuvits_krl171bt4_htoper_t1.instance.x_getset_semisparse_var(
   ht,s_varname,x_var_default_value)
   return x_out
end

Instance Method Details

#ht_clone_with_shared_references(ht_orig) ⇒ Object

Creates a new Hash instance that contains the same instances that the ht_orig has.



9334
9335
9336
9337
9338
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 9334

def ht_clone_with_shared_references(ht_orig)
   ht_out=Hash.new
   ht_orig.each_pair{|x_key,x_value| ht_out[x_key]=x_value}
   return ht_out
end

#insert_to_ht_without_overwriting_any_key_t1(ht_in, s_key, x_value, b_all_keys_will_contain_numeration_rpefix, i_minimum_amount_of_digits, s_suffix_of_the_prefix) ⇒ Object

If the ht_in has s_key, then new key candidates are generated by counting from N=1. The key candidate will have a form of

s_numeration="0"*<something>+N.to_s
s_candidate=s_numeration+"_"+s_key

where i_minimum_amount_of_digits<=s_numeration.length



9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 9355

def insert_to_ht_without_overwriting_any_key_t1(
   ht_in,s_key,x_value, b_all_keys_will_contain_numeration_rpefix,
   i_minimum_amount_of_digits, s_suffix_of_the_prefix)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck bn, Hash, ht_in
      kibuvits_krl171bt4_typecheck bn, String, s_key
      kibuvits_krl171bt4_typecheck bn, [TrueClass,FalseClass], b_all_keys_will_contain_numeration_rpefix
      kibuvits_krl171bt4_typecheck bn, Fixnum, i_minimum_amount_of_digits
      kibuvits_krl171bt4_typecheck bn, String, s_suffix_of_the_prefix
   end # if
   if !b_all_keys_will_contain_numeration_rpefix
      if !ht_in.has_key? s_key
         ht_in[s_key]=x_value
         return
      end # if
   end # if
   func_s_gen_key_candidate=lambda do |i_in|
      s_enum=Kibuvits_krl171bt4_str.s_to_s_with_assured_amount_of_digits_t1(
      i_minimum_amount_of_digits, i_in)
      s_out=s_enum+s_suffix_of_the_prefix+s_key
      return s_out
   end # func_s_gen_key_candidate
   i_enum=0
   s_key_candidate=func_s_gen_key_candidate.call(i_enum)
   while ht_in.has_key? s_key_candidate
      i_enum=i_enum+1
      s_key_candidate=func_s_gen_key_candidate.call(i_enum)
   end # loop
   ht_in[s_key_candidate]=x_value
end

#plus(ht, s_key, x_value_to_add, a_binding = nil) ⇒ Object

ht=ht+x_value_to_add

The ht must have the + operator/method defined for the type of the x_value_to_add and the key, s_key, must be present in the hashtable.

Returns the version of the instance of ht that exists after performing the operation.



9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 9250

def plus(ht,s_key,x_value_to_add,a_binding=nil)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck bn, Hash,ht
      kibuvits_krl171bt4_typecheck bn, String,s_key
      kibuvits_krl171bt4_typecheck bn, [NilClass,Binding],a_binding
      if a_binding!=nil
         kibuvits_krl171bt4_assert_ht_has_keys(a_binding,ht,s_key)
      else
         kibuvits_krl171bt4_assert_ht_has_keys(bn,ht,s_key)
      end # if
   end # if DEBUG
   a=ht[s_key]
   x_sum=a+x_value_to_add
   ht[s_key]=x_sum
   return x_sum
end

#run_in_htspace(ht, a_binding = nil, &block) ⇒ Object

Returns the value that is returned from the &block by the ruby block analogue of the ruby function return(…), the next(…).

def demo_for_storing_values_back_to_the_hashtable
   ht=Hash.new
   ht['aa']=42
   ht['bb']=74
   ht['cc']=2
   ht['ht']=ht
   x=Kibuvits_krl171bt4_htoper_t1.run_in_htspace(ht) do |bb,aa,ht|
      ht['cc']=aa+bb
   end # block
   raise Exception.new("x=="+x.to_s) if ht['cc']!=116
end # demo_for_storing_values_back_to_the_hashtable

May be one could figure out, how to improve the implementation of the run_in_htspace(…) so that the block in the demo_for_storing_values_back_to_the_hashtable() would look like:

cc=aa+bb

but the solution shown in the current version of the demo_for_storing_values_back_to_the_hashtable(…) seems to be more robust in terms of future changes in the Ruby language implementation.



9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 9187

def run_in_htspace(ht,a_binding=nil,&block)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck bn, Hash,ht
      kibuvits_krl171bt4_typecheck bn, Proc,block
      kibuvits_krl171bt4_typecheck bn, [NilClass,Binding],a_binding
   end # if
   ar_of_ar=block.parameters
   s_block_arg_name=nil
   ar_block_arg_names=Array.new
   i_nfr=ar_of_ar.size
   if KIBUVITS_krl171bt4_b_DEBUG
      i_nfr.times do |i|
         s_block_arg_name=(ar_of_ar[i])[1].to_s
         if !ht.has_key? s_block_arg_name
            b_ht_varkname_available=false
            s_ht_varname=nil
            if a_binding!=nil
               s_ht_varname=kibuvits_krl171bt4_s_varvalue2varname(a_binding,ht)
               if s_ht_varname.size!=0
                  b_ht_varkname_available=true
               end # if
            end # if
            if b_ht_varkname_available
               kibuvits_krl171bt4_throw("The hashtable named \""+s_ht_varname+
               "\" does not contain a key named \""+s_block_arg_name+"\".")
            else
               kibuvits_krl171bt4_throw("The hashtable "+
               "does not contain a key named \""+s_block_arg_name+"\".")
            end # if
         end # if
         ar_block_arg_names<<s_block_arg_name
      end # loop
   else
      i_nfr.times do |i|
         s_block_arg_name=(ar_of_ar[i])[1].to_s
         ar_block_arg_names<<s_block_arg_name
      end # loop
   end # if
   ar_method_arguments=Array.new
   i_nfr.times do |i|
      s_block_arg_name=ar_block_arg_names[i]
      ar_method_arguments<<ht[s_block_arg_name]
   end # loop
   x_out=kibuvits_krl171bt4_call_by_ar_of_args(block,:call,ar_method_arguments)
   return x_out
end

#x_getset_semisparse_var(ht, s_varname, x_var_default_value) ⇒ Object

A sparse variables are inspired by sparce matrices. A semi-sparse variable is a variable that is instantiated and inited to the default value at the very first read access.



9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 9278

def x_getset_semisparse_var(ht,s_varname,x_var_default_value)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck bn, Hash,ht
      kibuvits_krl171bt4_assert_ok_to_be_a_varname_t1(bn,s_varname)
   end # if DEBUG
   x_out=nil
   if ht.has_key? s_varname
      x_out=ht[s_varname]
   else
      x_out=x_var_default_value
      ht[s_varname]=x_var_default_value
   end # if
   return x_out
end