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



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'ext/list/list.c', line 410

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



388
389
390
391
392
393
394
395
# File 'ext/list/list.c', line 388

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



404
405
406
407
408
# File 'ext/list/list.c', line 404

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

Instance Method Details

#&(list2) ⇒ Object



2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
# File 'ext/list/list.c', line 2080

static VALUE
list_and(VALUE list1, VALUE list2)
{
	VALUE list3, hash;
	st_table *table;
	st_data_t vv;
	item_t *c1;

	list2 = to_list(list2);
	list3 = list_new();
	if (LIST_LEN(list2) == 0) return list3;
	hash = list_make_hash(list2);
	table = RHASH_TBL(hash);

	LIST_FOR(list1, c1) {
		vv = (st_data_t) c1->value;
		if (st_delete(table, &vv, 0)) {
			list_push(list3, c1->value);
		}
	}

	return list3;
}

#*(times) ⇒ Object



1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
# File 'ext/list/list.c', line 1976

static VALUE
list_times(VALUE self, VALUE times)
{
	VALUE result;
	VALUE tmp;
	long i, len;
	item_t *c;

	tmp = rb_check_string_type(times);
	if (!NIL_P(tmp)) {
		return list_join(self, tmp);
	}

	len = NUM2LONG(times);
	if (len == 0) {
		return rb_obj_alloc(rb_obj_class(self));
	}
	if (len < 0) {
		rb_raise(rb_eArgError, "negative argument");
	}

	if (LIST_MAX_SIZE / len < LIST_LEN(self)) {
		rb_raise(rb_eArgError, "argument too big");
	}

	result = rb_obj_alloc(rb_obj_class(self));
	if (0 < LIST_LEN(self)) {
		for (i = 0; i < len; i++) {
			LIST_FOR(self, c) {
				list_push(result, c->value);
			}
		}
	}
	return result;
}

#+(y) ⇒ Object



1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
# File 'ext/list/list.c', line 1956

static VALUE
list_plus(VALUE x, VALUE y)
{
	item_t *cx, *cy;
	long len;
	VALUE result;

	Check_Type(y, T_DATA);
	len = LIST_LEN(x) + LIST_LEN(y);

	result = list_new();
	LIST_FOR(x,cx) {
		list_push(result, cx->value);
	}
	LIST_FOR(y,cy) {
		list_push(result, cy->value);
	}
	return result;
}

#-(list2) ⇒ Object



2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
# File 'ext/list/list.c', line 2063

static VALUE
list_diff(VALUE list1, VALUE list2)
{
	VALUE list3;
	volatile VALUE hash;
	item_t *c;

	hash = list_make_hash(to_list(list2));
	list3 = list_new();

	LIST_FOR(list1, c) {
		if (st_lookup(RHASH_TBL(hash), c->value, 0)) continue;
		list_push(list3, c->value);
	}
	return list3;
}

#<<(obj) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'ext/list/list.c', line 337

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;
	}
	LIST_LEN(self)++;
	return self;
}

#<=>(list2) ⇒ Object



1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
# File 'ext/list/list.c', line 1863

static VALUE
list_cmp(VALUE list1, VALUE list2)
{
	VALUE v;
	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;
	len = LIST_LEN(list1) - LIST_LEN(list2);
	if (len == 0) return INT2FIX(0);
	if (0 < len) return INT2FIX(1);
	return INT2FIX(-1);
}

#==(obj) ⇒ Object



647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'ext/list/list.c', line 647

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, id_to_list)) {
			return Qfalse;
		}
		return rb_equal(obj, self);
	}
	return rb_exec_recursive_paired(recursive_equal, self, obj, obj);

}

#[](*args) ⇒ Object



761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
# File 'ext/list/list.c', line 761

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 += LIST_LEN(self);
		}
		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, LIST_LEN(self), 0)) {
	case Qfalse:
		break;
	case Qnil:
		return Qnil;
	default:
		return list_subseq(self, beg, len);
	}
	return list_entry(self, NUM2LONG(arg));
}

