Class: List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/list/list.c

Constant Summary collapse

VERSION =
rb_str_new2(LIST_VERSION)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'ext/list/list.c', line 371

static VALUE
list_initialize(int argc, VALUE *argv, VALUE self)
{
  VALUE size, val;
  long len;
  long i;

  list_modify_check(self);
  if (argc == 0) {
    return self;
  }
  if (argc == 1 && !FIXNUM_P(argv[0])) {
    switch (rb_type(argv[0])) {
    case T_ARRAY:
      return list_push_ary(self, argv[0]);
    case T_DATA:
      return list_replace(self, argv[0]);
    default:
      break;
    }
  }

  rb_scan_args(argc, argv, "02", &size, &val);

  len = NUM2LONG(size);
  if (len < 0) {
    rb_raise(rb_eArgError, "negative size");
  }
  if (LIST_MAX_SIZE < len) {
    rb_raise(rb_eArgError, "size too big");
  }

  if (rb_block_given_p()) {
    if (argc == 2) {
      rb_warn("block supersedes default value argument");
    }
    for (i = 0; i < len; i++) {
      list_push(self, rb_yield(LONG2NUM(i)));
    }
  } else {
    for (i = 0; i < len; i++) {
      list_push(self, val);
    }
  }
  return self;
}

Class Method Details

.[](*args) ⇒ Object



350
351
352
353
354
355
356
357
# File 'ext/list/list.c', line 350

static VALUE
list_s_create(int argc, VALUE *argv, VALUE klass)
{
  VALUE list;

  list = rb_obj_alloc(klass);
  return list_push_m(argc, argv, list);
}

.try_convert(obj) ⇒ Object



365
366
367
368
369
# File 'ext/list/list.c', line 365

static VALUE
list_s_try_convert(VALUE dummy, VALUE obj)
{
  return check_list_type(obj);
}

Instance Method Details

#<<(obj) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'ext/list/list.c', line 299

static VALUE
list_push(VALUE self, VALUE obj)
{
  list_t *ptr;
  item_t *next;

  list_modify_check(self);
  if (self == obj) {
    rb_raise(rb_eArgError, "`List' cannot set recursive");
  }

  next = item_alloc(obj, NULL);

  Data_Get_Struct(self, list_t, ptr);
  if (ptr->first == NULL) {
    ptr->first = next;
    ptr->last = next;
    ptr->last->next = NULL;
  } else {
    ptr->last->next = next;
    ptr->last = next;
  }
  ptr->len++;
  return self;
}

#<=>(list2) ⇒ Object



1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
# File 'ext/list/list.c', line 1895

static VALUE
list_cmp(VALUE list1, VALUE list2)
{
  VALUE v;
  list_t *p1, *p2;
  long len;

  if (rb_type(list2) != T_DATA) return Qnil;
  if (list1 == list2) return INT2FIX(0);
  v = rb_exec_recursive_paired(recursive_cmp, list1, list2, list2);
  if (v != Qundef) return v;
  Data_Get_Struct(list1, list_t, p1);
  Data_Get_Struct(list2, list_t, p2);
  len = p1->len - p2->len;
  if (len == 0) return INT2FIX(0);
  if (0 < len) return INT2FIX(1);
  return INT2FIX(-1);
}

#==(obj) ⇒ Object



614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'ext/list/list.c', line 614

static VALUE
list_equal(VALUE self, VALUE obj)
{
  if (self == obj)
    return Qtrue;

  if (!rb_obj_is_kind_of(obj, cList)) {
    if (rb_type(obj) == T_ARRAY) {
      return Qfalse;
    }
    if (!rb_respond_to(obj, rb_intern("to_list"))) {
      return Qfalse;
    }
    return rb_equal(obj, self);
  }
  return rb_exec_recursive_paired(recursive_equal, self, obj, obj);

}

#[](*args) ⇒ Object



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'ext/list/list.c', line 740

static VALUE
list_aref(int argc, VALUE *argv, VALUE self)
{
  VALUE arg;
  long beg, len;
  list_t *ptr;

  Data_Get_Struct(self, list_t, ptr);
  if (argc == 2) {
    beg = NUM2LONG(argv[0]);
    len = NUM2LONG(argv[1]);
    if (beg < 0) {
      beg += ptr->len;
    }
    return list_subseq(self, beg, len);
  }
  if (argc != 1) {
    rb_scan_args(argc, argv, "11", NULL, NULL);
  }
  arg = argv[0];

  /* special case - speeding up */
  if (FIXNUM_P(arg)) {
    return list_entry(self, FIX2LONG(arg));
  }
  /* check if idx is Range */
  switch (rb_range_beg_len(arg, &beg, &len, ptr->len, 0)) {
  case Qfalse:
    break;
  case Qnil:
    return Qnil;
  default:
    return list_subseq(self, beg, len);
  }
  return list_entry(self, NUM2LONG(arg));
}

