summaryrefslogtreecommitdiffstats
path: root/flist.py
blob: 2035221363e5f1af55bccfafcdb7c9f60fd9303c (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#/***************************************************************************
#                             flist.h
#                             -----------
#    begin                : sam nov 2 2002
#    copyright            : (C) 1992-2004 by Fabian Padilla
#    email                : fp@bridgethink.com
# ***************************************************************************/

# /***************************************************************************
#  *                                                                         *
#  *   This program is free software; you can redistribute it and/or modify  *
#  *   it under the terms of the Foundation Public License as published by   *
#  *   bridgethink sarl; either version 2 of the License, or                 *
#  *   (at your option) any later version.                                   *
#  *                                                                         *
#  ***************************************************************************/

#
#  from C++ to python by Jeremy Zurcher <jeremy@asynk.ch>
#

"""/** List that manage a table of pointer.

  function : maintain a list of pointer.

  description : pointers (or other 32bit values) can be
  - added
  - returned by giving the idx in the list
  - returned by giving the idx in the list and removed from them
  - older returned and removed from the list, by FisrtInfirstOut order

  *@author Fabian Padilla
  */"""

__all__ = ["Flist"]

from returncodes import RET_NOTEXIST

class Flist:
    """ This class is just an other wrapper around the python builtin list.
    It dosen't use exceptions 'cause exceptions suck ! """
    def __init__(self):
        self.L = []

    def __str__(self):
        ret = ''
        for I in self.L:
            ret += str(I.__class__)+' : '+I.getString()+'  - '
        return 'Flist - '+ret

    def add(self, el):
        self.L.append(el)
        #return RET_OK                # TzurTcH - no need

    def get(self, idx):
        if idx<0 or idx>=len(self.L):
            return RET_NOTEXIST
        return self.L[idx]

    def remove(self, idx):
        if idx<0 or idx>=len(self.L):
            return RET_NOTEXIST
        tmp,self.L = self.L[idx], self.L[:idx]+self.L[idx+1:]
        return tmp

    def removeFifo(self):
        if len(self.L) == 0:
            return RET_NOTEXIST
        tmp, self.L = self.L[0], self.L[1:]
        return tmp

    def removeStack(self):
        if len(self.L) == 0:
            return RET_NOTEXIST
        return self.L.pop()

    def getCount(self):
        return len(self.L)


if __name__ == '__main__':

    import unittest


    class FlistTestCase(unittest.TestCase):

        def test1AddGetRemove(self):
            list = Flist()
            list.add("A")
            list.add("B")
            list.add("C")
            list.add("D")
            list.add("E")
            self.assertEquals(list.get(0),"A")
            self.assertEquals(list.get(1),"B")
            self.assertEquals(list.get(2),"C")
            self.assertEquals(list.get(3),"D")
            self.assertEquals(list.get(4),"E")
            self.assertEquals(list.remove(5),RET_NOTEXIST)
            self.assertEquals(list.remove(0),"A")
            self.assertEquals(list.remove(1),"C")
            self.assertEquals(list.remove(2),"E")
            self.assertEquals(list.remove(2),RET_NOTEXIST)
            self.assertEquals(list.remove(1),"D")
            self.assertEquals(list.remove(0),"B")
            self.assertEquals(list.remove(0),RET_NOTEXIST)

        def test2RemoveFifo(self):
            list = Flist()
            list.add("A")
            list.add("B")
            list.add("C")
            list.add("D")
            list.add("E")
            self.assertEquals(list.removeFifo(),"A")
            self.assertEquals(list.removeFifo(),"B")
            self.assertEquals(list.removeFifo(),"C")
            self.assertEquals(list.removeFifo(),"D")
            self.assertEquals(list.removeFifo(),"E")
            self.assertEquals(list.removeFifo(),RET_NOTEXIST)

        def test3RemoveStack(self):
            list = Flist()
            list.add("A")
            list.add("B")
            list.add("C")
            list.add("D")
            list.add("E")
            self.assertEquals(list.removeStack(),"E")
            self.assertEquals(list.removeStack(),"D")
            self.assertEquals(list.removeStack(),"C")
            self.assertEquals(list.removeStack(),"B")
            self.assertEquals(list.removeStack(),"A")
            self.assertEquals(list.removeStack(),RET_NOTEXIST)

        def test4GetCount(self):
            list = Flist()
            list.add("A")
            list.add("B")
            list.add("C")
            list.add("D")
            list.add("E")
            self.assertEquals(list.getCount(),5)

    unittest.main()