rb_lovely 0.6.5 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fbacf68a03009f39933493ff077a97ace08685c0
4
- data.tar.gz: 4c8d5d57b559c93215e7b9be01c95fb3c6073e40
3
+ metadata.gz: ee5ff98c21526db5492808222dda0a1323a6e307
4
+ data.tar.gz: abdce3830c9a0be152907318f05f0f7267332814
5
5
  SHA512:
6
- metadata.gz: 6b457ed98975e79f3d567f32c5ba42efa38df04400918908d3213f2a5461133044f4dcde5f6565e0b57fe6364aeecfd607d1c34582c57f9b251ca132eaf51392
7
- data.tar.gz: 0402307048f4051120ccc841dc86e1971de96cfe4c927c67ad0c0c084219ca765a797c0a40944cbd7b15564ffe69015555ad89c7698b3c0e6eae866bf526781a
6
+ metadata.gz: 0ae92b111ea962b4e7e962d556a428c3627e62d724417e05846f875177c76e4b37fd9816103567ae50139dabe55da64e0ac1f835341b5ec6c883505d6be492c6
7
+ data.tar.gz: 91f3b2d225924117724ebdeb3a23948eeb728500d4bf60635a4687385e1038de8aa6552ee33cf67ec01d045555bced30c96030f85def4452d9d2dc6414d6640c
@@ -5,7 +5,7 @@
5
5
 
6
6
  namespace rb_lovely {
7
7
 
8
- auto rbInspect = [](VALUE val) { return RSTRING_PTR(rb_funcall(val, inspectSym, 0)); };
8
+ auto rbInspect = [](VALUE val) { return RSTRING_PTR(rb_inspect(val)); };
9
9
 
10
10
  }
11
11
  #endif
