queuePriority
Class:matlab.DiscreteEventSystem
Package:matlab
Define priority queue storage
Syntax
storage=queuePriority(entityType,capacity,key,order)
Description
defines a priority queue that sorts entities by custom attribute. Use this function when implementing thestorage
=queuePriority(entityType
,capacity
,key
,order
)getEntityStorageImpl
method.
Input Arguments
Output Arguments
Examples
Define Storage Element as a Priority Queue
Define storage element as a priority queue.
% Define a storage element as a priority queue% - Queue sorts entities using a specific attribute of the entities% - Queue can store entities of type 'myEntity'% - Queue can store no more than 25 entities% - Queue uses the attribute 'age' to sort entities% - Sorting direction is 'ascending', resulting entities with% smaller 'age' attribute values to appear in front of the queuestorage = obj.queuePriority('myEntity', 25,'age','ascending');
Custom Entity Storage Block with Iteration Event
In this example, a custom block allows entities to enter its storage element through its input port. The storage element is a priority queue that sorts the entities based on theirDiameter
属性按升序。每一个实体t条目o the block's storage invokes an iteration event to display the diameter and the position of each entity in the storage.
For more information, seeCreate a Custom Entity Storage Block with Iteration Event.
classdefCustomEntityStorageBlockIteration < matlab.DiscreteEventSystem% A custom entity storage block with one input port and one storage element.% Nontunable propertiesproperties(Nontunable)% CapacityCapacity = 5;end% Create the storage element with one input and one storage.methods(Access=protected)functionnum = getNumInputsImpl(obj) num = 1;endfunctionnum = getNumOutputsImpl(obj) num = 0;endfunctionentityTypes = getEntityTypesImpl(obj) entityType1 = obj.entityType('Wheel'); entityTypes = entityType1;endfunction[inputTypes,outputTypes] = getEntityPortsImpl(obj) inputTypes = {'Wheel'}; outputTypes={};endfunction[storageSpecs, I, O] = getEntityStorageImpl(obj) storageSpecs = obj.queuePriority('Wheel',obj.Capacity,'Diameter','ascending'); I = 1; O = [];endend%的实体进入事件actionmethodsfunction[entity, event] = WheelEntry(obj,storage,entity, source)% Entity entry invokes an iterate event.event = obj.eventIterate(1,'');end% itarate事件动作function[entity,event,next] = WheelIterate(obj,storage,entity,tag,cur)% Display wheel id, position in the storage, and diameter.coder.extrinsic('fprintf'); fprintf('Wheel id %d, Current position %d, Diameter %d\n',...entity.sys.id, cur.position, entity.data.Diameter);ifcur.size == cur.position fprintf('End of Iteration \n')endnext = true; event=[];endendend
Version History
Introduced in R2016a