Class: Usamin::Array

Inherits:
Value
  • Object
show all
Includes:
Enumerable
Defined in:
ext/usamin/usamin.cpp

Instance Method Summary collapse

Methods inherited from Value

#==, #===, #array?, #eql?, #eval, #eval_r, #frozen?, #hash, #hash?, #object?, #root

Instance Method Details

#[](nth) ⇒ Object | nil #[](start, length) ⇒ ::Array<Object> | nil #[](range) ⇒ ::Array<Object> | nil

Overloads:

  • #[](nth) ⇒ Object | nil

    Parameters:

    • nth (Integer)

    Returns:

    • (Object | nil)
  • #[](start, length) ⇒ ::Array<Object> | nil

    Parameters:

    • start (Integer)
    • length (Integer)

    Returns:

    • (::Array<Object> | nil)
  • #[](range) ⇒ ::Array<Object> | nil

    Parameters:

    • range (Range)

    Returns:

    • (::Array<Object> | nil)


1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
# File 'ext/usamin/usamin.cpp', line 1246

static VALUE w_array_operator_indexer(const int argc, const VALUE *argv, const VALUE self) {
    rb_check_arity(argc, 1, 2);
    UsaminValue *value = get_value(self);
    check_array(value);
    rapidjson::SizeType sz = value->value->Size();
    if (argc == 2) {
        long beg = FIX2LONG(argv[0]);
        long len = FIX2LONG(argv[1]);
        if (beg < 0)
            beg += sz;
        if (beg >= 0 && len >= 0) {
            rapidjson::SizeType end = static_cast<rapidjson::SizeType>(beg + len);
            if (end > sz)
                end = sz;
            VALUE ret = rb_ary_new2(end - beg);
            for (rapidjson::SizeType i = static_cast<rapidjson::SizeType>(beg); i < end; i++)
                rb_ary_push(ret, eval((*value->value)[i], value->root_document));
            return ret;
        }
    } else if (rb_obj_is_kind_of(argv[0], rb_cRange)) {
        long beg, len;
        if (rb_range_beg_len(argv[0], &beg, &len, sz, 0) == Qtrue) {
            VALUE ret = rb_ary_new2(len);
            for (rapidjson::SizeType i = static_cast<rapidjson::SizeType>(beg); i < beg + len; i++)
                rb_ary_push(ret, eval((*value->value)[i], value->root_document));
            return ret;
        }
    } else {
        long l = FIX2LONG(argv[0]);
        if (l < 0)
            l += sz;
        if (0 <= l && l < sz)
            return eval((*value->value)[static_cast<rapidjson::SizeType>(l)], value->root_document);
    }
    return Qnil;
}

#at(nth) ⇒ Object

Parameters:

  • nth (Integer)

Returns:

  • (Object)


1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
# File 'ext/usamin/usamin.cpp', line 1287

static VALUE w_array_at(const VALUE self, const VALUE nth) {
    UsaminValue *value = get_value(self);
    check_array(value);
    long l = FIX2LONG(nth);
    rapidjson::SizeType sz = value->value->Size();
    if (l < 0)
        l += sz;
    if (0 <= l && l < sz)
        return eval((*value->value)[static_cast<rapidjson::SizeType>(l)], value->root_document);
    return Qnil;
}

#compact(nth) ⇒ ::Array

Returns:

  • (::Array)


1302
1303
1304
1305
1306
1307
1308
1309
1310
# File 'ext/usamin/usamin.cpp', line 1302

static VALUE w_array_compact(const VALUE self, const VALUE nth) {
    UsaminValue *value = get_value(self);
    check_array(value);
    VALUE ret = rb_ary_new2(value->value->Size());
    for (auto &v : value->value->GetArray())
        if (!v.IsNull())
            rb_ary_push(ret, eval(v, value->root_document));
    return ret;
}

#dig(*args) ⇒ Object | nil

Returns:

  • (Object | nil)


1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
# File 'ext/usamin/usamin.cpp', line 1315

