# Priority queue in pytorch

is there any implementation of priority queues available in pytorch?

This can possibly work

``````class priorityQ_torch(object):
"""Priority Q implelmentation in PyTorch

Args:
object ([torch.Tensor]): [The Queue to work on]
"""

def __init__(self, val):
self.q = torch.tensor([[val, 0]])
# self.top = self.q
# self.isEmpty = self.q.shape == 0

def push(self, x):
"""Pushes x to q based on weightvalue in x. Maintains ascending order

Args:
q ([torch.Tensor]): [The tensor queue arranged in ascending order of weight value]
x ([torch.Tensor]): [[index, weight] tensor to be inserted]

Returns:
[torch.Tensor]: [The queue tensor after correct insertion]
"""
if type(x) == np.ndarray:
x = torch.tensor(x)
if self.isEmpty():
self.q = x
self.q = torch.unsqueeze(self.q, dim=0)
return
idx = torch.searchsorted(self.q.T, x)
print(idx)
self.q = torch.vstack([self.q[0:idx], x, self.q[idx:]]).contiguous()

def top(self):
"""Returns the top element from the queue

Returns:
[torch.Tensor]: [top element]
"""
return self.q

def pop(self):
"""pops(without return) the highest priority element with the minimum weight

Args:
q ([torch.Tensor]): [The tensor queue arranged in ascending order of weight value]

Returns:
[torch.Tensor]: [highest priority element]
"""
if self.isEmpty():
print("Can Not Pop")
self.q = self.q[1:]

def isEmpty(self):
"""Checks is the priority queue is empty

Args:
q ([torch.Tensor]): [The tensor queue arranged in ascending order of weight value]

Returns:
[Bool] : [Returns True is empty]
"""
return self.q.shape == 0
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.5170459,"math_prob":0.9097495,"size":1535,"snap":"2022-05-2022-21","text_gpt3_token_len":399,"char_repetition_ratio":0.1587198,"word_repetition_ratio":0.14215687,"special_character_ratio":0.29771987,"punctuation_ratio":0.23076923,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99802494,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-23T03:12:36Z\",\"WARC-Record-ID\":\"<urn:uuid:393434b2-4435-4fb4-bf38-043cb22ad23b>\",\"Content-Length\":\"14599\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4e782bbb-fe7d-42b1-9464-e36f593a2061>\",\"WARC-Concurrent-To\":\"<urn:uuid:d1cb5fd8-8ca8-4ff9-8a5f-b0d4386e57f6>\",\"WARC-IP-Address\":\"159.203.145.104\",\"WARC-Target-URI\":\"https://discuss.pytorch.org/t/priority-queue-in-pytorch/40410\",\"WARC-Payload-Digest\":\"sha1:TLAMYIC7VBK74SNRQOCLKVMNFODE2RWL\",\"WARC-Block-Digest\":\"sha1:KTGBZRJ3CIVOPCQUDPPM4JW7PXY4HTSE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662552994.41_warc_CC-MAIN-20220523011006-20220523041006-00345.warc.gz\"}"}