LoginLogin
Might make SBS readonly: thread

ListIt

Root / Submissions / [.]

MZ952Created:
Version:Size:
A simple data structure for storing lists of integer arrays. Similarly to how string arrays store an "array" of chars at each of their elements, this structure stores integer arrays at each element of a list. It basically takes advantage of this behavior of string arrays. Run the file to see the demo. The demo just draws a bunch of crap on the screen, compresses the image data into 16x16 blocks and stows them into the list structure, and then reads the blocks from the list structure and draws them back onto the screen. A notable disadvantage of using this method (i.e., storing ints in double chars) is that for large arrays of integer data, computational time for fetching and stowing grows (about linearly, I think), eventually exceeding just plain old integer array manipulation. An upside, however, is that fetching time for any list element is about O(1), which means it takes bout the same time to get an array at list position 0 as it does list position 9999. I think what dampens it is SB's own internal activities as it goes about reading data from far or near elements. I have a second version of this program that stores either integers or floats in a tree-like data structure, I'll probably put that up soon. That structure uses pointers to get an O(n) retrieval time, where n is the number of nodes to navigate in the tree. This version to my surprise exceeds that version in computational time for average-sized blocks of data. I mean, this version has to iterate over every single stored char and convert them back into integers. I thought for sure that simply moving pointers around and inserting and removing arrays would be faster (and it is for larger arrays), but huh, its not. What might make that difference for smaller cases of array sizes is the lack of needing to move pointers around when data is being manipulated. Because, like, the string arrays already have that bit of organization inherently as one accesses different elements of the string array. For example, LST$[] contains say 4 elements, each element an array of chars. LST$[2] contains an array of chars that may be converted to ints. The positional organization is there in the bracket notation.

Instructions:

'--------------------------------------
'|DIMLST(Number_of_lists, List_data[])|
'-----------------------------------------
'|Description: returns a list of length  |
'|“number_of_lists”, filled with the     |
'|integer array value(s) of “list_data[]”|
'------------------------------------------
'|“Number_of_lists”                       |
'|-number of lists to initialize.         |
'|-lists will be filled with “list_data[]”|
'------------------------------------------
'|“List_data[]”                  |
'|-integer array containg data to|
'| initialize the list with.     |
'---------------------------------
DEF DIMLST(LST_N,DAT[])
 IF LST_N<0 THEN DIM TMP$[0]:RETURN TMP$ ENDIF
 DIM LST$[LST_N+1],I_D,LST_D$
 FOR I_D=0 TO LEN(DAT)-1
  INC LST_D$,CHR$((DAT[I_D] AND -65536)>>16)+CHR$(DAT[I_D] AND 65535)
 NEXT I_D:FILL LST$,LST_D$
 LST$[0]=CHR$((LST_N AND -65536)>>16)+CHR$(LST_N AND 65535)
 RETURN LST$
END


'------------------------------------
'|GETLST(List_array$[], List_number)|
'----------------------------------------
'|Description: returns the integer array|
'|stored in the list “list_array$[]” at |
'|the position “list_number”.           |
'----------------------------------------
'|“List_array$[]”                     |
'|-string array containing the integer|
'| array data to read.                |
'--------------------------------------
'|“List_number”                      |
'|-position in the list from where to|
'| get the integer array data.       |
'|-range from 0 to List_len-1.       |
'-------------------------------------
DEF GETLST(LST$[],LST_N)
 IF 1+LST_N>LEN(LST$)-1 || LST_N<0 THEN RETURN ENDIF
 IF !LEN(LST$) || LEN(LST$[0])<2 THEN RETURN ENDIF
 DIM DAT[0],I_D,I_D_L=LEN(LST$[1+LST_N])
 FOR I_D=0 TO I_D_L-1 STEP 2
  PUSH DAT,ASC(LST$[1+LST_N][I_D])<<16 OR ASC(LST$[1+LST_N][I_D+1])
 NEXT I_D:RETURN DAT
END