#[]=(*args) ⇒ Object



917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
# File 'ext/list/list.c', line 917

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

	Data_Get_Struct(self, list_t, ptr);
	if (argc == 3) {
		list_modify_check(self);
		beg = NUM2LONG(argv[0]);
		len = NUM2LONG(argv[1]);
		list_splice(self, beg, len, argv[2]);
		return argv[2];
	}
	rb_check_arity(argc, 2, 2);
	list_modify_check(self);
	if (FIXNUM_P(argv[0])) {
		offset = FIX2LONG(argv[0]);
		goto fixnum;
	}
	if (rb_range_beg_len(argv[0], &beg, &len, LIST_LEN(self), 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];
}

#assoc(key) ⇒ Object



1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
# File 'ext/list/list.c', line 1920

static VALUE
list_assoc(VALUE self, VALUE key)
{
	VALUE v;
	item_t *c;

	if (list_empty_p(self)) return Qnil;
	LIST_FOR(self, c) {
		v = check_list_type(c->value);
		if (!NIL_P(v)) {
			if (0 < LIST_LEN(v) && rb_equal(list_first(0,NULL,v), key)) {
				return v;
			}
		}
	}
	return Qnil;
}

#at(pos) ⇒ Object



949
950
951
952
953
# File 'ext/list/list.c', line 949

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

#bsearchObject

from CRuby rb_ary_bsearch



2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
# File 'ext/list/list.c', line 2444

static VALUE
list_bsearch(VALUE self)
{
	long low, high, mid;
	int smaller = 0, satisfied = 0;
	VALUE v, val, ary;

	ary = list_to_a(self);
	low = 0;
	high = RARRAY_LEN(ary);

	RETURN_ENUMERATOR(self, 0, 0);
	while (low < high) {
		mid = low + ((high - low) / 2);
		val = rb_ary_entry(ary, mid);
		v = rb_yield(val);
		if (FIXNUM_P(v)) {
			if (FIX2INT(v) == 0) return val;
			smaller = FIX2INT(v) < 0;
		}
		else if (v == Qtrue) {
			satisfied = 1;
			smaller = 1;
		}
		else if (v == Qfalse || v == Qnil) {
			smaller = 0;
		}
		else if (rb_obj_is_kind_of(v, rb_cNumeric)) {
			const VALUE zero = INT2FIX(0);
			switch (rb_cmpint(rb_funcall(v, id_cmp, 1, zero), v, INT2FIX(0))) {
				case 0: return val;
				case 1: smaller = 1; break;
				case -1: smaller = 0;
			}
		}
		else {
			rb_raise(rb_eTypeError, "wrong argument type %s"
					" (must be numeric, true, false or nil)",
					rb_obj_classname(v));
		}
		if (smaller) {
			high = mid;
		}
		else {
			low = mid + 1;
		}
	}
	if (low == RARRAY_LEN(ary)) return Qnil;
	if (!satisfied) return Qnil;
	return rb_ary_entry(ary, low);
}

#clearObject



485
486
487
488
489
490
491
492
493
494
495
496
# File 'ext/list/list.c', line 485

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;
	LIST_LEN(self) = 0;
	return self;
}

#collectObject



1495
1496
1497
1498
1499
# File 'ext/list/list.c', line 1495

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

#collect!Object



1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
# File 'ext/list/list.c', line 1483

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

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

#combination(num) ⇒ Object



2364
2365
2366
2367
2368
# File 'ext/list/list.c', line 2364

static VALUE
list_combination(VALUE self, VALUE num)
{
	return list_delegate_rb(1, &num, self, rb_intern("combination"));
}

#compactObject



2203
2204
2205
2206
2207
2208
2209
# File 'ext/list/list.c', line 2203

static VALUE
list_compact(VALUE self)
{
	self = list_dup(self);
	list_compact_bang(self);
	return self;
}

