Queue#

class Queue(*args, **kwargs)#

Contains the public fields of a [Queue][glib-Double-ended-Queues].

Methods#

class Queue
clear() None#

Removes all the elements in queue. If queue elements contain dynamically-allocated memory, they should be freed first.

Added in version 2.14.

clear_full(free_func: Callable[[None], None] | None = None) None#

Convenience method, which frees all the memory used by a Queue, and calls the provided free_func on each item in the Queue.

Added in version 2.60.

Parameters:

free_func – the function to be called to free memory allocated

foreach(func: Callable[[...], None], *user_data: Any) None#

Calls func for each element in the queue passing user_data to the function.

It is safe for func to remove the element from queue, but it must not modify any part of the queue after that element.

Added in version 2.4.

Parameters:
  • func – the function to call for each element’s data

  • user_data – user data to pass to func

free() None#

Frees the memory allocated for the Queue. Only call this function if queue was created with new(). If queue elements contain dynamically-allocated memory, they should be freed first.

If queue elements contain dynamically-allocated memory, you should either use free_full() or free them manually first.

free_full(free_func: Callable[[None], None]) None#

Convenience method, which frees all the memory used by a Queue, and calls the specified destroy function on every element’s data.

free_func should not modify the queue (eg, by removing the freed element from it).

Added in version 2.32.

Parameters:

free_func – the function to be called to free each element’s data

get_length() int#

Returns the number of items in queue.

Added in version 2.4.

index(data: None) int#

Returns the position of the first element in queue which contains data.

Added in version 2.4.

Parameters:

data – the data to find

init() None#

A statically-allocated Queue must be initialized with this function before it can be used. Alternatively you can initialize it with %G_QUEUE_INIT. It is not necessary to initialize queues created with new().

Added in version 2.14.

insert_sorted(data: None, func: Callable[[...], int], *user_data: Any) None#

Inserts data into queue using func to determine the new position.

Added in version 2.4.

Parameters:
  • data – the data to insert

  • func – the CompareDataFunc used to compare elements in the queue. It is called with two elements of the queue and user_data. It should return 0 if the elements are equal, a negative value if the first element comes before the second, and a positive value if the second element comes before the first.

  • user_data – user data passed to func

is_empty() bool#

Returns True if the queue is empty.

peek_head() None#

Returns the first element of the queue.

peek_nth(n: int) None#

Returns the n’th element of queue.

Added in version 2.4.

Parameters:

n – the position of the element

peek_tail() None#

Returns the last element of the queue.

pop_head() None#

Removes the first element of the queue and returns its data.

pop_nth(n: int) None#

Removes the n’th element of queue and returns its data.

Added in version 2.4.

Parameters:

n – the position of the element

pop_tail() None#

Removes the last element of the queue and returns its data.

push_head(data: None) None#

Adds a new element at the head of the queue.

Parameters:

data – the data for the new element.

push_nth(data: None, n: int) None#

Inserts a new element into queue at the given position.

Added in version 2.4.

Parameters:
  • data – the data for the new element

  • n – the position to insert the new element. If n is negative or larger than the number of elements in the queue, the element is added to the end of the queue.

push_tail(data: None) None#

Adds a new element at the tail of the queue.

Parameters:

data – the data for the new element

remove(data: None) bool#

Removes the first element in queue that contains data.

Added in version 2.4.

Parameters:

data – the data to remove

Returns:

0 if the file was successfully removed, -1 if an error occurred

remove_all(data: None) int#

Remove all elements whose data equals data from queue.

Added in version 2.4.

Parameters:

data – the data to remove

reverse() None#

Reverses the order of the items in queue.

Added in version 2.4.

sort(compare_func: Callable[[...], int], *user_data: Any) None#

Sorts queue using compare_func.

Added in version 2.4.

Parameters:
  • compare_func – the CompareDataFunc used to sort queue. This function is passed two elements of the queue and should return 0 if they are equal, a negative value if the first comes before the second, and a positive value if the second comes before the first.

  • user_data – user data passed to compare_func

Fields#

class Queue
head#

A pointer to the first element of the queue

length#

The number of elements in the queue

tail#

A pointer to the last element of the queue