class WirisPlugin::Arrays

Public Class Methods

addAll(baseArray, additionArray) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/type/Arrays.rb, line 93
def self.containsInt(array, element)
    return Arrays.indexOfElementInt(array,element) >= 0
end
copyArray(a) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/type/Arrays.rb, line 196
def self.firstElement(elements)
    return elements::_(0)
end
fromCSV(s) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/type/Arrays.rb, line 109
def self.insertSortedSet(a, e)
    Arrays.insertSortedImpl(a,e,true)
end
intersectSorted(a, b) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/type/Arrays.rb, line 18
def self.isNotEmpty(array)
    return (array != nil) && (array::length > 0)
end
lastElement(elements) click to toggle source
# File lib/com/wiris/util/type/Arrays.rb, line 199
def self.lastElement(elements)
    return elements::_(elements::length() - 1)
end
new() click to toggle source
Calls superclass method
# File lib/com/wiris/util/type/Arrays.rb, line 6
def initialize()
    super()
end
newIntArray(length, initValue) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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