#[]=(*args) ⇒ Object



900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# File 'ext/list/list.c', line 900

static VALUE
list_aset(int argc, VALUE *argv, VALUE self)
{
  long offset, beg, len;
  list_t *ptr;

  list_modify_check(self);
  Data_Get_Struct(self, list_t, ptr);
  if (argc == 3) {
    beg = NUM2LONG(argv[0]);
    len = NUM2LONG(argv[1]);
    list_splice(self, beg, len, argv[2]);
    return argv[2];
  }
  rb_check_arity(argc, 2, 2);
  if (FIXNUM_P(argv[0])) {
    offset = FIX2LONG(argv[0]);
    goto fixnum;
  }
  if (rb_range_beg_len(argv[0], &beg, &len, ptr->len, 1)) {
    /* check if idx is Range */
    list_splice(self, beg, len, argv[1]);
    return argv[1];
  }

  offset = NUM2LONG(argv[0]);
fixnum:
  list_store(self, offset, argv[1]);
  return argv[1];
}

#at(pos) ⇒ Object



931
932
933
934
935
# File 'ext/list/list.c', line 931

static VALUE
list_at(VALUE self, VALUE pos)
{
  return list_entry(self, NUM2LONG(pos));
}

#clearObject



452
453
454
455
456
457
458
459
460
461
462
463
# File 'ext/list/list.c', line 452

static VALUE
list_clear(VALUE self)
{
  list_modify_check(self);
  list_t *ptr;
  Data_Get_Struct(self, list_t, ptr);
  list_free(ptr);
  ptr->first = NULL;
  ptr->last = NULL;
  ptr->len = 0;
  return self;
}

#collectObject



1503
1504
1505
1506
1507
# File 'ext/list/list.c', line 1503

static VALUE
list_collect(VALUE self)
{
  return list_collect_bang(rb_obj_clone(self));
}

#collect!Object



1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
# File 'ext/list/list.c', line 1489

static VALUE
list_collect_bang(VALUE self)
{
  list_t *ptr;
  item_t *c;

  RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);
  Data_Get_Struct(self, list_t, ptr);
  LIST_FOR(ptr, c) {
    c->value = rb_yield(c->value);
  }
  return self;
}

#concat(obj) ⇒ Object



1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
# File 'ext/list/list.c', line 1020

static VALUE
list_concat(VALUE self, VALUE obj)
{
  long len;
  list_t *ptr_self;
  list_t *ptr_obj;
  enum ruby_value_type type;

  list_modify_check(self);
  type = rb_type(obj);
  if (type == T_DATA) {
    Data_Get_Struct(obj, list_t, ptr_obj);
    len = ptr_obj->len;
  } else if (type == T_ARRAY) {
    len = RARRAY_LEN(obj);
  } else {
    obj = to_list(obj);
    Data_Get_Struct(obj, list_t, ptr_obj);
    len = ptr_obj->len;
  }

  Data_Get_Struct(self, list_t, ptr_self);
  if (0 < len) {
    list_splice(self, ptr_self->len, 0, obj);
  }
  return self;
}

#delete(item) ⇒ Object



1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
# File 'ext/list/list.c', line 1584

static VALUE
list_delete(VALUE self, VALUE item)
{
  list_t *ptr;
  item_t *c, *before = NULL, *next;
  long len;

  list_modify_check(self);
  Data_Get_Struct(self, list_t, ptr);
  if (ptr->len == 0) return Qnil;

  len = ptr->len;
  for (c = ptr->first; c; c = next) {
    next = c->next;
    if (rb_equal(c->value, item)) {
      if (ptr->first == ptr->last) {
        ptr->first = NULL;
        ptr->last = NULL;
      } else if (c == ptr->first) {
        ptr->first = c->next;
      } else if (c == ptr->last) {
        ptr->last = before;
        ptr->last->next = NULL;
      } else {
        before->next = c->next;
      }
      xfree(c);
      ptr->len--;
    } else {
      before = c;
    }
  }

  if (ptr->len == len) {
    if (rb_block_given_p()) {
      return rb_yield(item);
    }
    return Qnil;
  } else {
    return item;
  }
}