static VALUE w_array_dig(const int argc, const VALUE *argv, const VALUE self) {
    rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
    VALUE value = w_array_at(self, argv[0]);
    if (argc == 1)
        return value;
    else if (value == Qnil)
        return Qnil;
    else
        return rb_funcall3(value, id_dig, argc - 1, argv + 1);
}

#each {|value| ... } ⇒ Enumerator | self

Yields:

  • (value)

Returns:

  • (Enumerator | self)


1334
1335
1336
1337
1338
1339
1340
1341
# File 'ext/usamin/usamin.cpp', line 1334

static VALUE w_array_each(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    RETURN_SIZED_ENUMERATOR(self, 0, nullptr, array_enum_size);
    for (auto &v : value->value->GetArray())
        rb_yield(eval(v, value->root_document));
    return self;
}

#each_index {|index| ... } ⇒ Enumerator | self

Yields:

Yield Parameters:

  • index (Integer)

Returns:

  • (Enumerator | self)


1348
1349
1350
1351
1352
1353
1354
1355
# File 'ext/usamin/usamin.cpp', line 1348

static VALUE w_array_each_index(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    RETURN_SIZED_ENUMERATOR(self, 0, nullptr, array_enum_size);
    for (rapidjson::SizeType i = 0; i < value->value->Size(); i++)
        rb_yield(UINT2NUM(i));
    return self;
}

#empty?Boolean

Returns:

  • (Boolean)


1357
1358
1359
1360
1361
# File 'ext/usamin/usamin.cpp', line 1357

static VALUE w_array_isempty(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    return value->value->Size() == 0 ? Qtrue : Qfalse;
}

#fetch(nth) ⇒ Object #fetch(nth, ifnone) ⇒ Object #fetch(nth) {|nth| ... } ⇒ Object

Overloads:

  • #fetch(nth) ⇒ Object

    Parameters:

    • nth (Integer)

    Returns:

    • (Object)

    Raises:

    • (IndexError)

      if nth is out of array bounds

  • #fetch(nth, ifnone) ⇒ Object

    Parameters:

    • nth (Integer)
    • ifnone (Object)

    Returns:

    • (Object)
  • #fetch(nth) {|nth| ... } ⇒ Object

    Parameters:

    • nth (Integer)

    Yields:

    • (nth)

    Returns:

    • (Object)


1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
# File 'ext/usamin/usamin.cpp', line 1379

static VALUE w_array_fetch(const int argc, const VALUE *argv, const VALUE self) {
    rb_check_arity(argc, 1, 2);
    UsaminValue *value = get_value(self);
    check_array(value);
    rapidjson::SizeType sz = value->value->Size();

    long l = FIX2LONG(argv[0]);
    if (l < 0)
        l += sz;
    if (0 <= l && l < sz)
        return eval((*value->value)[static_cast<rapidjson::SizeType>(l)], value->root_document);

    if (argc == 2)
        return argv[1];
    else if (rb_block_given_p())
        return rb_yield(argv[0]);
    else
        rb_raise(rb_eIndexError, "index %ld outside of array bounds: %ld...%u", FIX2LONG(argv[0]), -static_cast<long>(sz), sz);
    return Qnil;
}

#find_index(val) ⇒ Integer | nil #find_index {|item| ... } ⇒ Integer | nil

Overloads:

  • #find_index(val) ⇒ Integer | nil

    Parameters:

    • val (Object)

    Returns:

    • (Integer | nil)
  • #find_index {|item| ... } ⇒ Integer | nil

    Yields:

    • (item)

    Yield Parameters:

    • item (Object)

    Returns:

    • (Integer | nil)


1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
# File 'ext/usamin/usamin.cpp', line 1410

static VALUE w_array_find_index(const int argc, const VALUE *argv, const VALUE self) {
    rb_check_arity(argc, 0, 1);
    UsaminValue *value = get_value(self);
    check_array(value);

    if (argc == 1) {
        for (rapidjson::SizeType i = 0; i < value->value->Size(); i++) {
            if (rb_equal(argv[0], eval_r((*value->value)[i])) == Qtrue)
                return UINT2NUM(i);
        }
        return Qnil;
    }

    RETURN_SIZED_ENUMERATOR(self, 0, nullptr, array_enum_size);
    for (rapidjson::SizeType i = 0; i < value->value->Size(); i++) {
        if (RTEST(rb_yield(eval((*value->value)[i], value->root_document))))
            return UINT2NUM(i);
    }
    return Qnil;
}

