# Queue¶

Salabim has a class Queue for queue handling of components. The advantage over the standard list and deque are:

- double linked, resulting in easy and efficient insertion and deletion at any place
- builtin data collection and statistics
- priority sorting

Salabim uses queues internally for resources and states as well.

Definition of a queue is simple:

```
waitingline=sim.Queue('waitingline')
```

The name of a queue can retrieved with `q.name()`

.

There is a set of methods for components to enter and leave a queue and retrieval:

Component | Queue | Description |
---|---|---|

c.enter(q) | q.add(c) or q.append(c) | c enters q at the tail |

c.enter_to_head(q) | q.add_at_head(c) | c enters q at the head |

c.enter_in_front(q, c1) | q.add_in_front_of(c, c1) | c enters q in front of c1 |

c.enter_behind(q, c1) | q.add_behind(c, c1)` | c enters q behind c1 |

c.enter_sorted(q, p) | q.add_sorted(c, p) | c enters q according to priority p |

c.leave(q) | q.remove(c) q.insert(c,i) q.pop() q.pop(i) q.head() or q[0] q.tail() or q[-1] q.index(c) q.component_with_name(n) | c leaves q insert c just before the i-th component in q removes head of q and returns it removes i-th component in q and returns it returns head of q returns tail of q returns the position of c in q returns the component with name n in q |

c.successor(q) | q.successor(c) | successor of c in q |

c.predecessor(q) | q.predecessor(c) | predecessor of c in q |

c.count(q) | q.count(c) | returns 1 if c in q, 0 otherwise |

c.queues() | returns a set with all queues where c is in | |

c.count() | returns number of queues c is in |

Queue is a standard ABC class, which means that the following methods are supported:

`len(q)`

to retrieve the length of a queue, alternatively via the level monitor with`q.length()`

`c in q`

to check whether a component is in a queue`for c in q:`

to traverse a queue (Note that it is even possible to remove and add components in the for body).`reversed(q)`

for the components in the queue in reverse order- slicing is supported, so it is possible to get the 2nd, 3rd and 4th component in a queue with
`q[1:4]`

or`q[::-1]`

for all elements in reverse order. `del q[i]`

removes the i’th component. Also slicing is supported, so e.g. to delete the last three elements from queue,`del q[-1:-4:-1]`

`q.append(c)`

is equivalent to`q.add(c)`

It is possible to do a number of operations that work on the queues:

`q.intersection(q1)`

or`q & q1`

returns a new queue with components that are both in q and q1`q.difference(q1)`

or`q - q1``

returns a new queue with components that are in q1 but not in q2`q.union(q1)`

or`q | q1`

returns a new queue with components that are in q or q1`q.symmetric_difference(q)`

or`q ^ q1`

returns a queue with components that are in q or q1, but not both`q.clear()`

empties a queue`q.copy()`

copies all components in q to a new queue. The queue q is untouched.`q.move()`

copies all components in q to a new queue. The queue q is emptied.`q.extend(q1)`

extends the q with elements in q1, that are not yet in q

Salabim keeps track of the enter time in a queue: `c.enter_time (q)`

Unless disabled explicitly, the length of the queue and length of stay of components are monitored in
`q.length`

and `q.length_of_stay`

. It is possible to obtain a number of statistics on these monitors (cf. Monitor).

With `q.print_statistics()`

the key statistics of these two monitors are printed.

E.g.:

```
-------------------------------------------- -------------- ------------ ------------ ------------
Length of waitingline duration 50000 48499.381 1500.619
mean 8.427 8.687
std.deviation 4.852 4.691
minimum 0 1
median 9 10
90% percentile 14 14
95% percentile 16 16
maximum 21 21
Length of stay in waitingline entries 4995 4933 62
mean 84.345 85.405
std.deviation 48.309 47.672
minimum 0 0.006
median 94.843 95.411
90% percentile 142.751 142.975
95% percentile 157.467 157.611
maximum 202.153 202.153
```

The arrival rate and departure rate (number of arrivals, departures per time unit) can be found with:

- q.arrival_rate()
- q.departur_rate()

With `q.print_info()`

a summary of the contents of a queue can be printed.

E.g.

```
Queue 0x20e116153c8
name=waitingline
component(s):
customer.4995 enter_time 49978.472 priority=0
customer.4996 enter_time 49991.298 priority=0
```