hashery 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.ruby +57 -92
- data/.yardopts +8 -0
- data/COPYING.rdoc +45 -0
- data/HISTORY.rdoc +18 -0
- data/QED.rdoc +1 -0
- data/README.rdoc +42 -16
- data/lib/hashery.rb +16 -9
- data/lib/hashery.yml +57 -92
- data/lib/hashery/association.rb +3 -1
- data/lib/hashery/basic_object.rb +74 -0
- data/lib/hashery/basic_struct.rb +288 -1
- data/lib/hashery/basicobject.rb +1 -74
- data/lib/hashery/basicstruct.rb +1 -280
- data/lib/hashery/casting_hash.rb +171 -1
- data/lib/hashery/castinghash.rb +1 -171
- data/lib/hashery/core_ext.rb +82 -0
- data/lib/hashery/dictionary.rb +3 -0
- data/lib/hashery/fuzzy_hash.rb +154 -1
- data/lib/hashery/fuzzyhash.rb +1 -154
- data/lib/hashery/ini.rb +3 -2
- data/lib/hashery/key_hash.rb +186 -0
- data/lib/hashery/keyhash.rb +1 -0
- data/lib/hashery/linked_list.rb +195 -1
- data/lib/hashery/linkedlist.rb +1 -195
- data/lib/hashery/lru_hash.rb +273 -1
- data/lib/hashery/lruhash.rb +1 -273
- data/lib/hashery/open_cascade.rb +99 -1
- data/lib/hashery/open_hash.rb +77 -1
- data/lib/hashery/opencascade.rb +1 -99
- data/lib/hashery/openhash.rb +1 -77
- data/lib/hashery/ordered_hash.rb +168 -1
- data/lib/hashery/orderedhash.rb +1 -167
- data/lib/hashery/property_hash.rb +97 -1
- data/lib/hashery/propertyhash.rb +1 -97
- data/lib/hashery/query_hash.rb +35 -1
- data/lib/hashery/queryhash.rb +1 -35
- data/lib/hashery/stash.rb +3 -174
- data/lib/hashery/static_hash.rb +48 -1
- data/lib/hashery/statichash.rb +1 -48
- data/qed/06_opencascade.rdoc +12 -12
- data/test/case_association.rb +29 -15
- data/test/case_basicstruct.rb +192 -0
- data/test/case_dictionary.rb +149 -109
- data/test/case_keyhash.rb +175 -0
- data/test/case_opencascade.rb +89 -43
- data/test/case_openhash.rb +15 -11
- metadata +85 -78
- data/LICENSE +0 -206
- data/NOTICE +0 -11
- data/lib/hashery/sparse_array.rb +0 -1
- data/lib/hashery/sparsearray.rb +0 -577
- data/test/case_openobject.rb +0 -130
- data/test/case_sparsearray.rb +0 -316
- data/test/case_stash.rb +0 -131
data/LICENSE
DELETED
@@ -1,206 +0,0 @@
|
|
1
|
-
.
|
2
|
-
|
3
|
-
Apache License
|
4
|
-
Version 2.0, January 2004
|
5
|
-
http://www.apache.org/licenses/
|
6
|
-
|
7
|
-
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
8
|
-
|
9
|
-
1. Definitions.
|
10
|
-
|
11
|
-
"License" shall mean the terms and conditions for use, reproduction,
|
12
|
-
and distribution as defined by Sections 1 through 9 of this document.
|
13
|
-
|
14
|
-
"Licensor" shall mean the copyright owner or entity authorized by
|
15
|
-
the copyright owner that is granting the License.
|
16
|
-
|
17
|
-
"Legal Entity" shall mean the union of the acting entity and all
|
18
|
-
other entities that control, are controlled by, or are under common
|
19
|
-
control with that entity. For the purposes of this definition,
|
20
|
-
"control" means (i) the power, direct or indirect, to cause the
|
21
|
-
direction or management of such entity, whether by contract or
|
22
|
-
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
23
|
-
outstanding shares, or (iii) beneficial ownership of such entity.
|
24
|
-
|
25
|
-
"You" (or "Your") shall mean an individual or Legal Entity
|
26
|
-
exercising permissions granted by this License.
|
27
|
-
|
28
|
-
"Source" form shall mean the preferred form for making modifications,
|
29
|
-
including but not limited to software source code, documentation
|
30
|
-
source, and configuration files.
|
31
|
-
|
32
|
-
"Object" form shall mean any form resulting from mechanical
|
33
|
-
transformation or translation of a Source form, including but
|
34
|
-
not limited to compiled object code, generated documentation,
|
35
|
-
and conversions to other media types.
|
36
|
-
|
37
|
-
"Work" shall mean the work of authorship, whether in Source or
|
38
|
-
Object form, made available under the License, as indicated by a
|
39
|
-
copyright notice that is included in or attached to the work
|
40
|
-
(an example is provided in the Appendix below).
|
41
|
-
|
42
|
-
"Derivative Works" shall mean any work, whether in Source or Object
|
43
|
-
form, that is based on (or derived from) the Work and for which the
|
44
|
-
editorial revisions, annotations, elaborations, or other modifications
|
45
|
-
represent, as a whole, an original work of authorship. For the purposes
|
46
|
-
of this License, Derivative Works shall not include works that remain
|
47
|
-
separable from, or merely link (or bind by name) to the interfaces of,
|
48
|
-
the Work and Derivative Works thereof.
|
49
|
-
|
50
|
-
"Contribution" shall mean any work of authorship, including
|
51
|
-
the original version of the Work and any modifications or additions
|
52
|
-
to that Work or Derivative Works thereof, that is intentionally
|
53
|
-
submitted to Licensor for inclusion in the Work by the copyright owner
|
54
|
-
or by an individual or Legal Entity authorized to submit on behalf of
|
55
|
-
the copyright owner. For the purposes of this definition, "submitted"
|
56
|
-
means any form of electronic, verbal, or written communication sent
|
57
|
-
to the Licensor or its representatives, including but not limited to
|
58
|
-
communication on electronic mailing lists, source code control systems,
|
59
|
-
and issue tracking systems that are managed by, or on behalf of, the
|
60
|
-
Licensor for the purpose of discussing and improving the Work, but
|
61
|
-
excluding communication that is conspicuously marked or otherwise
|
62
|
-
designated in writing by the copyright owner as "Not a Contribution."
|
63
|
-
|
64
|
-
"Contributor" shall mean Licensor and any individual or Legal Entity
|
65
|
-
on behalf of whom a Contribution has been received by Licensor and
|
66
|
-
subsequently incorporated within the Work.
|
67
|
-
|
68
|
-
2. Grant of Copyright License. Subject to the terms and conditions of
|
69
|
-
this License, each Contributor hereby grants to You a perpetual,
|
70
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
71
|
-
copyright license to reproduce, prepare Derivative Works of,
|
72
|
-
publicly display, publicly perform, sublicense, and distribute the
|
73
|
-
Work and such Derivative Works in Source or Object form.
|
74
|
-
|
75
|
-
3. Grant of Patent License. Subject to the terms and conditions of
|
76
|
-
this License, each Contributor hereby grants to You a perpetual,
|
77
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
78
|
-
(except as stated in this section) patent license to make, have made,
|
79
|
-
use, offer to sell, sell, import, and otherwise transfer the Work,
|
80
|
-
where such license applies only to those patent claims licensable
|
81
|
-
by such Contributor that are necessarily infringed by their
|
82
|
-
Contribution(s) alone or by combination of their Contribution(s)
|
83
|
-
with the Work to which such Contribution(s) was submitted. If You
|
84
|
-
institute patent litigation against any entity (including a
|
85
|
-
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
86
|
-
or a Contribution incorporated within the Work constitutes direct
|
87
|
-
or contributory patent infringement, then any patent licenses
|
88
|
-
granted to You under this License for that Work shall terminate
|
89
|
-
as of the date such litigation is filed.
|
90
|
-
|
91
|
-
4. Redistribution. You may reproduce and distribute copies of the
|
92
|
-
Work or Derivative Works thereof in any medium, with or without
|
93
|
-
modifications, and in Source or Object form, provided that You
|
94
|
-
meet the following conditions:
|
95
|
-
|
96
|
-
(a) You must give any other recipients of the Work or
|
97
|
-
Derivative Works a copy of this License; and
|
98
|
-
|
99
|
-
(b) You must cause any modified files to carry prominent notices
|
100
|
-
stating that You changed the files; and
|
101
|
-
|
102
|
-
(c) You must retain, in the Source form of any Derivative Works
|
103
|
-
that You distribute, all copyright, patent, trademark, and
|
104
|
-
attribution notices from the Source form of the Work,
|
105
|
-
excluding those notices that do not pertain to any part of
|
106
|
-
the Derivative Works; and
|
107
|
-
|
108
|
-
(d) If the Work includes a "NOTICE" text file as part of its
|
109
|
-
distribution, then any Derivative Works that You distribute must
|
110
|
-
include a readable copy of the attribution notices contained
|
111
|
-
within such NOTICE file, excluding those notices that do not
|
112
|
-
pertain to any part of the Derivative Works, in at least one
|
113
|
-
of the following places: within a NOTICE text file distributed
|
114
|
-
as part of the Derivative Works; within the Source form or
|
115
|
-
documentation, if provided along with the Derivative Works; or,
|
116
|
-
within a display generated by the Derivative Works, if and
|
117
|
-
wherever such third-party notices normally appear. The contents
|
118
|
-
of the NOTICE file are for informational purposes only and
|
119
|
-
do not modify the License. You may add Your own attribution
|
120
|
-
notices within Derivative Works that You distribute, alongside
|
121
|
-
or as an addendum to the NOTICE text from the Work, provided
|
122
|
-
that such additional attribution notices cannot be construed
|
123
|
-
as modifying the License.
|
124
|
-
|
125
|
-
You may add Your own copyright statement to Your modifications and
|
126
|
-
may provide additional or different license terms and conditions
|
127
|
-
for use, reproduction, or distribution of Your modifications, or
|
128
|
-
for any such Derivative Works as a whole, provided Your use,
|
129
|
-
reproduction, and distribution of the Work otherwise complies with
|
130
|
-
the conditions stated in this License.
|
131
|
-
|
132
|
-
5. Submission of Contributions. Unless You explicitly state otherwise,
|
133
|
-
any Contribution intentionally submitted for inclusion in the Work
|
134
|
-
by You to the Licensor shall be under the terms and conditions of
|
135
|
-
this License, without any additional terms or conditions.
|
136
|
-
Notwithstanding the above, nothing herein shall supersede or modify
|
137
|
-
the terms of any separate license agreement you may have executed
|
138
|
-
with Licensor regarding such Contributions.
|
139
|
-
|
140
|
-
6. Trademarks. This License does not grant permission to use the trade
|
141
|
-
names, trademarks, service marks, or product names of the Licensor,
|
142
|
-
except as required for reasonable and customary use in describing the
|
143
|
-
origin of the Work and reproducing the content of the NOTICE file.
|
144
|
-
|
145
|
-
7. Disclaimer of Warranty. Unless required by applicable law or
|
146
|
-
agreed to in writing, Licensor provides the Work (and each
|
147
|
-
Contributor provides its Contributions) on an "AS IS" BASIS,
|
148
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
149
|
-
implied, including, without limitation, any warranties or conditions
|
150
|
-
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
151
|
-
PARTICULAR PURPOSE. You are solely responsible for determining the
|
152
|
-
appropriateness of using or redistributing the Work and assume any
|
153
|
-
risks associated with Your exercise of permissions under this License.
|
154
|
-
|
155
|
-
8. Limitation of Liability. In no event and under no legal theory,
|
156
|
-
whether in tort (including negligence), contract, or otherwise,
|
157
|
-
unless required by applicable law (such as deliberate and grossly
|
158
|
-
negligent acts) or agreed to in writing, shall any Contributor be
|
159
|
-
liable to You for damages, including any direct, indirect, special,
|
160
|
-
incidental, or consequential damages of any character arising as a
|
161
|
-
result of this License or out of the use or inability to use the
|
162
|
-
Work (including but not limited to damages for loss of goodwill,
|
163
|
-
work stoppage, computer failure or malfunction, or any and all
|
164
|
-
other commercial damages or losses), even if such Contributor
|
165
|
-
has been advised of the possibility of such damages.
|
166
|
-
|
167
|
-
9. Accepting Warranty or Additional Liability. While redistributing
|
168
|
-
the Work or Derivative Works thereof, You may choose to offer,
|
169
|
-
and charge a fee for, acceptance of support, warranty, indemnity,
|
170
|
-
or other liability obligations and/or rights consistent with this
|
171
|
-
License. However, in accepting such obligations, You may act only
|
172
|
-
on Your own behalf and on Your sole responsibility, not on behalf
|
173
|
-
of any other Contributor, and only if You agree to indemnify,
|
174
|
-
defend, and hold each Contributor harmless for any liability
|
175
|
-
incurred by, or claims asserted against, such Contributor by reason
|
176
|
-
of your accepting any such warranty or additional liability.
|
177
|
-
|
178
|
-
END OF TERMS AND CONDITIONS
|
179
|
-
|
180
|
-
APPENDIX: How to apply the Apache License to your work.
|
181
|
-
|
182
|
-
To apply the Apache License to your work, attach the following
|
183
|
-
boilerplate notice, with the fields enclosed by brackets "[]"
|
184
|
-
replaced with your own identifying information. (Don't include
|
185
|
-
the brackets!) The text should be enclosed in the appropriate
|
186
|
-
comment syntax for the file format. We also recommend that a
|
187
|
-
file or class name and description of purpose be included on the
|
188
|
-
same "printed page" as the copyright notice for easier
|
189
|
-
identification within third-party archives.
|
190
|
-
|
191
|
-
Copyright [yyyy] [name of copyright owner]
|
192
|
-
|
193
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
194
|
-
you may not use this file except in compliance with the License.
|
195
|
-
You may obtain a copy of the License at
|
196
|
-
|
197
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
198
|
-
|
199
|
-
Unless required by applicable law or agreed to in writing, software
|
200
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
201
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
202
|
-
See the License for the specific language governing permissions and
|
203
|
-
limitations under the License.
|
204
|
-
|
205
|
-
.
|
206
|
-
|
data/NOTICE
DELETED
data/lib/hashery/sparse_array.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'hashery/sparsearray'
|
data/lib/hashery/sparsearray.rb
DELETED
@@ -1,577 +0,0 @@
|
|
1
|
-
# = SparseArray
|
2
|
-
#
|
3
|
-
# SparseArray is an implemenation of the Array class using only Hashes.
|
4
|
-
# Regular Arrays are never used except once to delegate the #pack method,
|
5
|
-
# (and *args parameters of course). SparseArray is almost fully compatible
|
6
|
-
# with Array. There are still a few missing methods that came in with
|
7
|
-
# Ruby 1.9 that need to be added, and negative indexes are not quite fully
|
8
|
-
# supported yet.
|
9
|
-
#
|
10
|
-
# Benchmarks comparing Ruby 1.6 circa 2004 compared to Ruby 1.8.7
|
11
|
-
# circa 2010, show that Ruby's Array implementation has improved quite
|
12
|
-
# a bit. Where as Array was about 2-4 times faster than SparseArray in 2004,
|
13
|
-
# it is now over 10x faster, and able to handle large sparse arrays quite
|
14
|
-
# easily. Though surely SparseArray could still be improved, <b>SparseArray
|
15
|
-
# is little more than an interesting novelty at this point</b>, as opposed
|
16
|
-
# to a useful class, but we will keep her nonetheless for simple interests
|
17
|
-
# sake.
|
18
|
-
#
|
19
|
-
# NOTE: SparseArray is also the first piece of code I used TDD to create.
|
20
|
-
#
|
21
|
-
# Copyright (c) 2004 Thomas Sawyer
|
22
|
-
#
|
23
|
-
#--
|
24
|
-
# TODO: Add auto-array features if needed (see work/autoarray).
|
25
|
-
#++
|
26
|
-
|
27
|
-
class SparseArray < Hash
|
28
|
-
|
29
|
-
def self.[](*args)
|
30
|
-
s = new
|
31
|
-
args.each{ |e| s << e }
|
32
|
-
s
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.new_h(hsh)
|
36
|
-
nha = new
|
37
|
-
nha.replace(hsh)
|
38
|
-
#nha.reindex!
|
39
|
-
end
|
40
|
-
|
41
|
-
def initialize(i=0,e=nil)
|
42
|
-
if i > 0
|
43
|
-
i.times { set(self.length,e) }
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def &(ha)
|
48
|
-
nha = self.class.new
|
49
|
-
(0..self.length-1).each do |i|
|
50
|
-
if ha.has_value?(self.fetch(i)) and !nha.has_value?(self.fetch(i))
|
51
|
-
nha.set(nha.length,self.fetch(i))
|
52
|
-
end
|
53
|
-
end
|
54
|
-
nha
|
55
|
-
end
|
56
|
-
|
57
|
-
def *(j)
|
58
|
-
if j.kind_of?(String)
|
59
|
-
return self.join(j)
|
60
|
-
else
|
61
|
-
nha = self.class.new
|
62
|
-
j.times { (0...self.length).each { |i| nha.set(nha.length,self.fetch(i)) } }
|
63
|
-
return nha
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def +(ha)
|
68
|
-
nha = self.dup
|
69
|
-
(0..ha.length-1).each { |i| nha.set(nha.length,ha.fetch(i)) }
|
70
|
-
nha
|
71
|
-
end
|
72
|
-
|
73
|
-
def -(ha)
|
74
|
-
nha = self.class.new
|
75
|
-
self.each { |v| nha << v if !ha.has_value?(v) }
|
76
|
-
#ha.each { |v| nha << i if !self.include?(v) }
|
77
|
-
nha
|
78
|
-
end
|
79
|
-
|
80
|
-
def <<(e)
|
81
|
-
set(length,e)
|
82
|
-
self
|
83
|
-
end
|
84
|
-
|
85
|
-
def <=>(ha)
|
86
|
-
(0..self.length-1).each do |i|
|
87
|
-
ieq = (self.fetch(i) <=> ha.fetch(i))
|
88
|
-
return ieq if ieq != 0
|
89
|
-
end
|
90
|
-
self.length <=> ha.length
|
91
|
-
end
|
92
|
-
|
93
|
-
def ===(ha)
|
94
|
-
self.==(ha)
|
95
|
-
end
|
96
|
-
|
97
|
-
alias_method :get, :[]
|
98
|
-
private :get
|
99
|
-
|
100
|
-
# TODO: Ranges with negative indexes not yet supported.
|
101
|
-
def [](i,l=nil)
|
102
|
-
if l
|
103
|
-
i = size + i if i < 0
|
104
|
-
i = i...i+l
|
105
|
-
elsif ! i.kind_of?(Range)
|
106
|
-
return self.at(i)
|
107
|
-
end
|
108
|
-
nha = self.class.new
|
109
|
-
i.each { |j| nha.set(nha.length,get(j)) if has_key?(j) }
|
110
|
-
nha
|
111
|
-
end
|
112
|
-
|
113
|
-
alias set []=
|
114
|
-
protected :set
|
115
|
-
|
116
|
-
def []=(i,b,c=nil)
|
117
|
-
if c
|
118
|
-
rng = (Integer(i)..Integer(i+b))
|
119
|
-
b = c
|
120
|
-
elsif i.kind_of? Range
|
121
|
-
rng = i
|
122
|
-
else
|
123
|
-
self.set(Integer(i),b)
|
124
|
-
return b
|
125
|
-
end
|
126
|
-
if b == nil
|
127
|
-
rng.each { |i| qdelete(i) }
|
128
|
-
self.reindex!
|
129
|
-
elsif b.kind_of?(Array) or b.kind_of?(self.class)
|
130
|
-
j = 0
|
131
|
-
rng.each { |i| self[i] = b[j]; j+=1 }
|
132
|
-
else
|
133
|
-
rng.each { |i| qdelete(i) }
|
134
|
-
self[rng.fist] = b
|
135
|
-
self.reindex!
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
def |(ha)
|
140
|
-
nha = self.dup
|
141
|
-
ha.each { |v| nha << v if !nha.has_value?(v) }
|
142
|
-
nha
|
143
|
-
end
|
144
|
-
|
145
|
-
def assoc(k)
|
146
|
-
(0...self.length).each { |i| return self.fetch(i) if self.fetch(i)[0] == k }
|
147
|
-
return nil
|
148
|
-
end
|
149
|
-
|
150
|
-
def at(i)
|
151
|
-
i = self.length + i if i <= -1
|
152
|
-
get(i)
|
153
|
-
#return nil if i < 0 or i >= self.length
|
154
|
-
#return self.fetch(i)
|
155
|
-
end
|
156
|
-
|
157
|
-
#def choice
|
158
|
-
#end
|
159
|
-
|
160
|
-
# clear okay
|
161
|
-
|
162
|
-
#
|
163
|
-
def collect
|
164
|
-
nha = self.class.new
|
165
|
-
(0...self.length).each { |i| nha << yield(self.fetch(i)) }
|
166
|
-
nha
|
167
|
-
end
|
168
|
-
|
169
|
-
def collect!
|
170
|
-
nha = self.class.new
|
171
|
-
(0...self.length).each { |i| nha << yield(self.fetch(i)) }
|
172
|
-
self.replace(nha)
|
173
|
-
end
|
174
|
-
|
175
|
-
#def combination
|
176
|
-
#end
|
177
|
-
|
178
|
-
#
|
179
|
-
def compact
|
180
|
-
nha, j = self.class.new, 0
|
181
|
-
(0..self.length-1).each do |i|
|
182
|
-
if self.fetch(i) != nil
|
183
|
-
nha.set(j,self.fetch(i))
|
184
|
-
j+=1
|
185
|
-
end
|
186
|
-
end
|
187
|
-
nha
|
188
|
-
end
|
189
|
-
|
190
|
-
def compact!
|
191
|
-
if self.has_value?(nil)
|
192
|
-
nha, j = self.class.new, 0
|
193
|
-
(0..self.length-1).each do |i|
|
194
|
-
if self.fetch(i) != nil
|
195
|
-
nha.set(j,self.fetch(i))
|
196
|
-
j+=1
|
197
|
-
end
|
198
|
-
end
|
199
|
-
return self.replace(nha)
|
200
|
-
else
|
201
|
-
return nil
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
#
|
206
|
-
def concat(ha)
|
207
|
-
(0...ha.length).each { |i| self.set(self.length,ha.fetch(i)) }
|
208
|
-
self
|
209
|
-
end
|
210
|
-
|
211
|
-
def count(e=nil)
|
212
|
-
if block_given?
|
213
|
-
cnt = 0
|
214
|
-
(0...self.length).each { |i| cnt += 1 if yield(self.fetch(i)) }
|
215
|
-
return cnt
|
216
|
-
else
|
217
|
-
cnt = 0
|
218
|
-
(0...self.length).each { |i| cnt += 1 if self.fetch(i) == e }
|
219
|
-
return cnt
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
#
|
224
|
-
alias qdelete delete
|
225
|
-
private :qdelete
|
226
|
-
|
227
|
-
#
|
228
|
-
def delete(e)
|
229
|
-
if has_value?(e)
|
230
|
-
qdelete_if { |i,v| v == e }
|
231
|
-
reindex!
|
232
|
-
return e
|
233
|
-
else
|
234
|
-
return yield if block_given?
|
235
|
-
return nil
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
#
|
240
|
-
def delete_at(i)
|
241
|
-
if self.has_key?(i)
|
242
|
-
e = self.fetch(i)
|
243
|
-
qdelete(i)
|
244
|
-
reindex!
|
245
|
-
return e
|
246
|
-
else
|
247
|
-
return nil
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
alias qdelete_if delete_if
|
252
|
-
private :qdelete_if
|
253
|
-
|
254
|
-
#
|
255
|
-
def delete_if
|
256
|
-
qdelete_if { |i,v| yield(v) }
|
257
|
-
reindex!
|
258
|
-
end
|
259
|
-
|
260
|
-
def each
|
261
|
-
(0...self.length).each{ |i| yield(get(i)) }
|
262
|
-
end
|
263
|
-
|
264
|
-
def each_index
|
265
|
-
(0...self.length).each{ |i| yield(i) }
|
266
|
-
end
|
267
|
-
|
268
|
-
# empty? okay as is
|
269
|
-
|
270
|
-
def eql?(ha)
|
271
|
-
return false if self.length != ha.length
|
272
|
-
return true if (0...self.length).all? { |i| self.fetch(i).eql?(ha.fetch(i)) }
|
273
|
-
return false
|
274
|
-
end
|
275
|
-
|
276
|
-
def fill(f,s=nil,l=nil)
|
277
|
-
if s.kind_of?(Range)
|
278
|
-
r = s
|
279
|
-
else
|
280
|
-
s = 0 if !s
|
281
|
-
l = self.length - s if !l
|
282
|
-
r = s...(s+l)
|
283
|
-
end
|
284
|
-
r.each{ |i| self.set(i,f) }
|
285
|
-
self
|
286
|
-
end
|
287
|
-
|
288
|
-
def first
|
289
|
-
return nil if self.empty?
|
290
|
-
self.fetch(0)
|
291
|
-
end
|
292
|
-
|
293
|
-
def flatten
|
294
|
-
nha = self.class.new
|
295
|
-
(0...self.length).each do |i|
|
296
|
-
sfi = self.fetch(i)
|
297
|
-
if sfi.kind_of?(self.class) or sfi.kind_of?(Array)
|
298
|
-
nha.concat(sfi.flatten)
|
299
|
-
else
|
300
|
-
nha.set(nha.length,sfi)
|
301
|
-
end
|
302
|
-
end
|
303
|
-
nha
|
304
|
-
end
|
305
|
-
|
306
|
-
def flatten!
|
307
|
-
return nil if !self.any? { |e| e.kind_of?(self.class) or e.kind_of?(Array) }
|
308
|
-
self.replace(self.flatten)
|
309
|
-
end
|
310
|
-
|
311
|
-
def include?(v)
|
312
|
-
self.has_value?(v)
|
313
|
-
end
|
314
|
-
|
315
|
-
#
|
316
|
-
def insert(index, *objs)
|
317
|
-
index = size + index + 1 if index < 0
|
318
|
-
tail = self[index...size]
|
319
|
-
objs.each_with_index do |obj, i|
|
320
|
-
set(index + i, obj)
|
321
|
-
end
|
322
|
-
tail.each_with_index do |obj, i|
|
323
|
-
set(objs.size + index + i, obj)
|
324
|
-
end
|
325
|
-
self
|
326
|
-
end
|
327
|
-
|
328
|
-
# index okay
|
329
|
-
|
330
|
-
#
|
331
|
-
def join(sep='')
|
332
|
-
s = ''
|
333
|
-
(0...self.length).each { |i| s << "#{self.fetch(i)}#{sep}" }
|
334
|
-
return s.chomp(sep)
|
335
|
-
end
|
336
|
-
|
337
|
-
#
|
338
|
-
def last
|
339
|
-
self[self.length-1]
|
340
|
-
end
|
341
|
-
|
342
|
-
# length okay
|
343
|
-
|
344
|
-
#
|
345
|
-
alias map! collect!
|
346
|
-
|
347
|
-
#
|
348
|
-
def nitems
|
349
|
-
cnt = 0
|
350
|
-
(0...self.length).each { |i| cnt += 1 if self.fetch(i) != nil }
|
351
|
-
cnt
|
352
|
-
end
|
353
|
-
|
354
|
-
def pack(*args)
|
355
|
-
self.to_a.pack(*args)
|
356
|
-
end
|
357
|
-
|
358
|
-
#def permutation
|
359
|
-
#end
|
360
|
-
|
361
|
-
#
|
362
|
-
def pop
|
363
|
-
self.delete_at(self.length-1)
|
364
|
-
end
|
365
|
-
|
366
|
-
#def product
|
367
|
-
#end
|
368
|
-
|
369
|
-
#
|
370
|
-
def push(*e)
|
371
|
-
self.concat(e)
|
372
|
-
end
|
373
|
-
|
374
|
-
def rassoc(k)
|
375
|
-
(0...self.length).each { |i| return self.fetch(i) if self.fetch(i)[1] == k }
|
376
|
-
return nil
|
377
|
-
end
|
378
|
-
|
379
|
-
def reindex
|
380
|
-
nha, j, k, tl = self.class.new, 0, 0, self.length
|
381
|
-
while k < tl
|
382
|
-
if self.has_key?(j)
|
383
|
-
nha.set(k,self.fetch(j))
|
384
|
-
j+=1; k+=1
|
385
|
-
else
|
386
|
-
j+=1
|
387
|
-
end
|
388
|
-
end
|
389
|
-
nha
|
390
|
-
end
|
391
|
-
|
392
|
-
def reindex!
|
393
|
-
self.replace(self.reindex)
|
394
|
-
end
|
395
|
-
|
396
|
-
def reject!
|
397
|
-
chg=nil
|
398
|
-
qdelete_if { |i,v| r=yield(v); chg=true if r; r }
|
399
|
-
return nil if !chg
|
400
|
-
reindex!
|
401
|
-
end
|
402
|
-
|
403
|
-
#def replace(ha)
|
404
|
-
# if ha.length < self.length
|
405
|
-
# (ha.length..self.length-1).each { |i| self.delete(i) }
|
406
|
-
# (0..ha.length-1).each { |i| self.set(i,ha[i]) }
|
407
|
-
# end
|
408
|
-
#end
|
409
|
-
|
410
|
-
def reverse
|
411
|
-
nha = self.class.new
|
412
|
-
(0...self.length).each { |i| nha.set(self.length-1-i,self.fetch(i)) }
|
413
|
-
nha
|
414
|
-
end
|
415
|
-
|
416
|
-
def reverse!
|
417
|
-
(0...self.length/2).each do |i|
|
418
|
-
ri = self.length-1-i
|
419
|
-
tmp = self.fetch(ri)
|
420
|
-
self.set(ri,self.fetch(i))
|
421
|
-
self.set(i,tmp)
|
422
|
-
end
|
423
|
-
self
|
424
|
-
end
|
425
|
-
|
426
|
-
def reverse_each
|
427
|
-
i = self.length - 1
|
428
|
-
while i >= 0
|
429
|
-
yield(self.fetch(i))
|
430
|
-
i -= 1
|
431
|
-
end
|
432
|
-
end
|
433
|
-
|
434
|
-
def rindex(e)
|
435
|
-
i = self.length - 1
|
436
|
-
while i >= 0
|
437
|
-
return i if self.fetch(i) == e
|
438
|
-
i -= 1
|
439
|
-
end
|
440
|
-
return nil
|
441
|
-
end
|
442
|
-
|
443
|
-
def shift
|
444
|
-
e1 = self[0]
|
445
|
-
tl = self.length - 1
|
446
|
-
(1..tl).each { |i| self.set(i-1,self.fetch(i)) }
|
447
|
-
self.delete_at(tl)
|
448
|
-
e1
|
449
|
-
end
|
450
|
-
|
451
|
-
#
|
452
|
-
def shuffle
|
453
|
-
dup.shuffle!
|
454
|
-
end
|
455
|
-
|
456
|
-
#
|
457
|
-
def shuffle!
|
458
|
-
size.times do
|
459
|
-
a = rand(size).to_i
|
460
|
-
b = rand(size).to_i
|
461
|
-
self[a], self[b] = self[b], self[a]
|
462
|
-
end
|
463
|
-
end
|
464
|
-
|
465
|
-
# size okay
|
466
|
-
|
467
|
-
#
|
468
|
-
def slice(*args)
|
469
|
-
self[*args]
|
470
|
-
end
|
471
|
-
|
472
|
-
def slice!(*args)
|
473
|
-
result = self[*args]
|
474
|
-
self[*args] = nil
|
475
|
-
result
|
476
|
-
end
|
477
|
-
|
478
|
-
def sort
|
479
|
-
raise "SparseArray does not currently support sorting with blocks" if block_given?
|
480
|
-
nha = self.dup
|
481
|
-
qsort(nha,0,nha.length-1)
|
482
|
-
end
|
483
|
-
|
484
|
-
def qsort(ha, l, r)
|
485
|
-
l_hold = l
|
486
|
-
r_hold = r
|
487
|
-
pivot = ha[l]
|
488
|
-
while l < r
|
489
|
-
r -= 1 while (ha[r] <=> pivot) >= 0 and l < r
|
490
|
-
if l != r
|
491
|
-
ha[l] = ha[r]
|
492
|
-
l += 1
|
493
|
-
end
|
494
|
-
l += 1 while (ha[l] <=> pivot) <= 0 and l < r
|
495
|
-
if l != r
|
496
|
-
ha[r] = ha[l]
|
497
|
-
r -= 1
|
498
|
-
end
|
499
|
-
end
|
500
|
-
ha[l] = pivot
|
501
|
-
pivot = l
|
502
|
-
l = l_hold
|
503
|
-
r = r_hold
|
504
|
-
qsort(ha,l,pivot-1) if l < pivot
|
505
|
-
qsort(ha,pivot+1,r) if r > pivot
|
506
|
-
ha
|
507
|
-
end
|
508
|
-
|
509
|
-
def sort!
|
510
|
-
raise "SparseArray does not currently support sorting with blocks" if block_given?
|
511
|
-
qsort(self,0,self.length-1)
|
512
|
-
end
|
513
|
-
|
514
|
-
def to_a
|
515
|
-
a = []
|
516
|
-
(0..self.length-1).each { |i| a << self.fetch(i) }
|
517
|
-
a
|
518
|
-
end
|
519
|
-
|
520
|
-
def to_ary
|
521
|
-
self
|
522
|
-
end
|
523
|
-
|
524
|
-
def to_h
|
525
|
-
h = Hash.new
|
526
|
-
self.each { |k,v| h[k] = v }
|
527
|
-
h
|
528
|
-
end
|
529
|
-
|
530
|
-
def to_s
|
531
|
-
self.join
|
532
|
-
end
|
533
|
-
|
534
|
-
#
|
535
|
-
#def transpose
|
536
|
-
#end
|
537
|
-
|
538
|
-
#
|
539
|
-
def uniq
|
540
|
-
nha = self.class.new
|
541
|
-
(0..self.length-1).each do |i|
|
542
|
-
nha[nha.length] = self[i] if !nha.has_value?(self[i])
|
543
|
-
end
|
544
|
-
nha
|
545
|
-
end
|
546
|
-
|
547
|
-
#
|
548
|
-
def uniq!
|
549
|
-
j = 0
|
550
|
-
(1..self.length-1).each do |i|
|
551
|
-
if !self[0..j].has_value?(self[i])
|
552
|
-
self[j+1] = self[i]
|
553
|
-
j+=1
|
554
|
-
end
|
555
|
-
end
|
556
|
-
(j+1..self.length-1).each { |i| qdelete(i) }
|
557
|
-
end
|
558
|
-
|
559
|
-
def unshift(e)
|
560
|
-
i = self.length - 1
|
561
|
-
while i >= 0
|
562
|
-
self.set(i+1,self.fetch(i))
|
563
|
-
return i if self.fetch(i) == e
|
564
|
-
i -= 1
|
565
|
-
end
|
566
|
-
self.set(0,e)
|
567
|
-
self
|
568
|
-
end
|
569
|
-
|
570
|
-
def values_at(*ix)
|
571
|
-
nha = self.class.new
|
572
|
-
ix.each {|i| nha[nha.length] = self.at(i)}
|
573
|
-
nha
|
574
|
-
end
|
575
|
-
|
576
|
-
end
|
577
|
-
|