Wanted: [M] lists of [N] items, where [M] and [N] are unknown in advance (redim instead of dim).
edit: [N] is not a constant, but can be different for each list
Result:
Code: Select all
'Description: Dynamic list of dynamic lists of values
type int2d
dim as integer x, y
declare operator cast () as string
end type
operator int2d.cast () as string
return "(" & str(x) & "," & str(y) & ")"
end operator
#define data_type int2d
type list_type
public:
declare sub add(byval value as data_type)
declare function size() as integer
declare function get(index as integer) as data_type
declare sub print()
declare destructor()
private:
dim as data_type list(any) 'dynamic array, list of items
end type
'increase list size + add new value
sub list_type.add(byval value as data_type)
redim preserve list(ubound(list) + 1)
list(ubound(list)) = value
end sub
'get stack size
function list_type.size() as integer
return ubound(list) + 1
end function
function list_type.get(index as integer) as data_type
dim as data_type value
if index >= lbound(list) and index <= ubound(list) then
value = list(index)
end if
return value
end function
sub list_type.print()
for i as integer = lbound(list) to ubound(list)
..print str(i) & ": " & list(i)
next
end sub
destructor list_type
erase list
end destructor
type list_list_type
public:
declare sub incList()
declare function addItem(byval value as data_type) as integer
declare function size() as integer
declare function numItems(listIndex as integer) as integer
declare function getItem(listIndex as integer, itemIndex as integer) as data_type
declare sub print()
declare destructor()
private:
dim as list_type list(any) 'dynamic array, list of lists
end type
'increase list size / add a list
sub list_list_type.incList()
redim preserve list(ubound(list) + 1)
end sub
'add item to last list
function list_list_type.addItem(byval value as data_type) as integer
if size() = 0 then return -1
list(ubound(list)).add(value)
return 0
end function
'get num lists
function list_list_type.size() as integer
return ubound(list) + 1
end function
function list_list_type.numItems(listIndex as integer) as integer
return list(listIndex).size()
end function
function list_list_type.getItem(listIndex as integer, itemIndex as integer) as data_type
dim as data_type value
if listIndex >= lbound(list) and listIndex <= ubound(list) then
value = list(listIndex).get(itemIndex)
end if
return value
end function
sub list_list_type.print()
for i as integer = lbound(list) to ubound(list)
..print "List: " & str(i)
list(i).print()
next
end sub
destructor list_list_type
for i as integer = lbound(list) to ubound(list)
list(i).destructor
next
erase list
end destructor
#undef data_type
dim as list_list_type list
print "== Make list =="
print list.addItem(type<int2d>(1, 2)) 'will fail
list.incList()
print list.addItem(type<int2d>(2, 3))
print list.addItem(type<int2d>(4, 5))
list.incList()
print list.addItem(type<int2d>(5, 6))
print "== Print list =="
list.print()
print "== Loop list, item =="
for i as integer = 0 to list.size() - 1
print "List:"; i, "size ="; list.numItems(i)
for j as integer = 0 to list.numItems(i) - 1
print list.getItem(i, j)
next
next
print "== Kill list =="
list.destructor()
print "== End =="