#compact!Object



2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
# File 'ext/list/list.c', line 2189

static VALUE
list_compact_bang(VALUE self)
{
	long len;

	list_modify_check(self);
	len = LIST_LEN(self);
	list_delete(self, Qnil);
	if (len == LIST_LEN(self)) {
		return Qnil;
	}
	return self;
}

#concat(obj) ⇒ Object



1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
# File 'ext/list/list.c', line 1038

static VALUE
list_concat(VALUE self, VALUE obj)
{
	long len;
	enum ruby_value_type type;

	list_modify_check(self);
	type = rb_type(obj);
	if (type == T_DATA) {
		len = LIST_LEN(obj);
	} else if (type == T_ARRAY) {
		len = RARRAY_LEN(obj);
	} else {
		obj = to_list(obj);
		len = LIST_LEN(obj);
	}

	if (0 < len) {
		list_splice(self, LIST_LEN(self), 0, obj);
	}
	return self;
}

#count(*args) ⇒ Object



2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
# File 'ext/list/list.c', line 2288

static VALUE
list_count(int argc, VALUE *argv, VALUE self)
{
	VALUE obj;
	item_t *c;
	long n = 0;

	if (argc == 0) {
		if (!rb_block_given_p()) {
			return list_length(self);
		}
		LIST_FOR(self,c) {
			if (RTEST(rb_yield(c->value))) n++;
		}
	} else {
		rb_scan_args(argc, argv, "1", &obj);
		if (rb_block_given_p()) {
			rb_warn("given block not used");
		}
		LIST_FOR(self,c) {
			if (rb_equal(c->value, obj)) n++;
		}
	}
	return LONG2NUM(n);
}

#cycle(*args) ⇒ Object



2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
# File 'ext/list/list.c', line 2332

static VALUE
list_cycle(int argc, VALUE *argv, VALUE self)
{
	VALUE nv = Qnil;
	item_t *c;
	long n;

	RETURN_SIZED_ENUMERATOR(self, argc, argv, list_cycle_size);
	rb_scan_args(argc, argv, "01", &nv);

	if (NIL_P(nv)) {
		n = -1;
	} else {
		n = NUM2LONG(nv);
		if (n <= 0) return Qnil;
	}

	while (0 < LIST_LEN(self) && (n < 0 || 0 < n--)) {
		LIST_FOR(self, c) {
			rb_yield(c->value);
			if (list_empty_p(self)) break;
		}
	}
	return Qnil;
}

#delete(item) ⇒ Object



1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
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
# File 'ext/list/list.c', line 1572

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 (LIST_LEN(self) == 0) return Qnil;

	len = LIST_LEN(self);
	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);
			LIST_LEN(self)--;
		} else {
			before = c;
		}
	}

	if (LIST_LEN(self) == len) {
		if (rb_block_given_p()) {
			return rb_yield(item);
		}
		return Qnil;
	} else {
		return item;
	}
}

#delete_at(pos) ⇒ Object



1662
1663
1664
1665
1666
# File 'ext/list/list.c', line 1662

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

#delete_ifObject



1736
1737
1738
1739
1740
1741
1742
# File 'ext/list/list.c', line 1736

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

#drop(n) ⇒ Object



2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
# File 'ext/list/list.c', line 2413

static VALUE
list_drop(VALUE self, VALUE n)
{
	VALUE result;
	long pos = NUM2LONG(n);

	if (pos < 0) {
		rb_raise(rb_eArgError, "attempt to drop negative size");
	}
	result = list_subseq(self, pos, LIST_LEN(self));
	if (result == Qnil) result = list_new();
	return result;
}

#drop_whileObject



2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
# File 'ext/list/list.c', line 2427

static VALUE
list_drop_while(VALUE self)
{
	long i = 0;
	item_t *c;

	RETURN_ENUMERATOR(self, 0, 0);
	LIST_FOR(self, c) {
		if (!RTEST(rb_yield(c->value))) break;
		i++;
	}
	return list_drop(self, LONG2FIX(i));
}

