Class: Kibuvits_krl171bt4_ix
- Inherits:
-
Object
- Object
- Kibuvits_krl171bt4_ix
- Includes:
- Singleton
- Defined in:
- lib/kibuvits_ruby_library_krl171bt4_.rb
Overview
The class Kibuvits_krl171bt4_ix is a namespace for functions that are meant for facilitating the use of indexes. In the context of the Kibuvits_krl171bt4_ix an index is an Array index, hash-table key, etc.
Class Method Summary collapse
-
.bisect_at_sindex(haystack, i_sindex, b_force_element_cloning = false) ⇒ Object
bisect_at_sindex.
-
.ht_merge_by_overriding_t1(ar_hashtables) ⇒ Object
ht_merge_by_overriding_t1.
-
.normalize2array(x_array_or_something_else, ht_values_that_result_an_empty_array = nil) ⇒ Object
normalize2array.
-
.normalize2array_insert_2_ht(ht_values_that_result_an_empty_array, x_that_is_not_an_array) ⇒ Object
normalize2array_insert_2_ht.
-
.sar(haystack, i_lower_separator_index, i_higher_separator_index) ⇒ Object
“sar” stands for sub-array.
-
.x_apply_binary_operator_t1(x_identity_element, ar_in, func_operator_that_might_be_noncommutative) ⇒ Object
x_apply_binary_operator_t1.
-
.x_filter_t1(ar_or_ht_in, func_returns_true_if_element_is_part_of_output) ⇒ Object
x_filter_t1.
Instance Method Summary collapse
-
#bisect_at_sindex(haystack, i_sindex, b_force_element_cloning = false) ⇒ Object
Array indices: 0 1 2 3 4 array= Separator indices: 0 1 2 3 4 5.
-
#ht_merge_by_overriding_t1(ar_hashtables) ⇒ Object
Explanation by example: ht_1=Hash.new ht_2=Hash.new.
-
#initialize ⇒ Kibuvits_krl171bt4_ix
constructor
A new instance of Kibuvits_krl171bt4_ix.
-
#normalize2array(x_array_or_something_else, ht_values_that_result_an_empty_array = nil) ⇒ Object
If the ht_values_that_result_an_empty_array!=nil, then the entries to it must be inserted by using.
- #normalize2array_insert_2_ht(ht_values_that_result_an_empty_array, x_that_is_not_an_array) ⇒ Object
-
#sar(haystack, i_lower_separator_index, i_higher_separator_index) ⇒ Object
An explanation by an example:.
-
#x_apply_binary_operator_t1(x_identity_element, ar_in, func_operator_that_might_be_noncommutative) ⇒ Object
This function is a generalisation of the kibuvits_krl171bt4_s_concat_array_of_strings(…), which is a memory access paterns based speed optimization of the 2-liner:.
-
#x_filter_t1(ar_or_ht_in, func_returns_true_if_element_is_part_of_output) ⇒ Object
The func_returns_true_if_element_is_part_of_output is fed 2 arguments: x_key, x_value.
Constructor Details
#initialize ⇒ Kibuvits_krl171bt4_ix
Returns a new instance of Kibuvits_krl171bt4_ix.
3775 3776 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 3775 def initialize end |
Class Method Details
.bisect_at_sindex(haystack, i_sindex, b_force_element_cloning = false) ⇒ Object
bisect_at_sindex
3943 3944 3945 3946 3947 3948 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 3943 def Kibuvits_krl171bt4_ix.bisect_at_sindex(haystack,i_sindex, b_force_element_cloning=false) x_left,x_right=Kibuvits_krl171bt4_ix.instance.bisect_at_sindex(haystack,i_sindex, b_force_element_cloning) return x_left,x_right end |
.ht_merge_by_overriding_t1(ar_hashtables) ⇒ Object
ht_merge_by_overriding_t1
4107 4108 4109 4110 4111 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 4107 def Kibuvits_krl171bt4_ix.ht_merge_by_overriding_t1(ar_hashtables) ar_out=Kibuvits_krl171bt4_ix.instance.ht_merge_by_overriding_t1( ar_hashtables) return ar_out end |
.normalize2array(x_array_or_something_else, ht_values_that_result_an_empty_array = nil) ⇒ Object
normalize2array
4007 4008 4009 4010 4011 4012 4013 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 4007 def Kibuvits_krl171bt4_ix.normalize2array(x_array_or_something_else, ht_values_that_result_an_empty_array=nil) ar_out=Kibuvits_krl171bt4_ix.instance.normalize2array( x_array_or_something_else, ht_values_that_result_an_empty_array) return ar_out end |
.normalize2array_insert_2_ht(ht_values_that_result_an_empty_array, x_that_is_not_an_array) ⇒ Object
normalize2array_insert_2_ht
3967 3968 3969 3970 3971 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 3967 def Kibuvits_krl171bt4_ix.normalize2array_insert_2_ht( ht_values_that_result_an_empty_array,x_that_is_not_an_array) Kibuvits_krl171bt4_ix.instance.normalize2array_insert_2_ht( ht_values_that_result_an_empty_array,x_that_is_not_an_array) end |
.sar(haystack, i_lower_separator_index, i_higher_separator_index) ⇒ Object
“sar” stands for sub-array.
3859 3860 3861 3862 3863 3864 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 3859 def Kibuvits_krl171bt4_ix.sar(haystack, i_lower_separator_index, i_higher_separator_index) x_out=Kibuvits_krl171bt4_ix.instance.sar(haystack,i_lower_separator_index, i_higher_separator_index) return x_out end |
.x_apply_binary_operator_t1(x_identity_element, ar_in, func_operator_that_might_be_noncommutative) ⇒ Object
x_apply_binary_operator_t1
4283 4284 4285 4286 4287 4288 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 4283 def Kibuvits_krl171bt4_ix.x_apply_binary_operator_t1(x_identity_element,ar_in, func_operator_that_might_be_noncommutative) x_out=Kibuvits_krl171bt4_ix.instance.x_apply_binary_operator_t1( x_identity_element,ar_in,func_operator_that_might_be_noncommutative) return x_out end |
.x_filter_t1(ar_or_ht_in, func_returns_true_if_element_is_part_of_output) ⇒ Object
x_filter_t1
4058 4059 4060 4061 4062 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 4058 def Kibuvits_krl171bt4_ix.x_filter_t1(ar_or_ht_in,func_returns_true_if_element_is_part_of_output) x_out=Kibuvits_krl171bt4_ix.instance.x_filter_t1( ar_or_ht_in,func_returns_true_if_element_is_part_of_output) return x_out end |
Instance Method Details
#bisect_at_sindex(haystack, i_sindex, b_force_element_cloning = false) ⇒ Object
Array indices: 0 1 2 3 4
array=["H","e","l","l","o"]
Separator indices: 0 1 2 3 4 5
3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 3893 def bisect_at_sindex(haystack,i_sindex, b_force_element_cloning=false) if KIBUVITS_krl171bt4_b_DEBUG bn=binding() kibuvits_krl171bt4_typecheck bn, Fixnum, i_sindex kibuvits_krl171bt4_typecheck bn, [TrueClass,FalseClass], b_force_element_cloning end # if kibuvits_krl171bt4_throw "i_sindex=="+i_sindex.to_s+" < 0" if i_sindex<0 i_hlen=haystack.length if i_hlen<i_sindex kibuvits_krl171bt4_throw "haystack.length=="+i_hlen.to_s+" < i_sindex=="+i_sindex.to_s end # if cl_name=haystack.class.to_s case cl_name when 'String' x_left,x_right=bisect_at_sindex_for_strings(haystack, i_sindex) return x_left,x_right when 'Array' if !b_force_element_cloning x_left,x_right=bisect_at_sindex_for_ar(haystack,i_sindex) return x_left,x_right end # if else end # case x_left=haystack.class.new x_right=haystack.class.new i_hlen=haystack.length i=0 if b_force_element_cloning while i<i_sindex do x_left<<haystack[i].clone i=i+1 end # loop while i<i_hlen do x_right<<haystack[i].clone i=i+1 end # loop else while i<i_sindex do x_left<<haystack[i] i=i+1 end # loop while i<i_hlen do x_right<<haystack[i] i=i+1 end # loop end # if return x_left,x_right end |
#ht_merge_by_overriding_t1(ar_hashtables) ⇒ Object
Explanation by example: ht_1=Hash.new ht_2=Hash.new
ht_merged_1_2=ht_merge_by_overriding_t1(ar_1_2) ht_merged_2_1=ht_merge_by_overriding_t1(ar_2_1)
#—————|————- # ht_merged_1_2 | ht_merged_2_1 #—————|————- # a==“aaa2” | a==“aaa1” # b==“bbb2” | b==“bbb2” # c==“ccc1” | c==“ccc1” #—————|————-
4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 4089 def ht_merge_by_overriding_t1(ar_hashtables) if KIBUVITS_krl171bt4_b_DEBUG bn=binding() kibuvits_krl171bt4_typecheck bn, Array,ar_hashtables ar_hashtables.each do |ht_candidate| bn=binding() kibuvits_krl171bt4_typecheck bn, Hash, ht_candidate end # loop end # if ht_out=Hash.new ar_hashtables.each do |ht| ht.each_key do |key| ht_out[key]=ht[key] end # loop end # loop return ht_out end |
#normalize2array(x_array_or_something_else, ht_values_that_result_an_empty_array = nil) ⇒ Object
If the ht_values_that_result_an_empty_array!=nil, then the entries to it must be inserted by using
normalize2array_insert_2_ht(
ht_values_that_result_an_empty_array,<the value>)
To normalize a commaseparated string to an array of strings,
Kibuvits_krl171bt4_str.normalize_str_2_array_of_s_t1(...)
should be used.
3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 3984 def normalize2array(x_array_or_something_else, ht_values_that_result_an_empty_array=nil) if KIBUVITS_krl171bt4_b_DEBUG bn=binding() kibuvits_krl171bt4_typecheck bn, [NilClass,Hash], ht_values_that_result_an_empty_array end # if cl=x_array_or_something_else.class return x_array_or_something_else if cl==Array ht_vls_for_empty=ht_values_that_result_an_empty_array ar_out=nil if ht_vls_for_empty==nil ar_out=[x_array_or_something_else] else s=normalize2array_searchstring(x_array_or_something_else) if ht_vls_for_empty.has_key? s ar_out=Array.new else ar_out=[x_array_or_something_else] end # if end # if return ar_out end |
#normalize2array_insert_2_ht(ht_values_that_result_an_empty_array, x_that_is_not_an_array) ⇒ Object
3961 3962 3963 3964 3965 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 3961 def normalize2array_insert_2_ht(ht_values_that_result_an_empty_array, x_that_is_not_an_array) s_key=normalize2array_searchstring(x_that_is_not_an_array) ht_values_that_result_an_empty_array[s_key]=42 end |
#sar(haystack, i_lower_separator_index, i_higher_separator_index) ⇒ Object
An explanation by an example:
Array indices: 0 1 2 3 4
array=["H","e","l","l","o"]
Separator indices: 0 1 2 3 4 5
0 1 2
Kibuvits_krl171bt4_ix.sar(,0,0)==[] # 0-0=0 Kibuvits_krl171bt4_ix.sar(,0,1)== # 1-0=1 Kibuvits_krl171bt4_ix.sar(,1,1)==[] # 1-1=0 Kibuvits_krl171bt4_ix.sar(,1,2)== # 2-1=1 Kibuvits_krl171bt4_ix.sar(,2,2)==[] # 2-2=0 Kibuvits_krl171bt4_ix.sar(,0,2)== # 2-0=2 Kibuvits_krl171bt4_ix.sar(,2,2)==[] # 2-2=0
Kibuvits_krl171bt4_ix([],0,0)== # 0-0=0
“sar” stands for sub-array.
3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 3822 def sar(haystack,i_lower_separator_index, i_higher_separator_index) if KIBUVITS_krl171bt4_b_DEBUG bn=binding() kibuvits_krl171bt4_typecheck bn, Fixnum, i_lower_separator_index kibuvits_krl171bt4_typecheck bn, Fixnum, i_higher_separator_index end # if i_low=i_lower_separator_index i_high=i_higher_separator_index if i_high<i_low kibuvits_krl171bt4_throw "i_higher_separator_index=="+i_high.to_s+ " < i_lower_separator_index=="+i_low.to_s end # if kibuvits_krl171bt4_throw "i_lower_separator_index=="+i_low.to_s+" < 0" if i_low<0 if haystack.length<i_high kibuvits_krl171bt4_throw "haystack.length=="+haystack.length.to_s+ " < i_higher_separator_index=="+i_high.to_s end # if cl_name=haystack.class.to_s case cl_name when 'String' x_out=sar_for_strings(haystack, i_low, i_high) return x_out when 'Array' x_out=sar_for_arrays(haystack, i_low, i_high) return x_out else end # case x_out=haystack.class.new i=i_low while i<i_high do x_out<<haystack[i].clone i=i+1 end return x_out end |
#x_apply_binary_operator_t1(x_identity_element, ar_in, func_operator_that_might_be_noncommutative) ⇒ Object
This function is a generalisation of the kibuvits_krl171bt4_s_concat_array_of_strings(…), which is a memory access paterns based speed optimization of the 2-liner:
s_sum=""
ar_strings.size.times{|ix| s_sum=s_sum+ar_strings[ix]}
and yes, in the case of huge strings and arrays with lots of elements the speed improvement can be 50%.
The x_identity_element is defined by the following formula:
( func_operator_that_might_be_noncommutative.call(ar_in[ix],x_identity_element)==
==func_operator_that_might_be_noncommutative.call(x_identity_element,ar_in[ix])==
==ar_in[ix] ) === true
—–demo–code—start—–
require "prime"
func_oper_star=lambda do |x_a,x_b|
x_out=x_a*x_b
return x_out
end # func_oper_star
i_n_of_primes=100000
ar_x=Prime.take(i_n_of_primes)
#----
ob_start_1=Time.new
x_0=Kibuvits_krl171bt4_ix.x_apply_binary_operator_t1(x_identity_element,ar_x,func_oper_star)
ob_end_1=Time.new
ob_duration_1=ob_end_1-ob_start_1
#----
x_0=1
ob_start_2=Time.new
i_n_of_primes.times do |ix|
x_0=x_0*ar_x[ix]
end # loop
ob_end_2=Time.new
ob_duration_2=ob_end_2-ob_start_2
#--------------
puts "elephant_1 ob_duration_1=="+ob_duration_1.to_s
puts "elephant_2 ob_duration_2=="+ob_duration_2.to_s
—–demo–code—end——-
The console output of the demo code:
elephant_1 ob_duration_1==0.245117211
elephant_2 ob_duration_2==28.308270365
Yes, speed improvement is over 300% (three hundred) percent!
4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 4167 def x_apply_binary_operator_t1(x_identity_element,ar_in, func_operator_that_might_be_noncommutative) # There is no point of reading this code, because # it is a slightly edited version of the # kibuvits_krl171bt4_s_concat_array_of_strings(...) core. # The comments and explanations are mostly there. if defined? KIBUVITS_krl171bt4_b_DEBUG if KIBUVITS_krl171bt4_b_DEBUG bn=binding() kibuvits_krl171bt4_typecheck bn, Array, ar_in kibuvits_krl171bt4_typecheck bn, Proc, func_operator_that_might_be_noncommutative end # if end # if func_oper=func_operator_that_might_be_noncommutative i_n=ar_in.size if i_n<3 if i_n==2 x_out=func_oper.call(ar_in[0],ar_in[1]) return x_out else if i_n==1 # For the sake of consistency one # wants to make sure that the returned # string instance always differs from those # that are within the ar_in. x_out=func_oper.call(x_identity_element,ar_in[0]) return x_out else # i_n==0 x_out=x_identity_element return x_out end # if end # if end # if x_out=x_identity_element # needs to be inited to the x_identity_element ar_1=ar_in b_ar_1_equals_ar_in=true # to avoid modifying the received Array ar_2=Array.new b_take_from_ar_1=true b_not_ready=true i_reminder=nil i_loop=nil i_ar_in_len=nil i_ar_out_len=0 # code after the while loop needs a number x_1=nil x_2=nil x_3=nil i_2=nil while b_not_ready # The next if-statement is to avoid copying temporary # strings between the ar_1 and the ar_2. if b_take_from_ar_1 i_ar_in_len=ar_1.size i_reminder=i_ar_in_len%2 i_loop=(i_ar_in_len-i_reminder)/2 i_loop.times do |i| i_2=i*2 x_1=ar_1[i_2] x_2=ar_1[i_2+1] x_3=func_oper.call(x_1,x_2) ar_2<<x_3 end # loop if i_reminder==1 x_3=ar_1[i_ar_in_len-1] ar_2<<x_3 end # if i_ar_out_len=ar_2.size if 1<i_ar_out_len if b_ar_1_equals_ar_in ar_1=Array.new b_ar_1_equals_ar_in=false else ar_1.clear end # if else b_not_ready=false end # if else # b_take_from_ar_1==false i_ar_in_len=ar_2.size i_reminder=i_ar_in_len%2 i_loop=(i_ar_in_len-i_reminder)/2 i_loop.times do |i| i_2=i*2 x_1=ar_2[i_2] x_2=ar_2[i_2+1] x_3=func_oper.call(x_1,x_2) ar_1<<x_3 end # loop if i_reminder==1 x_3=ar_2[i_ar_in_len-1] ar_1<<x_3 end # if i_ar_out_len=ar_1.size if 1<i_ar_out_len ar_2.clear else b_not_ready=false end # if end # if b_take_from_ar_1=!b_take_from_ar_1 end # loop if i_ar_out_len==1 if b_take_from_ar_1 x_out=ar_1[0] else x_out=ar_2[0] end # if else # The x_out has been inited to "". if 0<i_ar_out_len raise Exception.new("This function is flawed."+ "\n GUID='52eb32c4-586f-42d5-9161-c13290a118e7'\n\n") end # if end # if return x_out end |
#x_filter_t1(ar_or_ht_in, func_returns_true_if_element_is_part_of_output) ⇒ Object
The func_returns_true_if_element_is_part_of_output is fed 2 arguments: x_key, x_value. For arrays the x_key is an index.
If the ar_or_ht_in is an array, then the output will also be an array. Otherwise the output will be a hashtable.
4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 |
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 4023 def x_filter_t1(ar_or_ht_in,func_returns_true_if_element_is_part_of_output) if KIBUVITS_krl171bt4_b_DEBUG bn=binding() kibuvits_krl171bt4_typecheck bn, [Array,Hash],ar_or_ht_in kibuvits_krl171bt4_typecheck bn, Proc,func_returns_true_if_element_is_part_of_output end # if x_out=nil cl=ar_or_ht_in.class b_add_2_output=nil if cl==Array ar_in=ar_or_ht_in ar_out=Array.new i_ar_in_len=ar_in.size x_value=nil i_ar_in_len.times do |ix| x_value=ar_in[ix] b_add_2_output=func_returns_true_if_element_is_part_of_output.call( ix,x_value) ar_out<<x_value if b_add_2_output end # loop return ar_out else # cl==Hash ht_in=ar_or_ht_in ht_out=Hash.new ht_in.each_pair do |x_key, x_value| b_add_2_output=func_returns_true_if_element_is_part_of_output.call( x_key,x_value) ht_out[x_key]=x_vaoue if b_add_2_output end # loop return ht_out end # if kibuvits_krl171bt4_throw("There's a flaw. \n"+ "GUID='e6706e30-270d-4449-b281-c13290a118e7'\n\n") end |