@@ -17,7 +17,6 @@ static bool is_ordered_impl(PyObject* array, size_t lower, size_t upper,
1717 PyObject* i1_item = PyObject_GetItem (array, i1_PyObject);
1818 if (i_item == Py_None || i1_item == Py_None) continue ;
1919 if ( _comp (i_item, i1_item, comp) == 1 ) {
20- printf (" %d--\n " , i);
2120 return false ;
2221 }
2322 }
@@ -60,5 +59,197 @@ static PyObject* is_ordered(PyObject* self, PyObject* args, PyObject* kwds) {
6059 return res;
6160}
6261
62+ static PyObject* linear_search (PyObject* self, PyObject* args, PyObject* kwds) {
63+ PyObject *args0 = NULL , *start = NULL , *end = NULL ;
64+ PyObject *value = NULL , *res = NULL , *u = NULL ;
65+ size_t lower, upper;
66+ args0 = PyObject_GetItem (args, PyZero);
67+ int is_DynamicOneDimensionalArray = _check_type (args0, &DynamicOneDimensionalArrayType);
68+ int is_OneDimensionalArray = _check_type (args0, &OneDimensionalArrayType);
69+ if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
70+ raise_exception_if_not_array (args0);
71+ return NULL ;
72+ }
73+ start = PyObject_GetItem (kwds, PyUnicode_FromString (" start" ));
74+ if ( start == NULL ) {
75+ PyErr_Clear ();
76+ lower = 0 ;
77+ } else {
78+ lower = PyLong_AsSize_t (start);
79+ }
80+ end = PyObject_GetItem (kwds, PyUnicode_FromString (" end" ));
81+ if ( end == NULL ) {
82+ PyErr_Clear ();
83+ upper = PyObject_Length (args0) - 1 ;
84+ } else {
85+ upper = PyLong_AsSize_t (end);
86+ }
87+ value = PyObject_GetItem (args, PyLong_FromSize_t (1 ));
88+ if ( value == NULL ) {
89+ PyErr_Format (PyExc_ValueError,
90+ " Expected Value to be not NULL" );
91+ }
92+ for (size_t i = lower; i < upper + 1 ; i++) {
93+ u = PyObject_GetItem (args0, PyLong_FromSize_t (i));
94+ int result = PyObject_RichCompareBool (u, value, Py_EQ);
95+ if ( result == -1 ) {
96+ PyErr_Format (PyExc_ValueError,
97+ " Unable to compare %s object with %s object." ,
98+ PyObject_AsString (PyObject_Repr (PyObject_Type (u))),
99+ PyObject_AsString (PyObject_Repr (PyObject_Type (value)))
100+ );
101+ } else if (result == 1 ) {
102+ if (i == 0 ) {
103+ res = PyZero;
104+ } else {
105+ res = PyLong_FromSize_t (i);
106+ }
107+ Py_INCREF (res);
108+ return res;
109+ }
110+ }
111+ res = Py_None;
112+ Py_INCREF (res);
113+ return res;
114+ }
115+
116+ static PyObject* binary_search (PyObject* self, PyObject* args, PyObject* kwds) {
117+ PyObject *args0 = NULL , *start = NULL , *end = NULL ;
118+ PyObject *value = NULL , *res = NULL , *u = NULL , *comp = NULL ;
119+ size_t lower, upper;
120+ args0 = PyObject_GetItem (args, PyZero);
121+ int is_DynamicOneDimensionalArray = _check_type (args0, &DynamicOneDimensionalArrayType);
122+ int is_OneDimensionalArray = _check_type (args0, &OneDimensionalArrayType);
123+ if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
124+ raise_exception_if_not_array (args0);
125+ return NULL ;
126+ }
127+ comp = PyObject_GetItem (kwds, PyUnicode_FromString (" comp" ));
128+ if ( comp == NULL ) {
129+ PyErr_Clear ();
130+ }
131+ start = PyObject_GetItem (kwds, PyUnicode_FromString (" start" ));
132+ if ( start == NULL ) {
133+ PyErr_Clear ();
134+ lower = 0 ;
135+ } else {
136+ lower = PyLong_AsSize_t (start);
137+ }
138+ end = PyObject_GetItem (kwds, PyUnicode_FromString (" end" ));
139+ if ( end == NULL ) {
140+ PyErr_Clear ();
141+ upper = PyObject_Length (args0) - 1 ;
142+ } else {
143+ upper = PyLong_AsSize_t (end);
144+ }
145+ value = PyObject_GetItem (args, PyLong_FromSize_t (1 ));
146+ if ( value == NULL ) {
147+ PyErr_Format (PyExc_ValueError,
148+ " Expected Value to be not NULL" );
149+ }
150+
151+ int left = lower, right = upper;
152+ while (left <= right)
153+ {
154+ int middle = left/2 + right/2 + left % 2 * right % 2 ;
155+ u = PyObject_GetItem (args0, PyLong_FromSize_t (middle));
156+ int result = PyObject_RichCompareBool (u, value, Py_EQ);
157+ if ( result == -1 ) {
158+ PyErr_Format (PyExc_ValueError,
159+ " Unable to compare %s object with %s object." ,
160+ PyObject_AsString (PyObject_Repr (PyObject_Type (u))),
161+ PyObject_AsString (PyObject_Repr (PyObject_Type (value)))
162+ );
163+ } else if (result == 1 ) {
164+ if (middle == 0 ) {
165+ res = PyZero;
166+ } else {
167+ res = PyLong_FromSize_t (middle);
168+ }
169+ Py_INCREF (res);
170+ return res;
171+ }
172+ if ( _comp (u, value, comp) == 1 ) {
173+ left = middle + 1 ;
174+ } else {
175+ right = middle - 1 ;
176+ }
177+ }
178+ res = Py_None;
179+ Py_INCREF (res);
180+ return res;
181+ }
182+
183+ static PyObject* jump_search (PyObject* self, PyObject* args, PyObject* kwds) {
184+ PyObject *args0 = NULL , *start = NULL , *end = NULL ;
185+ PyObject *value = NULL , *res = NULL , *u = NULL , *comp = NULL ;
186+ size_t lower, upper;
187+ args0 = PyObject_GetItem (args, PyZero);
188+ int is_DynamicOneDimensionalArray = _check_type (args0, &DynamicOneDimensionalArrayType);
189+ int is_OneDimensionalArray = _check_type (args0, &OneDimensionalArrayType);
190+ if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
191+ raise_exception_if_not_array (args0);
192+ return NULL ;
193+ }
194+ comp = PyObject_GetItem (kwds, PyUnicode_FromString (" comp" ));
195+ if ( comp == NULL ) {
196+ PyErr_Clear ();
197+ }
198+ start = PyObject_GetItem (kwds, PyUnicode_FromString (" start" ));
199+ if ( start == NULL ) {
200+ PyErr_Clear ();
201+ lower = 0 ;
202+ } else {
203+ lower = PyLong_AsSize_t (start);
204+ }
205+ end = PyObject_GetItem (kwds, PyUnicode_FromString (" end" ));
206+ if ( end == NULL ) {
207+ PyErr_Clear ();
208+ upper = PyObject_Length (args0) - 1 ;
209+ } else {
210+ upper = PyLong_AsSize_t (end);
211+ }
212+ value = PyObject_GetItem (args, PyLong_FromSize_t (1 ));
213+ if ( value == NULL ) {
214+ PyErr_Format (PyExc_ValueError,
215+ " Expected Value to be not NULL" );
216+ }
217+ int step = int (sqrt (double (upper - lower + 1 )));
218+ int prev = lower;
219+ int element_pos = prev;
220+ u = PyObject_GetItem (args0, PyLong_FromSize_t (element_pos));
221+ while (element_pos <= upper && _comp (u, value, comp) == 1 ) {
222+ prev = element_pos;
223+ element_pos += step;
224+ if (element_pos > upper) {
225+ break ;
226+ }
227+ u = PyObject_GetItem (args0, PyLong_FromSize_t (element_pos));
228+ }
229+
230+ while (prev <= upper) {
231+ u = PyObject_GetItem (args0, PyLong_FromSize_t (prev));
232+ int result = PyObject_RichCompareBool (u, value, Py_EQ);
233+ if ( result == -1 ) {
234+ PyErr_Format (PyExc_ValueError,
235+ " Unable to compare %s object with %s object." ,
236+ PyObject_AsString (PyObject_Repr (PyObject_Type (u))),
237+ PyObject_AsString (PyObject_Repr (PyObject_Type (value)))
238+ );
239+ } else if (result == 1 ) {
240+ if (prev == 0 ) {
241+ res = PyZero;
242+ } else {
243+ res = PyLong_FromSize_t (prev);
244+ }
245+ Py_INCREF (res);
246+ return res;
247+ }
248+ prev += 1 ;
249+ }
250+ res = Py_None;
251+ Py_INCREF (res);
252+ return res;
253+ }
63254
64255#endif
0 commit comments