current position:Home>[entertainment] Python simple implementation of fixed length queue

[entertainment] Python simple implementation of fixed length queue

2022-07-19 08:21:33qq_ seven hundred and four million four hundred and forty-six t

Leisure code , The performance and function are not as good as deque

Please give priority to real use, such as collections.deque Such method libraries , Because the efficiency of what I wrote is too low , by comparison .

Just suddenly thought of similar needs , I want to straighten out my thoughts , Just wrote a

class FLenList(object):
    def __init__(self, length: int):
        assert isinstance(length, int) and length > 0, " Please give me an integer greater than zero to specify the length "
        self.len = length
        self.data = [None] * self.len
        self.hand = 0
        self.handleft = -1

    def append(self, obj):
        if self.data[self.handleft] is not None:
            self.handleft = (self.handleft + 1) % self.len
        self.data[self.hand] = obj
        self.hand = (self.hand + 1) % self.len

    def appendleft(self, obj):
        if self.data[self.hand] is not None:
            self.hand = (self.hand - 1) % self.len
        self.data[self.handleft] = obj
        self.handleft = (self.handleft - 1) % self.len

    def __len__(self):
        return self.len

    def get(self):
        ret = self.data[self.hand:]
        ret.extend(self.data[:self.hand])
        return ret

Want to add the effect when the length does not reach the specified maximum length , Don't write if you feel troublesome , I can't use it anyway

copyright notice
author[qq_ seven hundred and four million four hundred and forty-six t],Please bring the original link to reprint, thank you.
https://en.bfun.fun/2022/200/202207170713431496.html

Random recommended