Class: Xqsr3::Containers::FrequencyMap
- Inherits:
-
Object
- Object
- Xqsr3::Containers::FrequencyMap
show all
- Includes:
- Enumerable
- Defined in:
- lib/xqsr3/containers/frequency_map.rb
Constant Summary
collapse
- ByElement =
Class.new do
def self.[] *args
fm = FrequencyMap.new
args.each { |el| fm << el }
fm
end
private_class_method :new
end
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Enumerable
#collect_with_index, #detect_map, #unique
Constructor Details
Returns a new instance of FrequencyMap.
137
138
139
140
141
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 137
def initialize
@counts = {}
@count = 0
end
|
Class Method Details
.[](*args) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 74
def self.[] *args
return self.new if 0 == args.length
if 1 == args.length
arg = args[0]
case arg
when ::NilClass
return self.new
when ::Hash
fm = self.new
arg.each do |k, v|
fm.store k, v
end
return fm
when ::Array
return self.new if arg.empty?
if arg.all? { |el| ::Array === el && 2 == el.size }
return self.[](::Hash.[]([ *arg ]))
end
if (0 == (arg.size % 2)) && arg.each_with_index.select { |el, index| 1 == (index % 2) }.map(&:first).all? { |el| el.kind_of? ::Integer }
return self.[](::Hash.[](*arg))
end
raise ArgumentError, "array parameter not in an accepted form for subscript initialisation"
else
return self.[] arg.to_hash if arg.respond_to? :to_hash
raise TypeError, "given argument is neither a #{::Hash} nor an #{::Array} and does not respond to the to_hash method"
end
else
return self.[] [ *args ]
end
end
|
Instance Method Details
#<<(key) ⇒ Object
143
144
145
146
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 143
def << key
push key, 1
end
|
#==(rhs) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 148
def == rhs
case rhs
when ::NilClass
return false
when ::Hash
return rhs.size == @counts.size && rhs == @counts
when self.class
return rhs.count == self.count && rhs == @counts
else
raise TypeError, "can compare #{self.class} only to instances of #{self.class} and #{::Hash}, but #{rhs.class} given"
end
false
end
|
#[](key) ⇒ Object
164
165
166
167
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 164
def [] key
@counts[key]
end
|
#[]=(key, count) ⇒ Object
169
170
171
172
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 169
def []= key, count
store key, count
end
|
#assoc(key) ⇒ Object
174
175
176
177
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 174
def assoc key
@counts.assoc key
end
|
#clear ⇒ Object
179
180
181
182
183
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 179
def clear
@counts.clear
@count = 0
end
|
#count ⇒ Object
The total number of instances recorded
186
187
188
189
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 186
def count
@count
end
|
#default ⇒ Object
191
192
193
194
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 191
def default
@counts.default
end
|
#delete(key) ⇒ Object
196
197
198
199
200
201
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 196
def delete key
key_count = @counts.delete key
@count -= key_count if key_count
end
|
#dup ⇒ Object
203
204
205
206
207
208
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 203
def dup
fm = self.class.new
fm.merge! self
end
|
#each ⇒ Object
Also known as:
each_pair
210
211
212
213
214
215
216
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 210
def each
@counts.each do |k, v|
yield k, v
end
end
|
#each_by_frequency ⇒ Object
Enumerates each entry pair - element + frequency - in descending order of frequency
Note: this method is expensive, as it must create a new dictionary and map all entries into it in order to achieve the ordering
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 223
def each_by_frequency
tm = {}
@counts.each do |element, frequency|
if not tm.has_key? frequency
tm[frequency] = [element]
else
tm[frequency].push element
end
end
keys = tm.keys.sort.reverse
keys.each do |frequency|
elements = tm[frequency].sort
elements.each do |element|
yield element, frequency
end
end
end
|
#each_key ⇒ Object
248
249
250
251
252
253
254
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 248
def each_key
keys.each do |element|
yield element
end
end
|
#each_value ⇒ Object
258
259
260
261
262
263
264
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 258
def each_value
keys.each do |element|
yield @counts[element]
end
end
|
#empty? ⇒ Boolean
266
267
268
269
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 266
def empty?
0 == size
end
|
#eql?(rhs) ⇒ Boolean
271
272
273
274
275
276
277
278
279
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 271
def eql? rhs
case rhs
when self.class
return self == rhs
else
return false
end
end
|
#fetch(key, default = nil, &block) ⇒ Object
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 281
def fetch key, default = nil, &block
case default
when ::NilClass, ::Integer
;
else
raise TypeError, "default parameter ('#{default}') must be of type #{::Integer}, but was of type #{default.class}"
end
unless @counts.has_key? key
return default unless default.nil?
if block_given?
case block.arity
when 0
return yield
when 1
return yield key
else
raise ArgumentError, "given block must take a single parameter - #{block.arity} given"
end
end
raise KeyError, "given key '#{key}' (#{key.class}) does not exist"
end
@counts[key]
end
|
#flatten ⇒ Object
312
313
314
315
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 312
def flatten
@counts.flatten
end
|
#has_key?(key) ⇒ Boolean
Also known as:
include?, key?, member?
317
318
319
320
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 317
def has_key? key
@counts.has_key? key
end
|
#has_value?(value) ⇒ Boolean
322
323
324
325
326
327
328
329
330
331
332
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 322
def has_value? value
case value
when ::NilClass, ::Integer
;
else
raise TypeError, "parameter ('#{value}') must be of type #{::Integer}, but was of type #{value.class}"
end
@counts.has_value? value
end
|
#hash ⇒ Object
334
335
336
337
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 334
def hash
@counts.hash
end
|
#inspect ⇒ Object
Also known as:
to_s
341
342
343
344
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 341
def inspect
@counts.inspect
end
|
#key(value) ⇒ Object
def keep_if
@counts.keep_if end
351
352
353
354
355
356
357
358
359
360
361
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 351
def key value
case value
when ::NilClass, ::Integer
;
else
raise TypeError, "parameter ('#{value}') must be of type #{::Integer}, but was of type #{value.class}"
end
@counts.key value
end
|
#keys ⇒ Object
365
366
367
368
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 365
def keys
@counts.keys
end
|
#length ⇒ Object
Also known as:
size
370
371
372
373
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 370
def length
@counts.length
end
|
#merge(fm) ⇒ Object
377
378
379
380
381
382
383
384
385
386
387
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 377
def merge fm
raise TypeError, "parameter must be an instance of type #{self.class}" unless fm.instance_of? self.class
fm_new = self.class.new
fm_new.merge! self
fm_new.merge! fm
fm_new
end
|
#merge!(fm) ⇒ Object
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 389
def merge! fm
fm.each do |k, v|
if not @counts.has_key? k
@counts[k] = v
else
@counts[k] += v
end
@count += v
end
self
end
|
#push(element, count = 1) ⇒ Object
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 406
def push element, count = 1
raise TypeError, "count ('#{count}') must in an instance of #{::Integer}, but #{count.class} provided" unless count.kind_of? ::Integer
if not @counts.has_key? element
@counts[element] = count
else
@counts[element] += count
end
@count += count
self
end
|
#shift ⇒ Object
422
423
424
425
426
427
428
429
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 422
def shift
r = @counts.shift
@count -= r[1] if ::Array === r
r
end
|
#store(key, value) ⇒ Object
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 433
def store key, value
case value
when ::NilClass, ::Integer
;
else
raise TypeError, "value ('#{value}') must be of type #{::Integer}, but was of type #{value.class}"
end
key_count = @counts[key] || 0
@counts.store key, value
@count += value - key_count
end
|
#to_a ⇒ Object
449
450
451
452
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 449
def to_a
@counts.to_a
end
|
#to_h ⇒ Object
454
455
456
457
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 454
def to_h
@counts.to_h
end
|
#to_hash ⇒ Object
459
460
461
462
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 459
def to_hash
@counts.to_hash
end
|
#values ⇒ Object
466
467
468
469
|
# File 'lib/xqsr3/containers/frequency_map.rb', line 466
def values
@counts.values
end
|