rb_lovely 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/ext/rb_lovely/sorted_hash.cpp +22 -22
  3. data/yard.rb +15 -33
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b5d7473f46f078962567e35e26f31e444a221cbd
4
- data.tar.gz: 8ed3631fc234e853cae5cd35d3dbe16db41af6bf
3
+ metadata.gz: 67d03675c3c6b63a3ebfd789c053a98df9b4a55c
4
+ data.tar.gz: c77b233b9b82b11f00f1d0fb37ed1ac7be8d8c17
5
5
  SHA512:
6
- metadata.gz: aebd51f11653b4fcb7c198d54405d6d2394f624fa57dab474de57b34806c54f588fb29b3dc172a0538ef59993aab3f236363c28b925afa84582a0dcc14f225d3
7
- data.tar.gz: d3f14f1f337dfe379f0d9b279e02d6b2cf068b55d3e1a3263060c22b3d14617138c6e0e4a1d95e933b2d3a169e439077f21e8b51eedf4cf7e12b88b4ef424e28
6
+ metadata.gz: 8605022a32d4df8a51bfd6460774f64ea8fef9983f028f4eab839f663c6d5ec671b3d450c91525b84ff8f980e4e670f632f136cf9b022584749addb48512fe70
7
+ data.tar.gz: 256fbbace880eca76a8a3373e1622c57a9ac34324580f9904311770abd3b10579dfbb9ff719cfbcd7357dfdfd499440846594225b31bfda3b640f1cfc0afe55b
@@ -179,6 +179,9 @@ VALUE hashToString(VALUE self) {
179
179
  return rb_str_new(stlString.data(), stlString.size());
180
180
  }
181
181
 
