#
source:
orange/Orange/misc/counters.py
@
9671:a7b056375472

Revision 9671:a7b056375472, 3.5 KB checked in by anze <anze.staric@…>, 2 years ago (diff) |
---|

Line | |
---|---|

1 | """ |

2 | ======================= |

3 | Counters (``counters``) |

4 | ======================= |

5 | |

6 | .. index:: misc |

7 | .. index:: |

8 | single: misc; counters |

9 | """ |

10 | |

11 | class BooleanCounter: |

12 | def __init__(self, bits): |

13 | self.bits = bits |

14 | self.state = None |

15 | |

16 | def __iter__(self): |

17 | if self.state: |

18 | return self |

19 | else: |

20 | return BooleanCounter(self.bits) |

21 | |

22 | def next(self): |

23 | if self.state: |

24 | for bit in range(self.bits-1, -1, -1): |

25 | self.state[bit] = (self.state[bit]+1) % 2 |

26 | if self.state[bit]: |

27 | break |

28 | else: |

29 | self.state = None |

30 | else: |

31 | self.state = [0]*self.bits |

32 | |

33 | if not self.state: |

34 | raise StopIteration, "BooleanCounter: counting finished" |

35 | |

36 | return self.state |

37 | |

38 | |

39 | class LimitedCounter: |

40 | def __init__(self, limits): |

41 | self.limits = limits |

42 | self.state = None |

43 | |

44 | def __iter__(self): |

45 | if self.state: |

46 | return self |

47 | else: |

48 | return LimitedCounter(self.limits) |

49 | |

50 | def next(self): |

51 | if self.state: |

52 | i = len(self.limits)-1 |

53 | while (i>=0) and (self.state[i]==self.limits[i]-1): |

54 | self.state[i] = 0 |

55 | i -= 1 |

56 | if i==-1: |

57 | self.state = None |

58 | else: |

59 | self.state[i] += 1 |

60 | else: |

61 | self.state = [0]*len(self.limits) |

62 | |

63 | if not self.state: |

64 | raise StopIteration, "LimitedCounter: counting finished" |

65 | |

66 | return self.state |

67 | |

68 | |

69 | class MofNCounter: |

70 | def __init__(self, m, n): |

71 | if m > n: |

72 | raise TypeError, "Number of selected items exceeds the number of items" |

73 | |

74 | self.state = None |

75 | self.m = m |

76 | self.n = n |

77 | |

78 | def __iter__(self): |

79 | if self.state: |

80 | return self |

81 | else: |

82 | return MofNCounter(self.m, self.n) |

83 | |

84 | def next(self): |

85 | if self.state: |

86 | m, n, state = self.m, self.n, self.state |

87 | for place in range(m-1, -1, -1): |

88 | if state[place] + m-1-place < n-1: |

89 | state[place] += 1 |

90 | for place in range(place+1, m): |

91 | state[place] = state[place-1] + 1 |

92 | break |

93 | else: |

94 | self.state = None |

95 | raise StopIteration, "MofNCounter: counting finished" |

96 | else: |

97 | self.state = range(self.m) |

98 | |

99 | return self.state[:] |

100 | |

101 | class NondecreasingCounter: |

102 | def __init__(self, places): |

103 | self.state=None |

104 | self.subcounter=None |

105 | self.places=places |

106 | |

107 | def __iter__(self): |

108 | if self.state: |

109 | return self |

110 | else: |

111 | return NondecreasingCounter(self.places) |

112 | |

113 | def next(self): |

114 | if not self.subcounter: |

115 | self.subcounter=BooleanCounter(self.places-1) |

116 | if self.subcounter.next(): |

117 | self.state=[0] |

118 | for add_one in self.subcounter.state: |

119 | self.state.append(self.state[-1]+add_one) |

120 | else: |

121 | self.state=None |

122 | |

123 | if not self.state: |

124 | raise StopIteration, "NondecreasingCounter: counting finished" |

125 | |

126 | return self.state |

127 | |

128 | |

129 | class CanonicFuncCounter: |

130 | def __init__(self, places): |

131 | self.places = places |

132 | self.state = None |

133 | |

134 | def __iter__(self): |

135 | if self.state: |

136 | return self |

137 | else: |

138 | return CanonicFuncCounter(self.places) |

139 | |

140 | def next(self): |

141 | if self.state: |

142 | i = self.places-1 |

143 | while (i>0) and (self.state[i]==max(self.state[:i])+1): |

144 | self.state[i] = 0 |

145 | i -= 1 |

146 | if i: |

147 | self.state[i] += 1 |

148 | else: |

149 | self.state=None |

150 | else: |

151 | self.state = [0]*self.places |

152 | |

153 | if not self.state: |

154 | raise StopIteration, "CanonicFuncCounter: counting finished" |

155 | |

156 | return self.state |

**Note:**See TracBrowser for help on using the repository browser.