2 # Copyright 2003,2004 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING. If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
27 class seq_with_cursor (object):
28 __slots__ = [ 'items', 'index' ]
30 def __init__ (self, items, initial_index = None, initial_value = None):
31 assert len (items) > 0, "seq_with_cursor: len (items) == 0"
33 self.set_index (initial_index)
34 if initial_value is not None:
35 self.set_index_by_value(initial_value)
37 def set_index (self, initial_index):
38 if initial_index is None:
39 self.index = len (self.items) / 2
40 elif initial_index >= 0 and initial_index < len (self.items):
41 self.index = initial_index
43 raise exceptions.ValueError
45 def set_index_by_value(self, v):
47 Set index to the smallest value such that items[index] >= v.
48 If there is no such item, set index to the maximum value.
50 self.set_index(0) # side effect!
53 while cv < v and more:
54 cv, more = self.next() # side effect!
57 new_index = self.index + 1
58 if new_index < len (self.items):
59 self.index = new_index
60 return self.items[new_index], True
62 return self.items[self.index], False
65 new_index = self.index - 1
67 self.index = new_index
68 return self.items[new_index], True
70 return self.items[self.index], False
73 return self.items[self.index]
76 return self.items[:] # copy of items