' Sparse Array — parallel index/value arrays for sparse data
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    DIM idxs(1 TO 100) AS LONG
    DIM vals(1 TO 100) AS DOUBLE
    LOCAL count AS LONG
    LOCAL maxIdx AS LONG
    LOCAL found AS LONG
    LOCAL val AS DOUBLE
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL k AS LONG

    PRINT "======================="
    PRINT " Sparse Array Demo"
    PRINT "======================="
    PRINT

    maxIdx = 20
    count = 0

    PRINT "=== SPARSE ARRAY ==="
    j = 10
    found = 0
    FOR i = 1 TO count
        IF idxs(i) = j THEN
            vals(i) = 3.14
            found = 1
            EXIT FOR
        END IF
    NEXT i
    IF found = 0 AND count < maxIdx THEN
        count = count + 1
        idxs(count) = j
        vals(count) = 3.14
    END IF

    j = 100
    found = 0
    FOR i = 1 TO count
        IF idxs(i) = j THEN
            vals(i) = 2.718
            found = 1
            EXIT FOR
        END IF
    NEXT i
    IF found = 0 AND count < maxIdx THEN
        count = count + 1
        idxs(count) = j
        vals(count) = 2.718
    END IF

    j = 50
    found = 0
    FOR i = 1 TO count
        IF idxs(i) = j THEN
            vals(i) = 1.618
            found = 1
            EXIT FOR
        END IF
    NEXT i
    IF found = 0 AND count < maxIdx THEN
        count = count + 1
        idxs(count) = j
        vals(count) = 1.618
    END IF

    j = 1000
    found = 0
    FOR i = 1 TO count
        IF idxs(i) = j THEN
            vals(i) = 0.577
            found = 1
            EXIT FOR
        END IF
    NEXT i
    IF found = 0 AND count < maxIdx THEN
        count = count + 1
        idxs(count) = j
        vals(count) = 0.577
    END IF

    j = 10
    found = 0
    FOR i = 1 TO count
        IF idxs(i) = j THEN
            vals(i) = 6.283
            found = 1
            EXIT FOR
        END IF
    NEXT i
    IF found = 0 AND count < maxIdx THEN
        count = count + 1
        idxs(count) = j
        vals(count) = 6.283
    END IF

    PRINT "Sparse array (" & STR$(count) & " non-zero entries):"
    FOR i = 1 TO count
        PRINT "  [" & STR$(idxs(i)) & "] = " & STR$(vals(i))
    NEXT i
    PRINT

    val = 0.0
    found = 0
    j = 50
    FOR i = 1 TO count
        IF idxs(i) = j THEN
            found = 1
            val = vals(i)
            EXIT FOR
        END IF
    NEXT i
    IF found THEN PRINT "Get [50] = " & STR$(val) ELSE PRINT "[50] not found"

    val = 0.0
    found = 0
    j = 99
    FOR i = 1 TO count
        IF idxs(i) = j THEN
            found = 1
            val = vals(i)
            EXIT FOR
        END IF
    NEXT i
    IF found THEN PRINT "Get [99] = " & STR$(val) ELSE PRINT "[99] not found (correct)"
    PRINT

    j = 100
    FOR i = 1 TO count
        IF idxs(i) = j THEN
            FOR k = i TO count - 1
                idxs(k) = idxs(k + 1)
                vals(k) = vals(k + 1)
            NEXT k
            count = count - 1
            EXIT FOR
        END IF
    NEXT i
    PRINT "After removing [100]:"
    PRINT "Sparse array (" & STR$(count) & " non-zero entries):"
    FOR i = 1 TO count
        PRINT "  [" & STR$(idxs(i)) & "] = " & STR$(vals(i))
    NEXT i
    PRINT
    PRINT

    count = 0
    maxIdx = 100
    PRINT "=== LARGE SPARSE DEMO ==="
    FOR i = 1 TO 20
        j = i * 100
        IF count < maxIdx THEN
            count = count + 1
            idxs(count) = j
            vals(count) = i * 10.5
        END IF
    NEXT i
    PRINT "Sparse array (" & STR$(count) & " non-zero entries):"
    FOR i = 1 TO count
        PRINT "  [" & STR$(idxs(i)) & "] = " & STR$(vals(i))
    NEXT i
    PRINT

    val = 0.0
    found = 0
    j = 500
    FOR i = 1 TO count
        IF idxs(i) = j THEN
            found = 1
            val = vals(i)
            EXIT FOR
        END IF
    NEXT i
    IF found THEN PRINT "  Found: " & STR$(val) ELSE PRINT "  Not stored (correct - it's sparse)"
    PRINT

    PRINT "Sparse array demos complete."
    FUNCTION = 0
END FUNCTION
