Implementation of array of queues
Afficher commentaires plus anciens
Since matlab does not support queues, I am using arrays to handle the queueing functionality.
I have to implement an array of queues, which each queue of different length. 2D arrays in matlab support only matrices, hence should be of same no. of elements in each queue which is not my requirement.
I imported Java implementation of linkedlist, but it does not support struct or a class object. The element to be added into a queue is a struct or a class object.
How can I handle this in the most efficient way?
Réponse acceptée
Plus de réponses (1)
You may want to bite the bullet and implement your own queue class in matlab as I've suggested before.
Otherwise, one possibility would be to add a level of indirection to your current code. Instead of storing the objects in the queue(s), store them as values in a containers.Map and store the corresponding key in the queue. Ideally, the key would be a hash of the object but since matlab doesn't have a built-in hashing function and since containers.Map doesn't even say if it's implemented as a hashmap you could simply use continuously increasing integers. So the algorithm would go like this:
- set up:
keycount = 0;
queueelements = containers.Map;
queue = .. %your queue implementation
- to enqueue object s:
keycount = keycount + 1;
queueelements(keycount) = s;
queue.enqueue(keycount);
- to dequeue object s:
key = queue.dequeue;
s = queueelements(key);
queueelements.remove(key);
edit note however that the performance of containers.Map in matlab is ... not great.
Catégories
En savoir plus sur Desktop dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!