setfu 3.0.1 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 20de7e408d7b32f92cb46c72fb9aaaa5b3ecb21b
4
- data.tar.gz: 0b3d9d1b0fd55b4251498ec87b2187e6236e1587
2
+ SHA256:
3
+ metadata.gz: 6835cd9233c1ac203c4fafe4e768e9f8bb9516cc0117c3bf09af283e00701501
4
+ data.tar.gz: 1e7ada3f3e973a852920d6c7f63370be29850cc1ebd734e02de078071a764520
5
5
  SHA512:
6
- metadata.gz: 4f22cc1c2ebe5ccc90043b716e6737606b758fefa3fcb5204e61cd7a0eca28413b0a71d9dd99ed9c2ba023e15bba19f1ae0e26238aeacbedc2d502e64718453e
7
- data.tar.gz: fa7fcfb09c6916c30e32c400af291c0f9cf92e2b4614e044436d48128c427f7f127025a69d9cd6f8ffde9c98517b62a1c7945e1ae81442e7a33a6420b93fbe14
6
+ metadata.gz: 21568cf5048c7c855958a91f2c092936e1e4033a902b5bf18d3d6f5ff69914559205ce6539d5e2d1cebe355bd4fda58e21e60d7338315869c8572d53f8d5035a
7
+ data.tar.gz: 90ca1f585914e530cf9d05241c9d6cc3aeb6b49fcce4777b803512d7c6df767d7e5c24cf387d76d5e1fa18dccd8fcd0d30bfb96444846b4120a6d324f01d9197
data/README.md CHANGED
@@ -131,6 +131,7 @@ aaa < aaa # false
131
131
  BitSet defines array access operators `:[]` and `:[]=` as it it were an array of bits.
132
132
  If you pass an Integer type, you will get `true` (if member of set) or `false`.
133
133
  Anything else will return a BitSet instance. You can also set or clear members of the set as well.
134
+ Note that more than one value can be set or cleared.
134
135
  See the example below:
135
136
 