#firstObject | nil #first(n) ⇒ ::Array<Object>

Overloads:

  • #firstObject | nil

    Returns:

    • (Object | nil)
  • #first(n) ⇒ ::Array<Object>

    Returns:

    • (::Array<Object>)


1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
# File 'ext/usamin/usamin.cpp', line 1485

static VALUE w_array_first(const int argc, const VALUE *argv, const VALUE self) {
    rb_check_arity(argc, 0, 1);
    UsaminValue *value = get_value(self);
    check_array(value);
    rapidjson::SizeType sz = value->value->Size();

    if (argc == 0) {
        if (sz == 0)
            return Qnil;
        return eval(*value->value->Begin(), value->root_document);
    } else {
        long l = FIX2LONG(argv[0]);
        if (l > sz)
            l = sz;
        VALUE ret = rb_ary_new2(l);
        for (auto v = value->value->Begin(); v < value->value->Begin() + l; v++)
            rb_ary_push(ret, eval(*v, value->root_document));
        return ret;
    }
}

#flatten(lv = nil) ⇒ ::Array<Object>

Returns:

  • (::Array<Object>)


1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
# File 'ext/usamin/usamin.cpp', line 1447

static VALUE w_array_flatten(const int argc, const VALUE *argv, const VALUE self) {
    rb_check_arity(argc, 0, 1);
    UsaminValue *value = get_value(self);
    check_array(value);

    int level = -1;
    if (argc == 1 && !NIL_P(argv[0])) {
        level = NUM2INT(argv[0]);
        if (level <= 0)
            return eval_array(*value->value, value->root_document);
    }

    VALUE ret = rb_ary_new2(value->value->Size());
    flatten_array(*value->value, ret, level, value->root_document);
    return ret;
}

#include?(val) ⇒ Boolean

Returns:

  • (Boolean)


1509
1510
1511
1512
1513
1514
1515
1516
# File 'ext/usamin/usamin.cpp', line 1509

static VALUE w_array_include(const VALUE self, const VALUE val) {
    UsaminValue *value = get_value(self);
    check_array(value);
    for (auto &v : value->value->GetArray())
        if (rb_equal(val, eval_r(v)))
            return Qtrue;
    return Qfalse;
}

#index(val) ⇒ Integer | nil #index {|item| ... } ⇒ Integer | nil

Overloads:

  • #index(val) ⇒ Integer | nil

    Parameters:

    • val (Object)

    Returns:

    • (Integer | nil)
  • #index {|item| ... } ⇒ Integer | nil

    Yields:

    • (item)

    Yield Parameters:

    • item (Object)

    Returns:

    • (Integer | nil)


1474
1475
1476
# File 'ext/usamin/usamin.cpp', line 1474

static VALUE w_array_index(const int argc, const VALUE *argv, const VALUE self) {
    return w_array_find_index(argc, argv, self);
}

#inspectString

Returns:

  • (String)


1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
# File 'ext/usamin/usamin.cpp', line 1521

static VALUE w_array_inspect(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    VALUE ret = rb_str_new2("[");
    bool first = true;
    for (auto &v : value->value->GetArray()) {
        if (!first)
            ret = rb_str_cat2(ret, ", ");
        switch (v.GetType()) {
            case rapidjson::kObjectType:
                ret = rb_str_cat2(ret, "{...}");
                break;
            case rapidjson::kArrayType:
                ret = rb_str_cat2(ret, "[...]");
                break;
            default:
                ret = rb_str_append(ret, rb_inspect(eval(v, value->root_document)));
                break;
        }
        first = false;
    }
    ret = rb_str_cat2(ret, "]");
    return ret;
}

#lastObject | nil #last(n) ⇒ ::Array<Object>

