kyanite 0.3.1
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/0 start all Tests.bat +23 -0
- data/History.txt +4 -0
- data/License.txt +21 -0
- data/Manifest.txt +88 -0
- data/PostInstall.txt +4 -0
- data/README.txt +48 -0
- data/Rakefile.rb +79 -0
- data/init.rb +2 -0
- data/lib/kyanite.rb +39 -0
- data/lib/kyanite/array.rb +5 -0
- data/lib/kyanite/array/array.rb +172 -0
- data/lib/kyanite/array/array2.rb +140 -0
- data/lib/kyanite/array/matrix2.rb +120 -0
- data/lib/kyanite/array_of_enumerables.rb +2 -0
- data/lib/kyanite/array_of_numerics.rb +2 -0
- data/lib/kyanite/array_of_strings.rb +2 -0
- data/lib/kyanite/basics.rb +60 -0
- data/lib/kyanite/dictionary.rb +116 -0
- data/lib/kyanite/enumerable.rb +7 -0
- data/lib/kyanite/enumerable/enumerable_enumerables.rb +70 -0
- data/lib/kyanite/enumerable/enumerable_numerics.rb +171 -0
- data/lib/kyanite/enumerable/enumerable_strings.rb +58 -0
- data/lib/kyanite/enumerable/structure.rb +170 -0
- data/lib/kyanite/general.rb +8 -0
- data/lib/kyanite/general/callerutils.rb +128 -0
- data/lib/kyanite/general/classutils.rb +246 -0
- data/lib/kyanite/general/kernel.rb +105 -0
- data/lib/kyanite/general/nil.rb +64 -0
- data/lib/kyanite/general/object.rb +86 -0
- data/lib/kyanite/general/true_false.rb +65 -0
- data/lib/kyanite/general/undoable.rb +24 -0
- data/lib/kyanite/hash.rb +170 -0
- data/lib/kyanite/matrix2.rb +2 -0
- data/lib/kyanite/nil.rb +3 -0
- data/lib/kyanite/numeric.rb +4 -0
- data/lib/kyanite/numeric/float.rb +26 -0
- data/lib/kyanite/numeric/integer.rb +34 -0
- data/lib/kyanite/numeric/numeric.rb +45 -0
- data/lib/kyanite/operation.rb +5 -0
- data/lib/kyanite/operation/call_tracker.rb +69 -0
- data/lib/kyanite/operation/rake.rb +101 -0
- data/lib/kyanite/operation/regexp.rb +23 -0
- data/lib/kyanite/operation/unit_test.rb +53 -0
- data/lib/kyanite/optimizer.rb +119 -0
- data/lib/kyanite/rake.rb +2 -0
- data/lib/kyanite/range.rb +54 -0
- data/lib/kyanite/set.rb +219 -0
- data/lib/kyanite/smart_load_path.rb +2 -0
- data/lib/kyanite/string.rb +13 -0
- data/lib/kyanite/string/cast.rb +104 -0
- data/lib/kyanite/string/chars.rb +184 -0
- data/lib/kyanite/string/chars_const.rb +190 -0
- data/lib/kyanite/string/diff.rb +78 -0
- data/lib/kyanite/string/div.rb +19 -0
- data/lib/kyanite/string/include.rb +43 -0
- data/lib/kyanite/string/list.rb +84 -0
- data/lib/kyanite/string/mgsub.rb +35 -0
- data/lib/kyanite/string/nested.rb +253 -0
- data/lib/kyanite/string/random.rb +69 -0
- data/lib/kyanite/string/split.rb +136 -0
- data/lib/kyanite/symbol.rb +19 -0
- data/lib/kyanite/tree.rb +99 -0
- data/lib/kyanite/undoable.rb +2 -0
- data/lib/kyanite/unit_test.rb +2 -0
- data/test/_start_all.rb +17 -0
- data/test/array/test_array.rb +106 -0
- data/test/array/test_matrix2.rb +162 -0
- data/test/enumerable/test_enumerable_enumerables.rb +46 -0
- data/test/enumerable/test_enumerable_numerics.rb +93 -0
- data/test/enumerable/test_enumerable_strings.rb +22 -0
- data/test/enumerable/test_structure.rb +220 -0
- data/test/general/test_classutils.rb +45 -0
- data/test/general/test_nil.rb +44 -0
- data/test/general/test_object.rb +49 -0
- data/test/general/test_true_false.rb +28 -0
- data/test/numeric/test_numeric_integer.rb +28 -0
- data/test/string/test_cast.rb +108 -0
- data/test/string/test_chars.rb +255 -0
- data/test/string/test_diff.rb +95 -0
- data/test/string/test_list.rb +141 -0
- data/test/string/test_nested.rb +361 -0
- data/test/string/test_split.rb +187 -0
- data/test/test_dictionary.rb +128 -0
- data/test/test_hash.rb +59 -0
- data/test/test_optimizer.rb +150 -0
- data/test/test_range.rb +41 -0
- data/test/test_set.rb +210 -0
- data/test/test_tree.rb +94 -0
- metadata +217 -0
@@ -0,0 +1,70 @@
|
|
1
|
+
|
2
|
+
module Enumerable
|
3
|
+
|
4
|
+
# In-place-Variante von transpose.
|
5
|
+
def transpose!
|
6
|
+
self.replace(self.transpose)
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
# Enumerable, EnumerableNumerics, EnumerableStrings, EnumerableEnumerables
|
13
|
+
#
|
14
|
+
# ==Aufzählungen aufzählbarer Objekte
|
15
|
+
# Für zweidimensionale Enumerables bzw. Aufzählungen von Objekten, die wiederum aufzählbar sind.
|
16
|
+
# Ein ArrayOfEnumerables inkludiert dieses Modul.
|
17
|
+
#
|
18
|
+
module EnumerableEnumerables
|
19
|
+
|
20
|
+
# Macht das Enumerable rechteckig.
|
21
|
+
# Maßgeblich ist die erste Zeile.
|
22
|
+
#
|
23
|
+
# Tests und Beispiele siehe TestKyaniteEnumerableEnumerables.
|
24
|
+
def rectangle
|
25
|
+
qsize = self[0].size
|
26
|
+
result = []
|
27
|
+
self.each do |zeile|
|
28
|
+
size_diff = qsize - zeile.size
|
29
|
+
# so lassen oder zuschneiden
|
30
|
+
if size_diff <= 0
|
31
|
+
result << zeile[0..qsize-1]
|
32
|
+
# ergänzen
|
33
|
+
else
|
34
|
+
result << zeile + ([nil] * size_diff)
|
35
|
+
end # if
|
36
|
+
end # each zeile
|
37
|
+
result
|
38
|
+
end # def
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
# Array, ArrayOfNumerics, ArrayOfStrings, ArrayOfEnumerables
|
43
|
+
#
|
44
|
+
# Ein ArrayOfEnumerables ist ein Array mit inkludiertem Modul EnumerableEnumerables
|
45
|
+
#
|
46
|
+
class ArrayOfEnumerables < Array
|
47
|
+
include EnumerableEnumerables
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
class Array
|
52
|
+
|
53
|
+
# Liefert ein ArrayOfEnumerables (das ist ein Array mit inkludiertem Modul EnumerableEnumerables)
|
54
|
+
def to_array_of_enumerables
|
55
|
+
ArrayOfEnumerables.new(self)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
class NilClass
|
61
|
+
def transpose!; nil; end
|
62
|
+
def transpose; nil; end
|
63
|
+
def rectangle; nil; end
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
@@ -0,0 +1,171 @@
|
|
1
|
+
|
2
|
+
# Enumerable, EnumerableNumerics, EnumerableStrings, EnumerableEnumerables
|
3
|
+
#
|
4
|
+
# ==Aufzählungen numerischer Objekte
|
5
|
+
# Ein ArrayOfNumerics inkludiert dieses Modul.
|
6
|
+
#
|
7
|
+
module EnumerableNumerics
|
8
|
+
|
9
|
+
|
10
|
+
# ======================================================================================
|
11
|
+
# :section: Mittelwerte
|
12
|
+
#
|
13
|
+
|
14
|
+
# Arithmetrischer Mittelwert
|
15
|
+
# Tests und Beispiele siehe TestKyaniteEnumerableNumerics.
|
16
|
+
#
|
17
|
+
def mean
|
18
|
+
self.inject(0.0) { |sum, i | sum += i } / self.length.to_f
|
19
|
+
end
|
20
|
+
alias avg mean
|
21
|
+
alias average mean
|
22
|
+
alias mean_arithmetric mean
|
23
|
+
|
24
|
+
# Harmonischer Mittelwert
|
25
|
+
#
|
26
|
+
# Normalerweise ist der harmonische Mittelwert nur für positive Zahlen sinnvoll definiert.
|
27
|
+
# Mit der Option <tt>:allow_negative => true </tt>kann man aber auch negative Zahlen mit einbeziehen.
|
28
|
+
# Dann wird der harmonische Mittelwert aller positiven Elemente mit dem
|
29
|
+
# harmonische Mittelwert aller negativen Elemente verrechnet (als gewichtetes arithmetisches Mittel).
|
30
|
+
#
|
31
|
+
# Tests und Beispiele siehe TestKyaniteEnumerableNumerics.
|
32
|
+
#
|
33
|
+
def mean_harmonic( options={} )
|
34
|
+
return 0 if self.empty?
|
35
|
+
return self.first if self.size == 1
|
36
|
+
allow_negative = options[:allow_negative] || false
|
37
|
+
unless allow_negative
|
38
|
+
summe = 0
|
39
|
+
self.each { |x| summe += ( 1.0/x ) }
|
40
|
+
return self.size / summe
|
41
|
+
|
42
|
+
else
|
43
|
+
positives = []
|
44
|
+
negatives = []
|
45
|
+
self.each do |e|
|
46
|
+
if e >= 0
|
47
|
+
positives << e
|
48
|
+
else
|
49
|
+
negatives << -e
|
50
|
+
end
|
51
|
+
end #each
|
52
|
+
if positives.size > 0
|
53
|
+
if negatives.size > 0
|
54
|
+
return ( positives.mean_harmonic(:allow_negative => false) * positives.size -
|
55
|
+
negatives.mean_harmonic(:allow_negative => false) * negatives.size
|
56
|
+
) / (positives.size + negatives.size).to_f
|
57
|
+
else
|
58
|
+
return positives.mean_harmonic(:allow_negative => false)
|
59
|
+
end
|
60
|
+
else
|
61
|
+
return -negatives.mean_harmonic(:allow_negative => false)
|
62
|
+
end
|
63
|
+
|
64
|
+
end #if allow_negative
|
65
|
+
end #def
|
66
|
+
|
67
|
+
|
68
|
+
# Geometrischer Mittelwert
|
69
|
+
def mean_geometric
|
70
|
+
self.product**( 1.0/self.size )
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
# ======================================================================================
|
76
|
+
# :section: Summe, Produkt, Parallelschaltung
|
77
|
+
#
|
78
|
+
|
79
|
+
|
80
|
+
# Summe
|
81
|
+
#
|
82
|
+
# Methode darf nicht sum heißen, kollidiert sonst schnell mit ActiveRecord.
|
83
|
+
# Tests und Beispiele siehe TestKyaniteEnumerableNumerics.
|
84
|
+
#
|
85
|
+
def summation
|
86
|
+
self.inject(0.0) { |sum, i | sum += i }
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
# Produkt
|
91
|
+
#
|
92
|
+
# Tests und Beispiele siehe TestKyaniteEnumerableNumerics.
|
93
|
+
#
|
94
|
+
def product
|
95
|
+
self.inject(1.0) { |prd, i | prd *= i }
|
96
|
+
end
|
97
|
+
alias prd product
|
98
|
+
|
99
|
+
|
100
|
+
# Ergebnis entspricht der Parallelschaltung von Widerständen.
|
101
|
+
# Tests und Beispiele siehe TestKyaniteEnumerableNumerics.
|
102
|
+
#
|
103
|
+
def parallel
|
104
|
+
mean_harmonic / size
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
end #class
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
# Array, ArrayOfNumerics, ArrayOfStrings, ArrayOfEnumerables
|
114
|
+
#
|
115
|
+
# Ein ArrayOfNumerics ist ein Array mit inkludiertem Modul EnumerableNumerics
|
116
|
+
#
|
117
|
+
class ArrayOfNumerics < Array
|
118
|
+
include EnumerableNumerics
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
class Array
|
124
|
+
|
125
|
+
# Liefert ein ArrayOfNumerics (das ist ein Array mit inkludiertem Modul EnumerableNumerics)
|
126
|
+
def to_array_of_numerics
|
127
|
+
ArrayOfNumerics.new(self)
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
class NilClass
|
135
|
+
def average; nil; end
|
136
|
+
def avg; nil; end
|
137
|
+
def mean; nil; end
|
138
|
+
def mean_arithmetric; nil; end
|
139
|
+
def mean_geometric; nil; end
|
140
|
+
def mean_harmonic(*a); nil; end
|
141
|
+
def parallel(*a); nil; end
|
142
|
+
def prd; nil; end
|
143
|
+
def product; nil; end
|
144
|
+
def sum; nil; end
|
145
|
+
def summation; nil; end
|
146
|
+
end
|
147
|
+
|
148
|
+
|
149
|
+
# ==================================================================================
|
150
|
+
# Ausprobieren
|
151
|
+
#
|
152
|
+
if $0 == __FILE__
|
153
|
+
require 'perception'
|
154
|
+
class Array
|
155
|
+
include EnumerableNumerics
|
156
|
+
end
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
|
2
|
+
# Enumerable, EnumerableNumerics, EnumerableStrings, EnumerableEnumerables
|
3
|
+
#
|
4
|
+
# ==Aufzählungen textueller Objekte
|
5
|
+
# Ein ArrayOfStrings inkludiert dieses Modul.
|
6
|
+
#
|
7
|
+
module EnumerableStrings
|
8
|
+
|
9
|
+
|
10
|
+
# Bsp.:
|
11
|
+
# ['lut', 'lutm', 'lutmi', 'lutmil', 'lutmila', 'lutrika', 'lutrik', 'lutri', 'lutr', 'lut'].palindrom_rumpf =>
|
12
|
+
# ['lutm', 'lutmi', 'lutmil', 'lutmila', 'lutrika', 'lutrik', 'lutri', 'lutr']
|
13
|
+
# d.h. vorne und hinten wird alles Gleiche weggestrichen.
|
14
|
+
#
|
15
|
+
# Tests und Beispiele siehe TestKyaniteEnumerableStrings.
|
16
|
+
#
|
17
|
+
def palindrom_rumpf
|
18
|
+
result = self.dup
|
19
|
+
0.upto( size/2 - 1 ) do |i|
|
20
|
+
if result[0] == result[-1]
|
21
|
+
result.delete_at(0)
|
22
|
+
result.delete_at(-1)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
result
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
# Array, ArrayOfNumerics, ArrayOfStrings, ArrayOfEnumerables
|
36
|
+
#
|
37
|
+
# Ein ArrayOfStrings ist ein Array mit inkludiertem Modul EnumerableStrings
|
38
|
+
#
|
39
|
+
class ArrayOfStrings < Array
|
40
|
+
include EnumerableStrings
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
class Array
|
46
|
+
|
47
|
+
# Liefert ein ArrayOfStrings (das ist ein Array mit inkludiertem Modul EnumerableStrings)
|
48
|
+
def to_array_of_strings
|
49
|
+
ArrayOfStrings.new(self)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
@@ -0,0 +1,170 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# is_collection?
|
4
|
+
#
|
5
|
+
|
6
|
+
class Object
|
7
|
+
|
8
|
+
# Enthält ein Objekt mehrere Objekte?
|
9
|
+
# String und Range gelten nicht als Collection.
|
10
|
+
#
|
11
|
+
# Tests & Beispiele siehe TestKyaniteEnumerableStructure.
|
12
|
+
def is_collection?; false; end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
module Enumerable
|
17
|
+
|
18
|
+
# Enthält ein Objekt mehrere Objekte?
|
19
|
+
# String und Range gelten nicht als Collection.
|
20
|
+
#
|
21
|
+
# Tests & Beispiele siehe TestKyaniteEnumerableStructure.
|
22
|
+
def is_collection?; true; end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
class String # :nodoc:
|
27
|
+
def is_collection?; false; end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
class Range # :nodoc:
|
32
|
+
def is_collection?; false; end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
#
|
39
|
+
# Distribution
|
40
|
+
#
|
41
|
+
|
42
|
+
|
43
|
+
# Enumerable, EnumerableNumerics, EnumerableStrings, EnumerableEnumerables
|
44
|
+
#
|
45
|
+
# ==Allgemeine Aufzählungen
|
46
|
+
#
|
47
|
+
module Enumerable
|
48
|
+
|
49
|
+
# Liefert die Verteilung der size
|
50
|
+
# oder die Verteilung der class
|
51
|
+
# oder die Verteilung eines anderen Merkmals der aufgezählten Elemente.
|
52
|
+
#
|
53
|
+
# Tests & Beispiele siehe TestKyaniteEnumerableStructure.
|
54
|
+
#
|
55
|
+
def distribution( mode = :size)
|
56
|
+
verteilung = Hash.new
|
57
|
+
each do | element |
|
58
|
+
value = element.respond(mode)
|
59
|
+
if verteilung.has_key?(value)
|
60
|
+
verteilung[value] += 1
|
61
|
+
else
|
62
|
+
verteilung[value] = 1
|
63
|
+
end # if
|
64
|
+
end #each
|
65
|
+
verteilung.to_a.sort
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
#
|
72
|
+
# contentclass
|
73
|
+
#
|
74
|
+
|
75
|
+
# Was für Objekte beinhaltet die Collection?
|
76
|
+
# Liefert die Klasse der Contentelemente, oder <tt>Object</tt> wenn es verschiedene sind.
|
77
|
+
#
|
78
|
+
# Parameter ist die Genauigkeit, mit der der Inhalt geprüft wird.
|
79
|
+
# :precision => 1 nur das erste Element wird geprüft
|
80
|
+
# :precision => 2 das erste und das letzte Element werden geprüft (STANDARD)
|
81
|
+
# :precision => :all alle Elemente werden geprüft
|
82
|
+
# :ignore_nil => true NilClass wird nicht aufgeführt (STANDARD)
|
83
|
+
# :ignore_nil => false NilClass wird mit aufgeführt
|
84
|
+
#
|
85
|
+
# Tests & Beispiele siehe TestKyaniteEnumerableStructure.
|
86
|
+
#
|
87
|
+
def contentclass( options={} )
|
88
|
+
precision = options[:precision] || 2
|
89
|
+
ignore_nil = options[:ignore_nil]; ignore_nil = true if ignore_nil.nil?
|
90
|
+
return nil if self.empty?
|
91
|
+
case precision
|
92
|
+
|
93
|
+
when 1
|
94
|
+
result = self.first.class
|
95
|
+
if ( result == NilClass && ignore_nil )
|
96
|
+
return self.compact.contentclass( :precision => precision, :ignore_nil => false )
|
97
|
+
else
|
98
|
+
return result
|
99
|
+
end
|
100
|
+
|
101
|
+
when 2
|
102
|
+
f = self.first.class
|
103
|
+
l = self.last.class
|
104
|
+
if ( (f == NilClass || l == NilClass) && ignore_nil )
|
105
|
+
return self.compact.contentclass( :precision => precision, :ignore_nil => false )
|
106
|
+
end
|
107
|
+
if f == l
|
108
|
+
return f
|
109
|
+
else
|
110
|
+
result = f.ancestors & l.ancestors
|
111
|
+
#see result - [Object, Kernel, Precision, Perception::NumericI, PP::ObjectMixin, KyaniteKernel]
|
112
|
+
return Numeric if result.include?(Numeric)
|
113
|
+
return Enumerable if result.include?(Enumerable)
|
114
|
+
return Object
|
115
|
+
end
|
116
|
+
|
117
|
+
when :all
|
118
|
+
unless ( self.kind_of?(Hash) || self.kind_of?(Dictionary) )
|
119
|
+
c = self.collect {|e| e.class}
|
120
|
+
else
|
121
|
+
c = self.collect {| key, value | value.class}
|
122
|
+
end
|
123
|
+
c = c - [NilClass] if ignore_nil
|
124
|
+
c.uniq!
|
125
|
+
if c.empty?
|
126
|
+
return nil if ignore_nil
|
127
|
+
return NilClass
|
128
|
+
end
|
129
|
+
return c[0] if c.size == 1
|
130
|
+
result = c[0].ancestors
|
131
|
+
c[1..-1].each do |e|
|
132
|
+
result = result & e.ancestors
|
133
|
+
end
|
134
|
+
#see result - [Object, Kernel, Precision, Perception::NumericI, PP::ObjectMixin, KyaniteKernel]
|
135
|
+
return Numeric if result.include?(Numeric)
|
136
|
+
return Enumerable if result.include?(Enumerable)
|
137
|
+
return Object
|
138
|
+
|
139
|
+
else # case precision
|
140
|
+
raise ArgumentError, ':precision should be 1, 2 or :all'
|
141
|
+
end #case
|
142
|
+
|
143
|
+
end #def
|
144
|
+
|
145
|
+
end #module
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
# ==================================================================================
|
153
|
+
# Ausprobieren
|
154
|
+
#
|
155
|
+
if $0 == __FILE__
|
156
|
+
|
157
|
+
require 'kyanite/smart_load_path'; smart_load_path
|
158
|
+
class Array
|
159
|
+
include Enumerable
|
160
|
+
end
|
161
|
+
require 'perception'
|
162
|
+
|
163
|
+
test = [ ]
|
164
|
+
see test.contentclass(:precision => :all)
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
|