#delete_at(pos) ⇒ Object



1674
1675
1676
1677
1678
# File 'ext/list/list.c', line 1674

static VALUE
list_delete_at_m(VALUE self, VALUE pos)
{
  return list_delete_at(self, NUM2LONG(pos));
}

#delete_ifObject



1750
1751
1752
1753
1754
1755
1756
# File 'ext/list/list.c', line 1750

static VALUE
list_delete_if(VALUE self)
{
  RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);
  list_reject_bang(self);
  return self;
}

#eachObject



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'ext/list/list.c', line 418

static VALUE
list_each(VALUE self)
{
  item_t *c;
  list_t *ptr;

  RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);
  Data_Get_Struct(self, list_t, ptr);
  if (ptr->first == NULL) return self;

  LIST_FOR(ptr, c) {
    rb_yield(c->value);
  }
  return self;
}

#each_indexObject



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'ext/list/list.c', line 434

static VALUE
list_each_index(VALUE self)
{
  item_t *c;
  list_t *ptr;
  long index;

  RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);
  Data_Get_Struct(self, list_t, ptr);
  if (ptr->first == NULL) return self;

  index = 0;
  LIST_FOR(ptr, c) {
    rb_yield(LONG2NUM(index++));
  }
  return self;
}

#empty?Boolean

Returns:

  • (Boolean)


1175
1176
1177
1178
1179
1180
1181
1182
1183
# File 'ext/list/list.c', line 1175

static VALUE
list_empty_p(VALUE self)
{
  list_t *ptr;
  Data_Get_Struct(self, list_t, ptr);
  if (ptr->len == 0)
    return Qtrue;
  return Qfalse;
}

#fetch(*args) ⇒ Object



937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
# File 'ext/list/list.c', line 937

static VALUE
list_fetch(int argc, VALUE *argv, VALUE self)
{
  VALUE pos, ifnone;
  long block_given;
  long idx;
  list_t *ptr;

  Data_Get_Struct(self, list_t, ptr);
  rb_scan_args(argc, argv, "11", &pos, &ifnone);
  block_given = rb_block_given_p();
  if (block_given && argc == 2) {
    rb_warn("block supersedes default value argument");
  }
  idx = NUM2LONG(pos);

  if (idx < 0) {
    idx += ptr->len;
  }
  if (idx < 0 || ptr->len <= idx) {
    if (block_given) return rb_yield(pos);
    if (argc == 1) {
      rb_raise(rb_eIndexError, "index %ld outside of array bounds: %ld...%ld",
          idx - (idx < 0 ? ptr->len : 0), -ptr->len, ptr->len);
    }
    return ifnone;
  }
  return list_entry(self, idx);
}

#fill(*args) ⇒ Object



1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
# File 'ext/list/list.c', line 1791

static VALUE
list_fill(int argc, VALUE *argv, VALUE self)
{
  VALUE item, arg1, arg2;
  long beg = 0, len = 0, end = 0;
  long i;
  int block_p = FALSE;
  list_t *ptr;
  item_t *c;

  Data_Get_Struct(self, list_t, ptr);

  list_modify_check(self);
  if (rb_block_given_p()) {
    block_p = TRUE;
    rb_scan_args(argc, argv, "02", &arg1, &arg2);
    argc += 1; /* hackish */
  } else {
    rb_scan_args(argc, argv, "12", &item, &arg1, &arg2);
  }
  switch (argc) {
  case 1:
    beg = 0;
    len = ptr->len;
    break;
  case 2:
    if (rb_range_beg_len(arg1, &beg, &len, ptr->len, 1)) {
      break;
    }
    /* fall through */
  case 3:
    beg = NIL_P(arg1) ? 0 : NUM2LONG(arg1);
    if (beg < 0) {
      beg = ptr->len + beg;
      if (beg < 0) beg = 0;
    }
    len = NIL_P(arg2) ? ptr->len - beg : NUM2LONG(arg2);
    break;
  }
  if (len < 0) {
    return self;
  }
  if (LIST_MAX_SIZE <= beg || LIST_MAX_SIZE - beg < len) {
    rb_raise(rb_eArgError, "argument too big");
  }
  end = beg + len;
  if (ptr->len < end) {
    for (i = 0; i < end - ptr->len; i++) {
      list_push(self, Qnil);
    }
  }

  i = -1;
  LIST_FOR(ptr, c) {
    i++;
    if (i < beg) continue;
    if ((end - 1) < i) break;
    if (block_p) {
      c->value = rb_yield(LONG2NUM(i));
    } else {
      c->value = item;
    }
  }
  return self;
}

