DylanMuir/SimpleMap

버전 1.0.0.0 (21.3 KB) 작성자: Dylan Muir
Handle class for storing a mapping between an index and arbitrary data
다운로드 수: 40
업데이트 날짜: 2017/8/16

A matlab handle class for persistent storage of arbitrary data. Provides a mapping from a single subscript to arbitrary data.
Usage
-------
Create an empty map
>> smObj = SimpleMap
smObj =
SimpleMap containing 0 stored values, max index of 0.
Assign some data

>> smObj(1) = [1 2 3];
>> smObj(2) = 'a';
>> smObj(4) = {4 5 6}
smObj =
SimpleMap containing 3 stored values, max index of 4.
Extract the data

>> a = smObj(1);
>> b = smObj(3)
b =
[]
Find out which indices are used in the map

>> vbIndices = spy(smObj)
vbIndices =
1 1 0 1

>> find(smObj)
ans =
1 2 4
SimpleMaps are a handle class

>> smObj = SimpleMap; % Make a new empty SimpleMap
>> smObj2 = smObj; % Copy the SimpleMap object
>> smObj(1) = 'Map 1'; % Assign data to the first map
>> smObj2(1) % The data is also present in the copy
ans =
Map 1
The benefit of SimpleMap being a handle class, is to enable persistent data store within objects that are not themselves handle classes.

Using a SimpleMap for persistent data storage within a value class

classdef ValueClass
properties (Hidden)
smPersistent = SimpleMap;
end

methods
function fResult = LongCalculation(vcObj, nIndex)
% - Check whether the result has already been calculated
if (~isempty(vcObj.smPersistent(nIndex)))
fResult = vcObj.smPersistent(nIndex);
else
% - No, so compute the result from scratch
fResult = PerformLongCalculation(nIndex);

% - Cache the result
vcObj.smPersistent(nIndex) = fResult;
end
end
end
end

The property smPersistent will maintain data assigned to it from within methods of ValueClass, even though it is not a handle class. This is useful for internal hidden caching of results, not as useful for user-facing properties.

In the example above, a SimpleMap is used for "memoization" of the result of a long calculation. The result is cached, and this cache is maintained even though the object vcObj is not returned by the LongCalculation method.

This is most useful if the semantics of a class do not lend themselves to being a handle class.

인용 양식

Dylan Muir (2024). DylanMuir/SimpleMap (https://github.com/DylanMuir/SimpleMap), GitHub. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2016a
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Scope Variables and Generate Names에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

GitHub 디폴트 브랜치를 사용하는 버전은 다운로드할 수 없음

버전 게시됨 릴리스 정보
1.0.0.0

Updated description
Updated description
Updated the image
Updated description
Updated description

Updated description

이 GitHub 애드온의 문제를 보거나 보고하려면 GitHub 리포지토리로 가십시오.
이 GitHub 애드온의 문제를 보거나 보고하려면 GitHub 리포지토리로 가십시오.