1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
4 # This file is part of OpenAnno.
6 # OpenAnno is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # ###################################################
22 class stablelist(object):
23 def __init__(self
, *values
):
28 def _getFreeKey(self
):
29 if len(self
._free
) > 0:
30 return self
._free
.pop()
35 def append(self
, value
):
36 key
= self
._getFreeKey
()
37 self
._values
[key
] = value
46 return len(self
._values
)
48 def extend(self
, *values
):
52 def get(self
, key
, otherwise
= None):
53 assert(isinstance(key
, int) and key
>= 0)
54 return self
[key
] if key
in self
else otherwise
56 def has_key(self
, key
):
57 assert(isinstance(key
, int) and key
>= 0)
58 return key
in self
._values
60 def index(self
, value
):
61 for k
, v
in self
._values
.items():
68 return self
._values
.items()
71 return self
._values
.iteritems()
74 return self
._values
.iterkeys()
77 return self
._values
.itervalues()
80 return self
._values
.keys()
82 def pop(self
, index
= None):
86 assert(isinstance(key
, int) and key
>= 0)
87 #index can be 0 if no item exists... -> error
93 return (self
._last
, self
.pop(self
._last
))
95 def remove(self
, value
):
96 del self
[self
.index(value
)]
98 def setdefault(self
, key
, value
):
99 assert(isinstance(key
, int) and key
>= 0)
100 if key
not in self
._values
:
105 return self
._values
.values()
107 def __add__(self
, other
):
108 assert(self
.__class
__ == other
.__class
__)
109 ret
= stablelist(self
)
114 def __contains__(self
, value
):
116 return self
.index(value
) >= 0
120 def __delitem__(self
, key
):
121 assert(isinstance(key
, int) and key
>= 0)
122 del self
._values
[key
]
123 if key
== self
._last
:
124 for i
in xrange(key
- 1, -1, -1):
125 if i
in self
._values
:
133 self
._free
.append(key
)
135 def __eq__(self
, other
):
136 assert(self
.__class
__ == other
.__class
__)
137 return self
._values
== other
._values
139 def __getitem__(self
, key
):
140 assert(isinstance(key
, int) and key
>= 0)
141 return self
._values
[key
]
144 return hash(self
._values
)
146 def __iadd__(self
, other
):
147 assert(self
.__class
__ == other
.__class
__)
150 ret
= ret
+ [self
.append(v
)]
154 return self
._values
.itervalues()
157 return len(self
._values
)
159 def __ne__(self
, other
):
160 return not (self
== other
)
163 return repr(self
._values
)
166 return str(self
._values
)
168 def __setitem__(self
, key
, value
):
169 assert(isinstance(key
, int) and key
>= 0)
170 if key
not in self
._values
:
171 if key
in self
._free
:
172 self
._free
.remove(key
)
174 for i
in xrange(self
._last
+ 1, key
):
177 self
._values
[key
] = value