#first(*args) ⇒ Object



991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'ext/list/list.c', line 991

static VALUE
list_first(int argc, VALUE *argv, VALUE self)
{
  list_t *ptr;
  Data_Get_Struct(self, list_t, ptr);
  if (argc == 0) {
    if (ptr->first == NULL) return Qnil;
    return ptr->first->value;
  } else {
    return list_take_first_or_last(argc, argv, self, LIST_TAKE_FIRST);
  }
}

#frozen?Boolean

Returns:

  • (Boolean)


585
586
587
588
589
590
# File 'ext/list/list.c', line 585

static VALUE
list_frozen_p(VALUE self)
{
  if (OBJ_FROZEN(self)) return Qtrue;
  return Qfalse;
}

#hashObject



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'ext/list/list.c', line 633

static VALUE
list_hash(VALUE self)
{
  item_t *c;
  st_index_t h;
  list_t *ptr;
  VALUE n;

  Data_Get_Struct(self, list_t, ptr);
  h = rb_hash_start(ptr->len);
  h = rb_hash_uint(h, (st_index_t)list_hash);
  LIST_FOR(ptr, c) {
    n = rb_hash(c->value);
    h = rb_hash_uint(h, NUM2LONG(n));
  }
  h = rb_hash_end(h);
  return LONG2FIX(h);
}

#include?(item) ⇒ Boolean

Returns:

  • (Boolean)


1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
# File 'ext/list/list.c', line 1857

static VALUE
list_include_p(VALUE self, VALUE item)
{
  list_t *ptr;
  item_t *c;

  Data_Get_Struct(self, list_t, ptr);
  LIST_FOR (ptr, c) {
    if (rb_equal(c->value, item)) {
      return Qtrue;
    }
  }
  return Qfalse;
}

#initialize_copy(orig) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'ext/list/list.c', line 495

static VALUE
list_replace(VALUE copy, VALUE orig)
{
  list_t *ptr_copy;
  list_t *ptr_orig;
  item_t *c_orig;
  item_t *c_copy;
  long olen;

  list_modify_check(copy);
  if (copy == orig) return copy;

  switch (rb_type(orig)) {
  case T_ARRAY:
    return list_replace_ary(copy, orig);
  case T_DATA:
    break;
  default:
    rb_raise(rb_eTypeError, "cannot convert to list");
  }
  orig = to_list(orig);
  Data_Get_Struct(copy, list_t, ptr_copy);
  Data_Get_Struct(orig, list_t, ptr_orig);
  olen = ptr_orig->len;
  if (olen == 0) {
    return list_clear(copy);
  }
  if (olen == ptr_copy->len) {
    LIST_FOR_DOUBLE(ptr_orig, c_orig, ptr_copy, c_copy, {
      c_copy->value = c_orig->value;
    });
  } else {
    list_clear(copy);
    LIST_FOR(ptr_orig, c_orig) {
      list_push(copy, c_orig->value);
    }
  }

  return copy;
}

#insert(*args) ⇒ Object



1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
# File 'ext/list/list.c', line 1146

static VALUE
list_insert(int argc, VALUE *argv, VALUE self)
{
  list_t *ptr;
  long pos;

  Data_Get_Struct(self, list_t, ptr);
  rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
  list_modify_check(self);
  if (argc == 1) return self;
  pos = NUM2LONG(argv[0]);
  if (pos == -1) {
    pos = ptr->len;
  }
  if (pos < 0) {
    pos++;
  }
  list_splice(self, pos, 0, rb_ary_new4(argc - 1, argv + 1));
  return self;
}

#inspectObject Also known as: to_s



559
560
561
562
563
564
565
566
567
# File 'ext/list/list.c', line 559

static VALUE
list_inspect(VALUE self)
{
  list_t *ptr;
  Data_Get_Struct(self, list_t, ptr);
  if (ptr->len == 0)
    return rb_sprintf("#<%s: []>", rb_obj_classname(self));
  return rb_exec_recursive(inspect_list, self, 0);
}

#join(*args) ⇒ Object



1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
# File 'ext/list/list.c', line 1362