Overloads:

  • #lastObject | nil

    Returns:

    • (Object | nil)
  • #last(n) ⇒ ::Array<Object>

    Returns:

    • (::Array<Object>)


1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
# File 'ext/usamin/usamin.cpp', line 1553

static VALUE w_array_last(const int argc, const VALUE *argv, const VALUE self) {
    rb_check_arity(argc, 0, 1);
    UsaminValue *value = get_value(self);
    check_array(value);
    rapidjson::SizeType sz = value->value->Size();

    if (argc == 0) {
        return sz > 0 ? eval(*(value->value->End() - 1), value->root_document) : Qnil;
    } else {
        long l = FIX2LONG(argv[0]);
        if (l > sz)
            l = sz;
        VALUE ret = rb_ary_new2(l);
        for (auto v = value->value->End() - l; v < value->value->End(); v++)
            rb_ary_push(ret, eval(*v, value->root_document));
        return ret;
    }
}

#lengthInteger

Returns:

  • (Integer)


1575
1576
1577
1578
1579
# File 'ext/usamin/usamin.cpp', line 1575

static VALUE w_array_length(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    return UINT2NUM(value->value->Size());
}

#marshal_dumpString

Dumps data in JSON.

Returns:

  • (String)


740
741
742
743
744
745
746
747
# File 'ext/usamin/usamin.cpp', line 740

static VALUE w_value_marshal_dump(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_value(value);
    rapidjson::StringBuffer buf;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buf);
    write_value(writer, *value->value);
    return rb_str_new(buf.GetString(), buf.GetSize());
}

#marshal_load(source) ⇒ Object

Loads marshal data.



752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# File 'ext/usamin/usamin.cpp', line 752

static VALUE w_value_marshal_load(const VALUE self, const VALUE source) {
    Check_Type(source, T_STRING);
    RubynizedDocument *doc = new RubynizedDocument();
    rapidjson::ParseResult result = doc->Parse<rapidjson::kParseFullPrecisionFlag | rapidjson::kParseNanAndInfFlag>(RSTRING_PTR(source), RSTRING_LEN(source));
    if (!result) {
        delete doc;
        rb_raise(rb_eParserError, "%s Offset: %lu", GetParseError_En(result.Code()), result.Offset());
    }
    if (doc->IsObject() || doc->IsArray()) {
        UsaminValue *value = new UsaminValue(doc, true);
        set_value(self, value);
        value->root_document = self;
    } else {
        auto type = doc->GetType();
        delete doc;
        rb_raise(rb_eUsaminError, "Invalid Value Type for marshal_load: %d", type);
    }
    return Qnil;
}

#reverse::Array<Object>

Returns:

  • (::Array<Object>)


1584
1585
1586
1587
1588
1589
1590
1591
1592
# File 'ext/usamin/usamin.cpp', line 1584

static VALUE w_array_reverse(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);

    VALUE ret = rb_ary_new2(value->value->Size());
    for (rapidjson::SizeType i = 0, j = value->value->Size() - 1; i < value->value->Size(); i++, j--)
        rb_ary_push(ret, eval((*value->value)[j], value->root_document));
    return ret;
}

#rindex(val) ⇒ Integer | nil #rindex {|item| ... } ⇒ Integer | nil

Overloads:

  • #rindex(val) ⇒ Integer | nil

    Parameters:

    • val (Object)

    Returns:

    • (Integer | nil)
  • #rindex {|item| ... } ⇒ Integer | nil

    Yields:

    • (item)

    Yield Parameters:

    • item (Object)

    Returns:

    • (Integer | nil)


1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
# File 'ext/usamin/usamin.cpp', line 1604