136
137
  ```ruby
@@ -176,6 +177,29 @@ Recursion is used to unravel the Array.
176
177
 
177
178
  These methods remove elements from the BitSet instance if they exist.
178
179
 
180
+ #### bitset.neighbor(no_vars,direction)
181
+
182
+ This is used in a Karnaugh map to find adjacent neighboring cell coordinates.
183
+ The current bitset instance contains the initial vector in the table.
184
+ It is best to use the BitSet.int(initial_coordinate) as the constructor.
185
+ Note that for each variable used in the map, the self-same number determines the total possible number of neighbors.
186
+ The direction has values `0` to `(no_vars-1)`.
187
+ For a 5-variable system, we can have left, right, up, down, or over for possible connecting cells.
188
+ This method is extensively used in the upcoming `gatefu` gem which deals with logic systems.
189
+
190
+
191
+ #### bitset.double, bitset.double!
192
+
193
+ This method doubles the entropy and copies the original bottom to the top half.
194
+ This doubles the number of elements.
195
+ The bang version self-modifies while the non-bang version creates a new instance.
196
+
197
+ #### bitset.swap(position1, position2), bitset.swap!(position1, position2)
198
+
199
+ This method looks up the values of position1, and position2 and stores them in swapping fashion.
200
+ The bang version self modifies, while the non-bang version returns a new instance (without modification of original).
201
+
202
+
179
203
  #### bitset.include?(item)
180
204
 
181
205
  Returns true if p is a subset. Parameter `p` can be:
@@ -189,6 +213,10 @@ Returns true if there are no elements in the BitSet instance.
189
213
 
190
214
  This removes all elements from the BitSet instance making it empty. Self-modifies.
191
215
 
216
+ #### bitset.set_all!
217
+
218
+ This sets all possible elements of the set (up to the entropy).
219
+
192
220
  #### bitset.dup
193
221
 
194
222
  This create and returns a duplicate copy of the BitSet instance.
@@ -215,7 +243,7 @@ This routine is computationally slow as each bit must be examined.
215
243
  #### bitset.to_ra(int=true, th=3)
216
244
 
217
245
  This returns as array filled with either integers or ranges of integers. The threshold parameter decides the minimum range size
218
- that will be used. The minimum value is 2. The `int` parameter when set to false will return string characters and string ranges.
246
+ that will be used. The minimum allowed value for `th` is 2. The `int` parameter when set to false will return string characters and string ranges.
219
247
  This routine is computationally slow as each bit must be examined.
220
248
 
221
249
  #### bitset.set_bits!(n)
@@ -230,7 +258,7 @@ This returns the current entropy number of the set. You can also set this proper
230
258
  like the universe entropy can only increase.
231
259
 
232
260
  #### bitset.entropy_2n!
233
- This sets the entropy number to the nearest 2**n that will contain the highest set element.
261
+ This sets the entropy number to the nearest `2**n` that will contain the highest set element.
234
262
 
235
263
  #### bitset.first(int=true), bitset.first!(int=true)
236
264
  This returns the first element of the set, or nil if the set is empty.
@@ -354,7 +382,47 @@ This bisects the set into two roughly equally sized sets and returns an array of
354
382
  The first set contains the lower elements, while the second set contains the higher elements.
355
383
  The bang version retaines the lower elements are returns the higher elements.
356
384
 
357
- ###Class Methods
385
+ ### Utility Methods on standard objects
386
+
387
+ #### array.pop2
388
+ This is similar to `array.pop(2)` except that the returning array is reversed.
389
+
390
+ #### array.stack_swap
391
+ This reverses the data values of the last two elements of the array.
392
+
393
+ #### array.stack_rot
394
+ This rotates the last three values of the array as seen the the example below:
395
+ ```ruby
396
+ array = [1,2,3,4,5]
397
+ array.stack_rot # [1, 2, 5, 3, 4]
398
+ ```
399
+
400
+ #### array.peek(pos=0)
401
+ This indexes the array from right to left.
402
+
403
+ #### array.ascending?
404
+ Returns true if elements of the array are sorted in ascending order, have two or more elements, and first element not equal to last element.
405
+
406
+ #### array.descending?
407
+ Returns true if elements of the array are sorted in descending order, have two or more elements, and first element not equal to last element.
408
+
409
+ #### array.sorted?
410
+ Returns true if elements of the array are sorted in either ascending or descending order, and have one or more elements.
411
+
412
+ ### BitSet Class Methods
413
+
414
+ #### BitSet.generate_pattern(num_bits, zero_first_flag, num_zeros, num_ones)
415
+ This generates BitSet instance with a pattern of contiguous ones(elements of the set) and zeros(voids of the set)
416
+ starting from position zero and stopping on the num_bits parameter. See the example below:
417
+
418
+ ```ruby
419
+ bs = BitSet.generate_pattern(19, false, 3, 5)
420
+ bs.to_ra # [0..4, 8..12, 16..18]
421
+ ```
422
+
423
+ #### BitSet.int(int, ent=nil)
424
+ This constructor sets the internal Integer instance to `int` and if provided, sets the internal entropy to `ent`.
425
+ If not provided, the entropy will be automatically calculated.
358
426
 
359
427
  ### BitSet.fill(num_elements, start=0)
360
428
  This constructs a BitSet instance with contiguous values set to the parameter `num_elements`.
@@ -556,7 +624,7 @@ This utility method converts elements of the array to BitSets instances.
556
624
  During the reduction process some items in the array get tagged. This routine clears such tagging.
557
625
  This is a utility method called by the `#reduce_tuples` method.
558
626
 
559
- ###case subsumption operator ===
627
+ ### case subsumption operator ===
560
628
  The `===` operator has special meaning in ruby. It is known as the case subsumption operator.
561
629
  It is used during the `case when` statements as its comparison means.
562
630
  The left side of the operator's type is the owner and defines what it means to have the right side passed to it.
@@ -646,6 +714,14 @@ the right opperand takes precedence however.
646
714
 
647
715
  ## Revision History
648
716
 
717
+ ### Version 3.1.0
718
+
719
+ * added BitSet.int constructor
720
+ * added BitSet.generate_pattern constructor
721
+ * added BitSet methods: neighbor, double, double!, swap, swap! set_all!
722
+ * added Array stack methods: pop2, stack_swap, stack_rot, peek
723
+ * added Array utility methods: ascending?, descending?, sorted?
724
+
649
725
  ### Version 3.0.1
