Caches

This section contains the API documentation for the caches module.

class DLL(val)

Bases: handle

An element of a doubly-linked list. This is a list where each element consists of a value, a reference to the previous element and a reference to the next element.

Based on the work by Richard Lange (2022). Least-Recently Used (LRU) Cache (https://www.mathworks.com/matlabcentral/fileexchange/68836-least-recently-used-lru-cache), MATLAB Central File Exchange. Retrieved June 18, 2022.

Properties

val (any) – data stored in this element

DLL(val)

Construct an element of a doubly-linked list. By default, the detached links point to the object itself.

Arguments

val (any) – data stored in this element.

Returns

dll (DLL) – data wrapped in a doubly-linked list format.

pop()

Remove an object from a doubly-linked list, updating the links on the next and previous element.

Arguments

obj (DLL) – object to remove from the list.

Returns

obj (DLL) – removed object, with detached links.

append(other)

Append an object to a doubly-linked list, updating the links.

Arguments
  • obj (DLL) – list to append to.

  • other (DLL) – object to append.

Returns

obj (DLL) – updated list.

uplus()

Get the next element in the list.

Usage

other = +obj;

other = uplus(obj);

Arguments

obj (:class`.DLL`) – current element in the list.

Returns

other (:class`.DLL`) – next element in the list.

uminus()

Get the previous element in the list.

Usage

other = -obj;

other = uminus(obj);

Arguments

obj (:class`.DLL`) – current element in the list.

Returns

other (:class`.DLL`) – previous element in the list.

class LRU(itemlimit, memlimit)

Bases: handle

A least-recently-used cache. Stores data up to a preset memory limit, then removes the least-recently-used elements to free up space for additional data.

Properties
  • sentinel (DLL) – sentinel of DLL, +sentinel is MRU, -sentinel is LRU

  • map (containers.Map) – map of key –> dll

  • itemlimit (int) – maximum size of cache in number of items

  • memlimit (double) – maximum size of cache in bytes

  • mem (double) – current memory usage in bytes.

LRU(itemlimit, memlimit)

Construct a new LRU cache.

Arguments
  • itemlimit (int) – maximum size of cache in number of items.

  • memlimit (double) – maximum size of cache in number of bytes.

Returns

cache (LRU) – empty LRU cache.

get(cache, key)

Retrieve a value from the cache.

Arguments
  • cache (LRU) – data cache.

  • key (uint8) – data key.

Returns

val (any) – value that is stored with a key, or empty if key not in cache.

set(cache, key, val)

Add a value to the cache.

Arguments
  • cache (LRU) – data cache.

  • key (uint8) – data key.

  • val (any) – data value.

Returns

cache (LRU) – updated cache.

GetMD5

GetMD5(varargin)

Calculates a 128 bit MD5 checksum for arrays or files.

Usage

digest = GetMD5(Data, Mode, Format)

Arguments
  • Data – input data on which the checksum is computed.

  • Mode (char) – optional declaration of the type of the input data.

    • ‘File’ : data is a file name as a char.

    • ‘8Bit’ : If data is a char array, only the 8 bit ASCII part is used. Then the

    digest is the same as for a ASCII text file e.g. created by fwrite(fid, data, ‘uchar’). - ‘Binary’ : The MD5 sum is obtained for the contents of data. This works for numerical, char and logical arrays. - ‘Array’ : Include the class and size information of data in the MD5 sum. This can be applied for (nested) structs, objects, cells and sparse arrays also.

  • Format (char) – Format of the output, default value is ‘hex’.

    • ‘hex’ : (1, 32) lowercase hexadecimal char.

    • ‘HEX’ : (1, 32) uppercase hexadecimal char.

    • ‘double’ : (1, 16) double vector with uint8 values.

    • ‘uint8’ : (1, 16) uint8 vector.

    • ‘base64’ : (1, 22) char vector, encoded in base 64 (A:Z, a:z, 0:9, +, /), unpadded.

Returns

digest – A 128 bit number in the specified format.

Notes

For sparse arrays, function handles, java and user-defined objects GetMD5_helper() is called to convert into a data format that can be handled.

The C-Mex-file is compiled automatically when this function is called for the first time. See GetMD5.c for details or a manual installation.

References

Author: Jan Simon, Heidelberg, (C) 2009-2019 matlab.2010(a)n(MINUS)simon.de

License: This program is derived from the RSA Data Security, Inc.

MD5 Message Digest Algorithm, RFC 1321, R. Rivest, April 1992 This implementation is published under the BSD license.

See also

Other methods for checksums can be found: CalcCRC32, DataHash, etc.

For more checksum methods see here.

GetMD5_helper(V)

Convert non-elementary array types for GetMD5 The C-Mex function GetMD5 calls this function to obtain meaningful unique data for function handles, java or user-defined objects and sparse arrays. The applied processing can depend on the needs of the users, therefore it is implemented as an M-function, which is easier to modify than the C-code.

Arguments

V – array of any type, which is not handled in the C-Mex.

Returns

S – array or struct containing elementary types only. The implementation migth be changed by the user! Default:

  • Sparse arrays: Struct containing the indices and values.

  • Function handle: The reply of FUNCTIONS and the size and date of the file.

  • User defined and java objects: V.hashCode if existing, else: struct(V).

Note

For objects the function getByteStreamFromArray() might be exhaustive and efficient, but unfortunately it is not documented.

Tested: Matlab/64 7.8, 7.13, 8.6, 9.1, Win7/64 Author: Jan Simon, Heidelberg, (C) 2016-2019 matlab.2010(a)n(MINUS)simon.de

InstallMex(SourceFile, varargin)

Compile and install Mex file The C, C++ or FORTRAN mex file is compiled and additional installation routines are started. Advanced users can call MEX() manually instead, but some beginners are overwhelmed by instructions for a compilation sometimes. Therefore this function can be called automatically from an M-function, when the compiled Mex-Function does not exist already.

Usage

Ok = InstallMex(SourceFile, ...)

Arguments

SourceFile – Name of the source file, with or without absolute or partial path. The default extension ‘.c’ is appended on demand.

Optional Arguments
  • Function name – Function is started after compiling, e.g. a unit-test.

  • Cell string – additional arguments for the compilation, e.g. libraries.

  • ‘-debug’ – enable debug mode.

  • ‘-force32’ – use the compatibleArrayDims flag under 64 bit Matlab.

  • ‘-replace’ – overwrite existing mex file without confirmation.

Returns

Ok – logical flag, true if compilation was successful.

Note

  • A compiler must be installed and setup before: mex -setup

  • For Linux and MacOS the C99 style is enabled for C-files.

  • The optimization flag -O is set.

  • Defined compiler directives
    • MATLABVER<XYZ>: <XYZ> is the current version, e.g. 708 for v7.8.

    • _LITTLE_ENDIAN or _BIG_ENDIAN: according to processor type

    • HAS_HG2: Defined for Matlab >= R2014b with HG2 graphics.

    • -R2017b or -largeArrayDims for 64 bit addressing and C Matrix API -R2018a for C Data API (this is set, when the string “[R2018a API]” appears anywhere inside the source file.

Examples

Compile func1.c with LAPACK libraries:

InstallMex('func1', {'libmwlapack.lib', 'libmwblas.lib'})

Compile func2.cpp, enable debugging and call a test function:

InstallMex('func2.cpp', '-debug', 'Test_func2');

These commands can be appended after the help section of an M-file, when the compilation should be started automatically, if the compiled MEX is not found.

Note

Suggestions for improvements and comments are welcome! Feel free to add this function to your FEX submissions, when you change the URL in the variable “Precompiled” accordingly.

Tested: Matlab/64 7.8, 7.13, 8.6, 9.1, Win7/64 Author: Jan Simon, Heidelberg, (C) 2012-2019 matlab.2010(a)n(MINUS)simon.de

uTest_GetMD5(doSpeed)

Automatic test: GetMD5 (Mex) This is a routine for automatic testing. It is not needed for processing and can be deleted or moved to a folder, where it does not bother.

Usage

uTest_GetMD5(doSpeed)

Arguments

doSpeed – Optional logical flag to trigger time consuming speed tests. Defaults to true. If no speed test is defined, this is ignored.

Note

On failure the test stops with an error. The speed is compared to a Java method.

Tested: Matlab 6.5, 7.7, 7.8, 7.13, WinXP/32, Win7/64 Author: Jan Simon, Heidelberg, (C) 2009-2019 matlab.2010(a)n(MINUS)simon.de