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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
/*
# Miro - an RSS based video player application
# Copyright (C) 2005-2008 Participatory Culture Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the OpenSSL
# library.
#
# You must obey the GNU General Public License in all respects for all of
# the code used other than OpenSSL. If you modify file(s) with this
# exception, you may extend this exception to your version of the file(s),
# but you are not obligated to do so. If you do not wish to do so, delete
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.
*/
// fasttypes.cpp implements linked list and sorted list types for
// Python using the STL class libraries.
// TODO: Finalize and document the Python classes
// I duplicate a lot of code in LinkedList and SortedList. I probably
// could get around this by creating a template class containing the
// identical functions, then extending that to make LinkedList and
// SortedList.
#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/call.hpp>
#include <boost/python/exception_translator.hpp>
#include <list>
#include <set>
#include <iterator>
#include <exception>
using namespace boost::python;
using namespace std;
// Python None
object none = object();
// Code to pass index exceptions through to Python
struct indexException : std::exception
{
char const* what() throw() { return "list index out of range"; }
};
void indexExceptionTranslator(exception const& x) {
PyErr_SetString(PyExc_IndexError, "list index out of range");
}
struct indexPopException : std::exception
{
char const* what() throw() { return "pop from empty list"; }
};
void indexPopExceptionTranslator(exception const& x) {
PyErr_SetString(PyExc_IndexError, "pop from empty list");
}
typedef std::list<object>::iterator LinkedListIterator;
typedef std::multiset<object,object>::iterator SortedListIterator;
//Create a linked list type with Pythonish semantics, but backed by STL
class LinkedList:protected std::list<object> {
private:
size_t length ;
public:
LinkedList() {
length = 0;
}
object pop() {
if (length == 0)
throw indexPopException();
else {
length--;
object temp = back();
pop_back();
return temp;
}
}
LinkedListIterator append(const object &obj) {
length++;
return insert(end(),obj);
}
LinkedListIterator prepend(const object &obj) {
length++;
return insert(begin(),obj);
}
object getItem(LinkedListIterator &it) {
if (it == end())
throw indexException();
else
return *it;
}
object getItem(size_t n) {
if (n >= length)
throw indexException();
else {
LinkedListIterator it = begin();
advance(it,n);
return *it;
}
}
void setItem(LinkedListIterator &it, object &obj) {
//Raise an exception if iterator is pointing into space
if (it == end())
throw indexException();
else
*it = obj;
}
void setItem(size_t n, object &obj) {
if (n < length) {
LinkedListIterator it = begin();
advance(it,n);
*it = obj;
} else {
throw indexException();
}
}
LinkedListIterator insertBefore(LinkedListIterator &it, object &obj) {
length++;
return insert(it, obj);
}
LinkedListIterator delItem(LinkedListIterator &it) {
if (it == end())
throw indexException();
else {
length--;
return erase(it);
}
}
LinkedListIterator delItem(size_t n) {
if (n < length) {
length--;
LinkedListIterator it = begin();
advance(it,n);
return erase(it);
} else {
throw indexException();
}
}
LinkedListIterator first() {
return begin();
}
LinkedListIterator last() {
return end();
}
size_t len() {
return length;
}
};
//Create a sorted list type with Pythonish semantics, but backed by STL
class SortedList:public std::multiset<object,object> {
private:
size_t length ;
public:
SortedList(const key_compare& comp): std::multiset<object,object>(comp),
length(0) {}
object pop() {
if (length == 0)
throw indexPopException();
else {
length--;
SortedListIterator it = end();
advance(it, -1);
object temp = *it;
erase(it);
return temp;
}
}
SortedListIterator append(const object &obj) {
length++;
return insert(end(),obj);
}
SortedListIterator prepend(const object &obj) {
length++;
return insert(begin(),obj);
}
object getItem(SortedListIterator &it) {
if (it == end())
throw indexException();
else
return *it;
}
object getItem(size_t n) {
if (n >= length)
throw indexException();
else {
SortedListIterator it = begin();
advance(it,n);
return *it;
}
}
void setItem(SortedListIterator &it, object &obj) {
//Raise an exception if iterator is pointing into space
if (it == end())
throw indexException();
else {
insert(it, obj);
erase(it);
}
}
void setItem(size_t n, object &obj) {
if (n < length) {
SortedListIterator it = begin();
advance(it,n);
insert(it, obj);
erase(it);
} else {
throw indexException();
}
}
SortedListIterator insertBefore(SortedListIterator &it, object &obj) {
length++;
return insert(it, obj);
}
void delItem(SortedListIterator &it) {
if (it == end())
throw indexException();
else {
length--;
erase(it);
}
}
void delItem(size_t n) {
if (n < length) {
length--;
SortedListIterator it = begin();
advance(it,n);
erase(it);
} else {
throw indexException();
}
}
SortedListIterator first() {
return begin();
}
SortedListIterator last() {
return end();
}
size_t len() {
return length;
}
};
// Create some function pointers so we can pass the overloaded
// functions to Python
object (LinkedList::*LLgetItem1)(size_t) = &LinkedList::getItem;
object (LinkedList::*LLgetItem2)(LinkedListIterator&) = &LinkedList::getItem;
void (LinkedList::*LLsetItem1)(size_t, object&) = &LinkedList::setItem;
void (LinkedList::*LLsetItem2)(LinkedListIterator&, object&) = &LinkedList::setItem;
LinkedListIterator (LinkedList::*LLdelItem1)(size_t) = &LinkedList::delItem;
LinkedListIterator (LinkedList::*LLdelItem2)(LinkedListIterator&) = &LinkedList::delItem;
object (SortedList::*SLgetItem1)(size_t) = &SortedList::getItem;
object (SortedList::*SLgetItem2)(SortedListIterator&) = &SortedList::getItem;
void (SortedList::*SLsetItem1)(size_t, object&) = &SortedList::setItem;
void (SortedList::*SLsetItem2)(SortedListIterator&, object&) = &SortedList::setItem;
void (SortedList::*SLdelItem1)(size_t) = &SortedList::delItem;
void (SortedList::*SLdelItem2)(SortedListIterator&) = &SortedList::delItem;
// As far as I can tell, there's no easy way to test that this
// iterator isn't past-the-end, so calling either of these on a
// past-the-end iterator is undefined and may segfault some STL
// implementations
LinkedListIterator *copyLLIter(LinkedListIterator& it) {
return new LinkedListIterator(it);
}
void incLLIter(LinkedListIterator& it) {
advance(it,1);
}
void decLLIter(LinkedListIterator& it) {
advance(it,-1);
}
SortedListIterator *copySLIter(SortedListIterator& it) {
return new SortedListIterator(it);
}
void incSLIter(SortedListIterator& it) {
advance(it,1);
}
void decSLIter(SortedListIterator& it) {
advance(it,-1);
}
BOOST_PYTHON_MODULE(fasttypes)
{
register_exception_translator<indexException>(&indexExceptionTranslator);
register_exception_translator<
indexPopException>(&indexPopExceptionTranslator);
class_<LinkedListIterator>("LinkedListIterator")
.def("copy",©LLIter,return_value_policy<manage_new_object>())
.def("forward",&incLLIter)
.def("back",&decLLIter)
.def(self == self)
.def(self != self)
;
class_<SortedListIterator>("SortedListIterator")
.def("copy",©SLIter,return_value_policy<manage_new_object>())
.def("forward",&incSLIter)
.def("back",&decSLIter)
.def(self == self)
.def(self != self)
;
class_<LinkedList>("LinkedList")
.def("__len__",&LinkedList::len)
.def("append",&LinkedList::append)
.def("firstIter",&LinkedList::first)
.def("lastIter",&LinkedList::last)
.def("prepend",&LinkedList::prepend)
.def("pop",&LinkedList::pop)
.def("__delitem__",LLdelItem1)
.def("__delitem__",LLdelItem2)
.def("remove",LLdelItem1)
.def("remove",LLdelItem2)
.def("insertBefore", &LinkedList::insertBefore)
.def("__iter__",range(&LinkedList::first,&LinkedList::last))
.def("__setitem__",LLsetItem1)
.def("__setitem__",LLsetItem2)
.def("__getitem__",LLgetItem1)
.def("__getitem__",LLgetItem2)
;
class_<SortedList>("SortedList", init<object>())
.def("__len__",&SortedList::len)
.def("append",&SortedList::append)
.def("firstIter",&SortedList::first)
.def("lastIter",&SortedList::last)
.def("prepend",&SortedList::prepend)
.def("pop",&SortedList::pop)
.def("__delitem__",SLdelItem1)
.def("__delitem__",SLdelItem2)
.def("remove",SLdelItem1)
.def("remove",SLdelItem2)
.def("insertBefore", &SortedList::insertBefore)
.def("__iter__",range(&SortedList::first,&SortedList::last))
.def("__setitem__",SLsetItem1)
.def("__setitem__",SLsetItem2)
.def("__getitem__",SLgetItem1)
.def("__getitem__",SLgetItem2)
;
}
|