summaryrefslogtreecommitdiff
path: root/js/src/Ice/ArrayUtil.js
blob: e9111adbd30d093af92e748d83b2d3c12a03be7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// **********************************************************************
//
// Copyright (c) 2003-present ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

const Ice = require("../Ice/ModuleRegistry").Ice;
const _ModuleRegistry = Ice._ModuleRegistry;
const Slice = Ice.Slice;

const eq = function(e1, e2)
{
    if(e1 === e2)
    {
        return true; // If identity compare equals members are equal.
    }
    else if(e1 === null || e1 === undefined || e2 === null || e2 === undefined)
    {
        return false;
    }
    else if(e1.prototype !== e2.prototype)
    {
        return false;
    }
    else if(typeof e1.equals == "function")
    {
        return e1.equals(e2);
    }
    else if(e1 instanceof Array || e1 instanceof Uint8Array)
    {
        return ArrayUtil.equals(e1, e2, eq);
    }
    return false;
};

class ArrayUtil
{
    static clone(arr)
    {
        if(arr === undefined)
        {
            return arr;
        }
        else if(arr === null)
        {
            return [];
        }
        else
        {
            return arr.slice();
        }
    }

    static equals(v1, v2, valuesEqual)
    {
        if(v1.length != v2.length)
        {
            return false;
        }

        const equalFn = valuesEqual || eq;
        for(let i = 0; i < v1.length; ++i)
        {
            if(!equalFn.call(equalFn, v1[i], v2[i]))
            {
                return false;
            }
        }

        return true;
    }

    static shuffle(arr)
    {
        for(let i = arr.length; i > 1; --i)
        {
            const e = arr[i - 1];
            const rand = Math.floor(Math.random() * i);
            arr[i - 1] = arr[rand];
            arr[rand] = e;
        }
    }
}

ArrayUtil.eq = eq;

Slice.defineSequence = function(module, name, valueHelper, fixed, elementType)
{
    let helper = null;
    Object.defineProperty(module, name,
        {
            get: () =>
                {
                    if(helper === null)
                    {
                        helper = Ice.StreamHelpers.generateSeqHelper(_ModuleRegistry.type(valueHelper),
                                                                     fixed,
                                                                     _ModuleRegistry.type(elementType));
                    }
                    return helper;
                }
        });
};

Ice.ArrayUtil = ArrayUtil;
module.exports.Ice = Ice;