static VALUE
list_join_m(int argc, VALUE *argv, VALUE self)
{
  VALUE sep;

  rb_scan_args(argc, argv, "01", &sep);
  if (NIL_P(sep)) sep = rb_output_fs;

  return list_join(self, sep);
}

#keep_ifObject



1549
1550
1551
1552
1553
1554
1555
# File 'ext/list/list.c', line 1549

static VALUE
list_keep_if(VALUE self)
{
  RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);
  list_select_bang(self);
  return self;
}

#last(*args) ⇒ Object



1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'ext/list/list.c', line 1004

static VALUE
list_last(int argc, VALUE *argv, VALUE self)
{
  list_t *ptr;
  long len;

  Data_Get_Struct(self, list_t, ptr);
  if (argc == 0) {
    len = ptr->len;
    if (len == 0) return Qnil;
    return list_elt(self, len - 1);
  } else {
    return list_take_first_or_last(argc, argv, self, LIST_TAKE_LAST);
  }
}

#lengthObject Also known as: size



1167
1168
1169
1170
1171
1172
1173
# File 'ext/list/list.c', line 1167

static VALUE
list_length(VALUE self)
{
  list_t *ptr;
  Data_Get_Struct(self, list_t, ptr);
  return LONG2NUM(ptr->len);
}

#mapObject



1503
1504
1505
1506
1507
# File 'ext/list/list.c', line 1503

static VALUE
list_collect(VALUE self)
{
  return list_collect_bang(rb_obj_clone(self));
}

#map!Object



1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
# File 'ext/list/list.c', line 1489

static VALUE
list_collect_bang(VALUE self)
{
  list_t *ptr;
  item_t *c;

  RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);
  Data_Get_Struct(self, list_t, ptr);
  LIST_FOR(ptr, c) {
    c->value = rb_yield(c->value);
  }
  return self;
}

#pop(*args) ⇒ Object



1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
# File 'ext/list/list.c', line 1062

static VALUE
list_pop_m(int argc, VALUE *argv, VALUE self)
{
  list_t *ptr;
  VALUE result;
  long n;

  if (argc == 0) {
    return list_pop(self);
  }

  list_modify_check(self);
  result = list_take_first_or_last(argc, argv, self, LIST_TAKE_LAST);
  Data_Get_Struct(self, list_t, ptr);
  n = NUM2LONG(argv[0]);
  list_mem_clear(ptr, ptr->len - n, n);
  return result;
}

#push(*args) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
# File 'ext/list/list.c', line 337

static VALUE
list_push_m(int argc, VALUE *argv, VALUE self)
{
  long i;

  list_modify_check(self);
  if (argc == 0) return self;
  for (i = 0; i < argc; i++) {
    list_push(self, argv[i]);
  }
  return self;
}

#rejectObject



1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
# File 'ext/list/list.c', line 1739

static VALUE
list_reject(VALUE self)
{
  VALUE rejected_list;

  RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);
  rejected_list = list_new();
  reject(self, rejected_list);
  return rejected_list;
}

#reject!Object



1732
1733
1734
1735
1736
1737
# File 'ext/list/list.c', line 1732

static VALUE
list_reject_bang(VALUE self)
{
  RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);
  return reject_bang(self);
}

#replace(orig) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'ext/list/list.c', line 495

static VALUE
list_replace(VALUE copy, VALUE orig)
{
  list_t *ptr_copy;
  list_t *ptr_orig;
  item_t *c_orig;
  item_t *c_copy;
  long olen;

  list_modify_check(copy);
  if (copy == orig) return copy;

  switch (rb_type(orig)) {
  case T_ARRAY:
    return list_replace_ary(copy, orig);
  case T_DATA:
    break;
  default:
    rb_raise(rb_eTypeError, "cannot convert to list");
  }
  orig = to_list(orig);
  Data_Get_Struct(copy, list_t, ptr_copy);
  Data_Get_Struct(orig, list_t, ptr_orig);
  olen = ptr_orig->len;
  if (olen == 0) {
    return list_clear(copy);
  }
  if (olen == ptr_copy->len) {
    LIST_FOR_DOUBLE(ptr_orig, c_orig, ptr_copy, c_copy, {
      c_copy->value = c_orig->value;
    });
  } else {
    list_clear(copy);
    LIST_FOR(ptr_orig, c_orig) {
      list_push(copy, c_orig->value);
    }
  }

  return copy;
}

