ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
hash2comp.m
Go to the documentation of this file.
1%> \brief
2%> Return a copy of the input ``object``
3%> whose property (or field) values are overwritten
4%> with the corresponding ``(key, val)`` pairs in the
5%> input ``hashmap`` cell array.
6%>
7%> \details
8%> This functionality is useful for converting
9%> the variadic arguments of functions to the
10%> corresponding property values of objects
11%> with the functions.
12%>
13%> \param[in] hashmap : The input cell array of even number of elements
14%> containing the field names and values of the input ``object``
15%> as ``(key, val)`` pairs stored sequentially as the cell elements.<br>
16%> All keys in the input ``hashmap`` must already exist as
17%> fields/properties of the input object.<br>
18%> \param[in] object : The input scalar MATLAB struct
19%> or object whose property/field values
20%> are to be overwritten with the corresponding
21%> values from the input ``hashmap`` cell.<br>
22%> \param[in] insensitive : The input scalar MATLAB logical.
23%> If ``false``, then keys within the input ``hashmap`` will be matched
24%> against the input ``object`` properties(or fields) case-sensitively.<br>
25%> (**optional**, default = ``false``)
26%> \param[in] extensible : The input scalar MATLAB logical.<br>
27%> If ``true``, then keys in the input ``hashmap`` that are missing in the
28%> input ``object`` properties(or fields) will be added to output ``objnew``.<br>
29%> This functionality requires the input ``object`` to be either a MATLAB ``struct``
30%> or an object whose ultimate superclass is the MATLAB ``handle`` class,
31%> which allows adding new properties via ``addprop()`` method.<br>
32%> (**optional**, default = ``false``)
33%> \param[in] recursive : The input scalar MATLAB logical.<br>
34%> If ``true``, then key-val pairs in the input ``hashmap`` that match struct-cell
35%> or match object-cell pattern will be also converted recursively like the parent
36%> input ``object`` until all sub-components are obtained recursively.<br>
37%> (**optional**, default = ``false``)
38%>
39%> \return
40%> objnew : The output copy of the input object whose
41%> property/field values are overwritten with the
42%> corresponding values from the input ``hashmap`` cell.<br>
43%>
44%> \interface{hash2comp}
45%> \code{.m}
46%>
47%> objnew = pm.matlab.hashmap.hash2comp(hashmap, object)
48%> objnew = pm.matlab.hashmap.hash2comp(hashmap, object, [])
49%> objnew = pm.matlab.hashmap.hash2comp(hashmap, object, [], [])
50%> objnew = pm.matlab.hashmap.hash2comp(hashmap, object, [], [], [])
51%> objnew = pm.matlab.hashmap.hash2comp(hashmap, object, [], [], recursive)
52%> objnew = pm.matlab.hashmap.hash2comp(hashmap, object, [], extensible, [])
53%> objnew = pm.matlab.hashmap.hash2comp(hashmap, object, insensitive, [], [])
54%> objnew = pm.matlab.hashmap.hash2comp(hashmap, object, [], extensible, recursive)
55%> objnew = pm.matlab.hashmap.hash2comp(hashmap, object, insensitive, [], recursive)
56%> objnew = pm.matlab.hashmap.hash2comp(hashmap, object, insensitive, extensible, recursive)
57%>
58%> \endcode
59%>
60%> \example{hash2comp}
61%> \include{lineno} example/matlab/hashmap/hash2comp/main.m
62%> \output{hash2comp}
63%> \include{lineno} example/matlab/hashmap/hash2comp/main.out.m
64%>
65%> \final{hash2comp}
66%>
67%> \author
68%> \JoshuaOsborne, May 21 2024, 10:47 PM, University of Texas at Arlington<br>
69%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
70%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
71function objnew = hash2comp(hashmap, object, insensitive, extensible, recursive)
72 objnew = object;
73 hashmapLen = length(hashmap);
74 if rem(hashmapLen, 2) ~= 0
75 help("pm.matlab.hashmap.hash2comp");
76 disp("length(hashmap)");
77 disp( length(hashmap) );
78 error ( newline ...
79 + "The input argument ``hashmap`` must be a cell array of even length." + newline ...
80 + "For more information, see the documentation displayed above." + newline ...
81 + newline ...
82 );
83 end
84 if ~isstruct(objnew)
85 fieldList = properties(objnew);
86 fieldListLen = length(fieldList);
87 else
88 fieldList = fieldnames(objnew);
89 fieldListLen = length(fieldList);
90 end
91 if nargin < 5
92 recursive = [];
93 end
94 if nargin < 4
95 extensible = [];
96 end
97 if nargin < 3
98 insensitive = [];
99 end
100 if isempty(recursive)
101 recursive = false;
102 end
103 if isempty(extensible)
104 extensible = false;
105 end
106 if isempty(insensitive)
107 insensitive = false;
108 end
109 for i = 1 : 2 : hashmapLen
110 keyfound = false;
111 key = string(hashmap{i});
112 for ip = 1 : fieldListLen
113 if ~insensitive
114 keyfound = strcmp(key, string(fieldList(ip)));
115 else
116 keyfound = strcmpi(key, string(fieldList(ip)));
117 end
118 if keyfound
119 key = fieldList{ip};
120 break;
121 end
122 end
123 if ~keyfound
124 if ~extensible
125 error ( newline ...
126 + "The requested ``object`` property:" + newline ...
127 + newline ...
128 + pm.io.tab + """" + string(hashmap{i}) + """" + newline ...
129 + newline ...
130 + "does not exist in the specified input ``object``." + newline ...
131 + newline ...
132 );
133 elseif ~isstruct(objnew)
134 objnew.addprop(key);
135 end
136 end
137 if recursive && isa(hashmap{i + 1}, "cell") && (isa(objnew.(key), "struct") || isa(objnew.(key), "handle") || ~isempty(properties(objnew.(key))))
138 objnew.(key) = pm.matlab.hashmap.hash2comp(hashmap{i + 1}, objnew.(key), insensitive, extensible, recursive);
139 else
140 objnew.(key) = hashmap{i + 1};
141 end
142 end
143end
function copy(in from, in to, in fields)
Copy the contents of the struct/object from to the struct/object to recursively and without destroyin...
function hash2comp(in hashmap, in object, in insensitive, in extensible, in recursive)
Return a copy of the input object whose property (or field) values are overwritten with the correspon...
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...