#eachObject



457
458
459
460
461
462
463
464
465
466
467
468
# File 'ext/list/list.c', line 457

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

	RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);

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

#each_indexObject



470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'ext/list/list.c', line 470

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

	RETURN_SIZED_ENUMERATOR(self, 0, 0, list_enum_length);

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

#empty?Boolean

Returns:

  • (Boolean)


1182
1183
1184
1185
1186
1187
1188
1189
1190
# File 'ext/list/list.c', line 1182

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

#fetch(*args) ⇒ Object



955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
# File 'ext/list/list.c', line 955

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 += LIST_LEN(self);
	}
	if (idx < 0 || LIST_LEN(self) <= 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 ? LIST_LEN(self) : 0), -LIST_LEN(self), LIST_LEN(self));
		}
		return ifnone;
	}
	return list_entry(self, idx);
}

#fill(*args) ⇒ Object



1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
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
# File 'ext/list/list.c', line 1767

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;
	item_t *c;

	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 = LIST_LEN(self);
		break;
	case 2:
		if (rb_range_beg_len(arg1, &beg, &len, LIST_LEN(self), 1)) {
			break;
		}
		/* fall through */
	case 3:
		beg = NIL_P(arg1) ? 0 : NUM2LONG(arg1);
		if (beg < 0) {
			beg = LIST_LEN(self) + beg;
			if (beg < 0) beg = 0;
		}
		len = NIL_P(arg2) ? LIST_LEN(self) - 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 (LIST_LEN(self) < end) {
		for (i = 0; i < end - LIST_LEN(self); i++) {
			list_push(self, Qnil);
		}
	}

	i = -1;
	LIST_FOR(self, 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



1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'ext/list/list.c', line 1009

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);
	}
}

#flatten(*args) ⇒ Object



2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
# File 'ext/list/list.c', line 2253

static VALUE
list_flatten(int argc, VALUE *argv, VALUE self)
{
	int mod = 0, level = -1;
	VALUE result, lv;

	rb_scan_args(argc, argv, "01", &lv);
	if (!NIL_P(lv)) level = NUM2INT(lv);
	if (level == 0) return list_make_shared_copy(self);

	result = flatten(self, level, &mod);
	OBJ_INFECT(result, self);

	return result;
}

#flatten!(*args) ⇒ Object



2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
# File 'ext/list/list.c', line 2269

static VALUE
list_flatten_bang(int argc, VALUE *argv, VALUE self)
{
	int mod = 0, level = -1;
	VALUE result, lv;

	rb_scan_args(argc, argv, "01", &lv);
	list_modify_check(self);
	if (!NIL_P(lv)) level = NUM2INT(lv);
	if (level == 0) return Qnil;

	result = flatten(self, level, &mod);
	if (mod == 0) {
		return Qnil;
	}
	list_replace(self, result);
	return self;
}

#frozen?Boolean

Returns:

  • (Boolean)


606
607
608
609
610
611
# File 'ext/list/list.c', line 606

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

#hashObject



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'ext/list/list.c', line 666

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

	h = rb_hash_start(LIST_LEN(self));
	h = rb_hash_uint(h, (st_index_t)list_hash);
	LIST_FOR(self, 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)


1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
# File 'ext/list/list.c', line 1830

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

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

#initialize_copy(orig) ⇒ Object



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'ext/list/list.c', line 526

static VALUE
list_replace(VALUE copy, VALUE 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);
	olen = LIST_LEN(orig);
	if (olen == 0) {
		return list_clear(copy);
	}
	if (olen == LIST_LEN(copy)) {
		LIST_FOR_DOUBLE(orig, c_orig, copy, c_copy, {
			c_copy->value = c_orig->value;
		});
	} else {
		list_clear(copy);
		LIST_FOR(orig, c_orig) {
			list_push(copy, c_orig->value);
		}
	}

	return copy;
}