650
726
 
651
727
  * updated @@TRANS_HASH to include missing translation: (422,640)
@@ -15,6 +15,188 @@ class Object
15
15
  better_install_as!(:delete_many_at, :setfu_delete_many_at)
16
16
  end
17
17
 
18
+ ###
19
+ ### Version 3.2.0 additions
20
+ ###
21
+
22
+ # ::TODO:: add document
23
+ class Array
24
+ def stack_swap
25
+ a = pop
26
+ b = pop
27
+ push a
28
+ push b
29
+ end
30
+ def stack_rot
31
+ a = pop
32
+ b = pop
33
+ c = pop
34
+ push a
35
+ push c
36
+ push b
37
+ end
38
+ def pop2
39
+ a = pop
40
+ b = pop
41
+ return [a,b]
42
+ end
43
+ def peek(n=0)
44
+ return self[count-n-1]
45
+ end
46
+ end
47
+
48
+ # ::TODO:: add document
49
+ class Array # additions to be used by other future gems:
50
+ def ascending?
51
+ return false unless count >=2
52
+ cnt = self.length - 1
53
+ cnt.times do |ii|
54
+ return false unless self[ii+1] >= self[ii]
55
+ end
56
+ return false if first==last # ascending means something has to get bigger
57
+ true
58
+ end
59
+ def descending?
60
+ return false unless count >=2
61
+ cnt = self.length - 1
62
+ cnt.times do |ii|
63
+ return false unless self[ii+1] <= self[ii]
64
+ end
65
+ return false if first==last # descending means something has to get smaller
66
+ true
67
+ end
68
+ def sorted?
69
+ return false unless count >=1
70
+ return true if (self & self).count == 1 # [4,4,4,4] is sorted, but not ascending or desending
71
+ return true if ascending?
72
+ return true if descending?
73
+ false
74
+ end
75
+ end
76
+
77
+ # ::TODO:: add document
78
+ class BitSet
79
+ def swap!(pos1,pos2)
80
+ self[pos1],self[pos2] = self[pos2], self[pos1]
81
+ self
82
+ end
83
+ def swap(pos1,pos2)
84
+ bs = dup
85
+ bs[pos1],bs[pos2] = self[pos2], self[pos1]
86
+ bs
87
+ end
88
+ end
89
+
90
+ # ::TODO:: add document
91
+ # this routine takes a linear binary table and finds a coordinate neighbor.
92
+ # not to be confused with finding a neighbor in a Logic Map.
93
+ # of a given map
94
+ class BitSet
95
+ def neighbor(nvars, dir)
96
+ raise "too many neighbors for #{nvars} variables" if dir >= nvars
97
+ raise "dir must be positive" if dir.negative?
98
+ rtn = dup
99
+
100
+ if 1==nvars # two squares
101
+ rtn[dir]=!self[dir]
102
+ elsif 2==nvars # a 2x2 box
103
+ if 0==dir
104
+ rtn[0]=!self[0]
105
+ else
106
+ rtn[1]=!self[1]
107
+ end
108
+ end
109
+ # now programmatically do the remaining pattern
110
+ if nvars.even?
111
+ if dir.even? # up if dir==0 and nvars==4 ... left if dir==2 and nvars==4
112
+ rtn[dir]=self[dir+1]
113
+ rtn[dir+1]=!self[dir]
114
+ else # down if dir ==1 and nvars==4 ... right if dir==3 and nvars==4
115
+ rtn[dir-1]=!self[dir] # modified on dec 21, 2019 9:34AM
116
+ rtn[dir]=self[dir-1]
117
+ end
118
+ else # odd .......... compare with neighbor5 dir [0,1,2,3,4]
119
+ if dir==(nvars-1) # dir==4
120
+ rtn[dir]=!self[dir]
121
+ elsif dir.even? # 0,2 validated
122
+ rtn[dir]=self[dir+1]
123
+ rtn[dir+1]=!self[dir]
124
+ else # 1,3 validated
125
+ rtn[dir-1]=!self[dir]
126
+ rtn[dir]=self[dir-1]
127
+ end
128
+ end
129
+ return rtn
130
+ end
131
+ end
132
+
133
+ # this one will get added to setfu soon.
134
+ class BitSet
135
+ def self.generate_pattern(nbits, flag_zero_first, zeros, ones )
136
+ bs = BitSet.new
137
+ bs.entropy = nbits
138
+ return bs if ones<=0
139
+ return bs | [0..(nbits-1)] if zeros<=0
140
+ k=0
141
+ hash = {:zero => zeros, :one => ones}
142
+ if flag_zero_first
143
+ ptr = :zero
144
+ val = false
145
+ else
146
+ ptr = :one
147
+ val = true
148
+ end
149
+ nbits.times do |ii|
150
+ if (k >= hash[ptr])
151
+ k = 1
152
+ val = !val
153
+ ptr = (ptr == :zero) ? :one : :zero
154
+ else
155
+ k += 1
156
+ end
157
+ bs[ii] = val
158
+ end
159
+ return bs
160
+ end
161
+ end
162
+
163
+ # add this one to setfu ... also create non bang version
164
+ class BitSet
165
+ def double!
166
+ @bits |= @bits << @entropy
167
+ @entropy <<= 1
168
+ self
169
+ end
170
+ def double
171
+ dup.double!
172
+ end
173
+ end
174
+
175
+ # this should be added to gem ...
176
+ class BitSet
177
+ def self.int(int,ent=nil)
178
+ bs = BitSet.new
179
+ bs.set_bits!(int)
180
+ if ent.nil?
181
+ bs.recalculate_entropy!
182
+ else
183
+ bs.entropy=ent
184
+ end
185
+ bs
186
+ end
187
+ end
188
+
189
+ class BitSet
190
+ def set_all! # set all possible bits ... make sure this does not already exist ... if it does, then use the proper name
191
+ bits = (2 ** entropy) - 1
192
+ set_bits! bits
193
+ end
194
+ end
195
+
196
+ ###
197
+ ### Version 3.1.0 and older
198
+ ###
199
+
18
200
  module Setfu
