From 4e7c203a148bf0c1ab8656e0553fb11fa574ebdb Mon Sep 17 00:00:00 2001 From: abuts Date: Wed, 5 Feb 2025 17:26:20 +0000 Subject: [PATCH] Re #1786 Minor changes and comment to be nice and push up the build --- _test/test_utilities_herbert/test_fast_map.m | 13 ++++++++++ .../utilities/classes/@fast_map/fast_map.m | 24 +++++++++---------- .../@fast_map/private/check_combo_arg_.m | 3 ++- .../classes/@fast_map/private/optimize_.m | 13 ++++++++++ 4 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 herbert_core/utilities/classes/@fast_map/private/optimize_.m diff --git a/_test/test_utilities_herbert/test_fast_map.m b/_test/test_utilities_herbert/test_fast_map.m index 531f79168b..9f8bd0b7d6 100644 --- a/_test/test_utilities_herbert/test_fast_map.m +++ b/_test/test_utilities_herbert/test_fast_map.m @@ -11,6 +11,7 @@ obj = obj@TestCase(name); end %------------------------------------------------------------------ + %------------------------------------------------------------------ function test_insertion_in_optimized(~) n_keys = 100; base_key = 10+round(rand(1,10*n_keys)*(10*n_keys-1)); @@ -46,6 +47,18 @@ function test_optimization(~) end end %------------------------------------------------------------------ + function test_map_loadobj(~) + keys = uint32(1:10); + keys = num2cell(keys); + val = num2cell(10:-1:1); + fm = fast_map(keys,val ); + + struc = fm.to_struct(); + rec = serializable.loadobj(struc); + + assertEqual(fm,rec); + end + function test_fast_map_accepts_addition(~) keys = 10:-1:1; diff --git a/herbert_core/utilities/classes/@fast_map/fast_map.m b/herbert_core/utilities/classes/@fast_map/fast_map.m index 8803c9d609..03e6a9f17b 100644 --- a/herbert_core/utilities/classes/@fast_map/fast_map.m +++ b/herbert_core/utilities/classes/@fast_map/fast_map.m @@ -15,7 +15,10 @@ % % WARNING: intentianally disabled multiple reliability checks and % convenience properties in favour of access speed. - + % + % See fast_map_vs_map_performance in test_herbert_utilities + % to compare speed and optimize fast_map operations. + % properties % map optimization for doing fast access limit % @@ -184,20 +187,17 @@ end % function obj = optimize(obj) - % place values into expanded cellrarray, containing - % empty where keys are missing and values where - obj.min_max_key_val_ = min_max(obj.keys_); - obj.key_shif_ = obj.min_max_key_val_(1)-1; - n_places = obj.min_max_key_val_(2)-obj.min_max_key_val_(1)+1; - obj.keyval_optimized_ = nan(1,n_places); - keys_shifted = obj.keys_-obj.min_max_key_val_(1)+1; - obj.keyval_optimized_(keys_shifted) = obj.values_(:); - obj.optimized_ = true; + % place values into expanded array or cellarray, containing + % NaN or empty where keys are missing and values where + % keys are present. This array/cellarray is optimal for fast + % access to the values as function of keys. + % + obj = optimize_(obj); end end %---------------------------------------------------------------------- - % Overloaded indexers. DESPITE NICE, adding them makes fast_map 40-60 - % times slower even without using indexes itself. Disabled for this + % Overloaded indexers. DESPITE LOOKING NICE, adding them makes fast_map + % 40-60 times slower even without using indexes itself. Disabled for this % reason, until, may be mex is written which would deal with fast part % of indices. methods diff --git a/herbert_core/utilities/classes/@fast_map/private/check_combo_arg_.m b/herbert_core/utilities/classes/@fast_map/private/check_combo_arg_.m index 2c71a7d0e0..2b54efddd0 100644 --- a/herbert_core/utilities/classes/@fast_map/private/check_combo_arg_.m +++ b/herbert_core/utilities/classes/@fast_map/private/check_combo_arg_.m @@ -1,6 +1,7 @@ function obj = check_combo_arg_(obj) %CHECK_COMBO_ARG_ validate consistency of interdependent properties, namely -%key/value properties +%key/value properties. If conditions right, the function also builds +%cash for optimized access to values if numel(obj.keys_) ~= numel(obj.values_) error('HERBERT:fast_map:invalid_argument', ... diff --git a/herbert_core/utilities/classes/@fast_map/private/optimize_.m b/herbert_core/utilities/classes/@fast_map/private/optimize_.m new file mode 100644 index 0000000000..ef1f281d41 --- /dev/null +++ b/herbert_core/utilities/classes/@fast_map/private/optimize_.m @@ -0,0 +1,13 @@ +function obj = optimize_(obj) +%OPTIMIZE_ % place values into expanded array or cellarray, containing +% NaN or empty where keys are missing and values where +% keys are present. This array/cellarray is optimal for fast +% access to the values as function of keys. + +obj.min_max_key_val_ = min_max(obj.keys_); +obj.key_shif_ = obj.min_max_key_val_(1)-1; +n_places = obj.min_max_key_val_(2)-obj.min_max_key_val_(1)+1; +obj.keyval_optimized_ = nan(1,n_places); +keys_shifted = obj.keys_-obj.min_max_key_val_(1)+1; +obj.keyval_optimized_(keys_shifted) = obj.values_(:); +obj.optimized_ = true;