DISPATCH
findex.py
1 """Column-major Fortran iterator of indices across multipe dimensions.
2 
3 :copyright: Copyright 2015 Marshall Ward, see AUTHORS for details.
4 :license: Apache License, Version 2.0, see LICENSE for details.
5 """
6 
7 
8 class FIndex(object):
9  """Column-major multidimensional index iterator."""
10 
11  def __init__(self, bounds, first=None):
12  """Initialise the index iterator."""
13  self.start = [1 if b[0] is None else b[0] for b in bounds]
14  self.end = [b[1] for b in bounds]
15  self.step = [1 if b[2] is None else b[2] for b in bounds]
16 
17  self.current = self.start[:]
18 
19  # Default global starting index
20  if first is not None:
21  self.first = [min(first, s) for s in self.start]
22  else:
23  self.first = [b[0] for b in bounds]
24 
25  def __iter__(self):
26  """Declare object as iterator."""
27  return self
28 
29  def next(self):
30  """Python 2 interface to Python 3 iterator."""
31  return self.__next__()
32 
33  def __next__(self):
34  """Iterate to next contiguous index tuple."""
35  if self.end[-1] and self.current[-1] >= self.end[-1]:
36  raise StopIteration
37 
38  state = self.current[:]
39  # Allow the final index to exceed self.end[-1] as a finalisation check
40  for rank, idx in enumerate(self.current):
41  if ((not self.end[rank] or idx < (self.end[rank] - 1)) or
42  rank == (len(self.current) - 1)):
43  self.current[rank] = idx + self.step[rank]
44  break
45  else:
46  self.current[rank] = self.start[rank]
47 
48  return state
def next(self)
Definition: findex.py:29
def __init__(self, bounds, first=None)
Definition: findex.py:11
def __iter__(self)
Definition: findex.py:25
def __next__(self)
Definition: findex.py:33