#reverseObject



1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
# File 'ext/list/list.c', line 1391

static VALUE
list_reverse_m(VALUE self)
{
  VALUE result;
  item_t *c;
  list_t *ptr;

  Data_Get_Struct(self, list_t, ptr);
  result = list_new();
  if (ptr->len == 0) return result;
  LIST_FOR(ptr, c) {
    list_unshift(result, c->value);
  }
  return result;
}

#reverse!Object



1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
# File 'ext/list/list.c', line 1373

static VALUE
list_reverse_bang(VALUE self)
{
  VALUE tmp;
  item_t *c;
  list_t *ptr;
  long len;

  Data_Get_Struct(self, list_t, ptr);
  if (ptr->len == 0) return self;
  tmp = list_to_a(self);
  len = ptr->len;
  LIST_FOR(ptr, c) {
    c->value = rb_ary_entry(tmp, --len);
  }
  return self;
}

#rindex(*args) ⇒ Object



1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
# File 'ext/list/list.c', line 1185

static VALUE
list_rindex(int argc, VALUE *argv, VALUE self)
{
  list_t *ptr;
  long i;
  long len;
  VALUE val;

  Data_Get_Struct(self, list_t, ptr);
  i = ptr->len;
  if (argc == 0) {
    RETURN_ENUMERATOR(self, 0, 0);
    while (i--) {
      Data_Get_Struct(self, list_t, ptr);
      if (RTEST(rb_yield(list_elt_ptr(ptr, i))))
        return LONG2NUM(i);
      if (ptr->len < i) {
        i = ptr->len;
      }
    }
    return Qnil;
  }
  rb_check_arity(argc, 0, 1);
  val = argv[0];
  if (rb_block_given_p())
    rb_warn("given block not used");
  Data_Get_Struct(self, list_t, ptr);
  while (i--) {
    if (rb_equal(list_elt_ptr(ptr, i), val)) {
      return LONG2NUM(i);
    }
    len = ptr->len;
    if (len < i) {
      i = len;
    }
    Data_Get_Struct(self, list_t, ptr);
  }
  return Qnil;
}

#ringObject



1971
1972
1973
1974
1975
1976
# File 'ext/list/list.c', line 1971

static VALUE
list_ring(VALUE self)
{
  VALUE clone = rb_obj_clone(self);
  return list_ring_bang(clone);
}

#ring!Object



1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
# File 'ext/list/list.c', line 1958

static VALUE
list_ring_bang(VALUE self)
{
  list_t *ptr;

  Data_Get_Struct(self, list_t, ptr);
  if (ptr->first == NULL)
    rb_raise(rb_eRuntimeError, "length is zero list cannot to change ring");
  rb_obj_freeze(self);
  ptr->last->next = ptr->first;
  return self;
}

#ring?Boolean

Returns:

  • (Boolean)


1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
# File 'ext/list/list.c', line 1978

static VALUE
list_ring_p(VALUE self)
{
  list_t *ptr;

  Data_Get_Struct(self, list_t, ptr);
  if (ptr->first == NULL)
    return Qfalse;
  if (ptr->first == ptr->last->next)
    return Qtrue;
  return Qfalse;
}

#rotate(*args) ⇒ Object



1434
1435
1436
1437
1438
1439
# File 'ext/list/list.c', line 1434

static VALUE
list_rotate_m(int argc, VALUE *argv, VALUE self)
{
  VALUE clone = rb_obj_clone(self);
  return list_rotate_bang(argc, argv, clone);
}

#rotate!(*args) ⇒ Object



1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
# File 'ext/list/list.c', line 1407

static VALUE
list_rotate_bang(int argc, VALUE *argv, VALUE self)
{
  list_t *ptr;
  item_t *c;
  long cnt = 1;
  long i = 0;

  switch (argc) {
  case 1: cnt = NUM2LONG(argv[0]);
  case 0: break;
  default: rb_scan_args(argc, argv, "01", NULL);
  }

  Data_Get_Struct(self, list_t, ptr);
  if (ptr->len == 0) return self;
  cnt = (cnt < 0) ? (ptr->len - (~cnt % ptr->len) - 1) : (cnt & ptr->len);
  LIST_FOR(ptr, c) {
    if (cnt == ++i) break;
  }
  ptr->last->next = ptr->first;
  ptr->first = c->next;
  ptr->last = c;
  ptr->last->next = NULL;
  return self;
}

#selectObject



