Class: WirisPlugin::Arrays

Inherits:
Object
  • Object
show all
Includes:
Wiris
Defined in:
lib/com/wiris/util/type/Arrays.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArrays

Returns a new instance of Arrays.



6
7
8
# File 'lib/com/wiris/util/type/Arrays.rb', line 6

def initialize()
    super()
end

Class Method Details

.addAll(baseArray, additionArray) ⇒ Object



161
162
163
164
165
166
# File 'lib/com/wiris/util/type/Arrays.rb', line 161

def self.addAll(baseArray, additionArray)
    i = additionArray::iterator()
    while i::hasNext()
        baseArray::push(i::next())
    end
end

.arrayUnion(baseArray, unionArray) ⇒ Object



248
249
250
251
252
253
254
255
256
# File 'lib/com/wiris/util/type/Arrays.rb', line 248

def self.arrayUnion(baseArray, unionArray)
    it = unionArray::iterator()
    while it::hasNext()
        n = it::next()
        if !baseArray::contains_(n)
            baseArray::push(n)
        end
    end
end

.binarySearch(array, key) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/com/wiris/util/type/Arrays.rb', line 135

def self.binarySearch(array, key)
    imin = 0
    imax = array::length()
    while imin < imax
        imid = ((imin + imax)/2)
        cmp = Reflect::compare(array::_(imid),key)
        if cmp == 0
            return imid
        else 
            if cmp < 0
                imin = imid + 1
            else 
                imax = imid
            end
        end
    end
    return -1
end

.clear(a) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/com/wiris/util/type/Arrays.rb', line 96

def self.clear(a)
    i = a::length() - 1
    while i >= 0
        a::remove(a::_(i))
        i-=1
    end
end

.contains(array, element) ⇒ Object



69
70
71
# File 'lib/com/wiris/util/type/Arrays.rb', line 69

def self.contains(array, element)
    return Arrays.indexOfElement(array,element) >= 0
end

.containsArray(array, element) ⇒ Object



90
91
92
# File 'lib/com/wiris/util/type/Arrays.rb', line 90

def self.containsArray(array, element)
    return Arrays.indexOfElementArray(array,element) >= 0
end

.containsInt(array, element) ⇒ Object



93
94
95
# File 'lib/com/wiris/util/type/Arrays.rb', line 93

def self.containsInt(array, element)
    return Arrays.indexOfElementInt(array,element) >= 0
end

.copyArray(a) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/com/wiris/util/type/Arrays.rb', line 153

def self.copyArray(a)
    b = Array.new()
    i = a::iterator()
    while i::hasNext()
        b::push(i::next())
    end
    return b
end

.difference(a, b) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/com/wiris/util/type/Arrays.rb', line 230

def self.difference(a, b)
    v = Array.new()
    if a == nil
        return v
    else 
        if b == nil
            return Arrays.copyArray(a)
        end
    end
    it = a::iterator()
    while it::hasNext()
        e = it::next()
        if Arrays.indexOfElement(b,e) < 0
            v::push(e)
        end
    end
    return v
end

.equalAsSets(a, b) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/com/wiris/util/type/Arrays.rb', line 257

def self.equalAsSets(a, b)
    if (a == nil) || (b == nil)
        return a == b
    end
    if a::length() == b::length()
        it = b::iterator()
        while it::hasNext()
            t = it::next()
            if !a::contains_(t)
                return false
            end
        end
        return true
    end
    return false
end

.firstElement(elements) ⇒ Object



196
197
198
# File 'lib/com/wiris/util/type/Arrays.rb', line 196

def self.firstElement(elements)
    return elements::_(0)
end

.fromCSV(s) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/com/wiris/util/type/Arrays.rb', line 46

def self.fromCSV(s)
    words = Std::split(s,",")
    i = 0
    while i < words::length()
        w = words::_(i)::trim()
        if w::length() > 0
            words::_(i,w)
            i+=1
        else 
            words::splice(i,1)
        end
    end
    return words
end

.fromIterator(iterator) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/com/wiris/util/type/Arrays.rb', line 39

def self.fromIterator(iterator)
    array = Array.new()
    while iterator::hasNext()
        array::push(iterator::next())
    end
    return array
end

.getOrDefault(array, index, defaultValue) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/com/wiris/util/type/Arrays.rb', line 21

def self.getOrDefault(array, index, defaultValue)
    if ((array != nil) && (index >= 0)) && (index < array::length)
        return array[index]
    else 
        return defaultValue
    end
end

.indexOfElement(array, element) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/com/wiris/util/type/Arrays.rb', line 28

