Project

General

Profile

Feature #11518 ยป ruby_queue_promote.patch

jonathanscruz (Jonathan Cruz), 09/09/2015 03:04 PM

View differences:

ChangeLog (working copy)
1
Tue Sep  9 02:43:13 2015  Jonathan Cruz  <jcruz@trustwave.com>
2

  
3
	* thread_sync.c: Add Queue#promote! and Queue#promote_all!
4
	  Promotes elements to the front of the queue for which the provided
5
	  block returns a truthy value. #promote! only applies to the first
6
	  such element. #promote_all! applies to all matching elements,
7
	  maintaining their relative order.
8

  
1 9
Thu Sep  3 21:12:12 2015  Nobuyoshi Nakada  <nobu@ruby-lang.org>
2 10

  
3 11
	* lib/cgi/session.rb (create_new_id): use SHA512 instead of MD5.
thread_sync.c (working copy)
905 905
}
906 906

  
907 907
/*
908
 * Document-method: Queue#promote!
909
 * call-seq: promote!
910
 *
911
 * Promotes first object to the front of the queue for which the given +block+
912
 * returns a true value.
913
 */
914

  
915
static VALUE
916
rb_queue_promote(VALUE self)
917
{
918
    if (!rb_block_given_p()) {
919
        rb_raise(rb_eThreadError, "must be called with a block");
920
    }
921
    return queue_do_promote(self, FALSE);
922
}
923

  
924
/*
925
 * Document-method: Queue#promote_all!
926
 * call-seq: promote_all!
927
 *
928
 *
929
 * Promotes all objects to the front of the queue for which the given +block+
930
 * returns a true value, preserving their relative order.
931
 */
932

  
933
static VALUE
934
rb_queue_promote_all(VALUE self)
935
{
936
    if (!rb_block_given_p()) {
937
        rb_raise(rb_eThreadError, "must be called with a block");
938
    }
939
    return queue_do_promote(self, TRUE);
940
}
941

  
942
static VALUE
943
queue_do_promote(VALUE self, int promote_all)
944
{
945
    VALUE que = GET_QUEUE_QUE(self);
946
    VALUE promote = rb_ary_new2(RARRAY_LEN(que));
947
    long i;
948
    for (i = 0; i < RARRAY_LEN(que); i++) {
949
        VALUE obj = RARRAY_AREF(que, i);
950
        if (RTEST(rb_yield(obj))) {
951
            rb_ary_unshift(promote, obj);
952
            if (!promote_all) break;
953
        }
954
    }
955

  
956
    for (i = 0; i < RARRAY_LEN(promote); i++) {
957
        VALUE obj = RARRAY_AREF(promote, i);
958
        rb_ary_delete(que, obj);
959
        rb_ary_unshift(que, obj);
960
    }
961
    return self;
962
}
963

  
964

  
965
/*
908 966
 *  Document-class: SizedQueue
909 967
 *
910 968
 * This class represents queues of specified size capacity.  The push operation
......
1281 1339
    rb_define_method(rb_cQueue, "clear", rb_queue_clear, 0);
1282 1340
    rb_define_method(rb_cQueue, "length", rb_queue_length, 0);
1283 1341
    rb_define_method(rb_cQueue, "num_waiting", rb_queue_num_waiting, 0);
1342
    rb_define_method(rb_cQueue, "promote!", rb_queue_promote, 0);
1343
    rb_define_method(rb_cQueue, "promote_all!", rb_queue_promote_all, 0);
1284 1344

  
1285 1345
    rb_define_alias(rb_cQueue, "enq", "push");    /* Alias for #push. */
1286 1346
    rb_define_alias(rb_cQueue, "<<", "push");     /* Alias for #push. */