1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
# File 'ext/list/list.c', line 1509

static VALUE
list_select(VALUE self)
{
  VALUE result;
  list_t *ptr;
  item_t *c;

  RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);
  Data_Get_Struct(self, list_t, ptr);
  result = list_new();
  LIST_FOR(ptr, c) {
    if (RTEST(rb_yield(c->value))) {
      list_push(result, c->value);
    }
  }
  return result;
}

#select!Object



1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
# File 'ext/list/list.c', line 1527

static VALUE
list_select_bang(VALUE self)
{
  VALUE result;
  list_t *ptr;
  item_t *c;
  long i = 0;

  RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);
  Data_Get_Struct(self, list_t, ptr);
  result = list_new();
  LIST_FOR(ptr, c) {
    if (RTEST(rb_yield(c->value))) {
      i++;
      list_push(result, c->value);
    }
  }

  if (i == ptr->len) return Qnil;
  return list_replace(self, result);
}

#shift(*args) ⇒ Object



1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
# File 'ext/list/list.c', line 1094

static VALUE
list_shift_m(int argc, VALUE *argv, VALUE self)
{
  VALUE result;
  long i;
  list_t *ptr_res;

  if (argc == 0) {
    return list_shift(self);
  }

  list_modify_check(self);
  result = list_take_first_or_last(argc, argv, self, LIST_TAKE_FIRST);
  Data_Get_Struct(result, list_t, ptr_res);
  for (i = 0; i < ptr_res->len; i++) {
    list_shift(self);
  }
  return result;
}

#slice(*args) ⇒ Object



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'ext/list/list.c', line 740

static VALUE
list_aref(int argc, VALUE *argv, VALUE self)
{
  VALUE arg;
  long beg, len;
  list_t *ptr;

  Data_Get_Struct(self, list_t, ptr);
  if (argc == 2) {
    beg = NUM2LONG(argv[0]);
    len = NUM2LONG(argv[1]);
    if (beg < 0) {
      beg += ptr->len;
    }
    return list_subseq(self, beg, len);
  }
  if (argc != 1) {
    rb_scan_args(argc, argv, "11", NULL, NULL);
  }
  arg = argv[0];

  /* special case - speeding up */
  if (FIXNUM_P(arg)) {
    return list_entry(self, FIX2LONG(arg));
  }
  /* check if idx is Range */
  switch (rb_range_beg_len(arg, &beg, &len, ptr->len, 0)) {
  case Qfalse:
    break;
  case Qnil:
    return Qnil;
  default:
    return list_subseq(self, beg, len);
  }
  return list_entry(self, NUM2LONG(arg));
}

#slice!(*args) ⇒ Object



1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
# File 'ext/list/list.c', line 1914

static VALUE
list_slice_bang(int argc, VALUE *argv, VALUE self)
{
  long pos = 0, len = 0;
  list_t *ptr;

  Data_Get_Struct(self, list_t, ptr);

  list_modify_check(self);
  if (argc == 1) {
    if (FIXNUM_P(argv[0])) {
      return list_delete_at(self, NUM2LONG(argv[0]));
    } else {
      switch (rb_range_beg_len(argv[0], &pos, &len, ptr->len, 0)) {
      case Qtrue: /* valid range */
        break;
      case Qnil: /* invalid range */
        return Qnil;
      default: /* not a range */
        return list_delete_at(self, NUM2LONG(argv[0]));
      }
    }
  } else if (argc == 2) {
    pos = NUM2LONG(argv[0]);
    len = NUM2LONG(argv[1]);
  } else {
    rb_scan_args(argc, argv, "11", NULL, NULL);
  }
  if (len < 0) return Qnil;
  if (pos < 0) {
    pos += ptr->len;
    if (pos < 0) return Qnil;
  } else if (ptr->len < pos) {
    return Qnil;
  }
  if (ptr->len < pos + len) {
    len = ptr->len - pos;
  }
  if (len == 0) return list_new();
  VALUE list2 = list_subseq(self, pos, len);
  list_mem_clear(ptr, pos, len);
  return list2;
}

#sortObject



1457
1458
1459
1460
1461
1462
# File 'ext/list/list.c', line 1457

static VALUE
list_sort(VALUE self)
{
  VALUE clone = rb_obj_clone(self);
  return list_sort_bang(clone);
}

#sort!Object



1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
# File 'ext/list/list.c', line 1441

