| 136 | } |
| 137 | |
| 138 | static PyObject * |
| 139 | BPlusTreeIterator_next(BPlusTreeIterator *self) { |
| 140 | /* Check if the tree has been modified since iterator creation */ |
| 141 | if (self->modification_count != self->tree->modification_count) { |
| 142 | PyErr_SetString(PyExc_RuntimeError, |
| 143 | "tree changed size during iteration"); |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | if (!self->current_node) { |
| 148 | PyErr_SetNone(PyExc_StopIteration); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | /* Handle empty leaves at the beginning or during traversal */ |
| 153 | while (self->current_node && self->current_node->num_keys == 0) { |
| 154 | self->current_node = self->current_node->next; |
| 155 | } |
| 156 | |
| 157 | if (!self->current_node) { |
| 158 | PyErr_SetNone(PyExc_StopIteration); |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | if (self->current_index >= self->current_node->num_keys) { |
| 163 | /* Move to next leaf, skipping empty ones */ |
| 164 | self->current_node = self->current_node->next; |
| 165 | while (self->current_node && self->current_node->num_keys == 0) { |
| 166 | self->current_node = self->current_node->next; |
| 167 | } |
| 168 | |
| 169 | if (!self->current_node) { |
| 170 | PyErr_SetNone(PyExc_StopIteration); |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | self->current_index = 0; |
| 175 | } |
| 176 | |
| 177 | PyObject *key = node_get_key(self->current_node, self->current_index); |
| 178 | |
| 179 | if (self->include_values) { |
| 180 | PyObject *value = node_get_value(self->current_node, self->current_index); |
| 181 | PyObject *tuple = PyTuple_New(2); |
| 182 | if (!tuple) return NULL; |
| 183 | |
| 184 | Py_INCREF(key); |
| 185 | Py_INCREF(value); |
| 186 | PyTuple_SET_ITEM(tuple, 0, key); |
| 187 | PyTuple_SET_ITEM(tuple, 1, value); |
| 188 | self->current_index++; |
| 189 | return tuple; |
| 190 | } else { |
| 191 | self->current_index++; |
| 192 | Py_INCREF(key); |
| 193 | return key; |
| 194 | } |
| 195 | } |
nothing calls this directly
no test coverage detected