Are there any 'bounded container' libraries available to Matlab users?

1 回表示 (過去 30 日間)
Martin Dowie
Martin Dowie 2015 年 7 月 24 日
回答済み: Martin Dowie 2017 年 8 月 4 日
Vectors are supported but is there a ready-rolled library that can implement a 'bounded doubly linked list' using Matlab vectors?
  1 件のコメント
Cedric
Cedric 2015 年 7 月 28 日
編集済み: Cedric 2015 年 7 月 28 日
EDIT @ 5:26pm : small correction to destructor.
Depending what you need to achieve, you could build a "handle" class which does the trick. Have you tried?
I used my 10 minutes pause for building a very small example:
classdef Node < handle
% Basic example of doubly-linked list.
properties
nodeForward = []
nodeBackward = []
content = []
end
methods
function obj = Node( content, nodeBackward, nodeForward )
% Constructor.
if nargin > 0, obj.content = content ; end
if nargin > 1, obj.nodeBackward = nodeBackward ; end
if nargin > 2, obj.nodeForward = nodeForward ; end
end
function node = addNodeForward( obj, node )
% Add forward node and double-link.
if ~isa( node, 'Node' )
node = Node( node ) ;
end
obj.nodeForward = node ;
obj.nodeForward.nodeBackward = obj ;
end
function node = addNodeBackward( obj, node )
% Add backward node and double-link.
if ~isa( node, 'Node' )
node = Node( node ) ;
end
obj.nodeBackward = node ;
obj.nodeBackward.nodeForward = obj ;
end
function displayForward( obj, depth )
% Display current and call forward (if present) display.
disp( obj ) ;
if nargin < 2, depth = Inf ; end
if ~isempty( obj.nodeForward )
obj.nodeForward.displayForward( depth-1 ) ;
end
end
function displayBackward( obj, depth )
% Display current and call backward (if present) display.
disp( obj ) ;
if nargin < 2, depth = Inf ; end
if ~isempty( obj.nodeBackward )
obj.nodeBackward.displayBackward( depth-1 ) ;
end
end
function delete( obj )
% Destructor: cut and merge.
if ~isempty( obj.nodeForward )
if ~isempty( obj.nodeBackward )
obj.nodeBackward.nodeForward = obj.nodeForward ;
obj.nodeForward.nodeBackward = obj.nodeBackward ;
else
obj.nodeForward.nodeBackward = [] ;
end
elseif ~isempty( obj.nodeBackward )
obj.nodeBackward.nodeForward = [] ;
end
end
function disp( obj )
% Display content instead of obj properties.
% -> comment for debugging
disp( obj.content ) ;
end
end
end
With this, we can do e.g.
clear ;
clc ;
nodeStart = Node( 10 ) ;
node = nodeStart.addNodeForward( magic(3) ) ;
nodeEnd = node.addNodeForward( {'John', 'Doe'} ) ;
fprintf( 'Display forward from start.\n\n')
nodeStart.displayForward
fprintf( 'Delete middle node and display forward from start.\n\n')
delete( node )
nodeStart.displayForward
and we get
Display forward from start.
10
8 1 6
3 5 7
4 9 2
'John' 'Doe'
Delete middle node and display forward from start.
10
'John' 'Doe'

サインインしてコメントする。

採用された回答

Udaya Mallampati
Udaya Mallampati 2015 年 7 月 27 日
編集済み: Udaya Mallampati 2015 年 7 月 27 日
Hi Martin,
This functionality to implement bounded doubly linked list is not currently available. I work for MathWorks and have forwarded your feedback to the appropriate team.
  2 件のコメント
Martin Dowie
Martin Dowie 2015 年 7 月 28 日
Thanks - generally bounded forms of vector, list and map (ordered & hashed) are useful in embedded systems, where the performance of an implicit vector using built-in arrays can lead to very, very poor performance.
Perhaps looking at the Ada.Containers.Bounded_* package would provide some useful insight/inspiration?
A bounded vector class is fairly trivial to produce but the others are slightly trickier...
Steven Lord
Steven Lord 2015 年 7 月 28 日
For maps, take a look at the containers.Map functionality.

サインインしてコメントする。

その他の回答 (1 件)

Martin Dowie
Martin Dowie 2017 年 8 月 4 日
Bit late responding but the suggestions (while interesting) are 'unbounded' containers, and I'm specifically looking for bounded forms (check out the Ada library - it is specifically intended for use in High Integrity Systems - http://ada-auth.org/standards/12rm/html/RM-A-18-19.html and the next few pages).
I might try rolling my own based on the examples given, but it would be so much more useful if Matlab provided them out the box...
Thanks Martin

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by