Skip to content

Commit

Permalink
Re #1786 more unit tests and key insertion into optimized/non-optimiz…
Browse files Browse the repository at this point in the history
…ed map
  • Loading branch information
abuts committed Feb 5, 2025
1 parent c3fbd9a commit d75c825
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
21 changes: 20 additions & 1 deletion _test/test_utilities_herbert/test_fast_map.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@
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));
base_key = unique(base_key);
n_keys = numel(base_key);
val = 1:n_keys;

fm = fast_map(base_key,val);
fm.optimized = true;

fm = fm.add(base_key(1),n_keys+1);
assertEqual(fm.get(base_key(1)),n_keys+1);
assertTrue(fm.optimized);

new_key = 10+10*n_keys;
fm = fm.add(new_key,n_keys+2);
assertEqual(fm.get(new_key ),n_keys+2);
assertFalse(fm.optimized);
end

function test_optimization(~)
n_keys = 100;
base_key = 10+round(rand(1,10*n_keys)*(10*n_keys-1));
Expand All @@ -24,7 +44,6 @@ function test_optimization(~)
for i=1:n_keys
assertEqual(fm.get(base_key(i)),fmop.get(base_key(i)));
end

end
%------------------------------------------------------------------
function test_fast_map_accepts_addition(~)
Expand Down
22 changes: 17 additions & 5 deletions herbert_core/utilities/classes/@fast_map/fast_map.m
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
classdef fast_map < serializable
%FAST_MAP class provides map functionality for small subset of
%key-value pairs.
%key-value pairs, where keys are unit32 numbers and values are double.
%
% Initial purpose -- use it as the map for connecting run_id-s with
% IX_experiment number so that correspondence between
% Initial purpose -- use it as the fast map for connecting run_id-s with
% IX_experiment number to maintan correspondence between IX_experiment
% number and pixel ID.
%
% The class is necessary because MATLAB containers.Map class is
% incredibly slow.
%
% Class optimized for key access rather then key insertion.
% Further development and acceleration may be possible, including
% mexing and building container for
% mexing and building wrapper around C++ map class.
%
% WARNING: intentianally disabled multiple reliability checks and
% convenience properties in favour of access speed.

properties
% map optimization for doing fast access limit
%
% The map optimization works by allocating large array with places
empty_space_optimization_limit = 5;
end

properties(Dependent)
n_members; % number of elements in key-value map
keys % arrays of keys used to retrieve the values
Expand Down Expand Up @@ -73,6 +82,9 @@
present = self.keys_ == key;
if any(present)
self.values_(present) = value;
if self.optimized_
self.keyval_optimized_(key-self.key_shif_) = value;
end
else
self.keys_(end+1) = key;
self.values_(end+1) = value;
Expand Down Expand Up @@ -136,7 +148,7 @@
else
obj.optimized_ = false;
obj.min_max_key_val_ = [];
obj.keyval_optimized_= {};
obj.keyval_optimized_= [];
end
end
%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
end

obj.min_max_key_val_ = min_max(obj.keys_);
% always optimiza if key spread is bigger then specified number
if obj.min_max_key_val_(2)-obj.min_max_key_val_(1) <= obj.empty_space_optimization_limit * obj.n_members
obj.optimized = true;
end

end

0 comments on commit d75c825

Please sign in to comment.