Code: Select all
#include "crt/stdlib.bi"
#include "string.bi"
'------------------------------- class: row_type -------------------------------
type row_type
dim as ushort col(any)
dim as string bla1, bla2 'some extra (passive) columns
declare operator cast () as string
end type
operator row_type.cast () as string
dim as string tempStr
for i as integer = 0 to ubound(col)
if i = 0 then tempStr &= col(i) else tempStr &= !"\t" & col(i)
next
return tempStr & !"\t" & bla1 & !"\t" & bla2
end operator
'------------------------------ class: sort_type -------------------------------
type sort_type
dim as short column
dim as short direction
declare constructor()
declare constructor(column as short, direction as short)
end type
'a stupid constructor
constructor sort_type()
this.column = 0
this.direction = 0
end constructor
'another stupid constructor
constructor sort_type(column as short, direction as short)
this.column = column
this.direction = direction
end constructor
'------------------------------ class: data_type -------------------------------
type data_type
static as sort_type sortOrder(0 to 2)
dim as integer numRows, numCols
dim as row_type row(any)
declare constructor(numRows as integer, numPivotMS as integer)
declare destructor()
declare sub initRandom()
declare sub printSome()
declare sub copyTo(dst as data_type)
declare sub sort(sort1st as sort_type, sort2nd as sort_type, sort3rd as sort_type)
declare static function qSortCallback cdecl(pRow1 as row_type ptr, pRow2 as row_type ptr) as long
end type
dim as sort_type data_type.sortOrder(0 to 2)
constructor data_type(numRows as integer, numCols as integer)
redim row(numRows - 1)
this.numCols = numCols
for iRow as integer = 0 to numRows - 1
redim (row(iRow).col)(numCols - 1) 'weird syntax, compiler wants the extra ( )
next
end constructor
destructor data_type()
for iRow as integer = 0 to numRows - 1
erase row(iRow).col
next
erase row
end destructor
sub data_type.initRandom()
for iRow as integer = 0 to ubound(row)
with row(iRow)
for iCol as integer = 0 to ubound(.col)
.col(iCol) = int(rnd * 1000)
next
.bla1 = string(int(rnd * 7) + 1, int(rnd * 26) + asc("a"))
.bla2 = string(int(rnd * 7) + 1, int(rnd * 26) + asc("A"))
end with
next
end sub
sub data_type.printSome()
print "--- First 5 items ---"
for i as integer = 0 to 4
print row(i)
next
print "--- Last 5 items ---"
for i as integer = ubound(row) - 4 to ubound(row)
print row(i)
next
end sub
sub data_type.copyTo(dst as data_type)
if ubound(row) <> ubound(dst.row) then print "Error": exit sub
for i as integer = 0 to ubound(row)
dst.row(i) = row(i)
next
end sub
sub data_type.sort(sort1st as sort_type, sort2nd as sort_type, sort3rd as sort_type)
'disable invalid sort filters
sortOrder(0) = iif(sort1st.column < 0 or sort1st.column >= numCols, sort_type(0,0), sort1st)
sortOrder(1) = iif(sort2nd.column < 0 or sort2nd.column >= numCols, sort_type(0,0), sort2nd)
sortOrder(2) = iif(sort3rd.column < 0 or sort3rd.column >= numCols, sort_type(0,0), sort3rd)
qsort(@row(0), ubound(row) + 1, sizeof(row_type), cptr(any ptr, @qSortCallback))
end sub
function data_type.qSortCallback cdecl(pRow1 as row_type ptr, pRow2 as row_type ptr) as long
for i as integer = 0 to 2
with sortOrder(i)
select case .direction
case +1
if pRow1->col(.column) < pRow2->col(.column) then return -1
if pRow1->col(.column) > pRow2->col(.column) then return +1
case -1
if pRow1->col(.column) > pRow2->col(.column) then return -1
if pRow1->col(.column) < pRow2->col(.column) then return +1
case else
'skip, including direction = 0
end select
end with
next
return 0
end function
'-------------------------------- main program ---------------------------------
dim as integer numRows = 2000, numCols = 5
randomize timer
print "Allocate memory"
var myData = data_type(numRows, numCols), backupData = data_type(numRows, numCols)
print "Initialize with random data"
myData.initRandom()
print "Make backup"
myData.copyTo(backupData)
print "Unsorted data:"
myData.printSome()
print !"\nSort: col 0 up, col 2 down:"
myData.sort(sort_type(0, +1), sort_type(2, -1), sort_type(0, 0))
myData.printSome()
backupData.copyTo(myData)
print !"\nSort: col 1 up (only)"
myData.sort(sort_type(0, +1), sort_type(0, 0), sort_type(0, 0))
myData.printSome()
print !"\nPress any key to end"
sleep
print "End"