rb_lovely 0.7.0 → 0.7.1

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 +33 -36
  3. data/yard.rb +53 -2
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee5ff98c21526db5492808222dda0a1323a6e307
4
- data.tar.gz: abdce3830c9a0be152907318f05f0f7267332814
3
+ metadata.gz: b5d7473f46f078962567e35e26f31e444a221cbd
4
+ data.tar.gz: 8ed3631fc234e853cae5cd35d3dbe16db41af6bf
5
5
  SHA512:
6
- metadata.gz: 0ae92b111ea962b4e7e962d556a428c3627e62d724417e05846f875177c76e4b37fd9816103567ae50139dabe55da64e0ac1f835341b5ec6c883505d6be492c6
7
- data.tar.gz: 91f3b2d225924117724ebdeb3a23948eeb728500d4bf60635a4687385e1038de8aa6552ee33cf67ec01d045555bced30c96030f85def4452d9d2dc6414d6640c
6
+ metadata.gz: aebd51f11653b4fcb7c198d54405d6d2394f624fa57dab474de57b34806c54f588fb29b3dc172a0538ef59993aab3f236363c28b925afa84582a0dcc14f225d3
7
+ data.tar.gz: d3f14f1f337dfe379f0d9b279e02d6b2cf068b55d3e1a3263060c22b3d14617138c6e0e4a1d95e933b2d3a169e439077f21e8b51eedf4cf7e12b88b4ef424e28
@@ -228,19 +228,31 @@ VALUE hashDelete(VALUE self, VALUE toDelete) {
228
228
  }
229
229
  }
230
230
 
231
+ enum class ValueOrKey { VALUE, KEY, BOTH };
232
+
233
+ template <ValueOrKey V>
231
234
  VALUE hashShift(VALUE self) {
232
235
  Hash* hash = rubyCast<Hash>(self);
233
236
  if (hash->container.empty())
234
237
  return Qnil;
235
238
 
236
239
  auto& idx = hash->container.get<1>();
237
- auto keyBak = idx.begin()->key;
238
- auto valBak = idx.begin()->val;
239
- idx.erase(idx.begin());
240
+ auto first = idx.begin();
241
+ if (V == ValueOrKey::BOTH) {
242
+ auto keyBak = first->key;
243
+ auto valBak = first->val;
244
+ idx.erase(first);
240
245
 
241
- return rb_ary_new3(2, keyBak, valBak);
246
+ return rb_ary_new3(2, keyBak, valBak);
247
+ }
248
+ else {
249
+ auto bak = V == ValueOrKey::VALUE ? first->val : first->key;
250
+ idx.erase(first);
251
+ return bak;
252
+ }
242
253
  }
243
254
 
