Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TreeSetTemplate.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*****************************************************************************
  2. Copyright (c) 2001, 2002 Zope Foundation and Contributors.
  3. All Rights Reserved.
  4. This software is subject to the provisions of the Zope Public License,
  5. Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  7. WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  8. WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  9. FOR A PARTICULAR PURPOSE
  10. ****************************************************************************/
  11. #include "_compat.h"
  12. #define TREESETTEMPLATE_C "$Id$\n"
  13. static PyObject *
  14. TreeSet_insert(BTree *self, PyObject *args)
  15. {
  16. PyObject *key;
  17. int i;
  18. if (!PyArg_ParseTuple(args, "O:insert", &key))
  19. return NULL;
  20. i = _BTree_set(self, key, Py_None, 1, 1);
  21. if (i < 0)
  22. return NULL;
  23. return INT_FROM_LONG(i);
  24. }
  25. /* _Set_update and _TreeSet_update are identical except for the
  26. function they call to add the element to the set.
  27. */
  28. static int
  29. _TreeSet_update(BTree *self, PyObject *seq)
  30. {
  31. int n=0, ind=0;
  32. PyObject *iter, *v;
  33. iter = PyObject_GetIter(seq);
  34. if (iter == NULL)
  35. return -1;
  36. while (1)
  37. {
  38. v = PyIter_Next(iter);
  39. if (v == NULL)
  40. {
  41. if (PyErr_Occurred())
  42. goto err;
  43. else
  44. break;
  45. }
  46. ind = _BTree_set(self, v, Py_None, 1, 1);
  47. Py_DECREF(v);
  48. if (ind < 0)
  49. goto err;
  50. else
  51. n += ind;
  52. }
  53. err:
  54. Py_DECREF(iter);
  55. if (ind < 0)
  56. return -1;
  57. return n;
  58. }
  59. static PyObject *
  60. TreeSet_update(BTree *self, PyObject *args)
  61. {
  62. PyObject *seq = NULL;
  63. int n = 0;
  64. if (!PyArg_ParseTuple(args, "|O:update", &seq))
  65. return NULL;
  66. if (seq)
  67. {
  68. n = _TreeSet_update(self, seq);
  69. if (n < 0)
  70. return NULL;
  71. }
  72. return INT_FROM_LONG(n);
  73. }
  74. static PyObject *
  75. TreeSet_remove(BTree *self, PyObject *args)
  76. {
  77. PyObject *key;
  78. UNLESS (PyArg_ParseTuple(args, "O", &key))
  79. return NULL;
  80. if (_BTree_set(self, key, NULL, 0, 1) < 0)
  81. return NULL;
  82. Py_INCREF(Py_None);
  83. return Py_None;
  84. }
  85. static PyObject *
  86. TreeSet_setstate(BTree *self, PyObject *args)
  87. {
  88. int r;
  89. if (!PyArg_ParseTuple(args,"O",&args))
  90. return NULL;
  91. PER_PREVENT_DEACTIVATION(self);
  92. r=_BTree_setstate(self, args, 1);
  93. PER_UNUSE(self);
  94. if (r < 0)
  95. return NULL;
  96. Py_INCREF(Py_None);
  97. return Py_None;
  98. }
  99. static struct PyMethodDef TreeSet_methods[] =
  100. {
  101. {"__getstate__", (PyCFunction) BTree_getstate, METH_NOARGS,
  102. "__getstate__() -> state\n\n"
  103. "Return the picklable state of the TreeSet."},
  104. {"__setstate__", (PyCFunction) TreeSet_setstate, METH_VARARGS,
  105. "__setstate__(state)\n\n"
  106. "Set the state of the TreeSet."},
  107. {"has_key", (PyCFunction) BTree_has_key, METH_O,
  108. "has_key(key)\n\n"
  109. "Return true if the TreeSet contains the given key."},
  110. {"keys", (PyCFunction) BTree_keys, METH_VARARGS | METH_KEYWORDS,
  111. "keys([min, max]) -> list of keys\n\n"
  112. "Returns the keys of the TreeSet. If min and max are supplied, only\n"
  113. "keys greater than min and less than max are returned."},
  114. {"maxKey", (PyCFunction) BTree_maxKey, METH_VARARGS,
  115. "maxKey([max]) -> key\n\n"
  116. "Return the largest key in the BTree. If max is specified, return\n"
  117. "the largest key <= max."},
  118. {"minKey", (PyCFunction) BTree_minKey, METH_VARARGS,
  119. "minKey([mi]) -> key\n\n"
  120. "Return the smallest key in the BTree. If min is specified, return\n"
  121. "the smallest key >= min."},
  122. {"clear", (PyCFunction) BTree_clear, METH_NOARGS,
  123. "clear()\n\nRemove all of the items from the BTree."},
  124. {"add", (PyCFunction)TreeSet_insert, METH_VARARGS,
  125. "add(id) -- Add an item to the set"},
  126. {"insert", (PyCFunction)TreeSet_insert, METH_VARARGS,
  127. "insert(id) -- Add an item to the set"},
  128. {"update", (PyCFunction)TreeSet_update, METH_VARARGS,
  129. "update(collection)\n\n Add the items from the given collection."},
  130. {"remove", (PyCFunction)TreeSet_remove, METH_VARARGS,
  131. "remove(id) -- Remove a key from the set"},
  132. {"_check", (PyCFunction) BTree_check, METH_NOARGS,
  133. "Perform sanity check on TreeSet, and raise exception if flawed."},
  134. #ifdef PERSISTENT
  135. {"_p_resolveConflict",
  136. (PyCFunction) BTree__p_resolveConflict, METH_VARARGS,
  137. "_p_resolveConflict() -- Reinitialize from a newly created copy"},
  138. {"_p_deactivate",
  139. (PyCFunction) BTree__p_deactivate, METH_VARARGS | METH_KEYWORDS,
  140. "_p_deactivate()\n\nReinitialize from a newly created copy."},
  141. #endif
  142. {NULL, NULL} /* sentinel */
  143. };
  144. static PyMappingMethods TreeSet_as_mapping = {
  145. (lenfunc)BTree_length, /*mp_length*/
  146. };
  147. static PySequenceMethods TreeSet_as_sequence = {
  148. (lenfunc)0, /* sq_length */
  149. (binaryfunc)0, /* sq_concat */
  150. (ssizeargfunc)0, /* sq_repeat */
  151. (ssizeargfunc)0, /* sq_item */
  152. (ssizessizeargfunc)0, /* sq_slice */
  153. (ssizeobjargproc)0, /* sq_ass_item */
  154. (ssizessizeobjargproc)0, /* sq_ass_slice */
  155. (objobjproc)BTree_contains, /* sq_contains */
  156. 0, /* sq_inplace_concat */
  157. 0, /* sq_inplace_repeat */
  158. };
  159. static int
  160. TreeSet_init(PyObject *self, PyObject *args, PyObject *kwds)
  161. {
  162. PyObject *v = NULL;
  163. if (!PyArg_ParseTuple(args, "|O:" MOD_NAME_PREFIX "TreeSet", &v))
  164. return -1;
  165. if (v)
  166. return _TreeSet_update((BTree *)self, v);
  167. else
  168. return 0;
  169. }
  170. static PyTypeObject TreeSetType =
  171. {
  172. PyVarObject_HEAD_INIT(NULL, 0)
  173. MODULE_NAME MOD_NAME_PREFIX "TreeSet", /* tp_name */
  174. sizeof(BTree), /* tp_basicsize */
  175. 0, /* tp_itemsize */
  176. (destructor)BTree_dealloc, /* tp_dealloc */
  177. 0, /* tp_print */
  178. 0, /* tp_getattr */
  179. 0, /* tp_setattr */
  180. 0, /* tp_compare */
  181. 0, /* tp_repr */
  182. &BTree_as_number_for_nonzero, /* tp_as_number */
  183. &TreeSet_as_sequence, /* tp_as_sequence */
  184. &TreeSet_as_mapping, /* tp_as_mapping */
  185. 0, /* tp_hash */
  186. 0, /* tp_call */
  187. 0, /* tp_str */
  188. 0, /* tp_getattro */
  189. 0, /* tp_setattro */
  190. 0, /* tp_as_buffer */
  191. Py_TPFLAGS_DEFAULT |
  192. Py_TPFLAGS_HAVE_GC |
  193. Py_TPFLAGS_BASETYPE, /* tp_flags */
  194. 0, /* tp_doc */
  195. (traverseproc)BTree_traverse, /* tp_traverse */
  196. (inquiry)BTree_tp_clear, /* tp_clear */
  197. 0, /* tp_richcompare */
  198. 0, /* tp_weaklistoffset */
  199. (getiterfunc)BTree_getiter, /* tp_iter */
  200. 0, /* tp_iternext */
  201. TreeSet_methods, /* tp_methods */
  202. BTree_members, /* tp_members */
  203. 0, /* tp_getset */
  204. 0, /* tp_base */
  205. 0, /* tp_dict */
  206. 0, /* tp_descr_get */
  207. 0, /* tp_descr_set */
  208. 0, /* tp_dictoffset */
  209. TreeSet_init, /* tp_init */
  210. 0, /* tp_alloc */
  211. 0, /*PyType_GenericNew,*/ /* tp_new */
  212. };