facets 2.6.0 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/HISTORY.rdoc +331 -35
- data/MANIFEST +685 -826
- data/{doc/guide/notes.rd → NOTES} +0 -0
- data/README.rdoc +73 -25
- data/Rakefile +245 -2
- data/TODO +5 -0
- data/demo/hook.rd +47 -0
- data/demo/scenario_require.rd +9 -0
- data/doc/README.more +24 -6
- data/doc/manual/about.rb +47 -0
- data/doc/manual/annotations.rdoc +60 -0
- data/doc/manual/associations.rdoc +55 -0
- data/doc/manual/blockups.rdoc +101 -0
- data/doc/manual/capsule.rdoc +34 -0
- data/doc/manual/command.rdoc +177 -0
- data/doc/manual/core.rdoc +37 -0
- data/doc/manual/faq.rdoc +32 -0
- data/doc/manual/typecast.html +112 -0
- data/lib/core/facets.rb +359 -11
- data/lib/core/facets/array/combination.rb +3 -3
- data/lib/core/facets/array/index.rb +4 -1
- data/lib/core/facets/array/permutation.rb +2 -2
- data/lib/core/facets/array/product.rb +1 -1
- data/lib/core/facets/binding/eval.rb +1 -1
- data/lib/core/facets/denumerable.rb +76 -0
- data/lib/core/facets/duplicable.rb +34 -0
- data/lib/core/facets/enumerable/count.rb +10 -4
- data/lib/core/facets/enumerable/defer.rb +77 -0
- data/lib/core/facets/enumerable/each_by.rb +1 -1
- data/lib/core/facets/enumerable/every.rb +35 -0
- data/lib/{more/facets/elementwise.rb → core/facets/enumerable/ewise.rb} +0 -0
- data/lib/core/facets/enumerable/filter.rb +25 -0
- data/lib/core/facets/enumerable/group_by.rb +1 -1
- data/lib/core/facets/enumerable/none.rb +3 -2
- data/lib/core/facets/enumerable/one.rb +3 -2
- data/lib/core/facets/enumerable/per.rb +61 -0
- data/lib/core/facets/exception/raised.rb +14 -0
- data/lib/core/facets/integer/odd.rb +5 -1
- data/lib/core/facets/kernel/__dir__.rb +13 -3
- data/lib/core/facets/kernel/__here__.rb +14 -0
- data/lib/core/facets/kernel/__method__.rb +9 -3
- data/lib/core/facets/kernel/ask.rb +1 -0
- data/lib/core/facets/kernel/equate.rb +13 -0
- data/lib/core/facets/kernel/extension.rb +9 -0
- data/lib/core/facets/kernel/identical.rb +4 -0
- data/lib/core/facets/kernel/instance_exec.rb +2 -1
- data/lib/core/facets/kernel/method.rb +49 -0
- data/lib/core/facets/kernel/object_send.rb +2 -2
- data/lib/core/facets/kernel/{state.rb → object_state.rb} +23 -12
- data/lib/core/facets/kernel/require_all.rb +6 -1
- data/lib/core/facets/kernel/require_local.rb +8 -1
- data/lib/core/facets/kernel/require_relative.rb +52 -0
- data/lib/core/facets/kernel/source_location.rb +13 -0
- data/lib/core/facets/kernel/tap.rb +13 -6
- data/lib/core/facets/module/attr_setter.rb +57 -0
- data/lib/core/facets/module/instance_method.rb +24 -0
- data/lib/core/facets/module/module_load.rb +60 -44
- data/lib/core/facets/module/module_require.rb +1 -0
- data/lib/core/facets/nilclass/to_f.rb +1 -1
- data/lib/core/facets/objectspace/op_fetch.rb +3 -0
- data/lib/core/facets/proc/curry.rb +4 -3
- data/lib/core/facets/string/bytes.rb +10 -4
- data/lib/core/facets/string/camelcase.rb +6 -5
- data/lib/core/facets/string/chars.rb +5 -1
- data/lib/core/facets/string/each_char.rb +1 -1
- data/lib/core/facets/string/each_word.rb +1 -1
- data/lib/core/facets/string/lines.rb +11 -4
- data/lib/core/facets/string/start_with.rb +9 -2
- data/lib/core/facets/string/unfold.rb +27 -0
- data/lib/core/facets/symbol/succ.rb +3 -3
- data/lib/core/facets/symbol/thrown.rb +20 -0
- data/lib/core/facets/symbol/to_proc.rb +3 -2
- data/lib/core/facets/time/to_time.rb +1 -1
- data/lib/core/facets/to_hash.rb +41 -100
- data/lib/core/facets/unboundmethod/name.rb +20 -23
- data/lib/more/facets/ansicode.rb +1 -10
- data/lib/more/facets/autoarray.rb +3 -31
- data/lib/more/facets/basicobject.rb +73 -0
- data/lib/more/facets/blankslate.rb +2 -66
- data/lib/{lore → more}/facets/cgi.rb +0 -0
- data/lib/more/facets/class_extend.rb +1 -0
- data/lib/{lore → more}/facets/continuation.rb +0 -0
- data/lib/{lore → more}/facets/date.rb +3 -3
- data/lib/more/facets/enumargs.rb +192 -0
- data/lib/more/facets/enumerablepass.rb +2 -216
- data/lib/more/facets/enumerator.rb +62 -0
- data/lib/more/facets/{equatable.rb → equitable.rb} +11 -11
- data/lib/more/facets/expirable.rb +13 -41
- data/lib/{lore → more}/facets/fileutils.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/head.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/safe_ln.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/slice.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/tail.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/wc.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/whereis.rb +0 -0
- data/lib/{lore → more}/facets/fileutils/which.rb +0 -0
- data/lib/{lore → more}/facets/getoptlong.rb +0 -0
- data/lib/more/facets/hook.rb +2 -29
- data/lib/more/facets/inheritor.rb +2 -2
- data/lib/more/facets/instance_eval.rb +50 -0
- data/lib/more/facets/instance_function.rb +78 -0
- data/lib/more/facets/main.rb +20 -15
- data/lib/more/facets/memoize.rb +1 -113
- data/lib/more/facets/module/attr.rb +83 -0
- data/lib/more/facets/module/attr_tester.rb +44 -0
- data/lib/more/facets/module/attr_toggler.rb +59 -0
- data/lib/more/facets/module/attr_validator.rb +34 -0
- data/lib/more/facets/{class_extension.rb → module/class_extend.rb} +21 -13
- data/lib/more/facets/once.rb +59 -0
- data/lib/more/facets/openmodule.rb +1 -0
- data/lib/more/facets/orderedhash.rb +1 -33
- data/lib/{lore → more}/facets/ostruct.rb +0 -0
- data/lib/more/facets/ostructable.rb +1 -4
- data/lib/more/facets/partial.rb +18 -16
- data/lib/{lore → more}/facets/pathname.rb +0 -0
- data/lib/more/facets/preinitialize.rb +157 -0
- data/lib/{lore → more}/facets/rbconfig.rb +0 -0
- data/lib/more/facets/recorder.rb +1 -2
- data/lib/{lore → more}/facets/set.rb +0 -0
- data/lib/{lore → more}/facets/shellwords.rb +0 -0
- data/lib/{lore → more}/facets/uri.rb +0 -0
- data/lib/{lore → more}/facets/yaml.rb +0 -0
- data/lib/{lore → more}/facets/zlib.rb +0 -0
- data/meta/loadpath +0 -1
- data/meta/sitemap +4 -0
- data/meta/version +1 -1
- data/test/core/enumerable/test_count.rb +1 -1
- data/test/{more/test_filter.rb → core/enumerable/test_defer.rb} +24 -22
- data/test/{more/test_elementor.rb → core/enumerable/test_every.rb} +2 -15
- data/test/core/enumerable/test_ewise.rb +23 -0
- data/test/core/enumerable/test_per.rb +18 -0
- data/test/core/enumerable/test_take.rb +13 -0
- data/test/core/kernel/test_deepcopy.rb +1 -1
- data/test/{more/test_1stclassmethod.rb → core/kernel/test_method.rb} +2 -7
- data/test/core/kernel/test_tap.rb +1 -1
- data/test/core/proc/test_curry.rb +11 -0
- data/test/core/string/test_bytes.rb +1 -1
- data/test/core/string/test_camelcase.rb +23 -6
- data/test/core/string/test_lines.rb +1 -1
- data/test/core/string/test_unfold.rb +14 -0
- data/test/{more → core}/test_blank.rb +0 -0
- data/test/{more → core}/test_boolean.rb +0 -0
- data/test/{more → core}/test_functor.rb +0 -0
- data/test/{lore → more}/test_basicobject.rb +0 -0
- data/test/more/{test_class_extension.rb → test_class_extend.rb} +6 -6
- data/test/{lore → more}/test_continuation.rb +0 -0
- data/test/{lore → more}/test_date.rb +0 -0
- data/test/more/{test_enumerablepass.rb → test_enumargs.rb} +2 -4
- data/test/more/{test_equatable.rb → test_equitable.rb} +2 -2
- data/test/more/{test_instantise.rb → test_instance_function.rb} +3 -2
- data/test/more/test_memoize.rb +1 -1
- data/test/{lore → more}/test_ostruct.rb +0 -0
- metadata +865 -1016
- data/RELEASE +0 -38
- data/doc/README.lore +0 -51
- data/doc/log/basic_stats/index.html +0 -39
- data/doc/log/changelog.html +0 -648
- data/doc/log/changelog.txt +0 -217
- data/doc/log/stats/index.html +0 -39
- data/doc/log/testlog.txt +0 -278
- data/doc/notes/CHANGES +0 -2529
- data/doc/rdoc/lore/classes/Array.html +0 -176
- data/doc/rdoc/lore/classes/CGI.html +0 -191
- data/doc/rdoc/lore/classes/Config.html +0 -135
- data/doc/rdoc/lore/classes/Continuation.html +0 -113
- data/doc/rdoc/lore/classes/Date.html +0 -631
- data/doc/rdoc/lore/classes/DateTime.html +0 -583
- data/doc/rdoc/lore/classes/Enumerable.html +0 -89
- data/doc/rdoc/lore/classes/Enumerable/Enumerator.html +0 -147
- data/doc/rdoc/lore/classes/File.html +0 -128
- data/doc/rdoc/lore/classes/FileUtils.html +0 -434
- data/doc/rdoc/lore/classes/GetoptLong.html +0 -118
- data/doc/rdoc/lore/classes/GetoptLong/DSL.html +0 -208
- data/doc/rdoc/lore/classes/Kernel.html +0 -135
- data/doc/rdoc/lore/classes/Logger.html +0 -229
- data/doc/rdoc/lore/classes/Logger/Ansicolor.html +0 -277
- data/doc/rdoc/lore/classes/Logger/LogDevice.html +0 -121
- data/doc/rdoc/lore/classes/NilClass.html +0 -119
- data/doc/rdoc/lore/classes/OpenStruct.html +0 -432
- data/doc/rdoc/lore/classes/Pathname.html +0 -353
- data/doc/rdoc/lore/classes/Set.html +0 -117
- data/doc/rdoc/lore/classes/Shellwords.html +0 -111
- data/doc/rdoc/lore/classes/String.html +0 -140
- data/doc/rdoc/lore/classes/Time.html +0 -154
- data/doc/rdoc/lore/classes/URI.html +0 -454
- data/doc/rdoc/lore/classes/URI/Hash.html +0 -105
- data/doc/rdoc/lore/classes/URI/Kernel.html +0 -122
- data/doc/rdoc/lore/classes/Zlib.html +0 -188
- data/doc/rdoc/lore/created.rid +0 -1
- data/doc/rdoc/lore/files/README.html +0 -286
- data/doc/rdoc/lore/files/doc/README_lore.html +0 -155
- data/doc/rdoc/lore/files/lib/lore/facets/basicobject_rb.html +0 -118
- data/doc/rdoc/lore/files/lib/lore/facets/cgi_rb.html +0 -111
- data/doc/rdoc/lore/files/lib/lore/facets/continuation_rb.html +0 -147
- data/doc/rdoc/lore/files/lib/lore/facets/date_rb.html +0 -97
- data/doc/rdoc/lore/files/lib/lore/facets/enumerator_rb.html +0 -111
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/head_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/safe_ln_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/slice_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/tail_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/wc_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/whereis_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils/which_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/fileutils_rb.html +0 -131
- data/doc/rdoc/lore/files/lib/lore/facets/getoptlong_rb.html +0 -135
- data/doc/rdoc/lore/files/lib/lore/facets/logger_rb.html +0 -142
- data/doc/rdoc/lore/files/lib/lore/facets/ostruct_rb.html +0 -135
- data/doc/rdoc/lore/files/lib/lore/facets/pathname_rb.html +0 -145
- data/doc/rdoc/lore/files/lib/lore/facets/rbconfig_rb.html +0 -124
- data/doc/rdoc/lore/files/lib/lore/facets/set_rb.html +0 -96
- data/doc/rdoc/lore/files/lib/lore/facets/shellwords_rb.html +0 -124
- data/doc/rdoc/lore/files/lib/lore/facets/uri_rb.html +0 -125
- data/doc/rdoc/lore/files/lib/lore/facets/yaml_rb.html +0 -146
- data/doc/rdoc/lore/files/lib/lore/facets/zlib_rb.html +0 -97
- data/doc/rdoc/lore/fr_class_index.html +0 -73
- data/doc/rdoc/lore/fr_file_index.html +0 -71
- data/doc/rdoc/lore/fr_method_index.html +0 -177
- data/doc/rdoc/lore/index.html +0 -26
- data/doc/rdoc/lore/rdoc-style.css +0 -177
- data/doc/release-notes/RELEASE-2.0.5 +0 -8
- data/doc/release-notes/RELEASE-2.1.0 +0 -9
- data/doc/release-notes/RELEASE-2.1.1 +0 -5
- data/doc/release-notes/RELEASE-2.1.2 +0 -6
- data/doc/release-notes/RELEASE-2.1.3 +0 -5
- data/doc/release-notes/RELEASE-2.2.0 +0 -14
- data/doc/release-notes/RELEASE-2.2.1 +0 -4
- data/doc/release-notes/RELEASE-2.3.0 +0 -6
- data/doc/release-notes/RELEASE-2.4.0 +0 -70
- data/doc/release-notes/RELEASE-2.4.1 +0 -8
- data/doc/release-notes/RELEASE-2.4.2 +0 -4
- data/doc/release-notes/RELEASE-2.4.3 +0 -78
- data/doc/release-notes/RELEASE-2.4.4 +0 -38
- data/doc/release-notes/RELEASE-2.4.5 +0 -37
- data/doc/release-notes/RELEASE-2.5.0 +0 -83
- data/lib/core/facets/kernel/instance.rb +0 -19
- data/lib/lore/facets/basicobject.rb +0 -14
- data/lib/lore/facets/enumerator.rb +0 -67
- data/lib/lore/facets/logger.rb +0 -291
- data/lib/more/facets/1stclassmethod.rb +0 -140
- data/lib/more/facets/advisable.rb +0 -162
- data/lib/more/facets/association.rb +0 -210
- data/lib/more/facets/attr.rb +0 -209
- data/lib/more/facets/basex.rb +0 -37
- data/lib/more/facets/bbcode.rb +0 -397
- data/lib/more/facets/bicrypt.rb +0 -265
- data/lib/more/facets/binreadable.rb +0 -221
- data/lib/more/facets/censor.rb +0 -97
- data/lib/more/facets/classmethods.rb +0 -199
- data/lib/more/facets/consoleutils.rb +0 -99
- data/lib/more/facets/crypt.rb +0 -166
- data/lib/more/facets/dependency.rb +0 -151
- data/lib/more/facets/downloader.rb +0 -281
- data/lib/more/facets/duplicable.rb +0 -43
- data/lib/more/facets/elementor.rb +0 -133
- data/lib/more/facets/filter.rb +0 -121
- data/lib/more/facets/heap.rb +0 -22
- data/lib/more/facets/infinity.rb +0 -193
- data/lib/more/facets/ini.rb +0 -264
- data/lib/more/facets/instantise.rb +0 -1
- data/lib/more/facets/instantize.rb +0 -95
- data/lib/more/facets/interval.rb +0 -282
- data/lib/more/facets/iteration.rb +0 -65
- data/lib/more/facets/linkedlist.rb +0 -222
- data/lib/more/facets/lrucache.rb +0 -157
- data/lib/more/facets/matcher.rb +0 -140
- data/lib/more/facets/memoizer.rb +0 -74
- data/lib/more/facets/minitar.rb +0 -1063
- data/lib/more/facets/nackclass.rb +0 -41
- data/lib/more/facets/net/smtp_tls.rb +0 -131
- data/lib/more/facets/nilstatus.rb +0 -48
- data/lib/more/facets/overload.rb +0 -94
- data/lib/more/facets/paramix.rb +0 -202
- data/lib/more/facets/pool.rb +0 -91
- data/lib/more/facets/pqueue.rb +0 -449
- data/lib/more/facets/pry.rb +0 -32
- data/lib/more/facets/reflection.rb +0 -145
- data/lib/more/facets/semaphore.rb +0 -92
- data/lib/more/facets/settings.rb +0 -248
- data/lib/more/facets/snapshot.rb +0 -209
- data/lib/more/facets/sparse_array.rb +0 -809
- data/lib/more/facets/string/mask.rb +0 -278
- data/lib/more/facets/string/obfuscate.rb +0 -65
- data/lib/more/facets/string/stylize.rb +0 -169
- data/lib/more/facets/string/words.rb +0 -167
- data/lib/more/facets/syncarray.rb +0 -114
- data/lib/more/facets/synchash.rb +0 -157
- data/lib/more/facets/typecast.rb +0 -261
- data/lib/more/facets/uninheritable.rb +0 -50
- data/lib/more/facets/xmlhash.rb +0 -112
- data/lib/more/facets/xoxo.rb +0 -259
- data/lib/more/facets/ziputils.rb +0 -490
- data/task/conflicts +0 -63
- data/task/coverage.rake +0 -37
- data/task/methods +0 -49
- data/task/rdoc.rake +0 -17
- data/task/setup.rake +0 -38
- data/task/test.rake +0 -145
- data/test/more/test_advisable.rb +0 -71
- data/test/more/test_association.rb +0 -38
- data/test/more/test_bbcode.rb +0 -21
- data/test/more/test_binreadable.rb +0 -50
- data/test/more/test_buildable.rb +0 -73
- data/test/more/test_classmethods.rb +0 -56
- data/test/more/test_crypt.rb +0 -32
- data/test/more/test_dependency.rb +0 -69
- data/test/more/test_elementwise.rb +0 -25
- data/test/more/test_infinity.rb +0 -40
- data/test/more/test_interval.rb +0 -151
- data/test/more/test_linkedlist.rb +0 -41
- data/test/more/test_lrucache.rb +0 -14
- data/test/more/test_overload.rb +0 -160
- data/test/more/test_paramix.rb +0 -170
- data/test/more/test_prototype.rb +0 -35
- data/test/more/test_snapshot.rb +0 -21
- data/test/more/test_sparsearray.rb +0 -279
- data/test/more/test_syncarray.rb +0 -15
- data/test/more/test_synchash.rb +0 -16
- data/test/more/test_typecast.rb +0 -54
- data/test/more/test_uninheritable.rb +0 -31
- data/test/more/test_xoxo.rb +0 -274
- data/test/test_facets.rb +0 -9
@@ -1,65 +0,0 @@
|
|
1
|
-
# = Iteration
|
2
|
-
#
|
3
|
-
#
|
4
|
-
|
5
|
-
class It
|
6
|
-
attr_reader :index, :value, :prior, :after
|
7
|
-
def initialize(array)
|
8
|
-
@array = array
|
9
|
-
@index = 0
|
10
|
-
@value = array[0]
|
11
|
-
@prior = []
|
12
|
-
@after = array[1..-1]
|
13
|
-
end
|
14
|
-
def first? ; @index == 0 ; end
|
15
|
-
def last?
|
16
|
-
if Enumerable === self
|
17
|
-
nil
|
18
|
-
else
|
19
|
-
@index == @array.length
|
20
|
-
end
|
21
|
-
end
|
22
|
-
private
|
23
|
-
def next_iteration
|
24
|
-
@index += 1
|
25
|
-
@prior << @value
|
26
|
-
@value = @after.shift
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
class Array
|
31
|
-
# Iterate over each element of array using an iteration object.
|
32
|
-
#
|
33
|
-
# [1,2,3].each_iteration do |it|
|
34
|
-
# p it.index
|
35
|
-
# p it.value
|
36
|
-
# p it.first?
|
37
|
-
# p it.last?
|
38
|
-
# p it.prior
|
39
|
-
# p it.after
|
40
|
-
# end
|
41
|
-
#
|
42
|
-
# on each successive iteration produces:
|
43
|
-
#
|
44
|
-
# 0 1 2
|
45
|
-
# 1 2 3
|
46
|
-
# true false false
|
47
|
-
# false false true
|
48
|
-
# [] [1] [1,2]
|
49
|
-
# [2,3] [3] []
|
50
|
-
#
|
51
|
-
# CREDIT: Trans
|
52
|
-
|
53
|
-
def each_iteration
|
54
|
-
if block_given?
|
55
|
-
it = It.new(self)
|
56
|
-
each do |e|
|
57
|
-
yield(it)
|
58
|
-
it.send(:next_iteration)
|
59
|
-
end
|
60
|
-
else
|
61
|
-
return Enumerable::Enumerator.new(self, :each_iteration)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
@@ -1,222 +0,0 @@
|
|
1
|
-
# = LinkedList
|
2
|
-
#
|
3
|
-
# LinkedList implements a simple doubly linked list with efficient
|
4
|
-
# hash-like element access.
|
5
|
-
#
|
6
|
-
# This is a simple linked list implementation with efficient random
|
7
|
-
# access of data elements. It was inspired by George Moscovitis'
|
8
|
-
# LRUCache implementation found in Facets 1.7.30, but unlike the
|
9
|
-
# linked list in that cache, this one does not require the use of a
|
10
|
-
# mixin on any class to be stored. The linked list provides the
|
11
|
-
# push, pop, shift, unshift, first, last, delete and length methods
|
12
|
-
# which work just like their namesakes in the Array class, but it
|
13
|
-
# also supports setting and retrieving values by key, just like a
|
14
|
-
# hash.
|
15
|
-
#
|
16
|
-
# == History
|
17
|
-
#
|
18
|
-
# LinkedList was ported from the original in Kirk Hanes IOWA web framework.
|
19
|
-
#
|
20
|
-
# == Todo
|
21
|
-
#
|
22
|
-
# * Create an example of usage for docs.
|
23
|
-
#
|
24
|
-
# == Authors
|
25
|
-
#
|
26
|
-
# * Kirk Haines <khaines@enigo.com>.
|
27
|
-
#
|
28
|
-
# == Copying
|
29
|
-
#
|
30
|
-
# Copyright (C) 2006 Kirk Haines
|
31
|
-
#
|
32
|
-
# General Public License (GPL)
|
33
|
-
#
|
34
|
-
# Permission is hereby granted, free of charge, to any person obtaining
|
35
|
-
# a copy of this software and associated documentation files (the
|
36
|
-
# "Software"), to deal in the Software without restriction, including
|
37
|
-
# without limitation the rights to use, copy, modify, merge, publish,
|
38
|
-
# distribute, sublicense, and/or sell copies of the Software, and to
|
39
|
-
# permit persons to whom the Software is furnished to do so, subject to
|
40
|
-
# the following conditions:
|
41
|
-
#
|
42
|
-
# The above copyright notice and this permission notice shall be
|
43
|
-
# included in all copies or substantial portions of the Software.
|
44
|
-
#
|
45
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
46
|
-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
47
|
-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
48
|
-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
49
|
-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
50
|
-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
51
|
-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
52
|
-
|
53
|
-
require 'enumerator'
|
54
|
-
|
55
|
-
# = LinkedList
|
56
|
-
#
|
57
|
-
# LinkedList implements a simple doubly linked list with efficient
|
58
|
-
# hash-like element access.
|
59
|
-
#
|
60
|
-
# This is a simple linked list implementation with efficient random
|
61
|
-
# access of data elements. It was inspired by George Moscovitis'
|
62
|
-
# LRUCache implementation found in Facets 1.7.30, but unlike the
|
63
|
-
# linked list in that cache, this one does not require the use of a
|
64
|
-
# mixin on any class to be stored. The linked list provides the
|
65
|
-
# push, pop, shift, unshift, first, last, delete and length methods
|
66
|
-
# which work just like their namesakes in the Array class, but it
|
67
|
-
# also supports setting and retrieving values by key, just like a
|
68
|
-
# hash.
|
69
|
-
#
|
70
|
-
class LinkedList
|
71
|
-
|
72
|
-
include Enumerable
|
73
|
-
|
74
|
-
# Represents a single node of the linked list.
|
75
|
-
|
76
|
-
class Node
|
77
|
-
attr_accessor :key, :value, :prev_node, :next_node
|
78
|
-
|
79
|
-
def initialize(key=nil,value=nil,prev_node=nil,next_node=nil)
|
80
|
-
@key = key
|
81
|
-
@value = value
|
82
|
-
@prev_node = prev_node
|
83
|
-
@next_node = next_node
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def initialize
|
88
|
-
@head = Node.new
|
89
|
-
@tail = Node.new
|
90
|
-
@lookup = Hash.new
|
91
|
-
node_join(@head,@tail)
|
92
|
-
end
|
93
|
-
|
94
|
-
def [](v)
|
95
|
-
@lookup[v].value
|
96
|
-
end
|
97
|
-
|
98
|
-
def []=(k,v)
|
99
|
-
if @lookup.has_key?(k)
|
100
|
-
@lookup[k].value = v
|
101
|
-
else
|
102
|
-
n = Node.new(k,v,@head,@head.next_node)
|
103
|
-
node_join(n,@head.next_node)
|
104
|
-
node_join(@head,n)
|
105
|
-
@lookup[k] = n
|
106
|
-
end
|
107
|
-
v
|
108
|
-
end
|
109
|
-
|
110
|
-
def empty?
|
111
|
-
@lookup.empty?
|
112
|
-
end
|
113
|
-
|
114
|
-
def delete(k)
|
115
|
-
n = @lookup.delete(k)
|
116
|
-
v = n ? node_purge(n) : nil
|
117
|
-
v
|
118
|
-
end
|
119
|
-
|
120
|
-
def first
|
121
|
-
@head.next_node.value
|
122
|
-
end
|
123
|
-
|
124
|
-
def last
|
125
|
-
@tail.prev_node.value
|
126
|
-
end
|
127
|
-
|
128
|
-
def shift
|
129
|
-
k = @head.next_node.key
|
130
|
-
n = @lookup.delete(k)
|
131
|
-
node_delete(n) if n
|
132
|
-
end
|
133
|
-
|
134
|
-
def unshift(v)
|
135
|
-
if @lookup.has_key?(v)
|
136
|
-
n = @lookup[v]
|
137
|
-
node_delete(n)
|
138
|
-
node_join(n,@head.next_node)
|
139
|
-
node_join(@head,n)
|
140
|
-
else
|
141
|
-
n = Node.new(v,v,@head,@head.next_node)
|
142
|
-
node_join(n,@head.next_node)
|
143
|
-
node_join(@head,n)
|
144
|
-
@lookup[v] = n
|
145
|
-
end
|
146
|
-
v
|
147
|
-
end
|
148
|
-
|
149
|
-
def pop
|
150
|
-
k = @tail.prev_node.key
|
151
|
-
n = @lookup.delete(k)
|
152
|
-
node_delete(n) if n
|
153
|
-
end
|
154
|
-
|
155
|
-
def push(v)
|
156
|
-
if @lookup.has_key?(v)
|
157
|
-
n = @lookup[v]
|
158
|
-
node_delete(n)
|
159
|
-
node_join(@tail.prev_node,n)
|
160
|
-
node_join(n,@tail)
|
161
|
-
else
|
162
|
-
n = Node.new(v,v,@tail.prev_node,@tail)
|
163
|
-
node_join(@tail.prev_node,n)
|
164
|
-
node_join(n,@tail)
|
165
|
-
@lookup[v] = n
|
166
|
-
end
|
167
|
-
v
|
168
|
-
end
|
169
|
-
|
170
|
-
def queue
|
171
|
-
r = []
|
172
|
-
n = @head
|
173
|
-
while (n = n.next_node) and n != @tail
|
174
|
-
r << n.key
|
175
|
-
end
|
176
|
-
r
|
177
|
-
end
|
178
|
-
|
179
|
-
def to_a
|
180
|
-
r = []
|
181
|
-
n = @head
|
182
|
-
while (n = n.next_node) and n != @tail
|
183
|
-
r << n.value
|
184
|
-
end
|
185
|
-
r
|
186
|
-
end
|
187
|
-
|
188
|
-
def length
|
189
|
-
@lookup.length
|
190
|
-
end
|
191
|
-
|
192
|
-
def each
|
193
|
-
n = @head
|
194
|
-
while (n = n.next_node) and n != @tail
|
195
|
-
yield(n.key,n.value)
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
private
|
200
|
-
|
201
|
-
def node_delete(n)
|
202
|
-
node_join(n.prev_node,n.next_node)
|
203
|
-
v = n.value
|
204
|
-
end
|
205
|
-
|
206
|
-
def node_purge(n)
|
207
|
-
node_join(n.prev_node,n.next_node)
|
208
|
-
v = n.value
|
209
|
-
n.value = nil
|
210
|
-
n.key = nil
|
211
|
-
n.next_node = nil
|
212
|
-
n.prev_node = nil
|
213
|
-
v
|
214
|
-
end
|
215
|
-
|
216
|
-
def node_join(a,b)
|
217
|
-
a.next_node = b
|
218
|
-
b.prev_node = a
|
219
|
-
end
|
220
|
-
|
221
|
-
end
|
222
|
-
|
data/lib/more/facets/lrucache.rb
DELETED
@@ -1,157 +0,0 @@
|
|
1
|
-
# = LRUCache
|
2
|
-
#
|
3
|
-
# A cache utilizing a simple LRU (Least Recently Used) policy.
|
4
|
-
# The items managed by this cache must respond to the #key method.
|
5
|
-
# Attempts to optimize reads rather than inserts!
|
6
|
-
#
|
7
|
-
# LRU semantics are enforced by inserting the items in a queue.
|
8
|
-
# The lru item is always at the tail. Two special sentinels
|
9
|
-
# (head, tail) are used to simplify (?) the code.
|
10
|
-
#
|
11
|
-
# == Authors
|
12
|
-
#
|
13
|
-
# * George Moschovitis
|
14
|
-
# * Anastasios Koutoumanos
|
15
|
-
#
|
16
|
-
# == Copying
|
17
|
-
#
|
18
|
-
# Copyright 2004 George Moschovitis, Anastasios Koutoumanos
|
19
|
-
#
|
20
|
-
# Ruby License
|
21
|
-
#
|
22
|
-
# This module is free software. You may use, modify, and/or redistribute this
|
23
|
-
# software under the same terms as Ruby.
|
24
|
-
#
|
25
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
26
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
27
|
-
# FOR A PARTICULAR PURPOSE.
|
28
|
-
|
29
|
-
# = LRUCache
|
30
|
-
#
|
31
|
-
# A cache utilizing a simple LRU (Least Recently Used) policy.
|
32
|
-
# The items managed by this cache must respond to the #key method.
|
33
|
-
# Attempts to optimize reads rather than inserts!
|
34
|
-
#
|
35
|
-
# LRU semantics are enforced by inserting the items in a queue.
|
36
|
-
# The lru item is always at the tail. Two special sentinels
|
37
|
-
# (head, tail) are used to simplify (?) the code.
|
38
|
-
#
|
39
|
-
class LRUCache < Hash
|
40
|
-
|
41
|
-
# Mix this in your class to make LRU-managable.
|
42
|
-
|
43
|
-
module Item
|
44
|
-
attr_accessor :lru_key, :lru_prev, :lru_next
|
45
|
-
end
|
46
|
-
|
47
|
-
# head-tail sentinels
|
48
|
-
|
49
|
-
class Sentinel; include Item; end
|
50
|
-
|
51
|
-
# the maximum number of items in the cache.
|
52
|
-
|
53
|
-
attr_accessor :max_items
|
54
|
-
|
55
|
-
# the head sentinel and the tail sentinel, tail.prev points to the lru item.
|
56
|
-
|
57
|
-
attr_reader :head, :tail
|
58
|
-
|
59
|
-
def initialize(max_items)
|
60
|
-
@max_items = max_items
|
61
|
-
lru_clear()
|
62
|
-
end
|
63
|
-
|
64
|
-
# Lookup an item in the cache.
|
65
|
-
|
66
|
-
def [](key)
|
67
|
-
if item = super
|
68
|
-
return lru_touch(item)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
# The inserted item is considered mru!
|
73
|
-
|
74
|
-
def []=(key, item)
|
75
|
-
item = super
|
76
|
-
item.lru_key = key
|
77
|
-
lru_insert(item)
|
78
|
-
end
|
79
|
-
|
80
|
-
# Delete an item from the cache.
|
81
|
-
|
82
|
-
def delete(key)
|
83
|
-
if item = super
|
84
|
-
lru_delete(item)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
# Clear the cache.
|
89
|
-
|
90
|
-
def clear
|
91
|
-
super
|
92
|
-
lru_clear()
|
93
|
-
end
|
94
|
-
|
95
|
-
# The first (mru) element in the cache.
|
96
|
-
|
97
|
-
def first
|
98
|
-
@head.lru_next
|
99
|
-
end
|
100
|
-
|
101
|
-
# The last (lru) element in the cache.
|
102
|
-
|
103
|
-
def last
|
104
|
-
@tail.lru_prev
|
105
|
-
end
|
106
|
-
alias_method :lru, :last
|
107
|
-
|
108
|
-
private
|
109
|
-
|
110
|
-
# Delete an item from the lru list.
|
111
|
-
|
112
|
-
def lru_delete(item)
|
113
|
-
lru_join(item.lru_prev, item.lru_next)
|
114
|
-
return item
|
115
|
-
end
|
116
|
-
|
117
|
-
# Join two items in the lru list.
|
118
|
-
# Return y to allow for chaining.
|
119
|
-
|
120
|
-
def lru_join(x, y)
|
121
|
-
x.lru_next = y
|
122
|
-
y.lru_prev = x
|
123
|
-
return y
|
124
|
-
end
|
125
|
-
|
126
|
-
# Append a child item to a parent item in the lru list
|
127
|
-
# (Re)inserts the child in the list.
|
128
|
-
|
129
|
-
def lru_append(parent, child)
|
130
|
-
lru_join(child, parent.lru_next)
|
131
|
-
lru_join(parent, child)
|
132
|
-
end
|
133
|
-
|
134
|
-
# Insert an item
|
135
|
-
|
136
|
-
def lru_insert(item)
|
137
|
-
delete(last.lru_key) if size() > @max_items
|
138
|
-
lru_append(@head, item)
|
139
|
-
end
|
140
|
-
|
141
|
-
# Touch an item, make mru!
|
142
|
-
# Returns the item.
|
143
|
-
|
144
|
-
def lru_touch(item)
|
145
|
-
lru_append(@head, lru_delete(item))
|
146
|
-
end
|
147
|
-
|
148
|
-
# Clear the lru.
|
149
|
-
|
150
|
-
def lru_clear
|
151
|
-
@head = Sentinel.new
|
152
|
-
@tail = Sentinel.new
|
153
|
-
lru_join(@head, @tail)
|
154
|
-
end
|
155
|
-
|
156
|
-
end
|
157
|
-
|
data/lib/more/facets/matcher.rb
DELETED
@@ -1,140 +0,0 @@
|
|
1
|
-
#= Matcher
|
2
|
-
#
|
3
|
-
# Matcher derives from Ruby Quiz #103, the DictionaryMatcher quiz.
|
4
|
-
#
|
5
|
-
class Matcher
|
6
|
-
attr_reader :word_count
|
7
|
-
|
8
|
-
#Contains the index matched, and the word matched
|
9
|
-
class MatchData < Struct.new(:index,:match)
|
10
|
-
def inspect
|
11
|
-
"#{match.inspect}@#{index}"
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def inspect
|
16
|
-
to_s
|
17
|
-
end
|
18
|
-
|
19
|
-
#Create a DictionaryMatcher with no words in it
|
20
|
-
def initialize
|
21
|
-
@trie = {}
|
22
|
-
@word_count = 0
|
23
|
-
end
|
24
|
-
|
25
|
-
#Add a word to the DictionaryMatcher
|
26
|
-
def add(word)
|
27
|
-
@word_count += 1
|
28
|
-
container = @trie
|
29
|
-
containers=[]
|
30
|
-
|
31
|
-
i=0
|
32
|
-
word.each_byte do |b|
|
33
|
-
container[b] = {} unless container.has_key? b
|
34
|
-
container[:depth]=i
|
35
|
-
containers << container
|
36
|
-
container = container[b]
|
37
|
-
i+=1
|
38
|
-
end
|
39
|
-
containers << container
|
40
|
-
|
41
|
-
container[0] = true # Mark end of word
|
42
|
-
container[:depth]=i
|
43
|
-
|
44
|
-
ff=compute_failure_function word
|
45
|
-
ff.zip(containers).each do |pointto,container|
|
46
|
-
container[:failure]=containers[pointto] if pointto
|
47
|
-
end
|
48
|
-
|
49
|
-
self
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
alias << add
|
54
|
-
|
55
|
-
def compute_failure_function p
|
56
|
-
m=p.size
|
57
|
-
pi=[nil,0]
|
58
|
-
k=0
|
59
|
-
2.upto m do |q|
|
60
|
-
k=pi[k] while k>0 and p[k] != p[q-1]
|
61
|
-
k=k+1 if p[k]==p[q-1]
|
62
|
-
pi[q]=k
|
63
|
-
end
|
64
|
-
pi
|
65
|
-
end
|
66
|
-
private :compute_failure_function
|
67
|
-
|
68
|
-
#Determine whether +string+ was previously <tt>add</tt>ed to the
|
69
|
-
#Trie.
|
70
|
-
def include?(word)
|
71
|
-
container = @trie
|
72
|
-
word.each_byte do |b|
|
73
|
-
break unless container.has_key? b
|
74
|
-
container = container[b]
|
75
|
-
end
|
76
|
-
container[0]
|
77
|
-
end
|
78
|
-
|
79
|
-
#Determines whether one of the words in the DictionaryMatcher is a
|
80
|
-
#substring of
|
81
|
-
#+string+. Returns the index of the match if found, +nil+ if not
|
82
|
-
#found.
|
83
|
-
def =~ text
|
84
|
-
internal_match(text){|md| return md.index}
|
85
|
-
nil
|
86
|
-
end
|
87
|
-
|
88
|
-
#Determine whether one of the words in the DictionaryMatcher is a
|
89
|
-
#substring of
|
90
|
-
#+string+. Returns a DictionaryMatcher::MatchData object if found,
|
91
|
-
#+nil+ if not #found.
|
92
|
-
def match text
|
93
|
-
internal_match(text){|md| return md}
|
94
|
-
nil
|
95
|
-
end
|
96
|
-
|
97
|
-
def internal_match string
|
98
|
-
node=@trie
|
99
|
-
pos=0
|
100
|
-
string.each_byte do |b|
|
101
|
-
advance=false
|
102
|
-
until advance
|
103
|
-
nextnode=node[b]
|
104
|
-
if not nextnode
|
105
|
-
if node[:failure]
|
106
|
-
node=node[:failure]
|
107
|
-
else
|
108
|
-
advance=true
|
109
|
-
end
|
110
|
-
elsif nextnode[0]
|
111
|
-
yield MatchData.new(pos, string[pos+1-nextnode[:depth],nextnode[:depth]])
|
112
|
-
advance=true
|
113
|
-
node=@trie
|
114
|
-
else
|
115
|
-
advance=true
|
116
|
-
node=nextnode
|
117
|
-
end
|
118
|
-
pos+=1
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
|
-
private :internal_match
|
123
|
-
|
124
|
-
#Scans +string+ for all occurrances of strings in the
|
125
|
-
#DictionaryMatcher.
|
126
|
-
#Overlapping matches are skipped (only the first one is yielded), and
|
127
|
-
#when some strings in the
|
128
|
-
#DictionaryMatcher are substrings of others, only the shortest match
|
129
|
-
#at a given position is found.
|
130
|
-
def scan(text, &block)
|
131
|
-
matches=[]
|
132
|
-
block= lambda{ |md| matches << md } unless block
|
133
|
-
internal_match(text,&block)
|
134
|
-
matches
|
135
|
-
end
|
136
|
-
|
137
|
-
#Case equality. Similar to =~.
|
138
|
-
alias_method :===, :=~
|
139
|
-
end
|
140
|
-
|