| 125 | cdef class DenseGraphOutNeighborIterator: |
| 126 | """ |
| 127 | Iterator over all ``v`` such that ``(u, v)`` is an arc of a DenseGraph. |
| 128 | |
| 129 | Note: This class duplicates the code in out_neighbors_unsafe in order to |
| 130 | have a fast iterator available at Python level. |
| 131 | |
| 132 | EXAMPLES:: |
| 133 | |
| 134 | sage: from sage.graphs.base.dense_graph import DenseGraph |
| 135 | sage: from sage.graphs.base.dense_graph import DenseGraphOutNeighborIterator |
| 136 | |
| 137 | sage: G = DenseGraph(nverts=10, extra_vertices=10) |
| 138 | sage: G.add_vertex(0) |
| 139 | 0 |
| 140 | sage: it = DenseGraphOutNeighborIterator(G, 0) |
| 141 | sage: it.next() |
| 142 | Traceback (most recent call last): |
| 143 | ... |
| 144 | StopIteration |
| 145 | |
| 146 | sage: G.add_arc(5,8) |
| 147 | sage: G.add_arc(5,2) |
| 148 | sage: it = DenseGraphOutNeighborIterator(G, 5) |
| 149 | sage: list(it) |
| 150 | [2, 8] |
| 151 | |
| 152 | sage: it = DenseGraphOutNeighborIterator(G, 12) |
| 153 | Traceback (most recent call last): |
| 154 | ... |
| 155 | LookupError: Vertex (12) is not a vertex of the graph. |
| 156 | """ |
| 157 | def __init__(self, G, u): |
| 158 | r""" |
| 159 | INPUT: |
| 160 | |
| 161 | - G -- a DenseGraph |
| 162 | |
| 163 | - u -- a non-negative integer |
| 164 | """ |
| 165 | cdef int uu = <int> u |
| 166 | |
| 167 | self.G = G |
| 168 | self.G.check_vertex(uu) |
| 169 | |
| 170 | self.place = uu * self.G.num_longs |
| 171 | self.i = -1 |
| 172 | self.v = 0 |
| 173 | self.word = 0 |
| 174 | |
| 175 | def __iter__(self): |
| 176 | return self |
| 177 | |
| 178 | def __next__(self): |
| 179 | r""" |
| 180 | Return the next item or raise StopIteration |
| 181 | """ |
| 182 | if self.i == self.G.num_longs: |
| 183 | raise StopIteration |
| 184 | |
| 185 | if not self.word: |
| 186 | self.i += 1 |
| 187 | if self.i == self.G.num_longs: |
| 188 | raise StopIteration |
| 189 | self.data = self.G.edges[self.place + self.i] |
| 190 | self.word = 1 |
| 191 | |
| 192 | else: |
| 193 | self.word = self.word << 1 |
| 194 | self.v += 1 |
| 195 | |
| 196 | while not (self.word & self.data): |
| 197 | if not self.word: |
| 198 | self.i += 1 |
| 199 | if self.i == self.G.num_longs: |
| 200 | raise StopIteration |
| 201 | self.data = self.G.edges[self.place + self.i] |
| 202 | self.word = 1 |
| 203 | |
| 204 | else: |
| 205 | self.word = self.word << 1 |
| 206 | self.v += 1 |
| 207 | |
| 208 | return self.v |
| 209 | |