PriorityStore Items Not Sorted; __lt__ Defined

24 views
Skip to first unread message

Constantine Zakkaroff

unread,
Feb 26, 2025, 6:33:18 PMFeb 26
to python-simpy
Hello Everyone,

The docs say:

Unlike Store which provides first-in first-out discipline, PriorityStore maintains items in sorted order such that the smallest items value are retrieved first from the store.

All items in a PriorityStore instance must be order-able; which is to say that items must implement __lt__().

I've implemented __lt__ such that higher numbers should come first.

However, when I start putting items into an instance of PriorityStore, after a few inserts they don't appear sorted:

not_sorted.png

And when I trace the calls to __lt__ it is only called once on any given put(item) call. Shouldn't __lt__ be called multiple times to find the right place for any given item?

Or else, am I missing something in my understanding?

Kind regards,
Constantine

Constantine Zakkaroff

unread,
Feb 27, 2025, 5:46:35 PMFeb 27
to python-simpy
To respond to my own post, I'd like to confirm that when calling get() on the PriorityStore instance, it does in fact return items in correct order, and that's when __lt__ is called a few more times, to find the right item to return. This turns out to be a clever implementation, where the cost of retrieving items in smallest-first order is split between put() and get() operations. Well done, fellas. 🥇🧠👩‍🎓❤
Reply all
Reply to author
Forward
0 new messages