File: UserQueue.py

package info (click to toggle)
egenix-mx-base 3.2.1-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,496 kB
  • sloc: ansic: 21,976; python: 17,192; sh: 137; makefile: 116
file content (63 lines) | stat: -rw-r--r-- 1,439 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
""" A pure Python Queue implementation modelled after mxQueue.

    Copyright (c) 2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
    Copyright (c) 2000-2010, eGenix.com Software GmbH; mailto:info@egenix.com
    See the documentation for further information on copyrights,
    or contact the author. All Rights Reserved.
"""

__version__ = '0.1'

class UserQueue:

    def __init__(self):

        self.queue = []
        self.headindex = 0

    def push(self,x):

        self.queue.append(x)

    def pop(self):

        headindex = self.headindex
        queue = self.queue
        x = queue[headindex]
        queue[headindex] = None # del. reference
        headindex = headindex + 1
        if headindex > 100  and headindex > len(queue) / 2:
            # compactify
            self.queue = self.queue[headindex:]
            self.headindex = 0
        else:
            self.headindex = headindex
        return x

    def not_empty(self):

        return self.headindex != len(self.queue)

    def head(self):

        return self.queue[self.headindex]

    def tail(self):

        return self.queue[-1]

    def __len__(self):

        return len(self.queue) - self.headindex

    def __repr__(self):

        l = self.queue[self.headindex:]
        l.reverse()
        return '<UserQueue [%s]>' % repr(l)[1:-1]

    def __str__(self):

        l = self.queue[self.headindex:]
        l.reverse()
        return 'q' + repr(l)