array-sorted 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +12 -0
- data/README.md +44 -0
- data/lib/array-hooked-sorted.rb +2 -0
- data/lib/array-sorted.rb +2 -0
- data/lib/array/hooked/sorted.rb +2 -0
- data/lib/array/namespaces.rb +6 -0
- data/lib/array/requires.rb +27 -0
- data/lib/array/sorted.rb +14 -0
- data/lib/array/sorted/array_interface.rb +173 -0
- data/lib/hooked/array/sorted.rb +2 -0
- data/lib/sorted_array.rb +6 -0
- data/lib/sorted_array/array_interface.rb +23 -0
- data/spec/array/sorted_spec.rb +667 -0
- metadata +74 -0
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
== 6/14/2012
|
3
|
+
|
4
|
+
Initial release - split from compositing-sorted-array.
|
5
|
+
|
6
|
+
== 6/15/2012
|
7
|
+
|
8
|
+
fixed SortedArray::Interface to include Array::Sorted::ArrayInterface
|
9
|
+
|
10
|
+
== 6/30/2012
|
11
|
+
|
12
|
+
Renamed from sorted-array to array-sorted. File schema updated to reflect gem name.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Sorted Array #
|
2
|
+
|
3
|
+
http://rubygems.org/gems/array-sorted
|
4
|
+
|
5
|
+
# Description #
|
6
|
+
|
7
|
+
Provides ::Array::Sorted and ::SortedArray.
|
8
|
+
|
9
|
+
# Summary #
|
10
|
+
|
11
|
+
A subclass of Array::Hooked that also keeps array sorted.
|
12
|
+
|
13
|
+
# Install #
|
14
|
+
|
15
|
+
* sudo gem install array-sorted
|
16
|
+
|
17
|
+
# Usage #
|
18
|
+
|
19
|
+
Just like Array or Array::Hooked (provided by hooked_array)! Provides additional support in :initialize to declare sorting block.
|
20
|
+
|
21
|
+
# License #
|
22
|
+
|
23
|
+
(The MIT License)
|
24
|
+
|
25
|
+
Copyright (c) Asher
|
26
|
+
|
27
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
28
|
+
a copy of this software and associated documentation files (the
|
29
|
+
'Software'), to deal in the Software without restriction, including
|
30
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
31
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
32
|
+
permit persons to whom the Software is furnished to do so, subject to
|
33
|
+
the following conditions:
|
34
|
+
|
35
|
+
The above copyright notice and this permission notice shall be
|
36
|
+
included in all copies or substantial portions of the Software.
|
37
|
+
|
38
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
39
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
40
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
41
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
42
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
43
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
44
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/array-sorted.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
basepath = 'sorted'
|
3
|
+
|
4
|
+
files = [
|
5
|
+
|
6
|
+
'array_interface'
|
7
|
+
|
8
|
+
]
|
9
|
+
|
10
|
+
second_basepath = '../sorted_array'
|
11
|
+
|
12
|
+
second_files = [
|
13
|
+
|
14
|
+
'array_interface'
|
15
|
+
|
16
|
+
]
|
17
|
+
|
18
|
+
files.each do |this_file|
|
19
|
+
require_relative( File.join( basepath, this_file ) + '.rb' )
|
20
|
+
end
|
21
|
+
second_files.each do |this_file|
|
22
|
+
require_relative( File.join( second_basepath, this_file ) + '.rb' )
|
23
|
+
end
|
24
|
+
|
25
|
+
require_relative( basepath + '.rb' )
|
26
|
+
require_relative( second_basepath + '.rb' )
|
27
|
+
|
data/lib/array/sorted.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
|
2
|
+
require 'array/hooked'
|
3
|
+
|
4
|
+
# namespaces that have to be declared ahead of time for proper load order
|
5
|
+
require_relative './namespaces'
|
6
|
+
|
7
|
+
# source file requires
|
8
|
+
require_relative './requires.rb'
|
9
|
+
|
10
|
+
class ::Array::Sorted < ::Array::Hooked
|
11
|
+
|
12
|
+
include ::Array::Sorted::ArrayInterface
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
|
2
|
+
module ::Array::Sorted::ArrayInterface
|
3
|
+
|
4
|
+
include ::Array::Hooked::ArrayInterface
|
5
|
+
|
6
|
+
instances_identify_as!( ::Array::Sorted )
|
7
|
+
|
8
|
+
################
|
9
|
+
# initialize #
|
10
|
+
################
|
11
|
+
|
12
|
+
# Adds optional block for retaining sort order.
|
13
|
+
# @yield Block to use to determine sort order.
|
14
|
+
def initialize( *args, & sort_object_block )
|
15
|
+
|
16
|
+
super( *args )
|
17
|
+
|
18
|
+
if block_given?
|
19
|
+
@sort_object_block = sort_object_block
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
##############
|
25
|
+
# reverse! #
|
26
|
+
##############
|
27
|
+
|
28
|
+
def reverse!
|
29
|
+
|
30
|
+
@sort_order_reversed = ! @sort_order_reversed
|
31
|
+
|
32
|
+
super
|
33
|
+
|
34
|
+
return self
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
##############
|
39
|
+
# collect! #
|
40
|
+
# map! #
|
41
|
+
##############
|
42
|
+
|
43
|
+
def collect!
|
44
|
+
|
45
|
+
return to_enum unless block_given?
|
46
|
+
|
47
|
+
replacement_array = [ ]
|
48
|
+
self.each_with_index do |this_object, index|
|
49
|
+
replacement_object = yield( this_object )
|
50
|
+
replacement_array[ index ] = replacement_object
|
51
|
+
end
|
52
|
+
|
53
|
+
replace( replacement_array )
|
54
|
+
|
55
|
+
return self
|
56
|
+
|
57
|
+
end
|
58
|
+
alias_method :map!, :collect!
|
59
|
+
|
60
|
+
###########
|
61
|
+
# sort! #
|
62
|
+
###########
|
63
|
+
|
64
|
+
def sort!( & block )
|
65
|
+
|
66
|
+
if block_given?
|
67
|
+
self.each_with_index do |this_member, index|
|
68
|
+
unless index + 1 == count
|
69
|
+
sort_object = @sort_object_block ? @sort_object_block.call( this_member ) : this_member
|
70
|
+
yield( sort_object, self[ index + 1 ] )
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
return self
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
##############
|
80
|
+
# sort_by! #
|
81
|
+
##############
|
82
|
+
|
83
|
+
def sort_by!( & block )
|
84
|
+
|
85
|
+
return to_enum unless block_given?
|
86
|
+
|
87
|
+
self.each do |this_member|
|
88
|
+
sort_object = @sort_object_block ? @sort_object_block.call( this_member ) : this_member
|
89
|
+
yield( sort_object )
|
90
|
+
end
|
91
|
+
|
92
|
+
return self
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
###############################
|
97
|
+
# perform_set_between_hooks #
|
98
|
+
###############################
|
99
|
+
|
100
|
+
def perform_set_between_hooks( index, object )
|
101
|
+
|
102
|
+
unless index >= count
|
103
|
+
delete_at( index )
|
104
|
+
end
|
105
|
+
|
106
|
+
return perform_insert_between_hooks( index, object )
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
##################################
|
111
|
+
# perform_insert_between_hooks #
|
112
|
+
##################################
|
113
|
+
|
114
|
+
def perform_insert_between_hooks( index, *objects )
|
115
|
+
|
116
|
+
# we ignore the index and insert in sorted order
|
117
|
+
|
118
|
+
# we have to have at least one member for comparison-based insert (to retain sorted order)
|
119
|
+
|
120
|
+
objects.each do |this_object|
|
121
|
+
|
122
|
+
insert_occurred = false
|
123
|
+
|
124
|
+
if this_object.nil?
|
125
|
+
if @sort_order_reversed
|
126
|
+
super( count, this_object )
|
127
|
+
else
|
128
|
+
super( 0, this_object )
|
129
|
+
end
|
130
|
+
next
|
131
|
+
end
|
132
|
+
|
133
|
+
self.each_with_index do |this_member, this_index|
|
134
|
+
|
135
|
+
insert_sort_object = this_object
|
136
|
+
existing_sort_object = this_member
|
137
|
+
|
138
|
+
if @sort_object_block
|
139
|
+
insert_sort_object = @sort_object_block.call( this_object )
|
140
|
+
existing_sort_object = @sort_object_block.call( this_member )
|
141
|
+
end
|
142
|
+
|
143
|
+
if @sort_order_reversed
|
144
|
+
|
145
|
+
case insert_sort_object <=> existing_sort_object
|
146
|
+
when 0, 1
|
147
|
+
super( this_index, this_object )
|
148
|
+
insert_occurred = true
|
149
|
+
break
|
150
|
+
end
|
151
|
+
|
152
|
+
else
|
153
|
+
|
154
|
+
case insert_sort_object <=> existing_sort_object
|
155
|
+
when 0, -1
|
156
|
+
super( this_index, this_object )
|
157
|
+
insert_occurred = true
|
158
|
+
break
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
unless insert_occurred
|
166
|
+
super( count, this_object )
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
data/lib/sorted_array.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
module ::SortedArray::Interface
|
3
|
+
|
4
|
+
include ::Array::Sorted::ArrayInterface
|
5
|
+
include ::HookedArray::Interface
|
6
|
+
|
7
|
+
instances_identify_as!( ::SortedArray )
|
8
|
+
|
9
|
+
################
|
10
|
+
# initialize #
|
11
|
+
################
|
12
|
+
|
13
|
+
def initialize( configuration_instance = nil, & sort_object_block )
|
14
|
+
|
15
|
+
super( configuration_instance )
|
16
|
+
|
17
|
+
if block_given?
|
18
|
+
@sort_object_block = sort_object_block
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,667 @@
|
|
1
|
+
|
2
|
+
require_relative '../../lib/array-sorted.rb'
|
3
|
+
|
4
|
+
describe ::Array::Sorted do
|
5
|
+
|
6
|
+
################
|
7
|
+
# initialize #
|
8
|
+
################
|
9
|
+
|
10
|
+
it 'can add initialize with an ancestor, inheriting its values and linking to it as a child' do
|
11
|
+
|
12
|
+
sorted_array = ::Array::Sorted.new
|
13
|
+
|
14
|
+
sorted_array.should == [ ]
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
#########
|
19
|
+
# []= #
|
20
|
+
#########
|
21
|
+
|
22
|
+
it 'can add elements' do
|
23
|
+
|
24
|
+
sorted_array = ::Array::Sorted.new
|
25
|
+
|
26
|
+
sorted_array[ 0 ] = :A
|
27
|
+
sorted_array.should == [ :A ]
|
28
|
+
|
29
|
+
sorted_array[ 1 ] = :B
|
30
|
+
sorted_array.should == [ :A, :B ]
|
31
|
+
|
32
|
+
sorted_array[ 0 ] = :D
|
33
|
+
sorted_array.should == [ :B, :D ]
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
############
|
38
|
+
# insert #
|
39
|
+
############
|
40
|
+
|
41
|
+
it 'can insert elements' do
|
42
|
+
|
43
|
+
sorted_array = ::Array::Sorted.new
|
44
|
+
|
45
|
+
sorted_array.insert( 3, :D )
|
46
|
+
sorted_array.should == [ nil, nil, nil, :D ]
|
47
|
+
|
48
|
+
sorted_array.insert( 1, :B )
|
49
|
+
sorted_array.should == [ nil, nil, nil, :B, :D ]
|
50
|
+
|
51
|
+
sorted_array.insert( 2, :C )
|
52
|
+
sorted_array.should == [ nil, nil, nil, :B, :C, :D ]
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
##########
|
57
|
+
# push #
|
58
|
+
# << #
|
59
|
+
##########
|
60
|
+
|
61
|
+
it 'can add elements' do
|
62
|
+
|
63
|
+
sorted_array = ::Array::Sorted.new
|
64
|
+
|
65
|
+
sorted_array << :A
|
66
|
+
sorted_array.should == [ :A ]
|
67
|
+
|
68
|
+
sorted_array << :B
|
69
|
+
sorted_array.should == [ :A, :B ]
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
############
|
74
|
+
# concat #
|
75
|
+
# + #
|
76
|
+
############
|
77
|
+
|
78
|
+
it 'can add elements' do
|
79
|
+
|
80
|
+
# NOTE: this breaks + by causing it to modify the array like +=
|
81
|
+
# The alternative was worse.
|
82
|
+
|
83
|
+
sorted_array = ::Array::Sorted.new
|
84
|
+
|
85
|
+
sorted_array.concat( [ :A ] )
|
86
|
+
sorted_array.should == [ :A ]
|
87
|
+
|
88
|
+
sorted_array += [ :B ]
|
89
|
+
sorted_array.should == [ :A, :B ]
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
####################
|
94
|
+
# delete_objects #
|
95
|
+
####################
|
96
|
+
|
97
|
+
it 'can delete multiple elements' do
|
98
|
+
|
99
|
+
sorted_array = ::Array::Sorted.new
|
100
|
+
|
101
|
+
sorted_array += [ :A, :B ]
|
102
|
+
sorted_array.should == [ :A, :B ]
|
103
|
+
|
104
|
+
sorted_array.delete_objects( :A, :B )
|
105
|
+
sorted_array.should == [ ]
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
#######
|
110
|
+
# - #
|
111
|
+
#######
|
112
|
+
|
113
|
+
it 'can exclude elements' do
|
114
|
+
|
115
|
+
sorted_array = ::Array::Sorted.new
|
116
|
+
|
117
|
+
sorted_array.push( :A )
|
118
|
+
sorted_array.should == [ :A ]
|
119
|
+
|
120
|
+
sorted_array -= [ :A ]
|
121
|
+
sorted_array.should == [ ]
|
122
|
+
|
123
|
+
sorted_array.push( :B )
|
124
|
+
sorted_array.should == [ :B ]
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
############
|
129
|
+
# delete #
|
130
|
+
############
|
131
|
+
|
132
|
+
it 'can delete elements' do
|
133
|
+
|
134
|
+
sorted_array = ::Array::Sorted.new
|
135
|
+
|
136
|
+
sorted_array.push( :A )
|
137
|
+
sorted_array.should == [ :A ]
|
138
|
+
|
139
|
+
sorted_array.delete( :A )
|
140
|
+
sorted_array.should == [ ]
|
141
|
+
|
142
|
+
sorted_array.push( :B )
|
143
|
+
sorted_array.should == [ :B ]
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
###############
|
148
|
+
# delete_at #
|
149
|
+
###############
|
150
|
+
|
151
|
+
it 'can delete by indexes' do
|
152
|
+
|
153
|
+
sorted_array = ::Array::Sorted.new
|
154
|
+
|
155
|
+
sorted_array.push( :A )
|
156
|
+
sorted_array.should == [ :A ]
|
157
|
+
|
158
|
+
sorted_array.delete_at( 0 )
|
159
|
+
sorted_array.should == [ ]
|
160
|
+
|
161
|
+
sorted_array.push( :B )
|
162
|
+
sorted_array.should == [ :B ]
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
#######################
|
167
|
+
# delete_at_indexes #
|
168
|
+
#######################
|
169
|
+
|
170
|
+
it 'can delete by indexes' do
|
171
|
+
|
172
|
+
sorted_array = ::Array::Sorted.new
|
173
|
+
|
174
|
+
sorted_array.push( :A, :B, :C )
|
175
|
+
sorted_array.should == [ :A, :B, :C ]
|
176
|
+
|
177
|
+
sorted_array.delete_at_indexes( 0, 1 )
|
178
|
+
sorted_array.should == [ :C ]
|
179
|
+
|
180
|
+
end
|
181
|
+
|
182
|
+
###############
|
183
|
+
# delete_if #
|
184
|
+
###############
|
185
|
+
|
186
|
+
it 'can delete by block' do
|
187
|
+
|
188
|
+
sorted_array = ::Array::Sorted.new
|
189
|
+
|
190
|
+
sorted_array.push( :A, :B, :C )
|
191
|
+
sorted_array.should == [ :A, :B, :C ]
|
192
|
+
sorted_array.delete_if do |object|
|
193
|
+
object != :C
|
194
|
+
end
|
195
|
+
sorted_array.should == [ :C ]
|
196
|
+
|
197
|
+
sorted_array.delete_if.is_a?( Enumerator ).should == true
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
#############
|
202
|
+
# keep_if #
|
203
|
+
#############
|
204
|
+
|
205
|
+
it 'can keep by block' do
|
206
|
+
|
207
|
+
sorted_array = ::Array::Sorted.new
|
208
|
+
|
209
|
+
sorted_array.push( :A, :B, :C )
|
210
|
+
sorted_array.should == [ :A, :B, :C ]
|
211
|
+
sorted_array.keep_if do |object|
|
212
|
+
object == :C
|
213
|
+
end
|
214
|
+
sorted_array.should == [ :C ]
|
215
|
+
|
216
|
+
end
|
217
|
+
|
218
|
+
##############
|
219
|
+
# compact! #
|
220
|
+
##############
|
221
|
+
|
222
|
+
it 'can compact' do
|
223
|
+
|
224
|
+
sorted_array = ::Array::Sorted.new
|
225
|
+
|
226
|
+
sorted_array.push( :A, nil, :B, nil, :C, nil )
|
227
|
+
sorted_array.should == [ nil, nil, nil, :A, :B, :C ]
|
228
|
+
sorted_array.compact!
|
229
|
+
sorted_array.should == [ :A, :B, :C ]
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
##############
|
234
|
+
# flatten! #
|
235
|
+
##############
|
236
|
+
|
237
|
+
it 'can flatten' do
|
238
|
+
|
239
|
+
sorted_array = ::Array::Sorted.new
|
240
|
+
|
241
|
+
sorted_array.push( :A, [ :F_A, :F_B ], :B, [ :F_C ], :C, [ :F_D ], [ :F_E ] )
|
242
|
+
sorted_array.should == [ :A, [ :F_A, :F_B ], :B, [ :F_C ], :C, [ :F_D ], [ :F_E ] ]
|
243
|
+
sorted_array.flatten!
|
244
|
+
sorted_array.should == [ :A, :B, :C, :F_A, :F_B, :F_C, :F_D, :F_E ]
|
245
|
+
|
246
|
+
end
|
247
|
+
|
248
|
+
#############
|
249
|
+
# reject! #
|
250
|
+
#############
|
251
|
+
|
252
|
+
it 'can reject' do
|
253
|
+
|
254
|
+
sorted_array = ::Array::Sorted.new
|
255
|
+
|
256
|
+
sorted_array.push( :A, :B, :C )
|
257
|
+
sorted_array.should == [ :A, :B, :C ]
|
258
|
+
sorted_array.reject! do |object|
|
259
|
+
object != :C
|
260
|
+
end
|
261
|
+
sorted_array.should == [ :C ]
|
262
|
+
|
263
|
+
sorted_array.reject!.is_a?( Enumerator ).should == true
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
#############
|
268
|
+
# replace #
|
269
|
+
#############
|
270
|
+
|
271
|
+
it 'can replace self' do
|
272
|
+
|
273
|
+
sorted_array = ::Array::Sorted.new
|
274
|
+
|
275
|
+
sorted_array.push( :A, :B, :C )
|
276
|
+
sorted_array.should == [ :A, :B, :C ]
|
277
|
+
sorted_array.replace( [ :D, :E, :F ] )
|
278
|
+
sorted_array.should == [ :D, :E, :F ]
|
279
|
+
|
280
|
+
end
|
281
|
+
|
282
|
+
##############
|
283
|
+
# reverse! #
|
284
|
+
##############
|
285
|
+
|
286
|
+
it 'can reverse self' do
|
287
|
+
|
288
|
+
sorted_array = ::Array::Sorted.new
|
289
|
+
|
290
|
+
sorted_array.push( :A, :B, :C )
|
291
|
+
sorted_array.should == [ :A, :B, :C ]
|
292
|
+
sorted_array.reverse!
|
293
|
+
sorted_array.should == [ :C, :B, :A ]
|
294
|
+
|
295
|
+
end
|
296
|
+
|
297
|
+
#############
|
298
|
+
# rotate! #
|
299
|
+
#############
|
300
|
+
|
301
|
+
it 'can rotate self' do
|
302
|
+
|
303
|
+
sorted_array = ::Array::Sorted.new
|
304
|
+
|
305
|
+
sorted_array.push( :A, :B, :C )
|
306
|
+
sorted_array.should == [ :A, :B, :C ]
|
307
|
+
|
308
|
+
sorted_array.rotate!
|
309
|
+
sorted_array.should == [ :A, :B, :C ]
|
310
|
+
|
311
|
+
sorted_array.rotate!( -1 )
|
312
|
+
sorted_array.should == [ :A, :B, :C ]
|
313
|
+
|
314
|
+
end
|
315
|
+
|
316
|
+
#############
|
317
|
+
# select! #
|
318
|
+
#############
|
319
|
+
|
320
|
+
it 'can keep by select' do
|
321
|
+
|
322
|
+
sorted_array = ::Array::Sorted.new
|
323
|
+
|
324
|
+
sorted_array.push( :A, :B, :C )
|
325
|
+
sorted_array.should == [ :A, :B, :C ]
|
326
|
+
sorted_array.select! do |object|
|
327
|
+
object == :C
|
328
|
+
end
|
329
|
+
sorted_array.should == [ :C ]
|
330
|
+
|
331
|
+
sorted_array.select!.is_a?( Enumerator ).should == true
|
332
|
+
|
333
|
+
end
|
334
|
+
|
335
|
+
##############
|
336
|
+
# shuffle! #
|
337
|
+
##############
|
338
|
+
|
339
|
+
it 'can shuffle self' do
|
340
|
+
|
341
|
+
sorted_array = ::Array::Sorted.new
|
342
|
+
|
343
|
+
sorted_array.push( :A, :B, :C )
|
344
|
+
sorted_array.should == [ :A, :B, :C ]
|
345
|
+
|
346
|
+
first_shuffle_version = sorted_array.dup
|
347
|
+
sorted_array.shuffle!
|
348
|
+
sorted_array.should == first_shuffle_version
|
349
|
+
|
350
|
+
end
|
351
|
+
|
352
|
+
##############
|
353
|
+
# collect! #
|
354
|
+
# map! #
|
355
|
+
##############
|
356
|
+
|
357
|
+
it 'can replace by collect/map' do
|
358
|
+
|
359
|
+
sorted_array = ::Array::Sorted.new
|
360
|
+
|
361
|
+
sorted_array.push( :A, :B, :C )
|
362
|
+
sorted_array.should == [ :A, :B, :C ]
|
363
|
+
sorted_array.collect! do |object|
|
364
|
+
:C
|
365
|
+
end
|
366
|
+
sorted_array.should == [ :C, :C, :C ]
|
367
|
+
|
368
|
+
sorted_array.collect!.is_a?( Enumerator ).should == true
|
369
|
+
|
370
|
+
end
|
371
|
+
|
372
|
+
###########
|
373
|
+
# sort! #
|
374
|
+
###########
|
375
|
+
|
376
|
+
it 'can replace by collect/map' do
|
377
|
+
|
378
|
+
sorted_array = ::Array::Sorted.new
|
379
|
+
|
380
|
+
sorted_array.push( :A, :B, :C )
|
381
|
+
sorted_array.should == [ :A, :B, :C ]
|
382
|
+
sorted_array.sort! do |a, b|
|
383
|
+
if a < b
|
384
|
+
1
|
385
|
+
elsif a > b
|
386
|
+
-1
|
387
|
+
elsif a == b
|
388
|
+
0
|
389
|
+
end
|
390
|
+
end
|
391
|
+
sorted_array.should == [ :A, :B, :C ]
|
392
|
+
|
393
|
+
sorted_array.sort!
|
394
|
+
sorted_array.should == [ :A, :B, :C ]
|
395
|
+
|
396
|
+
end
|
397
|
+
|
398
|
+
##############
|
399
|
+
# sort_by! #
|
400
|
+
##############
|
401
|
+
|
402
|
+
it 'can replace by collect/map' do
|
403
|
+
|
404
|
+
sorted_array = ::Array::Sorted.new
|
405
|
+
|
406
|
+
sorted_array.push( :A, :B, :C )
|
407
|
+
sorted_array.should == [ :A, :B, :C ]
|
408
|
+
sorted_array.sort_by! do |object|
|
409
|
+
case object
|
410
|
+
when :A
|
411
|
+
:B
|
412
|
+
when :B
|
413
|
+
:A
|
414
|
+
when :C
|
415
|
+
:C
|
416
|
+
end
|
417
|
+
end
|
418
|
+
sorted_array.should == [ :A, :B, :C ]
|
419
|
+
|
420
|
+
sorted_array.sort_by!.is_a?( Enumerator ).should == true
|
421
|
+
|
422
|
+
end
|
423
|
+
|
424
|
+
###########
|
425
|
+
# uniq! #
|
426
|
+
###########
|
427
|
+
|
428
|
+
it 'can remove non-unique elements' do
|
429
|
+
|
430
|
+
sorted_array = ::Array::Sorted.new
|
431
|
+
|
432
|
+
sorted_array.push( :A, :B, :C, :C, :C, :B, :A )
|
433
|
+
sorted_array.should == [ :A, :A, :B, :B, :C, :C, :C ]
|
434
|
+
sorted_array.uniq!
|
435
|
+
sorted_array.should == [ :A, :B, :C ]
|
436
|
+
|
437
|
+
end
|
438
|
+
|
439
|
+
#############
|
440
|
+
# unshift #
|
441
|
+
#############
|
442
|
+
|
443
|
+
it 'can unshift onto the first element' do
|
444
|
+
|
445
|
+
sorted_array = ::Array::Sorted.new
|
446
|
+
|
447
|
+
sorted_array += :A
|
448
|
+
sorted_array.should == [ :A ]
|
449
|
+
|
450
|
+
sorted_array.unshift( :B )
|
451
|
+
sorted_array.should == [ :A, :B ]
|
452
|
+
|
453
|
+
end
|
454
|
+
|
455
|
+
#########
|
456
|
+
# pop #
|
457
|
+
#########
|
458
|
+
|
459
|
+
it 'can pop the final element' do
|
460
|
+
|
461
|
+
sorted_array = ::Array::Sorted.new
|
462
|
+
|
463
|
+
sorted_array += :A
|
464
|
+
sorted_array.should == [ :A ]
|
465
|
+
|
466
|
+
sorted_array.pop.should == :A
|
467
|
+
sorted_array.should == [ ]
|
468
|
+
|
469
|
+
sorted_array += :B
|
470
|
+
sorted_array.should == [ :B ]
|
471
|
+
|
472
|
+
end
|
473
|
+
|
474
|
+
###########
|
475
|
+
# shift #
|
476
|
+
###########
|
477
|
+
|
478
|
+
it 'can shift the first element' do
|
479
|
+
|
480
|
+
sorted_array = ::Array::Sorted.new
|
481
|
+
|
482
|
+
sorted_array += :A
|
483
|
+
sorted_array.should == [ :A ]
|
484
|
+
|
485
|
+
sorted_array.shift.should == :A
|
486
|
+
sorted_array.should == [ ]
|
487
|
+
|
488
|
+
sorted_array += :B
|
489
|
+
sorted_array.should == [ :B ]
|
490
|
+
|
491
|
+
end
|
492
|
+
|
493
|
+
############
|
494
|
+
# slice! #
|
495
|
+
############
|
496
|
+
|
497
|
+
it 'can slice elements' do
|
498
|
+
|
499
|
+
sorted_array = ::Array::Sorted.new
|
500
|
+
|
501
|
+
sorted_array += :A
|
502
|
+
sorted_array.should == [ :A ]
|
503
|
+
|
504
|
+
sorted_array.slice!( 0, 1 ).should == [ :A ]
|
505
|
+
sorted_array.should == [ ]
|
506
|
+
|
507
|
+
sorted_array += :B
|
508
|
+
sorted_array.should == [ :B ]
|
509
|
+
|
510
|
+
end
|
511
|
+
|
512
|
+
###########
|
513
|
+
# clear #
|
514
|
+
###########
|
515
|
+
|
516
|
+
it 'can clear, causing present elements to be excluded' do
|
517
|
+
|
518
|
+
sorted_array = ::Array::Sorted.new
|
519
|
+
|
520
|
+
sorted_array += :A
|
521
|
+
sorted_array.should == [ :A ]
|
522
|
+
|
523
|
+
sorted_array.clear
|
524
|
+
sorted_array.should == [ ]
|
525
|
+
|
526
|
+
sorted_array += :B
|
527
|
+
sorted_array.should == [ :B ]
|
528
|
+
|
529
|
+
end
|
530
|
+
|
531
|
+
##################
|
532
|
+
# pre_set_hook #
|
533
|
+
##################
|
534
|
+
|
535
|
+
it 'has a hook that is called before setting a value; return value is used in place of object' do
|
536
|
+
|
537
|
+
class ::Array::Sorted::SubMockPreSet < ::Array::Sorted
|
538
|
+
|
539
|
+
def pre_set_hook( index, object, is_insert = false )
|
540
|
+
return :some_other_value
|
541
|
+
end
|
542
|
+
|
543
|
+
end
|
544
|
+
|
545
|
+
sorted_array = ::Array::Sorted::SubMockPreSet.new
|
546
|
+
|
547
|
+
sorted_array.push( :some_value )
|
548
|
+
|
549
|
+
sorted_array.should == [ :some_other_value ]
|
550
|
+
|
551
|
+
end
|
552
|
+
|
553
|
+
###################
|
554
|
+
# post_set_hook #
|
555
|
+
###################
|
556
|
+
|
557
|
+
it 'has a hook that is called after setting a value' do
|
558
|
+
|
559
|
+
class ::Array::Sorted::SubMockPostSet < ::Array::Sorted
|
560
|
+
|
561
|
+
def post_set_hook( index, object, is_insert = false )
|
562
|
+
return :some_other_value
|
563
|
+
end
|
564
|
+
|
565
|
+
end
|
566
|
+
|
567
|
+
sorted_array = ::Array::Sorted::SubMockPostSet.new
|
568
|
+
|
569
|
+
sorted_array.push( :some_value ).should == [ :some_other_value ]
|
570
|
+
|
571
|
+
sorted_array.should == [ :some_value ]
|
572
|
+
|
573
|
+
end
|
574
|
+
|
575
|
+
##################
|
576
|
+
# pre_get_hook #
|
577
|
+
##################
|
578
|
+
|
579
|
+
it 'has a hook that is called before getting a value; if return value is false, get does not occur' do
|
580
|
+
|
581
|
+
class ::Array::Sorted::SubMockPreGet < ::Array::Sorted
|
582
|
+
|
583
|
+
def pre_get_hook( index )
|
584
|
+
return false
|
585
|
+
end
|
586
|
+
|
587
|
+
end
|
588
|
+
|
589
|
+
sorted_array = ::Array::Sorted::SubMockPreGet.new
|
590
|
+
|
591
|
+
sorted_array.push( :some_value )
|
592
|
+
sorted_array[ 0 ].should == nil
|
593
|
+
|
594
|
+
sorted_array.should == [ :some_value ]
|
595
|
+
|
596
|
+
end
|
597
|
+
|
598
|
+
###################
|
599
|
+
# post_get_hook #
|
600
|
+
###################
|
601
|
+
|
602
|
+
it 'has a hook that is called after getting a value' do
|
603
|
+
|
604
|
+
class ::Array::Sorted::SubMockPostGet < ::Array::Sorted
|
605
|
+
|
606
|
+
def post_get_hook( index, object )
|
607
|
+
return :some_other_value
|
608
|
+
end
|
609
|
+
|
610
|
+
end
|
611
|
+
|
612
|
+
sorted_array = ::Array::Sorted::SubMockPostGet.new
|
613
|
+
|
614
|
+
sorted_array.push( :some_value )
|
615
|
+
sorted_array[ 0 ].should == :some_other_value
|
616
|
+
|
617
|
+
sorted_array.should == [ :some_value ]
|
618
|
+
|
619
|
+
end
|
620
|
+
|
621
|
+
#####################
|
622
|
+
# pre_delete_hook #
|
623
|
+
#####################
|
624
|
+
|
625
|
+
it 'has a hook that is called before deleting an index; if return value is false, delete does not occur' do
|
626
|
+
|
627
|
+
class ::Array::Sorted::SubMockPreDelete < ::Array::Sorted
|
628
|
+
|
629
|
+
def pre_delete_hook( index )
|
630
|
+
return false
|
631
|
+
end
|
632
|
+
|
633
|
+
end
|
634
|
+
|
635
|
+
sorted_array = ::Array::Sorted::SubMockPreDelete.new
|
636
|
+
|
637
|
+
sorted_array.push( :some_value )
|
638
|
+
sorted_array.delete_at( 0 )
|
639
|
+
|
640
|
+
sorted_array.should == [ :some_value ]
|
641
|
+
|
642
|
+
end
|
643
|
+
|
644
|
+
######################
|
645
|
+
# post_delete_hook #
|
646
|
+
######################
|
647
|
+
|
648
|
+
it 'has a hook that is called after deleting an index' do
|
649
|
+
|
650
|
+
class ::Array::Sorted::SubMockPostDelete < ::Array::Sorted
|
651
|
+
|
652
|
+
def post_delete_hook( index, object )
|
653
|
+
return :some_other_value
|
654
|
+
end
|
655
|
+
|
656
|
+
end
|
657
|
+
|
658
|
+
sorted_array = ::Array::Sorted::SubMockPostDelete.new
|
659
|
+
|
660
|
+
sorted_array.push( :some_value )
|
661
|
+
sorted_array.delete_at( 0 ).should == :some_other_value
|
662
|
+
|
663
|
+
sorted_array.should == [ ]
|
664
|
+
|
665
|
+
end
|
666
|
+
|
667
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: array-sorted
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Asher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: array-hooked
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: A subclass of Array::Hooked that also keeps array sorted.
|
31
|
+
email: asher@ridiculouspower.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- lib/array/hooked/sorted.rb
|
37
|
+
- lib/array/namespaces.rb
|
38
|
+
- lib/array/requires.rb
|
39
|
+
- lib/array/sorted/array_interface.rb
|
40
|
+
- lib/array/sorted.rb
|
41
|
+
- lib/array-hooked-sorted.rb
|
42
|
+
- lib/array-sorted.rb
|
43
|
+
- lib/hooked/array/sorted.rb
|
44
|
+
- lib/sorted_array/array_interface.rb
|
45
|
+
- lib/sorted_array.rb
|
46
|
+
- spec/array/sorted_spec.rb
|
47
|
+
- README.md
|
48
|
+
- CHANGELOG.rdoc
|
49
|
+
homepage: http://rubygems.org/gems/array-sorted
|
50
|
+
licenses: []
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project: array-sorted
|
69
|
+
rubygems_version: 1.8.23
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Provides Array::Sorted and SortedArray.
|
73
|
+
test_files: []
|
74
|
+
has_rdoc:
|