@@ -762,6 +762,91 @@ deque_len(dequeobject *deque)
762762 return Py_SIZE (deque );
763763}
764764
765+ static PyObject *
766+ deque_index (dequeobject * deque , PyObject * args )
767+ {
768+ Py_ssize_t i , start = 0 , stop = Py_SIZE (deque );
769+ PyObject * v , * item ;
770+ block * b = deque -> leftblock ;
771+ Py_ssize_t index = deque -> leftindex ;
772+ size_t start_state = deque -> state ;
773+
774+ if (!PyArg_ParseTuple (args , "O|O&O&:index" , & v ,
775+ _PyEval_SliceIndex , & start ,
776+ _PyEval_SliceIndex , & stop ))
777+ return NULL ;
778+ if (start < 0 ) {
779+ start += Py_SIZE (deque );
780+ if (start < 0 )
781+ start = 0 ;
782+ }
783+ if (stop < 0 ) {
784+ stop += Py_SIZE (deque );
785+ if (stop < 0 )
786+ stop = 0 ;
787+ }
788+
789+ for (i = 0 ; i < stop ; i ++ ) {
790+ if (i >= start ) {
791+ int cmp ;
792+ CHECK_NOT_END (b );
793+ item = b -> data [index ];
794+ cmp = PyObject_RichCompareBool (item , v , Py_EQ );
795+ if (cmp > 0 )
796+ return PyLong_FromSsize_t (i );
797+ else if (cmp < 0 )
798+ return NULL ;
799+ if (start_state != deque -> state ) {
800+ PyErr_SetString (PyExc_RuntimeError ,
801+ "deque mutated during iteration" );
802+ return NULL ;
803+ }
804+ }
805+ index ++ ;
806+ if (index == BLOCKLEN ) {
807+ b = b -> rightlink ;
808+ index = 0 ;
809+ }
810+ }
811+ PyErr_Format (PyExc_ValueError , "%R is not in deque" , v );
812+ return NULL ;
813+ }
814+
815+ PyDoc_STRVAR (index_doc ,
816+ "D.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
817+ "Raises ValueError if the value is not present." );
818+
819+ static PyObject *
820+ deque_insert (dequeobject * deque , PyObject * args )
821+ {
822+ Py_ssize_t index ;
823+ Py_ssize_t n = Py_SIZE (deque );
824+ PyObject * value ;
825+ PyObject * rv ;
826+
827+ if (!PyArg_ParseTuple (args , "nO:insert" , & index , & value ))
828+ return NULL ;
829+ if (index >= n )
830+ return deque_append (deque , value );
831+ if (index <= - n || index == 0 )
832+ return deque_appendleft (deque , value );
833+ if (_deque_rotate (deque , - index ))
834+ return NULL ;
835+ if (index < 0 )
836+ rv = deque_append (deque , value );
837+ else
838+ rv = deque_appendleft (deque , value );
839+ if (rv == NULL )
840+ return NULL ;
841+ Py_DECREF (rv );
842+ if (_deque_rotate (deque , index ))
843+ return NULL ;
844+ Py_RETURN_NONE ;
845+ }
846+
847+ PyDoc_STRVAR (insert_doc ,
848+ "D.insert(index, object) -- insert object before index" );
849+
765850static PyObject *
766851deque_remove (dequeobject * deque , PyObject * value )
767852{
@@ -1208,12 +1293,18 @@ static PyMethodDef deque_methods[] = {
12081293 METH_NOARGS , clear_doc },
12091294 {"__copy__" , (PyCFunction )deque_copy ,
12101295 METH_NOARGS , copy_doc },
1296+ {"copy" , (PyCFunction )deque_copy ,
1297+ METH_NOARGS , copy_doc },
12111298 {"count" , (PyCFunction )deque_count ,
12121299 METH_O , count_doc },
12131300 {"extend" , (PyCFunction )deque_extend ,
12141301 METH_O , extend_doc },
12151302 {"extendleft" , (PyCFunction )deque_extendleft ,
12161303 METH_O , extendleft_doc },
1304+ {"index" , (PyCFunction )deque_index ,
1305+ METH_VARARGS , index_doc },
1306+ {"insert" , (PyCFunction )deque_insert ,
1307+ METH_VARARGS , insert_doc },
12171308 {"pop" , (PyCFunction )deque_pop ,
12181309 METH_NOARGS , pop_doc },
12191310 {"popleft" , (PyCFunction )deque_popleft ,
0 commit comments