comparison Modules/arraymodule.c @ 13772:eea51275acbf legacy-trunk

added count, extend, index, pop and remove to arraymodule
author Peter Schneider-Kamp <nowonder@nowonder.de>
date Mon, 31 Jul 2000 20:52:21 +0000
parents 8a063423e0b7
children b6d93dba120e
comparison
equal deleted inserted replaced
13771:2cffcd71d5e4 13772:eea51275acbf
672 Py_INCREF(Py_None); 672 Py_INCREF(Py_None);
673 return Py_None; 673 return Py_None;
674 } 674 }
675 675
676 static PyObject * 676 static PyObject *
677 array_count(arrayobject *self, PyObject *args)
678 {
679 int count = 0;
680 int i;
681 PyObject *v;
682
683 if (!PyArg_ParseTuple(args, "O:count", &v))
684 return NULL;
685 for (i = 0; i < self->ob_size; i++) {
686 PyObject *selfi = getarrayitem((PyObject *)self, i);
687 if (PyObject_Compare(selfi, v) == 0)
688 count++;
689 Py_DECREF(selfi);
690 if (PyErr_Occurred())
691 return NULL;
692 }
693 return PyInt_FromLong((long)count);
694 }
695
696 static char count_doc [] =
697 "count (x)\n\
698 \n\
699 Return number of occurences of x in the array.";
700
701 static PyObject *
702 array_index(arrayobject *self, PyObject *args)
703 {
704 int i;
705 PyObject *v;
706
707 if (!PyArg_ParseTuple(args, "O:index", &v))
708 return NULL;
709 for (i = 0; i < self->ob_size; i++) {
710 PyObject *selfi = getarrayitem((PyObject *)self, i);
711 if (PyObject_Compare(selfi, v) == 0) {
712 Py_DECREF(selfi);
713 return PyInt_FromLong((long)i);
714 }
715 Py_DECREF(selfi);
716 if (PyErr_Occurred())
717 return NULL;
718 }
719 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
720 return NULL;
721 }
722
723 static char index_doc [] =
724 "index (x)\n\
725 \n\
726 Return index of first occurence of x in the array.";
727
728 static PyObject *
729 array_remove(arrayobject *self, PyObject *args)
730 {
731 int i;
732 PyObject *v;
733
734 if (!PyArg_ParseTuple(args, "O:remove", &v))
735 return NULL;
736 for (i = 0; i < self->ob_size; i++) {
737 PyObject *selfi = getarrayitem((PyObject *)self,i);
738 if (PyObject_Compare(selfi, v) == 0) {
739 Py_DECREF(selfi);
740 if (array_ass_slice(self, i, i+1,
741 (PyObject *)NULL) != 0)
742 return NULL;
743 Py_INCREF(Py_None);
744 return Py_None;
745 }
746 Py_DECREF(selfi);
747 if (PyErr_Occurred())
748 return NULL;
749 }
750 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
751 return NULL;
752 }
753
754 static char remove_doc [] =
755 "remove (x)\n\
756 \n\
757 Remove the first occurence of x in the array.";
758
759 static PyObject *
760 array_pop(arrayobject *self, PyObject *args)
761 {
762 int i = -1;
763 PyObject *v;
764 if (!PyArg_ParseTuple(args, "|i:pop", &i))
765 return NULL;
766 if (self->ob_size == 0) {
767 /* Special-case most common failure cause */
768 PyErr_SetString(PyExc_IndexError, "pop from empty array");
769 return NULL;
770 }
771 if (i < 0)
772 i += self->ob_size;
773 if (i < 0 || i >= self->ob_size) {
774 PyErr_SetString(PyExc_IndexError, "pop index out of range");
775 return NULL;
776 }
777 v = getarrayitem((PyObject *)self,i);
778 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
779 Py_DECREF(v);
780 return NULL;
781 }
782 return v;
783 }
784
785 static char pop_doc [] =
786 "pop ([i])\n\
787 \n\
788 Return the i-th element and delete it from the array. i defaults to -1.";
789
790 static PyObject *
791 array_extend(self, args)
792 arrayobject *self;
793 PyObject *args;
794 {
795 int size;
796 PyObject *bb;
797 arrayobject *np;
798
799 if (!PyArg_ParseTuple(args, "O:extend", &bb))
800 return NULL;
801
802 if (!is_arrayobject(bb)) {
803 PyErr_Format(PyExc_TypeError,
804 "can only append array (not \"%.200s\") to array",
805 bb->ob_type->tp_name);
806 return NULL;
807 }
808 #define b ((arrayobject *)bb)
809 if (self->ob_descr != b->ob_descr) {
810 PyErr_SetString(PyExc_TypeError,
811 "can only append arrays of same kind");
812 return NULL;
813 }
814 size = self->ob_size + b->ob_size;
815 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
816 if (self->ob_item == NULL) {
817 PyObject_Del(self);
818 return PyErr_NoMemory();
819 }
820 memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize,
821 b->ob_item, b->ob_size*b->ob_descr->itemsize);
822 self->ob_size = size;
823 Py_INCREF(Py_None);
824 return Py_None;
825 #undef b
826 }
827
828 static char extend_doc [] =
829 "extend(array)\n\
830 \n\
831 Append array items to the end of the array.";
832
833 static PyObject *
677 array_insert(arrayobject *self, PyObject *args) 834 array_insert(arrayobject *self, PyObject *args)
678 { 835 {
679 int i; 836 int i;
680 PyObject *v; 837 PyObject *v;
681 if (!PyArg_Parse(args, "(iO)", &i, &v)) 838 if (!PyArg_Parse(args, "(iO)", &i, &v))
813 970
814 static char reverse_doc [] = 971 static char reverse_doc [] =
815 "reverse()\n\ 972 "reverse()\n\
816 \n\ 973 \n\
817 Reverse the order of the items in the array."; 974 Reverse the order of the items in the array.";
818
819 /* The following routines were adapted from listobject.c but not converted.
820 To make them work you will have to work! */
821
822 #if 0
823 static PyObject *
824 array_index(arrayobject *self, PyObject *args)
825 {
826 int i;
827
828 if (args == NULL) {
829 PyErr_BadArgument();
830 return NULL;
831 }
832 for (i = 0; i < self->ob_size; i++) {
833 if (PyObject_Compare(self->ob_item[i], args) == 0)
834 return PyInt_FromLong((long)i);
835 /* XXX PyErr_Occurred */
836 }
837 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
838 return NULL;
839 }
840 #endif
841
842 #if 0
843 static PyObject *
844 array_count(arrayobject *self, PyObject *args)
845 {
846 int count = 0;
847 int i;
848
849 if (args == NULL) {
850 PyErr_BadArgument();
851 return NULL;
852 }
853 for (i = 0; i < self->ob_size; i++) {
854 if (PyObject_Compare(self->ob_item[i], args) == 0)
855 count++;
856 /* XXX PyErr_Occurred */
857 }
858 return PyInt_FromLong((long)count);
859 }
860 #endif
861
862 #if 0
863 static PyObject *
864 array_remove(arrayobject *self, PyObject *args)
865 {
866 int i;
867
868 if (args == NULL) {
869 PyErr_BadArgument();
870 return NULL;
871 }
872 for (i = 0; i < self->ob_size; i++) {
873 if (PyObject_Compare(self->ob_item[i], args) == 0) {
874 if (array_ass_slice(self, i, i+1,
875 (PyObject *)NULL) != 0)
876 return NULL;
877 Py_INCREF(Py_None);
878 return Py_None;
879 }
880 /* XXX PyErr_Occurred */
881 }
882 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
883 return NULL;
884 }
885 #endif
886 975
887 static PyObject * 976 static PyObject *
888 array_fromfile(arrayobject *self, PyObject *args) 977 array_fromfile(arrayobject *self, PyObject *args)
889 { 978 {
890 PyObject *f; 979 PyObject *f;
1093 PyMethodDef array_methods[] = { 1182 PyMethodDef array_methods[] = {
1094 {"append", (PyCFunction)array_append, 0, append_doc}, 1183 {"append", (PyCFunction)array_append, 0, append_doc},
1095 {"buffer_info", (PyCFunction)array_buffer_info, 0, buffer_info_doc}, 1184 {"buffer_info", (PyCFunction)array_buffer_info, 0, buffer_info_doc},
1096 {"byteswap", (PyCFunction)array_byteswap, METH_VARARGS, 1185 {"byteswap", (PyCFunction)array_byteswap, METH_VARARGS,
1097 byteswap_doc}, 1186 byteswap_doc},
1098 /* {"count", (method)array_count},*/ 1187 {"count", (PyCFunction)array_count, 1, count_doc},
1188 {"extend", (PyCFunction)array_extend, 1, extend_doc},
1099 {"fromfile", (PyCFunction)array_fromfile, 0, fromfile_doc}, 1189 {"fromfile", (PyCFunction)array_fromfile, 0, fromfile_doc},
1100 {"fromlist", (PyCFunction)array_fromlist, 0, fromlist_doc}, 1190 {"fromlist", (PyCFunction)array_fromlist, 0, fromlist_doc},
1101 {"fromstring", (PyCFunction)array_fromstring, 0, fromstring_doc}, 1191 {"fromstring", (PyCFunction)array_fromstring, 0, fromstring_doc},
1102 /* {"index", (method)array_index},*/ 1192 {"index", (PyCFunction)array_index, 1, index_doc},
1103 {"insert", (PyCFunction)array_insert, 0, insert_doc}, 1193 {"insert", (PyCFunction)array_insert, 0, insert_doc},
1194 {"pop", (PyCFunction)array_pop, 1, pop_doc},
1104 {"read", (PyCFunction)array_fromfile, 0, fromfile_doc}, 1195 {"read", (PyCFunction)array_fromfile, 0, fromfile_doc},
1105 /* {"remove", (method)array_remove},*/ 1196 {"remove", (PyCFunction)array_remove, 1, remove_doc},
1106 {"reverse", (PyCFunction)array_reverse, 0, reverse_doc}, 1197 {"reverse", (PyCFunction)array_reverse, 0, reverse_doc},
1107 /* {"sort", (method)array_sort},*/ 1198 /* {"sort", (PyCFunction)array_sort, 0, sort_doc},*/
1108 {"tofile", (PyCFunction)array_tofile, 0, tofile_doc}, 1199 {"tofile", (PyCFunction)array_tofile, 0, tofile_doc},
1109 {"tolist", (PyCFunction)array_tolist, 0, tolist_doc}, 1200 {"tolist", (PyCFunction)array_tolist, 0, tolist_doc},
1110 {"tostring", (PyCFunction)array_tostring, 0, tostring_doc}, 1201 {"tostring", (PyCFunction)array_tostring, 0, tostring_doc},
1111 {"write", (PyCFunction)array_tofile, 0, tofile_doc}, 1202 {"write", (PyCFunction)array_tofile, 0, tofile_doc},
1112 {NULL, NULL} /* sentinel */ 1203 {NULL, NULL} /* sentinel */
1362 Methods:\n\ 1453 Methods:\n\
1363 \n\ 1454 \n\
1364 append() -- append a new item to the end of the array\n\ 1455 append() -- append a new item to the end of the array\n\
1365 buffer_info() -- return information giving the current memory info\n\ 1456 buffer_info() -- return information giving the current memory info\n\
1366 byteswap() -- byteswap all the items of the array\n\ 1457 byteswap() -- byteswap all the items of the array\n\
1458 count() -- return number of occurences of an object\n\
1459 extend() -- extend array by appending array elements\n\
1367 fromfile() -- read items from a file object\n\ 1460 fromfile() -- read items from a file object\n\
1368 fromlist() -- append items from the list\n\ 1461 fromlist() -- append items from the list\n\
1369 fromstring() -- append items from the string\n\ 1462 fromstring() -- append items from the string\n\
1463 index() -- return index of first occurence of an object\n\
1370 insert() -- insert a new item into the array at a provided position\n\ 1464 insert() -- insert a new item into the array at a provided position\n\
1465 pop() -- remove and return item (default last)\n\
1371 read() -- DEPRECATED, use fromfile()\n\ 1466 read() -- DEPRECATED, use fromfile()\n\
1467 remove() -- remove first occurence of an object\n\
1372 reverse() -- reverse the order of the items in the array\n\ 1468 reverse() -- reverse the order of the items in the array\n\
1373 tofile() -- write all items to a file object\n\ 1469 tofile() -- write all items to a file object\n\
1374 tolist() -- return the array converted to an ordinary list\n\ 1470 tolist() -- return the array converted to an ordinary list\n\
1375 tostring() -- return the array converted to a string\n\ 1471 tostring() -- return the array converted to a string\n\
1376 write() -- DEPRECATED, use tofile()\n\ 1472 write() -- DEPRECATED, use tofile()\n\