static VALUE w_array_rindex(const int argc, const VALUE *argv, const VALUE self) {
    rb_check_arity(argc, 0, 1);
    UsaminValue *value = get_value(self);
    check_array(value);

    if (argc == 1) {
        for (rapidjson::SizeType i = 0, j = value->value->Size() - 1; i < value->value->Size(); i++, j--) {
            if (rb_equal(argv[0], eval_r((*value->value)[j])) == Qtrue)
                return UINT2NUM(j);
        }
        return Qnil;
    }

    RETURN_SIZED_ENUMERATOR(self, 0, nullptr, array_enum_size);
    for (rapidjson::SizeType i = 0, j = value->value->Size() - 1; i < value->value->Size(); i++, j--) {
        if (RTEST(rb_yield(eval((*value->value)[j], value->root_document))))
            return UINT2NUM(j);
    }
    return Qnil;
}

#rotate(cnt = 1) ⇒ ::Array<Object>

Returns:

  • (::Array<Object>)


1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
# File 'ext/usamin/usamin.cpp', line 1629

static VALUE w_array_rotate(const int argc, const VALUE *argv, const VALUE self) {
    rb_check_arity(argc, 0, 1);
    UsaminValue *value = get_value(self);
    check_array(value);

    switch (value->value->Size()) {
        case 0:
            return rb_ary_new();
        case 1:
            return rb_ary_new3(1, eval(value->value->operator[](0), value->root_document));
    }

    int cnt = argc == 1 ? NUM2INT(argv[0]) : 1;
    if (cnt >= 0) {
        cnt = cnt % value->value->Size();
    } else {
        cnt = -cnt % value->value->Size();
        if (cnt)
            cnt = value->value->Size() - cnt;
    }
    if (cnt == 0)
        return eval_array(*(value->value), value->root_document);

    rapidjson::SizeType ucnt = cnt;
    VALUE ret = rb_ary_new2(value->value->Size());
    for (rapidjson::SizeType i = ucnt; i < value->value->Size(); i++)
        rb_ary_push(ret, eval((*value->value)[i], value->root_document));
    for (rapidjson::SizeType i = 0; i < ucnt; i++)
        rb_ary_push(ret, eval((*value->value)[i], value->root_document));
    return ret;
}

#sizeInteger

Returns:

  • (Integer)


1575
1576
1577
1578
1579
# File 'ext/usamin/usamin.cpp', line 1575

static VALUE w_array_length(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    return UINT2NUM(value->value->Size());
}

#slice(nth) ⇒ Object | nil #slice(start, length) ⇒ ::Array<Object> | nil #slice(range) ⇒ ::Array<Object> | nil

Overloads:

  • #slice(nth) ⇒ Object | nil

    Parameters:

    • nth (Integer)

    Returns:

    • (Object | nil)
  • #slice(start, length) ⇒ ::Array<Object> | nil

    Parameters:

    • start (Integer)
    • length (Integer)

    Returns:

    • (::Array<Object> | nil)
  • #slice(range) ⇒ ::Array<Object> | nil

    Parameters:

    • range (Range)

    Returns:

    • (::Array<Object> | nil)


1675
1676
1677
# File 'ext/usamin/usamin.cpp', line 1675

static VALUE w_array_slice(const int argc, const VALUE *argv, const VALUE self) {
    return w_array_operator_indexer(argc, argv, self);
}

#to_ary::Array<Object>

Convert to Ruby data structures. Same as Value#eval.

Returns:

  • (::Array<Object>)


1684
1685
1686
1687
1688
# File 'ext/usamin/usamin.cpp', line 1684

static VALUE w_array_eval(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    return eval_array(*(value->value), value->root_document);
}

#to_sString

Returns:

  • (String)


1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
# File 'ext/usamin/usamin.cpp', line 1521

static VALUE w_array_inspect(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    VALUE ret = rb_str_new2("[");
    bool first = true;
    for (auto &v : value->value->GetArray()) {
        if (!first)
            ret = rb_str_cat2(ret, ", ");
        switch (v.GetType()) {
            case rapidjson::kObjectType:
                ret = rb_str_cat2(ret, "{...}");
                break;
            case rapidjson::kArrayType:
                ret = rb_str_cat2(ret, "[...]");
                break;
            default:
                ret = rb_str_append(ret, rb_inspect(eval(v, value->root_document)));
                break;
        }
        first = false;
    }
    ret = rb_str_cat2(ret, "]");
    return ret;
}