'------------------------------------
'|SETLST List_array$[], List_number,|
'|       Data_array[]               |
'-------------------------------------
'|Description: sets the integer array|
'|contents of the list at position   |
'|“list_number” to the contents of   |
'|“data_array[]”.                    |
'--------------------------------------
'|“List_array$[]”                     |
'|-string array containing the integer|
'| array data to manipulate.          |
'--------------------------------------
'|“List_number”               |
'|-position in the list to set|
'| the integer array data.    |
'|-range from 0 to List_len-1.|
'------------------------------
'|“Data_array[]”           |
'|-integer array containing|
'| the data to set.        |
'---------------------------
DEF SETLST LST$[],LST_N,DAT[]
 IF 1+LST_N>LEN(LST$)-1 || LST_N<0 THEN RETURN ENDIF
 IF !LEN(LST$) || LEN(LST$[0])<2 THEN RETURN ENDIF
 DIM LST_D$[0]:LST_D$=DIMLST(1,DAT):LST$[1+LST_N]=LST_D$[1]
END


'------------------------------------
'|INSLST List_array$[], List_number,|
'|       Data_array[]               |
'-------------------------------------------
'|Description: inserts the integer array   |
'|“data_array[]” into the list “list_array”|
'|at position “list_number”.               |
'-------------------------------------------
'|“List_array$[]”                     |
'|-string array containing the integer|
'| array data to manipulate.          |
'--------------------------------------
'|“List_number”                  |
'|-position in the list to insert|
'| the integer array data.       |
'|-range from 0 to List_len.     |
'---------------------------------
'|“Data_array[]”           |
'|-integer array containing|
'| the data to insert.     |
'---------------------------
DEF INSLST LST$[],LST_N,DAT[]
 IF !LEN(LST$) || LEN(LST$[0])<2 THEN RETURN ENDIF
 DIM LST_L=ASC(LST$[0][0])<<16 OR ASC(LST$[0][1])
 IF LST_N>LST_L || LST_N<0 THEN RETURN ENDIF
 DIM LST_D$[0]:LST_D$=DIMLST(1,DAT)
 INS LST$,1+LST_N,LST_D$[1]:INC LST_L
 LST$[0]=CHR$((LST_L AND -65536)>>16)+CHR$(LST_L AND 65535)
END


'------------------------------------
'|DELLST List_array$[], List_number,|
'|       Number_of_lists            |
'------------------------------------------
'|Description: deletes lists from the list|
'|“list_array$[]”, starting from position |
'|“list_number” and ending at position    |
'|“list_number”+“number_of_lists”-1.      |
'------------------------------------------
'|“List_array$[]”                     |
'|-string array containing the integer|
'| array data to manipulate.          |
'--------------------------------------
'|“List_number”                 |
'|-position in the list to begin|
'| deleting lists.              |
'|-range from 0 to List_len-1.  |
'------------------------------------------
'|“Number_of_lists”                       |
'|-Number of lists to be deleted, starting|
'| at position “list_number”.             |
'------------------------------------------
DEF DELLST LST$[],LST_N,NUMOF_LST
 IF !LEN(LST$) || LEN(LST$[0])<2 THEN RETURN ENDIF'not a lst
 DIM LST_L=ASC(LST$[0][0])<<16 OR ASC(LST$[0][1])
 IF LST_N>LST_L-1 || LST_N<0 THEN RETURN ENDIF
 RMV LST$,1+LST_N,MIN(NUMOF_LST,LEN(LST$)-LST_N)
 DEC LST_L:LST$[0]=CHR$((LST_L AND -65536)>>16)+CHR$(LST_L AND 65535)
END


'----------------------
'|LENLST List_array$[]|
'-------------------------------------
'|Description: returns length of list|
'-------------------------------------
'|“List_array$[]”        |
'|-list to get length of.|
'-------------------------
DEF LENLST(LST$[])
 IF !LEN(LST$) || LEN(LST$[0])<2 THEN RETURN 0 ENDIF
 RETURN ASC(LST$[0][0])<<16 OR ASC(LST$[0][1])
END


'supplemental (& required) functions
'---------------------------------
DEF INS A[],P,V UNSHIFT A,V:COPY A,A,1,P:A[P]=V END
DEF RMV A[],P,_N
 DIM N=_N:COPY A,P,A,P+N,LEN(A)-P-N
 WHILE N IF POP(A) THEN ENDIF:DEC N WEND
END
'---------------------------------------------

No posts yet (will you be the first?)