def self.indexOfElement(array, element)
    i = 0
    n = array::length()
    while i < n
        if (array::_(i) != nil) && ((array::_(i) == element))
            return i
        end
        i+=1
    end
    return -1
end

.indexOfElementArray(array, element) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/com/wiris/util/type/Arrays.rb', line 72

def self.indexOfElementArray(array, element)
    for i in 0..array::length - 1
        if (array[i] != nil) && (array[i] == element)
            return i
        end
        i+=1
    end
    return -1
end

.indexOfElementInt(array, element) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/com/wiris/util/type/Arrays.rb', line 81

def self.indexOfElementInt(array, element)
    for i in 0..array::length - 1
        if array[i] == element
            return i
        end
        i+=1
    end
    return -1
end

.insertSorted(a, e) ⇒ Object



106
107
108
# File 'lib/com/wiris/util/type/Arrays.rb', line 106

def self.insertSorted(a, e)
    Arrays.insertSortedImpl(a,e,false)
end

.insertSortedImpl(a, e, set) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/com/wiris/util/type/Arrays.rb', line 112

def self.insertSortedImpl(a, e, set)
    imin = 0
    imax = a::length()
    while imin < imax
        imid = ((imax + imin)/2)
        cmp = Reflect::compare(a::_(imid),e)
        if cmp == 0
            if set
                return 
            else 
                imin = imid
                imax = imid
            end
        else 
            if cmp < 0
                imin = imid + 1
            else 
                imax = imid
            end
        end
    end
    a::insert(imin,e)
end

.insertSortedSet(a, e) ⇒ Object



109
110
111
# File 'lib/com/wiris/util/type/Arrays.rb', line 109

def self.insertSortedSet(a, e)
    Arrays.insertSortedImpl(a,e,true)
end

.intersectSorted(a, b) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/com/wiris/util/type/Arrays.rb', line 202

def self.intersectSorted(a, b)
    if a == nil
        return b == nil ? nil : Arrays.copyArray(b)
    else 
        if b == nil
            return Arrays.copyArray(a)
        else 
            v = Array.new()
            i = 0
            j = 0
            while (i < a::length()) && (j < b::length())
                cmp = Reflect::compare(a::_(i),b::_(j))
                if cmp == 0
                    v::push(a::_(i))
                    i+=1
                    j+=1
                else 
                    if cmp < 0
                        i+=1
                    else 
                        j+=1
                    end
                end
            end
            return v
        end
    end
end

.isNotEmpty(array) ⇒ Object



18
19
20
# File 'lib/com/wiris/util/type/Arrays.rb', line 18

def self.isNotEmpty(array)
    return (array != nil) && (array::length > 0)
end

.lastElement(elements) ⇒ Object



199
200
201
# File 'lib/com/wiris/util/type/Arrays.rb', line 199

def self.lastElement(elements)
    return elements::_(elements::length() - 1)
end

.newIntArray(length, initValue) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/com/wiris/util/type/Arrays.rb', line 9

def self.newIntArray(length, initValue)
    data = []
    length-=1
    while length >= 0
        data[length] = initValue
        length-=1
    end
    return data
end

.partition(elements, lower, higher, comparator) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/com/wiris/util/type/Arrays.rb', line 174

def self.partition(elements, lower, higher, comparator)
    pivot = elements::_(higher)
    i = lower - 1
    j = lower
    while j < higher
        if comparator::compare(pivot,elements::_(j)) > 0
            i+=1
            if i != j
                swapper = elements::_(i)
                elements::_(i,elements::_(j))
                elements::_(j,swapper)
            end
        end
        j+=1
    end
    if comparator::compare(elements::_(i + 1),elements::_(higher)) > 0
        finalSwap = elements::_(i + 1)
        elements::_(i + 1,elements::_(higher))
        elements::_(higher,finalSwap)
    end
    return i + 1
end

.quicksort(elements, lower, higher, comparator) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/com/wiris/util/type/Arrays.rb', line 167

def self.quicksort(elements, lower, higher, comparator)
    if lower < higher
        p = Arrays::partition(elements,lower,higher,comparator)
        Arrays::quicksort(elements,lower,p - 1,comparator)
        Arrays::quicksort(elements,p + 1,higher,comparator)
    end
end

.sort(elements, comparator) ⇒ Object



103
104
105
# File 'lib/com/wiris/util/type/Arrays.rb', line 103

def self.sort(elements, comparator)
    Arrays::quicksort(elements,0,elements::length() - 1,comparator)
end

.toIntArray(array) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/com/wiris/util/type/Arrays.rb', line 60

def self.toIntArray(array)
    result = ArrayInt.new()
    it = array::iterator()
    while it::hasNext()
        value = Std::parseInt(it::next())
        result::push(value)
    end
    return result
end