static VALUE
list_sort_bang(VALUE self)
{
  list_t *ptr;
  item_t *c;
  long i = 0;

  VALUE ary = list_to_a(self);
  rb_ary_sort_bang(ary);
  Data_Get_Struct(self, list_t, ptr);
  LIST_FOR(ptr, c) {
    c->value = rb_ary_entry(ary, i++);
  }
  return self;
}

#sort_byObject



1470
1471
1472
1473
1474
1475
1476
1477
1478
# File 'ext/list/list.c', line 1470

static VALUE
list_sort_by(VALUE self)
{
  VALUE ary;

  RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);
  ary = rb_block_call(list_to_a(self), rb_intern("sort_by"), 0, 0, sort_by_i, 0);
  return to_list(ary);
}

#sort_by!Object



1480
1481
1482
1483
1484
1485
1486
1487
# File 'ext/list/list.c', line 1480

static VALUE
list_sort_by_bang(VALUE self)
{
  VALUE sorted;

  sorted = list_sort_by(self);
  return list_replace(self, sorted);
}

#to_aObject



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'ext/list/list.c', line 569

static VALUE
list_to_a(VALUE self)
{
  list_t *ptr;
  item_t *c;
  VALUE ary;
  long i = 0;

  Data_Get_Struct(self, list_t, ptr);
  ary = rb_ary_new2(ptr->len);
  LIST_FOR(ptr, c) {
    rb_ary_store(ary, i++, c->value);
  }
  return ary;
}

#to_aryObject



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'ext/list/list.c', line 569

static VALUE
list_to_a(VALUE self)
{
  list_t *ptr;
  item_t *c;
  VALUE ary;
  long i = 0;

  Data_Get_Struct(self, list_t, ptr);
  ary = rb_ary_new2(ptr->len);
  LIST_FOR(ptr, c) {
    rb_ary_store(ary, i++, c->value);
  }
  return ary;
}

#to_listObject



1991
1992
1993
1994
1995
# File 'ext/list/list.c', line 1991

static VALUE
list_to_list(VALUE self)
{
  return self;
}

#transposeObject



1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
# File 'ext/list/list.c', line 1778

static VALUE
list_transpose(VALUE self)
{
  VALUE ary;
  long i;

  ary = rb_funcall(list_to_a(self), rb_intern("transpose"), 0);
  for (i = 0; i < RARRAY_LEN(ary); i++) {
    rb_ary_store(ary, i, to_list(rb_ary_entry(ary, i)));
  }
  return to_list(ary);
}

#unshift(*args) ⇒ Object



1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
# File 'ext/list/list.c', line 1133

static VALUE
list_unshift_m(int argc, VALUE *argv, VALUE self)
{
  long i;

  list_modify_check(self);
  if (argc == 0) return self;
  for (i = 0; i < argc; i++) {
    list_unshift(self, argv[i]);
  }
  return self;
}

#values_at(*args) ⇒ Object



1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
# File 'ext/list/list.c', line 1557

static VALUE
list_values_at(int argc, VALUE *argv, VALUE self)
{
  VALUE result = list_new();
  long beg, len;
  long i, j;
  list_t *ptr;

  Data_Get_Struct(self, list_t, ptr);
  for (i = 0; i < argc; i++) {
    if (FIXNUM_P(argv[i])) {
      list_push(result, list_entry(self, FIX2LONG(argv[i])));
    } else if (rb_range_beg_len(argv[i], &beg, &len, ptr->len, 1)) {
      for (j = beg; j < beg + len; j++) {
        if (ptr->len < j) {
          list_push(result, Qnil);
        } else {
          list_push(result, list_elt_ptr(ptr, j));
        }
      }
    } else {
      list_push(result, list_entry(self, NUM2LONG(argv[i])));
    }
  }
  return result;
}

#zip(*args) ⇒ Object



1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
# File 'ext/list/list.c', line 1758

static VALUE
list_zip(int argc, VALUE *argv, VALUE self)
{
  VALUE ary;
  long i;

  ary = rb_funcall2(list_to_a(self), rb_intern("zip"), argc, argv);
  for (i = 0; i < RARRAY_LEN(ary); i++) {
    rb_ary_store(ary, i, to_list(rb_ary_entry(ary, i)));
  }
  if (rb_block_given_p()) {
    for (i = 0; i < RARRAY_LEN(ary); i++) {
      rb_yield(rb_ary_entry(ary, i));
    }
    return Qnil;
  } else {
    return to_list(ary);
  }
}