#insert(*args) ⇒ Object



1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
# File 'ext/list/list.c', line 1153

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 = LIST_LEN(self);
	}
	if (pos < 0) {
		pos++;
	}
	list_splice(self, pos, 0, rb_ary_new4(argc - 1, argv + 1));
	return self;
}

#inspectObject Also known as: to_s



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

static VALUE
list_inspect(VALUE self)
{
	if (LIST_LEN(self) == 0)
		return rb_sprintf("#<%s: []>", rb_obj_classname(self));
	return rb_exec_recursive(inspect_list, self, 0);
}

#join(*args) ⇒ Object



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

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



1537
1538
1539
1540
1541
1542
1543
# File 'ext/list/list.c', line 1537

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



1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
# File 'ext/list/list.c', line 1022

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 = LIST_LEN(self);
		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



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

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

#mapObject



1495
1496
1497
1498
1499
# File 'ext/list/list.c', line 1495

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

#map!Object



1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
# File 'ext/list/list.c', line 1483

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

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

#pack(str) ⇒ Object



2535
2536
2537
2538
2539
# File 'ext/list/list.c', line 2535

static VALUE
list_pack(VALUE self, VALUE str)
{
	return list_delegate_rb(1, &str, self, rb_intern("pack"));
}

#permutation(*args) ⇒ Object



2358
2359
2360
2361
2362
# File 'ext/list/list.c', line 2358

static VALUE
list_permutation(int argc, VALUE *argv, VALUE self)
{
	return list_delegate_rb(argc, argv, self, rb_intern("permutation"));
}

#pop(*args) ⇒ Object



1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
# File 'ext/list/list.c', line 1073

static VALUE
list_pop_m(int argc, VALUE *argv, VALUE self)
{
	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);
	n = NUM2LONG(argv[0]);
	list_mem_clear(self, LIST_LEN(self) - n, n);
	return result;
}

#product(*args) ⇒ Object



2382
2383
2384
2385
2386
# File 'ext/list/list.c', line 2382

static VALUE
list_product(int argc, VALUE *argv, VALUE self)
{
	return list_delegate_rb(argc, argv, self, rb_intern("product"));
}

#push(*args) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
# File 'ext/list/list.c', line 375

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;
}

#rassoc(value) ⇒ Object



1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
# File 'ext/list/list.c', line 1938

static VALUE
list_rassoc(VALUE self, VALUE value)
{
	VALUE v;
	item_t *c;

	if (list_empty_p(self)) return Qnil;
	LIST_FOR(self, c) {
		v = check_list_type(c->value);
		if (!NIL_P(v)) {
			if (1 < LIST_LEN(v) && rb_equal(list_elt(v,1), value)) {
				return v;
			}
		}
	}
	return Qnil;
}

#rejectObject



1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
# File 'ext/list/list.c', line 1725

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



1718
1719
1720
1721
1722
1723
# File 'ext/list/list.c', line 1718

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

#repeated_combination(num) ⇒ Object



2376
2377
2378
2379
2380
# File 'ext/list/list.c', line 2376

static VALUE
list_repeated_combination(VALUE self, VALUE num)
{
	return list_delegate_rb(1, &num, self, rb_intern("repeated_combination"));
}

#repeated_permutation(num) ⇒ Object



2370
2371
2372
2373
2374
# File 'ext/list/list.c', line 2370

static VALUE
list_repeated_permutation(VALUE self, VALUE num)
{
	return list_delegate_rb(1, &num, self, rb_intern("repeated_permutation"));
}

#replace(orig) ⇒ Object



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'ext/list/list.c', line 526

static VALUE
list_replace(VALUE copy, VALUE 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);
	olen = LIST_LEN(orig);
	if (olen == 0) {
		return list_clear(copy);
	}
	if (olen == LIST_LEN(copy)) {
		LIST_FOR_DOUBLE(orig, c_orig, copy, c_copy, {
			c_copy->value = c_orig->value;
		});
	} else {
		list_clear(copy);
		LIST_FOR(orig, c_orig) {
			list_push(copy, c_orig->value);
		}
	}

	return copy;
}

