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%> \todo
66%> \pvhigh
67%> Is the last condition ``~isempty(properties(objnew.(key)))`` in the implementation of this procedure necessary?<br>
68%> Its presence contradicts the function behavior at the zeroth level of iteration and subsequent iterations.<br>
69%> This issue must be addressed as soon as the correction is verified to not change the behavior of other
70%> functions calling this function.<br>
71%>
72%> \final{hash2comp}
73%>
74%> \author
75%> \JoshuaOsborne, May 21 2024, 10:47 PM, University of Texas at Arlington<br>
76%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
77%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
78function objnew = hash2comp(hashmap, object, insensitive, extensible, recursive)
79 objnew = object;
80 hashmapLen = length(hashmap);
81 if rem(hashmapLen, 2) ~= 0
82 help("pm.matlab.hashmap.hash2comp");
83 disp("length(hashmap)");
84 disp( length(hashmap) );
85 error ( newline ...
86 + "The input argument ``hashmap`` must be a cell array of even length." + newline ...
87 + "For more information, see the documentation displayed above." + newline ...
88 + newline ...
89 );
90 end
91 if ~isstruct(objnew)
92 fieldList = properties(objnew);
93 fieldListLen = length(fieldList);
94 else
95 fieldList = fieldnames(objnew);
96 fieldListLen = length(fieldList);
97 end
98 if nargin < 5
99 recursive = [];
100 end
101 if nargin < 4
102 extensible = [];
103 end
104 if nargin < 3
105 insensitive = [];
106 end
107 if isempty(recursive)
108 recursive = false;
109 end
110 if isempty(extensible)
111 extensible = false;
112 end
113 if isempty(insensitive)
114 insensitive = false;
115 end
116 for i = 1 : 2 : hashmapLen
117 keyfound = false;
118 key = string(hashmap{i});
119 for ip = 1 : fieldListLen
120 if ~insensitive
121 keyfound = strcmp(key, string(fieldList(ip)));
122 else
123 keyfound = strcmpi(key, string(fieldList(ip)));
124 end
125 if keyfound
126 key = fieldList{ip};
127 break;
128 end
129 end
130 if ~keyfound
131 if ~extensible
132 error ( newline ...
133 + "The requested ``object`` property:" + newline ...
134 + newline ...
135 + pm.io.tab() + """" + string(hashmap{i}) + """" + newline ...
136 + newline ...
137 + "does not exist in the specified input ``object``." + newline ...
138 + newline ...
139 );
140 elseif ~isstruct(objnew)
141 objnew.addprop(key);
142 end
143 end
144 % \todo:
145 % Is the last condition ``~isempty(properties(objnew.(key)))`` necessary?
146 % Its presence contradicts the function behavior at the zeroth level of iteration and subsequent iterations.
147 % This issue must be addressed as soon as it is clear the correction does not change the behavior of other
148 % functions calling this function.
149 if recursive && isa(hashmap{i + 1}, "cell") && (isa(objnew.(key), "struct") || isa(objnew.(key), "handle") || ~isempty(properties(objnew.(key))))
150 objnew.(key) = pm.matlab.hashmap.hash2comp(hashmap{i + 1}, objnew.(key), insensitive, extensible, recursive);
151 else
152 objnew.(key) = hashmap{i + 1};
153 end
154 end
155end
function copy(in from, in to, in field, in exclude)
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 verified(in varval, in vartype, in maxlen)
Return true if and only if the input varval conforms with the specified input type vartype and maximu...
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...