@@ -5,7 +5,6 @@ namespace rb_lovely {
5
5
  VALUE rbMod;
6
6
  VALUE cmpMethSym;
7
7
  VALUE hashEqualitySym;
8
- VALUE inspectSym;
9
8
  VALUE hashSym;
10
9
  VALUE compareSym;
11
10
  VALUE callSym;
@@ -7,7 +7,6 @@ namespace rb_lovely {
7
7
  extern VALUE rbMod;
8
8
  extern VALUE cmpMethSym;
9
9
  extern VALUE hashEqualitySym;
10
- extern VALUE inspectSym;
11
10
  extern VALUE hashSym;
12
11
  extern VALUE callSym;
13
12
  extern VALUE compareSym;
@@ -39,7 +38,6 @@ static void initRubyUtil() {
39
38
  rbMod = rb_define_module("RbLovely");
40
39
  cmpMethSym = rb_intern("<=>");
41
40
  hashEqualitySym = rb_intern("eql?");
42
- inspectSym = rb_intern("inspect");
43
41
  hashSym = rb_intern("hash");
44
42
  callSym = rb_intern("call");
45
43
  compareSym = ID2SYM(rb_intern("compare"));
@@ -181,10 +181,31 @@ VALUE hashToString(VALUE self) {
181
181
 
182
182
  VALUE hashFirst(VALUE self) {
183
183
  Hash* hash = rubyCast<Hash>(self);
184
- return hash->container.empty() ? Qnil : hash->container.get<1>().begin()->val;
184
+ if (hash->container.empty()) {
185
+ return Qnil;
186
+ }
187
+
188
+ auto &pair = *hash->container.get<1>().begin();
189
+ return rb_ary_new3(2, pair.key, pair.val);
185
190
  }
186
191
 
187
192
  VALUE hashLast(VALUE self) {
193
+ Hash* hash = rubyCast<Hash>(self);
194
+ if (hash->container.empty()) {
195
+ return Qnil;
196
+ }
197
+
198
+ auto it = hash->container.get<1>().end();
199
+ --it;
200
+ return rb_ary_new3(2, it->key, it->val);
201
+ }
202
+
203
+ VALUE hashFirstValue(VALUE self) {
204
+ Hash* hash = rubyCast<Hash>(self);
205
+ return hash->container.empty() ? Qnil : hash->container.get<1>().begin()->val;
206
+ }
207
+
208
+ VALUE hashLastValue(VALUE self) {
188
209
  Hash* hash = rubyCast<Hash>(self);
189
210
  if (hash->container.empty())
190
211
  return Qnil;
@@ -212,13 +233,41 @@ VALUE hashShift(VALUE self) {
212
233
  if (hash->container.empty())
213
234
  return Qnil;
214
235
 
236
+ auto& idx = hash->container.get<1>();
237
+ auto keyBak = idx.begin()->key;
238
+ auto valBak = idx.begin()->val;
239
+ idx.erase(idx.begin());
240
+
241
+ return rb_ary_new3(2, keyBak, valBak);
242
+ }
243
+
244
+ VALUE hashPop(VALUE self) {
245
+ Hash* hash = rubyCast<Hash>(self);
246
+ if (hash->container.empty())
247
+ return Qnil;
248
+
249
+ auto& idx = hash->container.get<1>();
250
+ auto last = idx.end();
251
+ --last;
252
+ auto keyBak = last->key;
253
+ auto valBak = last->val;
254
+ idx.erase(last);
255
+
256
+ return rb_ary_new3(2, keyBak, valBak);
257
+ }
258
+
259
+ VALUE hashShiftValue(VALUE self) {
260
+ Hash* hash = rubyCast<Hash>(self);
261
+ if (hash->container.empty())
262
+ return Qnil;
263
+
215
264
  auto& idx = hash->container.get<1>();
216
265
  auto bak = idx.begin()->val;
217
266
  idx.erase(idx.begin());
218
267
  return bak;
219
268
  }
220
269
 
221
- VALUE hashPop(VALUE self) {
270
+ VALUE hashPopValue(VALUE self) {
222
271
  Hash* hash = rubyCast<Hash>(self);
223
272
  if (hash->container.empty())
224
273
  return Qnil;
@@ -269,12 +318,16 @@ extern "C" {
269
318
  rb_define_method(rbHash, "to_s", RUBY_METHOD_FUNC(hashToString), 0);
270
319
  rb_define_method(rbHash, "first", RUBY_METHOD_FUNC(hashFirst), 0);
271
320
  rb_define_method(rbHash, "last", RUBY_METHOD_FUNC(hashLast), 0);
321
+ rb_define_method(rbHash, "first_value", RUBY_METHOD_FUNC(hashFirstValue), 0);
322
+ rb_define_method(rbHash, "last_value", RUBY_METHOD_FUNC(hashLastValue), 0);
272
323
  rb_define_method(rbHash, "delete", RUBY_METHOD_FUNC(hashDelete), 1);
273
324
  // rb_define_method(rbHash, "reject!", RUBY_METHOD_FUNC(hashMutatingReject), 0);
274
325
  // rb_define_method(rbHash, "reject_first!", RUBY_METHOD_FUNC(hashMutatingRejectFirst), 0);
275
326
  // rb_define_method(rbHash, "select!", RUBY_METHOD_FUNC(hashMutatingSelect), 0);
276
327
  rb_define_method(rbHash, "shift", RUBY_METHOD_FUNC(hashShift), 0);
277
328
  rb_define_method(rbHash, "pop", RUBY_METHOD_FUNC(hashPop), 0);
329
+ rb_define_method(rbHash, "shift_value", RUBY_METHOD_FUNC(hashShiftValue), 0);
330
+ rb_define_method(rbHash, "pop_value", RUBY_METHOD_FUNC(hashPopValue), 0);
278
331
  // Enumerable would test both key and value for include?
279
332
  rb_define_method(rbHash, "include?", RUBY_METHOD_FUNC(hashHas), 1);
280
333
  rb_define_method(rbHash, "has_key?", RUBY_METHOD_FUNC(hashHas), 1);
data/yard.rb CHANGED
@@ -22,7 +22,7 @@ module RbLovely
22
22
  #
23
23
  # empty_set = RbLovely::SortedSet.new
24
24
  #
25
- # set = RbLovely::SortedSet [ Person.new('Nyamuk', 2), Person.new('Cold Rain', 9999) ]
25
+ # set = RbLovely::SortedSet [Person.new('Nyamuk', 2), Person.new('Cold Rain', 9999)]
26
26
  # set.add Person.new('Beards', 15)
27
27
  # set << Person.new('Anna', 12)
28
28
  # set.add Person.new('Moust', 18)
@@ -37,12 +37,14 @@ module RbLovely
37
37
  include Enumerable
38
38
 
39
39
  # @param content [Array] An array of values to insert into the set.
40
+ # @complexity O(n).
40
41
  # @example
41
42
  # set = RbLovely::SortedSet.new [3,1,2]
42
43
  # expect(set.to_a).to eql [1,2,3]
43
44
  def initialize content = [] ; end
44
45
 
45
46
  # Factory method for creating sorted set from array.
47
+ # @complexity O(n).
46
48
  # @param content [Array] An array of values to insert into the created set.
47
49
  # @return [SortedSet] New sorted set instance.
48
50
  # @example
@@ -55,7 +57,7 @@ module RbLovely
55
57
  # @return The value that was removed or nil if no value was removed.
56
58
  # @param value Value to remove (each member is compared to value using the <=> method).
57
59
  # @example
58
- # set = RbLovely::SortedSet [ 1, 5, 3 ]
60
+ # set = RbLovely::SortedSet [1, 5, 3]
59
61
  # set.delete 3
60
62
  # expect(set.to_a).to eql [1, 5]
61
63
  def delete value ; end
@@ -77,8 +79,8 @@ module RbLovely
77
79
  def last ; end
78
80
 
79
81
  # Remove the first element from the set.
80
- # @see #pop
81
82
  # @complexity O(c).
83
+ # @see #pop
82
84
  # @return The first value according to the <=> method defined on each member or nil if the set is empty.
83
85
  # @example
84
86
  # set = RbLovely::SortedSet [4, 0, 2]
@@ -86,8 +88,8 @@ module RbLovely
86
88
  def shift ; end
87
89
 
88
90
  # Remove the last element from the set.
89
- # @see #shift
90
91
  # @complexity O(c).
92
+ # @see #shift
91
93
  # @return The last value according to the <=> method defined on each member or nil if the set is empty.
92
94
  # @example
93
95
  # set = RbLovely::SortedSet [4, 0, 2]
@@ -129,10 +131,12 @@ module RbLovely
129
131
  def each(&block) ; end
130
132
 
131
133
  # Gets the number of elements in the set.
134
+ # @complexity O(c).
132
135
  # @return [Number] Number of items in set.
133
136
  def length ; end
134
137
 
135
138
  # Return true if the set is empty.
139
+ # @complexity O(c).
136
140
  # @return [Boolean] True only if the set is empty else false.
137
141
  def empty? ; end
138
142
  end
@@ -149,15 +153,18 @@ module RbLovely
149
153
  # @example
150
154
  # empty_hash = RbLovely::SortedHash.new
151
155
  #
152
- # # constructor is like: hash[20] = 5 ; hash[9] = 1
153
- # hash = RbLovely::SortedHash [20, 5, 9, 1]
154
- # hash[2] = 16
155
- # hash[20] = 4 # updates previous value
156
- # expect(hash[20]).to equal 4
156
+ # # constructor is like: hash[:y] = 5 ; hash[:i] = 1
157
+ # hash = RbLovely::SortedHash [:y, 5, :i, 1]
158
+ # hash[:b] = 16
159
+ # hash[:y] = 4 # updates previous value
160
+ # expect(hash.first).to equal [:i, 1]
161
+ # expect(hash[:y]).to equal 4
157
162
  # expect(hash.length).to equal 3
163
+ # expect(hash.to_a).to eql [[:i, 1], [:y, 4], [:b, 16]]
158
164
  class SortedHash
159
165
  include Enumerable
160
166
 
167
+ # @complexity O(n).
161
168
  # @param content [Array] An array containing key, value, key, value ...
162
169
  # @param compare [Proc] Comparison function used to order values (rather than default
163
170
  # of using <=> method).
@@ -174,6 +181,7 @@ module RbLovely
174
181
  def initialize content = [], compare: nil ; end
175
182
 
176
183
  # Factory method for creating sorted hash from array.
184
+ # @complexity O(n).
177
185
  # @param content [Array] An array of values to insert into the created hash.
178
186
  # @return [SortedHash] New sorted set instance.
179
187
  # @example
@@ -182,6 +190,7 @@ module RbLovely
182
190
  def self.[](*content) ; end
183
191
 
184
192
  # Set the value associated with a key. If the key already then it and its value are removed.
193
+ # @complexity O(c).
185
194
  # @return The value that was passed.
186
195
  # @example
187
196
  # hash = RbLovely::SortedHash.new
@@ -197,6 +206,7 @@ module RbLovely
197
206
  def replace(key, value) ; end
198
207
 
199
208
  # Delete the value associated with a key.
209
+ # @complexity O(c)
200
210
  # @return The value associated with the deleted key or nil if the key was not in the hash.
201
211
  # @example
202
212
  # hash = RbLovely::SortedHash [:a, 5 ]
@@ -213,6 +223,7 @@ module RbLovely
213
223
  def each(&block) ; end
214
224
 
215
225
  # Remove all values from the hash.
226
+ # @complexity O(n)
216
227
  # @example
217
228
  # hash = RbLovely::SortedHash [:a, 10]
218
229
  # hash.clear
@@ -228,41 +239,84 @@ module RbLovely
228
239
  def [](key) ; end
229
240
 
230
241
  # Return true if the key is contained in the hash.
242
+ # @complexity O(c)
231
243
  def include?(key) ; end
232
244
  alias :has_key? :include?
233
245
  alias :key? :include?
234
246
 
235
- # Retrieve first value as determined by value sort order or nil if the hash is empty.
247
+ # Retrieve first key-value pair as determined by value sort order or nil if the hash is empty.
236
248
  # @complexity O(c)
249
+ # @example
250
+ # @set = RbLovely::SortedHash [:a, 2, :b, 1]
251
+ # expect(@set.first).to equal [:b, 1]
237
252
  def first ; end
238
253
 
239
- # Retrieve last value as determined by value sort order or nil if the hash is empty.
254
+ # Retrieve first_value value as determined by value sort order or nil if the hash is empty.
255
+ # @complexity O(c)
256
+ # @return [Array] The first key-value pair in the hash.
257
+ # @example
258
+ # @set = RbLovely::SortedHash [:a, 2, :b, 1]
259
+ # expect(@set.first_value).to equal 1
260
+ def first_value ; end
261
+
262
+ # Retrieve last key-value pair as determined by value sort order or nil if the hash is empty.
240
263
  # @complexity O(c)
264
+ # @return [Array] The last key-value pair in the hash.
265
+ # @example
266
+ # @set = RbLovely::SortedHash [:a, 2, :b, 1]
267
+ # expect(@set.last).to equal [:a, 2]
241
268
  def last ; end
242
269
 
243
- # Remove the first value in the hash and return it or return nil if the hash is empty.
270
+ # Retrieve last value as determined by value sort order or nil if the hash is empty.
271
+ # @complexity O(c)
272
+ # @example
273
+ # @set = RbLovely::SortedHash [:a, 2, :b, 1]
274
+ # expect(@set.last_value).to equal 2
275
+ def last_value ; end
276
+
277
+ # Remove the first key-value pair in the hash and return it or return nil if the hash is empty.
244
278
  # @complexity O(c).
245
279
  # @see #pop
246
- # @return The first value according to the <=> method defined on each member or nil if the hash is empty.
280
+ # @return [Array] The first key-value pair according to the <=> method defined on each member or nil if the hash is empty.
247
281
  # @example
248
282
  # set = RbLovely::SortedHash [:a, 2, :b, 10]
249
- # expect(hash.shift).to equal 2
283
+ # expect(hash.shift).to equal [:a, 2]
250
284
  def shift ; end
251
285
 
252
- # Remove the last value in the hash and return it or return nil if the hash is empty.
286
+ # Remove the last key-value pair in the hash and return it or return nil if the hash is empty.
253
287
  # @complexity O(c).
254
288
  # @see #shift
255
- # @return The last value according to the <=> method defined on each member or nil if the hash is empty.
289
+ # @return [Array] The last key-value pair according to the <=> method defined on each member or nil if the hash is empty.
256
290
  # @example
257
291
  # set = RbLovely::SortedHash [:a, 2, :b, 10]
258
- # expect(hash.pop).to equal 10
292
+ # expect(hash.pop).to equal [:b, 10]
259
293
  def pop ; end
260
294
 
295
+ # Remove the first key-value pair in the hash and return it or return nil if the hash is empty.
296
+ # @complexity O(c).
297
+ # @see #pop_value
298
+ # @return The first value according to the <=> method defined on each member or nil if the hash is empty.
299
+ # @example
300
+ # set = RbLovely::SortedHash [:a, 2, :b, 10]
301
+ # expect(hash.shift_value).to equal 2
302
+ def shift_value ; end
303
+
304
+ # Remove the last key-value pair in the hash and return it or return nil if the hash is empty.
305
+ # @complexity O(c).
306
+ # @see #shift_value
307
+ # @return The last value according to the <=> method defined on each member or nil if the hash is empty.
308
+ # @example
309
+ # set = RbLovely::SortedHash [:a, 2, :b, 10]
310
+ # expect(hash.pop_value).to equal 10
311
+ def pop_value ; end
312
+
261
313
  # Gets the number of elements in the hash.
314
+ # @complexity O(c).
262
315
  # @return [Number] Number of items in hash.
263
316
  def length ; end
264
317
 
265
318
  # Return true if the hash is empty.
319
+ # @complexity O(c).
266
320
  # @return [Boolean] True only if the hash is empty else false.
267
321
  def empty? ; end
268
322
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb_lovely
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.5
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Pike