#reverseObject



1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
# File 'ext/list/list.c', line 1388

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

	result = list_new();
	if (LIST_LEN(self) == 0) return result;
	LIST_FOR(self, c) {
		list_unshift(result, c->value);
	}
	return result;
}

#reverse!Object



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

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

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

#rindex(*args) ⇒ Object



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
1224
1225
# File 'ext/list/list.c', line 1192

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

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

#ringObject



2509
2510
2511
2512
2513
2514
# File 'ext/list/list.c', line 2509

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

#ring!Object



2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
# File 'ext/list/list.c', line 2496

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)


2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
# File 'ext/list/list.c', line 2516

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



1430
1431
1432
1433
1434
1435
# File 'ext/list/list.c', line 1430

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



1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
# File 'ext/list/list.c', line 1402

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);
	}

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

	ptr->last->next = ptr->first;
	ptr->first = c->next;
	ptr->last = c;
	ptr->last->next = NULL;
	return self;
}

#sample(*args) ⇒ Object



2326
2327
2328
2329
2330
# File 'ext/list/list.c', line 2326

static VALUE
list_sample(int argc, VALUE *argv, VALUE self)
{
	return list_delegate_rb(argc, argv, self, rb_intern("sample"));
}

#selectObject



1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
# File 'ext/list/list.c', line 1501

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

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

#select!Object



1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
# File 'ext/list/list.c', line 1517

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

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

	if (i == LIST_LEN(self)) return Qnil;
	return list_replace(self, result);
}

#shift(*args) ⇒ Object



1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
# File 'ext/list/list.c', line 1101

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

	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);
	for (i = 0; i < LIST_LEN(self); i++) {
		list_shift(self);
	}
	return result;
}

#shuffle(*args) ⇒ Object



2314
2315
2316
2317
2318
# File 'ext/list/list.c', line 2314

static VALUE
list_shuffle(int argc, VALUE *argv, VALUE self)
{
	return list_delegate_rb(argc, argv, self, rb_intern("shuffle"));
}

#shuffle!(*args) ⇒ Object



2320
2321
2322
2323
2324
# File 'ext/list/list.c', line 2320

static VALUE
list_shuffle_bang(int argc, VALUE *argv, VALUE self)
{
	return list_replace(self, list_shuffle(argc, argv, self));
}

#slice(*args) ⇒ Object



761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
# File 'ext/list/list.c', line 761

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 += LIST_LEN(self);
		}
		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, LIST_LEN(self), 0)) {
	case Qfalse:
		break;
	case Qnil:
		return Qnil;
	default:
		return list_subseq(self, beg, len);
	}
	return list_entry(self, NUM2LONG(arg));
}

#slice!(*args) ⇒ Object



1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
# File 'ext/list/list.c', line 1879

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

	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, LIST_LEN(self), 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 += LIST_LEN(self);
		if (pos < 0) return Qnil;
	} else if (LIST_LEN(self) < pos) {
		return Qnil;
	}
	if (LIST_LEN(self) < pos + len) {
		len = LIST_LEN(self) - pos;
	}
	if (len == 0) return list_new();
	VALUE list2 = list_subseq(self, pos, len);
	list_mem_clear(self, pos, len);
	return list2;
}

#sortObject



1451
1452
1453
1454
1455
1456
# File 'ext/list/list.c', line 1451

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

#sort!Object



1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
# File 'ext/list/list.c', line 1437

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

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

#sort_byObject



1464
1465
1466
1467
1468
1469
1470
1471
1472
# File 'ext/list/list.c', line 1464

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



1474
1475
1476
1477
1478
1479
1480
1481
# File 'ext/list/list.c', line 1474