19
201
  def self.bset_elements(ary)
20
202
  ary.count.times do |ii|
@@ -1,3 +1,3 @@
1
1
  module Setfu
2
- VERSION = "3.0.1"
2
+ VERSION = "3.1.0"
3
3
  end
@@ -26,7 +26,7 @@ When characters are used, the ordinate value sets the element bit of the interna
26
26
  spec.add_runtime_dependency 'pred', '~> 0.1', '>= 0.1.2'
27
27
  spec.add_runtime_dependency 'betterobject', '~> 1.2', '>= 1.2.0'
28
28
  spec.add_runtime_dependency 'yieldhelper', '~> 0.1', '>= 0.1.0'
29
- spec.add_runtime_dependency 'primitive_wrapper', '~> 2', '>= 2.0.0'
29
+ spec.add_runtime_dependency 'primitive_wrapper', '~> 2', '>= 2.3.0'
30
30
 
31
31
  spec.add_development_dependency "bundler", "~> 1.3"
32
32
  spec.add_development_dependency 'rake', '~> 10.1', '>= 10.1.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: setfu
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Colvin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-26 00:00:00.000000000 Z
11
+ date: 2020-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pred
@@ -79,7 +79,7 @@ dependencies:
79
79
  version: '2'
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: 2.0.0
82
+ version: 2.3.0
83
83
  type: :runtime
84
84
  prerelease: false
85
85
  version_requirements: !ruby/object:Gem::Requirement
@@ -89,7 +89,7 @@ dependencies:
89
89
  version: '2'
90
90
  - - ">="
91
91
  - !ruby/object:Gem::Version
92
- version: 2.0.0
92
+ version: 2.3.0
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: bundler
95
95
  requirement: !ruby/object:Gem::Requirement
@@ -206,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
206
  version: '0'
207
207
  requirements: []
208
208
  rubyforge_project:
209
- rubygems_version: 2.5.1
209
+ rubygems_version: 2.7.6
210
210
  signing_key:
211
211
  specification_version: 4
212
212
  summary: Set class