Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QB64_GJ_LIB ARRay Library
#1
Hi.

I've added some handy stuff to my library for dealing with arrays:
https://github.com/grymmjack/QB64_GJ_LIB/tree/main/ARR

What is in the library? Read the README.md, but:

TYPES SUPPORTED:
  • _BYTE
  • _UNSIGNED _BYTE
  • INTEGER
  • _UNSIGNED INTEGER
  • _INTEGER64
  • _UNSIGNED _INTEGER64
  • LONG
  • _UNSIGNED LONG
  • SINGLE
  • DOUBLE
  • _FLOAT
  • STRING

Every numeric type contains the following SUBs/FUNCTIONs e.g. ARR_INT.slice for the slice SUB for INTEGER type.

SUBS AND FUNCTIONS FOR NUMERIC TYPES:
Code: (Select All)
.slice        Slice an array from source to destination starting at index and count slices
.push         Push a element onto the end of the array
.pop          Pop a element off the end of the array
.shift        Pop a element off the beginning of the array
.unshift      Push a element on the beginning of the array
.copy         Copy an array
.join         Return array contents as comma delimited string
.new          Create new array using comma delimited string
.longest      Return the longest element of an array
.shortest     Return the shortest element of an array
.math         Do math on every element of an array
.min          Return minimum element of an array
.max          Return maximum element of an array
.first        Return 1st element of an array
.last         Return last element of an array
.nth          Return every nth element of an array
.in           Determine if a value exists in an array
.find         Find a value in an array and return it's index
.count        Return the number of elements in an array
.size         Return the size in bytes of all elements in an array
.reverse      Reverse the index of elements in an array
.random       Return a random element from the array
.sum          Return the sum of all elements in an array
.avg          Return the average of all elements in an array
.shuffle      Randomize the indexes of all elements in an array
.unique       Return unique elements in an array
.gt           Return elements greater than (>) value in an array
.gte          Return elements greater than or equal (>=) value in an array
.lt           Return elements less than (<>=) value in an array
.lte          Return elements less than or equal (<>=) value in an array
.replace      Replace elements in array with replacement value
.insert       Insert element in an array at index
.remove       Remove element in an array at index
.odd          Return odd numbered indexed elements in an array
.even         Return even numbered indexed elements in an array
.mod          Return evenly divisible by n numbered indexed elements in an array
.between      Return elements between a start and end index in an array
.sort         Sort elements of an array in ascending order
.rsort        Sort elements of an array in desscending order

SUBS AND FUNCTIONS FOR STRING TYPE:
Code: (Select All)
.slice        Slice an array from source to destination starting at index and count slices
.push         Push a element onto the end of the array
.pop          Pop a element off the end of the array
.shift        Pop a element off the beginning of the array
.unshift      Push a element on the beginning of the array
.copy         Copy an array
.join         Return array contents as comma delimited string
.new          Create new array using comma delimited string
.longest      Return the longest element of an array
.shortest     Return the shortest element of an array
.first        Return 1st element of an array
.last         Return last element of an array
.nth          Return every nth element of an array
.in           Determine if a value exists in an array
.find         Find a value in an array and return it's index
.count        Return the number of elements in an array
.size         Return the size in bytes of all elements in an array
.reverse      Reverse the index of elements in an array
.random       Return a random element from the array
.shuffle      Randomize the indexes of all elements in an array
.unique       Return unique elements in an array
.replace      Replace elements in array with replacement value
.insert       Insert element in an array at index
.remove       Remove element in an array at index
.odd          Return odd numbered indexed elements in an array
.even         Return even numbered indexed elements in an array
.mod          Return evenly divisible by n numbered indexed elements in an array
.between      Return elements between a start and end index in an array
.sort         Sort elements of an array in ascending order
.rsort        Sort elements of an array in desscending order
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply
#2
You may be wondering why did I make this? I am sick of writing the same code over and over again, and thought a higher level library would keep me from going crazy.

All of these things are built into a lot of other languages I use; python, php, javascript.

So...

Instead of:

Code: (Select All)

    DIM AS LONG ub, lb, i, n
    lb& = LBOUND(source_arr$) : ub& = UBOUND(source_arr$)
    IF start_idx% < lb& OR start_idx% + count% > ub& THEN EXIT SUB ' out of range
    IF ub& - lb& < count% THEN EXIT SUB ' too many and not enough
    REDIM dest_arr(0 TO ABS(count%)) AS STRING
    IF SGN(count%) = -1 THEN
        IF ((start_idx% - 1) - ABS(count%)) < 0 THEN EXIT SUB ' out of range
        n& = 0
        FOR i& = (start_idx% - 1) TO ((start_idx% - 1) - ABS(count%)) STEP -1
            dest_arr$(n&) = source_arr$(i&)
            n& = n& + 1
        NEXT i&
    ELSE
        IF ((start_idx% + 1) + ABS(count%)) > (ub& - lb&) THEN EXIT SUB ' out of range
        n& = 0
        FOR i& = start_idx% + 1 TO ((start_idx% + 1) + count%) STEP 1
            dest_arr$(n&) = source_arr$(i&)
            n& = n& + 1
        NEXT i&
    END IF

I can:
Code: (Select All)

CALL ARR_STR.slice(s1$(), s2$(), 1, 3)

etc.
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply
#3
This is good stuff. Thanks, GJ!
Reply
#4
Thumbs Up 
Thank you for this. It will come in handy somewhere.

For my own use, though, I would change the names of a couple of "methods". Like "unshift" is not really a name I prefer for what it does. Also "asort" and "dsort" instead of "sort" and "rsort" although always have to type in one extra character. But that's just me.

For someone used to programming in Lua, and/or has a few Gawk scripts, the string "methods" called "join", "new" and "in" will make using arrays in BASIC less clunky.
Reply




Users browsing this thread: 1 Guest(s)