static VALUE
list_sort_by_bang(VALUE self)
{
	VALUE sorted;

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

#take(n) ⇒ Object



2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
# File 'ext/list/list.c', line 2388

static VALUE
list_take(VALUE self, VALUE n)
{
	int len = NUM2LONG(n);

	if (len < 0) {
		rb_raise(rb_eArgError, "attempt to take negative size");
	}
	return list_subseq(self, 0, len);
}

#take_whileObject



2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
# File 'ext/list/list.c', line 2399

static VALUE
list_take_while(VALUE self)
{
	item_t *c;
	long i = 0;

	RETURN_ENUMERATOR(self, 0, 0);
	LIST_FOR(self, c) {
		if (!RTEST(rb_yield(c->value))) break;
		i++;
	}
	return list_take(self, LONG2FIX(i));
}

#to_aObject



592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'ext/list/list.c', line 592

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

	ary = rb_ary_new2(LIST_LEN(self));
	LIST_FOR(self, c) {
		rb_ary_store(ary, i++, c->value);
	}
	return ary;
}

#to_aryObject



592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'ext/list/list.c', line 592

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

	ary = rb_ary_new2(LIST_LEN(self));
	LIST_FOR(self, c) {
		rb_ary_store(ary, i++, c->value);
	}
	return ary;
}

#to_listObject



2529
2530
2531
2532
2533
# File 'ext/list/list.c', line 2529

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

#transposeObject



1761
1762
1763
1764
1765
# File 'ext/list/list.c', line 1761

static VALUE
list_transpose(VALUE self)
{
	return list_delegate_rb(0, NULL, self, rb_intern("transpose"));
}

#uniqObject



2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
# File 'ext/list/list.c', line 2154

static VALUE
list_uniq(VALUE self)
{
	VALUE hash, uniq;

	if (LIST_LEN(self) <= 1)
		return list_dup(self);

	if (rb_block_given_p()) {
		hash = list_make_hash_by(self);
	} else {
		hash = list_make_hash(self);
	}
	uniq = list_hash_values(hash);
	return uniq;
}

#uniq!Object



2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
# File 'ext/list/list.c', line 2171

static VALUE
list_uniq_bang(VALUE self)
{
	long len;

	list_modify_check(self);
	len = LIST_LEN(self);
	if (len <= 1)
		return Qnil;

	list_replace(self, list_uniq(self));
	if (len == LIST_LEN(self)) {
		return Qnil;
	}

	return self;
}

#unshift(*args) ⇒ Object



1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
# File 'ext/list/list.c', line 1140

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

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

#values_at(*args) ⇒ Object



1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
# File 'ext/list/list.c', line 1545

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, LIST_LEN(self), 1)) {
			for (j = beg; j < beg + len; j++) {
				if (LIST_LEN(self) < j) {
					list_push(result, Qnil);
				} else {
					list_push(result, list_elt(self, j));
				}
			}
		} else {
			list_push(result, list_entry(self, NUM2LONG(argv[i])));
		}
	}
	return result;
}

#zip(*args) ⇒ Object



1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
# File 'ext/list/list.c', line 1744

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);
	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);
	}
}

#|(list2) ⇒ Object



2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
# File 'ext/list/list.c', line 2122

static VALUE
list_or(VALUE list1, VALUE list2)
{
	VALUE hash, list3;
	st_data_t vv;
	item_t *c1, *c2;

	list2 = to_list(list2);
	list3 = list_new();
	hash = list_add_hash(list_make_hash(list1), list2);

	LIST_FOR(list1,c1) {
		vv = (st_data_t)c1->value;
		if (st_delete(RHASH_TBL(hash), &vv, 0)) {
			list_push(list3, c1->value);
		}
	}
	LIST_FOR(list2,c2) {
		vv = (st_data_t)c2->value;
		if (st_delete(RHASH_TBL(hash), &vv, 0)) {
			list_push(list3, c2->value);
		}
	}
	return list3;
}