マッピング クラスの小かっこインデックスのカスタマイズ
この例では、クラスの小かっこインデックスをカスタマイズする方法を説明します。MyMap
クラスは、cell 配列の要素 ("値") に関連付けられた string ("キー") を格納します。このクラスは matlab.mixin.indexing.RedefinesParen
から継承され、小かっこを使用したインデックスのカスタム動作を定義します。このクラスは、次の 3 つのカスタマイズされたインデックス演算をサポートします。
は、キーに関連付けられた値を返します。instanceName
("keyName
")
は、新しいキーとそれに関連付けられた値を追加します。instanceName
("keyName
") =value
は、キーとそれに関連付けられた値を削除します。instanceName
("keyName
") = []
クラスとその補助関数 validateKeys
の完全なコードは、この例の最後に示します。
MyMap クラス | 説明 |
---|---|
classdef MyMap... < matlab.mixin.indexing.RedefinesParen |
|
properties (Access = private)
Keys (:,1) string
Values (:,1) cell
end | プライベート プロパティ |
methods (Static, Access = public) function obj = empty(varargin) if nargin == 0 obj = MyMap(string.empty(0,1),cell.empty(0,1)); return; end keys = string.empty(varargin{:}); if ~all(size(keys) == [0, 1]) error("MyMap:MustBeEmptyColumnVector",... "The only supported empty size is 0x1."); end obj = MyMap(keys,cell.empty(varargin{:})); end end | キーと値のいずれも含まない |
methods (Access = public) function obj = MyMap(keys_in,values_in) if nargin == 0 obj = MyMap.empty(0,1); return; end narginchk(2,2); if ~all(size(keys_in) == size(values_in)) error("MyMap:InputSizesDoNotMatch",... "The sizes of the input keys and values must match."); end obj.Keys = keys_in; obj.Values = values_in; end end | コンストラクターはキーと値を入力引数として受け入れ、配列が同じサイズであることを確認します。 |
function keys = getKeys(obj) keys = obj.Keys; end function values = getValues(obj) values = obj.Values; end | 2 つのパブリック メソッドは、キーと値への読み取りアクセスを提供します。 |
function varargout = size(obj,varargin) [varargout{1:nargout}] = size(obj.Keys,varargin{:}); end function C = cat(dim,varargin) error("MyMap:ConcatenationNotSupported",... "Concatenation is not supported."); end function lastKey = end(~,~,~) error("MyMap:EndNotSupported",... "Using end with MyMap objects is not supported.");" end | 抽象メソッド |
methods (Access = private) function [keyExists,idx] = convertKeyToIndex(obj,keyCellArray) arguments obj keyCellArray cell {validateKeys} end requestedKey = keyCellArray{1}; idx = find(contains(obj.Keys,requestedKey)); keyExists = ~isempty(idx); end end |
|
methods (Access = protected) function varargout = parenReference(obj,indexOp) [keyExists,idx] = convertKeyToIndex(obj,indexOp(1).Indices); if ~keyExists error("MyMap:KeyDoesNotExist",... "The requested key does not exist."); end if numel(indexOp) == 1 nargoutchk(0,1); varargout{1} = obj.Values{idx}; else % Additional operations [varargout{1:nargout}] = obj.Values{idx}.(indexOp(2:end)); end end |
|
function obj = parenAssign(obj,indexOp,varargin) indicesCell = indexOp(1).Indices; [keyExists,idx] = convertKeyToIndex(obj,indicesCell); if numel(indexOp) == 1 value = varargin{1}; if keyExists obj.Values{idx} = value; else obj.Keys(end+1) = indicesCell{1}; obj.Values{end+1} = value; end return; end if ~keyExists error("MyMap:MultiLevelAssignKeyDoesNotExist", ... "Assignment failed because key %s does not exist",... indicesCell{1}); end [obj.Values{idx}.(indexOp(2:end))] = varargin{:}; end |
|
function obj = parenDelete(obj,indexOp) [keyExists,idx] = convertKeyToIndex(obj,indexOp(1).Indices); if keyExists obj.Keys(idx) = []; obj.Values(idx) = []; else error("MyMap:DeleteNonExistentKey",... "Unable to perform deletion. The key %s does not exist.",... indexOp(1).Indices{1}); end end |
|
function n = parenListLength(obj,indexOp,indexingContext) [keyExists,idx] = convertKeyToIndex(obj,indexOp(1).Indices); if ~keyExists if indexingContext == matlab.indexing.IndexingContext.Assignment error("MyMap:MultiLevelAssignKeyDoesNotExist", ... "Unable to perform assignment. Key %s does not exist",... indexOp(1).Indices{1}); end error("MyMap:KeyDoesNotExist",... "The requested key does not exist."); end n = listLength(obj.Values{idx},indexOp(2:end),indexingContext); end end | 小かっこインデックス式から返す値の数を決定する抽象メソッド |
MyMap
と validateKeys
のコードを MATLAB パスに保存します。3 つのキーと値の初期リストをもつ MyMap
のインスタンスを作成します。
map = MyMap(["apple","cherry","orange"],{1,3,15});
map("
構文を使用して、特定のキーに対応する値を返します。keyName
")
map("cherry")
ans = 3
map("
を使用して、新しいキーを配列に追加します。keyName
") = value
map("banana") = 2; map("banana")
ans = 2
map("
を使用して、キーとそれに関連付けられた値を配列から削除します。キーが既に配列内にないことを確認します。keyName
") = []
map("orange") = []; map("orange")
Error using MyMap/parenReference (line 88) The requested key does not exist.
参考
matlab.mixin.indexing.RedefinesParen
| matlab.indexing.IndexingOperation