255
+ template <ValueOrKey V>
244
256
  VALUE hashPop(VALUE self) {
245
257
  Hash* hash = rubyCast<Hash>(self);
246
258
  if (hash->container.empty())
@@ -249,35 +261,18 @@ VALUE hashPop(VALUE self) {
249
261
  auto& idx = hash->container.get<1>();
250
262
  auto last = idx.end();
251
263
  --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
264
 
259
- VALUE hashShiftValue(VALUE self) {
260
- Hash* hash = rubyCast<Hash>(self);
261
- if (hash->container.empty())
262
- return Qnil;
263
-
264
- auto& idx = hash->container.get<1>();
265
- auto bak = idx.begin()->val;
266
- idx.erase(idx.begin());
267
- return bak;
268
- }
269
-
270
- VALUE hashPopValue(VALUE self) {
271
- Hash* hash = rubyCast<Hash>(self);
272
- if (hash->container.empty())
273
- return Qnil;
274
-
275
- auto& idx = hash->container.get<1>();
276
- auto last = idx.end();
277
- --last;
278
- auto bak = last->val;
279
- idx.erase(last);
280
- return bak;
265
+ if (V == ValueOrKey::BOTH) {
266
+ auto keyBak = last->key;
267
+ auto valBak = last->val;
268
+ idx.erase(last);
269
+ return rb_ary_new3(2, keyBak, valBak);
270
+ }
271
+ else {
272
+ auto bak = V == ValueOrKey::VALUE ? last->val : last->key;
273
+ idx.erase(last);
274
+ return bak;
275
+ }
281
276
  }
282
277
 
283
278
  VALUE hashHas(VALUE self, VALUE key) {
@@ -324,10 +319,12 @@ extern "C" {
324
319
  // rb_define_method(rbHash, "reject!", RUBY_METHOD_FUNC(hashMutatingReject), 0);
325
320
  // rb_define_method(rbHash, "reject_first!", RUBY_METHOD_FUNC(hashMutatingRejectFirst), 0);
326
321
  // rb_define_method(rbHash, "select!", RUBY_METHOD_FUNC(hashMutatingSelect), 0);
327
- rb_define_method(rbHash, "shift", RUBY_METHOD_FUNC(hashShift), 0);
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);
322
+ rb_define_method(rbHash, "shift", RUBY_METHOD_FUNC(hashShift<ValueOrKey::BOTH>), 0);
323
+ rb_define_method(rbHash, "shift_value", RUBY_METHOD_FUNC(hashShift<ValueOrKey::VALUE>), 0);
324
+ rb_define_method(rbHash, "shift_key", RUBY_METHOD_FUNC(hashShift<ValueOrKey::KEY>), 0);
325
+ rb_define_method(rbHash, "pop", RUBY_METHOD_FUNC(hashPop<ValueOrKey::BOTH>), 0);
326
+ rb_define_method(rbHash, "pop_value", RUBY_METHOD_FUNC(hashPop<ValueOrKey::VALUE>), 0);
327
+ rb_define_method(rbHash, "pop_key", RUBY_METHOD_FUNC(hashPop<ValueOrKey::KEY>), 0);
331
328
  // Enumerable would test both key and value for include?
332
329
  rb_define_method(rbHash, "include?", RUBY_METHOD_FUNC(hashHas), 1);
333
330
  rb_define_method(rbHash, "has_key?", RUBY_METHOD_FUNC(hashHas), 1);
data/yard.rb CHANGED
@@ -248,10 +248,10 @@ module RbLovely
248
248
  # @complexity O(c)
249
249
  # @example
250
250
  # @set = RbLovely::SortedHash [:a, 2, :b, 1]
251
- # expect(@set.first).to equal [:b, 1]
251
+ # expect(@set.first).to eql [:b, 1]
252
252
  def first ; end
253
253
 
254
- # Retrieve first_value value as determined by value sort order or nil if the hash is empty.
254
+ # Retrieve first value as determined by value sort order or nil if the hash is empty.
255
255
  # @complexity O(c)
256
256
  # @return [Array] The first key-value pair in the hash.
257
257
  # @example
@@ -259,6 +259,14 @@ module RbLovely
259
259
  # expect(@set.first_value).to equal 1
260
260
  def first_value ; end
261
261
 
262
+ # Retrieve first key as determined by value sort order or nil if the hash is empty.
263
+ # @complexity O(c)
264
+ # @return The first key in the hash
265
+ # @example
266
+ # @set = RbLovely::SortedHash [:a, 1, :b, 2]
267
+ # expect(@set.first_value).to equal :a
268
+ def first_key ; end
269
+
262
270
  # Retrieve last key-value pair as determined by value sort order or nil if the hash is empty.
263
271
  # @complexity O(c)
264
272
  # @return [Array] The last key-value pair in the hash.
@@ -274,6 +282,13 @@ module RbLovely
274
282
  # expect(@set.last_value).to equal 2
275
283
  def last_value ; end
276
284
 
285
+ # Retrieve last key as determined by value sort order or nil if the hash is empty.
286
+ # @complexity O(c)
287
+ # @example
288
+ # @set = RbLovely::SortedHash [:a, 1, :b, 2]
289
+ # expect(@set.last_key).to equal :b
290
+ def last_key ; end
291
+
277
292
  # Remove the first key-value pair in the hash and return it or return nil if the hash is empty.
278
293
  # @complexity O(c).
279
294
  # @see #pop
@@ -283,6 +298,24 @@ module RbLovely
283
298
  # expect(hash.shift).to equal [:a, 2]
284
299
  def shift ; end
285
300
 
301
+ # Remove the first key-value pair in the hash and return the key or return nil if the hash is empty.
302
+ # @complexity O(c).
303
+ # @see #pop_key
304
+ # @return The first key pair according to the <=> method defined on each member or nil if the hash is empty.
305
+ # @example
306
+ # set = RbLovely::SortedHash [:a, 2, :b, 10]
307
+ # expect(hash.shift_key).to equal :a
308
+ def shift_key ; end
309
+
310
+ # Remove the first key-value pair in the hash and return the value or return nil if the hash is empty.
311
+ # @complexity O(c).
312
+ # @see #pop_value
313
+ # @return The first key pair according to the <=> method defined on each member or nil if the hash is empty.
314
+ # @example
315
+ # set = RbLovely::SortedHash [:a, 2, :b, 10]
316
+ # expect(hash.shift_value).to equal 2
317
+ def shift_value ; end
318
+
286
319
  # Remove the last key-value pair in the hash and return it or return nil if the hash is empty.
287
320
  # @complexity O(c).
288
321
  # @see #shift
@@ -292,6 +325,24 @@ module RbLovely
292
325
  # expect(hash.pop).to equal [:b, 10]
293
326
  def pop ; end
294
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
+ # Remove the last key-value pair in the hash and return the value or return nil if the hash is empty.
338
+ # @complexity O(c).
339
+ # @see #shift_value
340
+ # @return The last key according to the <=> method defined on each member or nil if the hash is empty.
341
+ # @example
342
+ # set = RbLovely::SortedHash [:a, 2, :b, 10]
343
+ # expect(hash.pop_value).to equal 10
344
+ def pop_value ; end
345
+
295
346
  # Remove the first key-value pair in the hash and return it or return nil if the hash is empty.
296
347
  # @complexity O(c).
297
348
  # @see #pop_value
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb_lovely
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Pike
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-06 00:00:00.000000000 Z
11
+ date: 2014-09-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A fast sorted set built using std::set and a fast sorted hash built using
14
14
  boost::multi_index_container.