Skip to content

Commit

Permalink
Re #1786 Minor changes and comment to be nice and push up the build
Browse files Browse the repository at this point in the history
  • Loading branch information
abuts committed Feb 5, 2025
1 parent 0deaf34 commit 4e7c203
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
13 changes: 13 additions & 0 deletions _test/test_utilities_herbert/test_fast_map.m
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;

Expand Down
24 changes: 12 additions & 12 deletions herbert_core/utilities/classes/@fast_map/fast_map.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
%
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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', ...
Expand Down
13 changes: 13 additions & 0 deletions herbert_core/utilities/classes/@fast_map/private/optimize_.m
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 4e7c203

Please sign in to comment.