182
+ enum class ValueOrKey { VALUE, KEY, BOTH };
183
+
184
+ template <ValueOrKey V>
182
185
  VALUE hashFirst(VALUE self) {
183
186
  Hash* hash = rubyCast<Hash>(self);
184
187
  if (hash->container.empty()) {
@@ -186,9 +189,15 @@ VALUE hashFirst(VALUE self) {
186
189
  }
187
190
 
188
191
  auto &pair = *hash->container.get<1>().begin();
189
- return rb_ary_new3(2, pair.key, pair.val);
192
+ if (V == ValueOrKey::BOTH) {
193
+ return rb_ary_new3(2, pair.key, pair.val);
194
+ }
195
+ else {
196
+ return V == ValueOrKey::VALUE ? pair.val : pair.key;
197
+ }
190
198
  }
191
199
 
200
+ template <ValueOrKey V>
192
201
  VALUE hashLast(VALUE self) {
193
202
  Hash* hash = rubyCast<Hash>(self);
194
203
  if (hash->container.empty()) {
@@ -197,22 +206,13 @@ VALUE hashLast(VALUE self) {
197
206
 
198
207
  auto it = hash->container.get<1>().end();
199
208
  --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
209
 
208
- VALUE hashLastValue(VALUE self) {
209
- Hash* hash = rubyCast<Hash>(self);
210
- if (hash->container.empty())
211
- return Qnil;
212
-
213
- auto last = hash->container.get<1>().end();
214
- --last;
215
- return last->val;
210
+ if (V == ValueOrKey::BOTH) {
211
+ return rb_ary_new3(2, it->key, it->val);
212
+ }
213
+ else {
214
+ return V == ValueOrKey::VALUE ? it->val : it->key;
215
+ }
216
216
  }
217
217
 
218
218
  VALUE hashDelete(VALUE self, VALUE toDelete) {
@@ -228,8 +228,6 @@ VALUE hashDelete(VALUE self, VALUE toDelete) {
228
228
  }
229
229
  }
230
230
 
231
- enum class ValueOrKey { VALUE, KEY, BOTH };
232
-
233
231
  template <ValueOrKey V>
234
232
  VALUE hashShift(VALUE self) {
235
233
  Hash* hash = rubyCast<Hash>(self);
@@ -311,10 +309,12 @@ extern "C" {
311
309
  rb_define_method(rbHash, "[]", RUBY_METHOD_FUNC(hashGet), 1);
312
310
  rb_define_method(rbHash, "each", RUBY_METHOD_FUNC(hashEach), 0);
313
311
  rb_define_method(rbHash, "to_s", RUBY_METHOD_FUNC(hashToString), 0);
314
- rb_define_method(rbHash, "first", RUBY_METHOD_FUNC(hashFirst), 0);
315
- rb_define_method(rbHash, "last", RUBY_METHOD_FUNC(hashLast), 0);
316
- rb_define_method(rbHash, "first_value", RUBY_METHOD_FUNC(hashFirstValue), 0);
317
- rb_define_method(rbHash, "last_value", RUBY_METHOD_FUNC(hashLastValue), 0);
312
+ rb_define_method(rbHash, "first", RUBY_METHOD_FUNC(hashFirst<ValueOrKey::BOTH>), 0);
313
+ rb_define_method(rbHash, "first_value", RUBY_METHOD_FUNC(hashFirst<ValueOrKey::VALUE>), 0);
314
+ rb_define_method(rbHash, "first_key", RUBY_METHOD_FUNC(hashFirst<ValueOrKey::KEY>), 0);
315
+ rb_define_method(rbHash, "last", RUBY_METHOD_FUNC(hashLast<ValueOrKey::BOTH>), 0);
316
+ rb_define_method(rbHash, "last_value", RUBY_METHOD_FUNC(hashLast<ValueOrKey::VALUE>), 0);
317
+ rb_define_method(rbHash, "last_key", RUBY_METHOD_FUNC(hashLast<ValueOrKey::KEY>), 0);
318
318
  rb_define_method(rbHash, "delete", RUBY_METHOD_FUNC(hashDelete), 1);
319
319
  // rb_define_method(rbHash, "reject!", RUBY_METHOD_FUNC(hashMutatingReject), 0);
320
320
  // rb_define_method(rbHash, "reject_first!", RUBY_METHOD_FUNC(hashMutatingRejectFirst), 0);
data/yard.rb CHANGED
@@ -209,7 +209,7 @@ module RbLovely
209
209
  # @complexity O(c)
210
210
  # @return The value associated with the deleted key or nil if the key was not in the hash.
211
211
  # @example
212
- # hash = RbLovely::SortedHash [:a, 5 ]
212
+ # hash = RbLovely::SortedHash [:a, 5]
213
213
  # expect(hash.delete(:a)).to equal 5
214
214
  # expect(hash.delete(:b)).to equal nil
215
215
  def delete key ; end
@@ -264,7 +264,7 @@ module RbLovely
264
264
  # @return The first key in the hash
265
265
  # @example
266
266
  # @set = RbLovely::SortedHash [:a, 1, :b, 2]
267
- # expect(@set.first_value).to equal :a
267
+ # expect(@set.first_key).to equal :a
268
268
  def first_key ; end
269
269
 
270
270
  # Retrieve last key-value pair as determined by value sort order or nil if the hash is empty.
@@ -298,23 +298,23 @@ module RbLovely
298
298
  # expect(hash.shift).to equal [:a, 2]
299
299
  def shift ; end
300
300
 
301
- # Remove the first key-value pair in the hash and return the key or return nil if the hash is empty.
301
+ # Remove the first key-value pair in the hash and return the value or return nil if the hash is empty.
302
302
  # @complexity O(c).
303
- # @see #pop_key
303
+ # @see #pop_value
304
304
  # @return The first key pair according to the <=> method defined on each member or nil if the hash is empty.
305
305
  # @example
306
306
  # set = RbLovely::SortedHash [:a, 2, :b, 10]
307
- # expect(hash.shift_key).to equal :a
308
- def shift_key ; end
307
+ # expect(hash.shift_value).to equal 2
308
+ def shift_value ; end
309
309
 
310
- # Remove the first key-value pair in the hash and return the value or return nil if the hash is empty.
310
+ # Remove the first key-value pair in the hash and return the key or return nil if the hash is empty.
311
311
  # @complexity O(c).
312
- # @see #pop_value
312
+ # @see #pop_key
313
313
  # @return The first key pair according to the <=> method defined on each member or nil if the hash is empty.
314
314
  # @example
315
315
  # set = RbLovely::SortedHash [:a, 2, :b, 10]
316
- # expect(hash.shift_value).to equal 2
317
- def shift_value ; end
316
+ # expect(hash.shift_key).to equal :a
317
+ def shift_key ; end
318
318
 
319
319
  # Remove the last key-value pair in the hash and return it or return nil if the hash is empty.
320
320
  # @complexity O(c).
@@ -325,15 +325,6 @@ module RbLovely
325
325
  # expect(hash.pop).to equal [:b, 10]
326
326
  def pop ; end
327
327
 
328
- # Remove the last key-value pair in the hash and return the key or return nil if the hash is empty.
329
- # @complexity O(c).
330
- # @see #shift_key
331
- # @return The last key according to the <=> method defined on each member or nil if the hash is empty.
332
- # @example
333
- # set = RbLovely::SortedHash [:a, 2, :b, 10]
334
- # expect(hash.pop_key).to equal :b
335
- def pop_key ; end
336
-
337
328
  # Remove the last key-value pair in the hash and return the value or return nil if the hash is empty.
338
329
  # @complexity O(c).
339
330
  # @see #shift_value
@@ -343,23 +334,14 @@ module RbLovely
343
334
  # expect(hash.pop_value).to equal 10
344
335
  def pop_value ; end
345
336
 
346
- # Remove the first key-value pair in the hash and return it or return nil if the hash is empty.
347
- # @complexity O(c).
348
- # @see #pop_value
349
- # @return The first value according to the <=> method defined on each member or nil if the hash is empty.
350
- # @example
351
- # set = RbLovely::SortedHash [:a, 2, :b, 10]
352
- # expect(hash.shift_value).to equal 2
353
- def shift_value ; end
354
-
355
- # Remove the last key-value pair in the hash and return it or return nil if the hash is empty.
337
+ # Remove the last key-value pair in the hash and return the key or return nil if the hash is empty.
356
338
  # @complexity O(c).
357
- # @see #shift_value
358
- # @return The last value according to the <=> method defined on each member or nil if the hash is empty.
339
+ # @see #shift_key
340
+ # @return The last key according to the <=> method defined on each member or nil if the hash is empty.
359
341
  # @example
360
342
  # set = RbLovely::SortedHash [:a, 2, :b, 10]
361
- # expect(hash.pop_value).to equal 10
362
- def pop_value ; end
343
+ # expect(hash.pop_key).to equal :b
344
+ def pop_key ; end
363
345
 
364
346
  # Gets the number of elements in the hash.
365
347
  # @complexity O(c).
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.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Pike