Dynamic list of lists

General FreeBASIC programming questions.
Post Reply
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Dynamic list of lists

Post by badidea »

A continuation of https://freebasic.net/forum/viewtopic.p ... 89#p254689 which was getting a bit off-topic...

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 =="
It works, but I have the feeling that I made it too complicated (again). Any suggestions for simplification?
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Dynamic list of lists

Post by badidea »

A different version, but something goes wrong and a don't see why. It should print (2, 3), i get (0, 49):

Code: Select all

'Description: Dynamic list of dynamic lists of values
'badidea / 2018-11-23

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 dyn2d_list_type
	public:
	declare sub inc()
	declare function size() as integer
	declare function addItem(byval value as data_type) as integer
	declare function numItems(listIndex as integer) as integer
	declare function getItem(listIndex as integer, itemIndex as integer) as data_type
	declare sub show()
	declare destructor()
	private:
	dim as data_type ptr pList(any) 'array of pointers to lists
	dim as integer nItems(any) 'array list sizes
end type

'increase list size / add a list
sub dyn2d_list_type.inc()
	dim as integer listSize = size()
	redim preserve nItems(listSize)
	nItems(listSize) = 0
	redim preserve pList(listSize)
	pList(listSize) = 0
end sub

'get num lists
function dyn2d_list_type.size() as integer
	return ubound(pList) + 1
end function

'add item to last list
function dyn2d_list_type.addItem(byval value as data_type) as integer
	dim as integer ubList = ubound(pList)
	if ubList < 0 then
		return -1 'list not existing
	else
		pList(ubList) = reallocate(pList(ubList), (nItems(ubList) + 1) * sizeof(data_type))
		pList(ubList)[nItems(ubList)] = value
		nItems(ubList) += 1
		return 0
	end if
end function

'get number of items in a list 
function dyn2d_list_type.numItems(listIndex as integer) as integer
	if listIndex > ubound(nItems) then 'or ubound(pList)
		return -1
	else
		return nItems(listIndex)
	end if
end function

function dyn2d_list_type.getItem(listIndex as integer, itemIndex as integer) as data_type
	dim as data_type value
	if listIndex <= ubound(pList) then
		if itemIndex <= nItems(listIndex) then
			value = pList(listIndex)[nItems(listIndex)]
		end if
	end if
	return value 
end function

sub dyn2d_list_type.show()
	print "pList size: " & str(ubound(plist) + 1) & ", nItems size: " & str(ubound(nItems) + 1)
	for i as integer = 0 to ubound(plist)
		print "- List: " & str(i) & ", size: " & nItems(i)
		for j as integer = 0 to nItems(i) - 1
			print "  - Value: " & pList(i)[nItems(j)]
		next
	next
end sub

destructor dyn2d_list_type
	for i as integer = 0 to ubound(plist)
		deallocate(pList(i))
	next
	erase pList
	erase nItems
end destructor

#undef data_type

dim as dyn2d_list_type list

print !"\n== Make list ==\n"
'print list.addItem(type<int2d>(1, 2)) 'will fail as it should
list.inc()
print list.addItem(type<int2d>(2, 3))
'print list.addItem(type<int2d>(3, 4))
'list.inc()
'print list.addItem(type<int2d>(4, 5))
'print list.addItem(type<int2d>(5, 6))
'print list.addItem(type<int2d>(6, 7))
'print list.addItem(type<int2d>(7, 8))
print !"\n== Print list ==\n"
list.show()
'~ 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 !"\n== Kill list ==\n"
list.destructor()
print !"\n== End ==\n"
Also, can this be done with 'dim/redim' instead of 'allocate/reallocate'?
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dynamic list of lists

Post by fxm »

Code: Select all

sub dyn2d_list_type.show()
   print "pList size: " & str(ubound(plist) + 1) & ", nItems size: " & str(ubound(nItems) + 1)
   for i as integer = 0 to ubound(plist)
      print "- List: " & str(i) & ", size: " & nItems(i)
      for j as integer = 0 to nItems(i) - 1
         print "  - Value: " & pList(i)[nItems(j) - 1]
      next
   next
end sub
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Dynamic list of lists

Post by badidea »

fxm wrote:

Code: Select all

sub dyn2d_list_type.show()
   print "pList size: " & str(ubound(plist) + 1) & ", nItems size: " & str(ubound(nItems) + 1)
   for i as integer = 0 to ubound(plist)
      print "- List: " & str(i) & ", size: " & nItems(i)
      for j as integer = 0 to nItems(i) - 1
         print "  - Value: " & pList(i)[nItems(j) - 1]
      next
   next
end sub
Thanks, I was looking at the wrong place, it should be however:

Code: Select all

sub dyn2d_list_type.show()
   print "pList size: " & str(ubound(plist) + 1) & ", nItems size: " & str(ubound(nItems) + 1)
   for i as integer = 0 to ubound(plist)
      print "- List: " & str(i) & ", size: " & nItems(i)
      for j as integer = 0 to nItems(i) - 1
         print "  - Value: " & pList(i)[j]
      next
   next
end sub
I see that I have another bug with "print list.getItem(0, 0)"...
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dynamic list of lists

Post by fxm »

Yes (my recovery is difficult!)
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Dynamic list of lists

Post by badidea »

fxm wrote:Yes (my recovery is difficult!)
I thought health recovery for a moment, but it is your movement internet recovery, I hope.

Working now:

Code: Select all

'Description: Dynamic list of dynamic lists of values
'badidea / 2018-11-23

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 dyn2d_list_type
	public:
	declare sub inc()
	declare function size() as integer
	declare function addItem(byval value as data_type) as integer
	declare function numItems(listIndex as integer) as integer
	declare function getItem(listIndex as integer, itemIndex as integer) as data_type
	declare sub show()
	declare destructor()
	private:
	dim as data_type ptr pList(any) 'array of pointers to lists
	dim as integer nItems(any) 'array list sizes
end type

'increase list size / add a list
sub dyn2d_list_type.inc()
	dim as integer listSize = size()
	redim preserve nItems(listSize)
	nItems(listSize) = 0
	redim preserve pList(listSize)
	pList(listSize) = 0
end sub

'get num lists
function dyn2d_list_type.size() as integer
	return ubound(pList) + 1
end function

'add item to last list
function dyn2d_list_type.addItem(byval value as data_type) as integer
	dim as integer ubList = ubound(pList)
	if ubList < 0 then
		return -1 'list not existing
	else
		pList(ubList) = reallocate(pList(ubList), (nItems(ubList) + 1) * sizeof(data_type))
		pList(ubList)[nItems(ubList)] = value
		nItems(ubList) += 1
		return 0
	end if
end function

'get number of items in a list 
function dyn2d_list_type.numItems(listIndex as integer) as integer
	if listIndex > ubound(nItems) then 'or ubound(pList)
		return -1
	else
		return nItems(listIndex)
	end if
end function

function dyn2d_list_type.getItem(listIndex as integer, itemIndex as integer) as data_type
	dim as data_type value
	if listIndex <= ubound(pList) then
		if itemIndex <= nItems(listIndex) then
			value = pList(listIndex)[iTemIndex]
		end if
	end if
	return value 
end function

sub dyn2d_list_type.show()
   print "pList size: " & str(ubound(plist) + 1) & ", nItems size: " & str(ubound(nItems) + 1)
   for i as integer = 0 to ubound(plist)
      print "- List: " & str(i) & ", size: " & nItems(i)
      for j as integer = 0 to nItems(i) - 1
         print "  - Value: " & pList(i)[j]
      next
   next
end sub

destructor dyn2d_list_type
	for i as integer = 0 to ubound(plist)
		deallocate(pList(i))
	next
	erase pList
	erase nItems
end destructor

#undef data_type

dim as dyn2d_list_type list

print !"\n== Make list ==\n"
print list.addItem(type<int2d>(1, 2)) 'will fail as it should
list.inc()
print list.addItem(type<int2d>(2, 3))
print list.addItem(type<int2d>(3, 4))
list.inc()
print list.addItem(type<int2d>(4, 5))
print list.addItem(type<int2d>(5, 6))
print list.addItem(type<int2d>(6, 7))
print list.addItem(type<int2d>(7, 8))
print !"\n== Print list ==\n"
list.show()
print !"\n== Loop list, item ==\n"
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 !"\n== Kill list ==\n"
list.destructor()
print !"\n== End ==\n"
edit: solved another bug
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dynamic list of lists

Post by fxm »

badidea wrote:
fxm wrote:Yes (my recovery is difficult!)
I thought health recovery for a moment, but it is your movement internet recovery, I hope.
Yes, 2 weeks without internet because of my move (I stay in the suburbs of Paris while moving a bit away from Paris).
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dynamic list of lists

Post by fxm »

Code: Select all

sub dyn2d_list_type.show()
   print "pList size: " & str(ubound(plist) + 1)
'   print "pList size: " & str(ubound(plist) + 1) & ", nItems size: " & str(ubound(nItems) + 1)
   for i as integer = 0 to ubound(plist)
      print "- List: " & str(i) & ", size: " & nItems(i)
      for j as integer = 0 to nItems(i) - 1
         print "  - Value: " & pList(i)[j]
      next
   next
end sub
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dynamic list of lists

Post by fxm »

Since arrays of arrays are not supported and in order to have a more homogeneous notation, maybe downright use pointers to pointers:

Code: Select all

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 dyn2d_list_type
   public:
   declare sub inc()
   declare function size() as integer
   declare function addItem(byval value as data_type) as integer
   declare function numItems(listIndex as integer) as integer
   declare function getItem(listIndex as integer, itemIndex as integer) as data_type
   declare sub show()
   declare destructor()
   private:
   dim as data_type ptr ptr ppList  'pointers to pointers to lists
'   dim as data_type ptr pList(any) 'array of pointers to lists
   dim as integer nItems(any) 'array list sizes
end type

'increase list size / add a list
sub dyn2d_list_type.inc()
   dim as integer listSize = size()
   redim preserve nItems(listSize)
   nItems(listSize) = 0
   ppList = reallocate(pplist, (listSize + 1) * sizeof(data_type ptr))
   ppList[listSize] = 0
'   redim preserve pList(listSize)
'   pList(listSize) = 0
end sub

'get num lists
function dyn2d_list_type.size() as integer
   return ubound(nItems) + 1
end function

'add item to last list
function dyn2d_list_type.addItem(byval value as data_type) as integer
   dim as integer ubList = ubound(nItems)
'   dim as integer ubList = ubound(pList)
   if ubList < 0 then
      return -1 'list not existing
   else
      ppList[ubList] = reallocate(ppList[ubList], (nItems(ubList) + 1) * sizeof(data_type))
      ppList[ubList][nItems(ubList)] = value
'      pList(ubList) = reallocate(pList(ubList), (nItems(ubList) + 1) * sizeof(data_type))
'      pList(ubList)[nItems(ubList)] = value
      nItems(ubList) += 1
      return 0
   end if
end function

'get number of items in a list
function dyn2d_list_type.numItems(listIndex as integer) as integer
   if listIndex > ubound(nItems) then
'   if listIndex > ubound(nItems) then 'or ubound(pList)
      return -1
   else
      return nItems(listIndex)
   end if
end function

function dyn2d_list_type.getItem(listIndex as integer, itemIndex as integer) as data_type
   dim as data_type value
   if listIndex <= ubound(nItems) then
'   if listIndex <= ubound(pList) then
      if itemIndex <= nItems(listIndex) then
         value = ppList[listIndex][iTemIndex]
'         value = pList(listIndex)[iTemIndex]
      end if
   end if
   return value
end function

sub dyn2d_list_type.show()
   print "pList size: " & str(ubound(nItems) + 1)
   for i as integer = 0 to ubound(nItems)
      print "- List: " & str(i) & ", size: " & nItems(i)
      for j as integer = 0 to nItems(i) - 1
         print "  - Value: " & ppList[i][j]
      next
   next
'   print "pList size: " & str(ubound(plist) + 1)
'   for i as integer = 0 to ubound(plist)
'      print "- List: " & str(i) & ", size: " & nItems(i)
'      for j as integer = 0 to nItems(i) - 1
'         print "  - Value: " & pList(i)[j]
'      next
'   next
end sub

destructor dyn2d_list_type
   for i as integer = 0 to ubound(nItems)
      deallocate(ppList[i])
   next
   deallocate(ppList)
   erase nItems
'   for i as integer = 0 to ubound(plist)
'      deallocate(pList(i))
'   next
'   erase pList
'   erase nItems
end destructor

#undef data_type

dim as dyn2d_list_type list

print !"\n== Make list ==\n"
print list.addItem(type<int2d>(1, 2)) 'will fail as it should
list.inc()
print list.addItem(type<int2d>(2, 3))
print list.addItem(type<int2d>(3, 4))
list.inc()
print list.addItem(type<int2d>(4, 5))
print list.addItem(type<int2d>(5, 6))
print list.addItem(type<int2d>(6, 7))
print list.addItem(type<int2d>(7, 8))
print !"\n== Print list ==\n"
list.show()
print !"\n== Loop list, item ==\n"
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 !"\n== Kill list ==\n"
list.destructor()
print !"\n== End ==\n"
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Dynamic list of lists

Post by badidea »

I agree, looks better.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Dynamic list of lists

Post by badidea »

fxm wrote:Since arrays of arrays are not supported and in order to have a more homogeneous notation, maybe downright use pointers to pointers
Something is wrong, cannot re-use the list after calling the destructor.

Code: Select all

for nLoop as integer = 0 to 1
	print !"\n== Make list ==\n"
	print list.addItem(type<int2d>(1, 2)) 'will fail as it should
	list.inc()
	print list.addItem(type<int2d>(2, 3))
	print list.addItem(type<int2d>(3, 4))
	list.inc()
	print list.addItem(type<int2d>(4, 5))
	print list.addItem(type<int2d>(5, 6))
	print list.addItem(type<int2d>(6, 7))
	print list.addItem(type<int2d>(7, 8))
	print !"\n== Print list ==\n"
	list.show()
	print !"\n== Loop list, item ==\n"
	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 !"\n== Kill list ==\n"
	list.destructor()
next
Edit:

Code: Select all

	deallocate(ppList) : ppList = 0
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dynamic list of lists

Post by fxm »

Yes, or use 'Reallocate' everywhere:

Code: Select all

destructor dyn2d_list_type
   for i as integer = 0 to ubound(nItems)
      ppList[i] = reallocate(ppList[i], 0)
'      deallocate(ppList[i])
   next
   ppList = reallocate(ppList, 0)
'   deallocate(ppList)
   erase nItems
'   for i as integer = 0 to ubound(plist)
'      deallocate(pList(i))
'   next
'   erase pList
'   erase nItems
end destructor
MOD
Posts: 557
Joined: Jun 11, 2009 20:15

Re: Dynamic list of lists

Post by MOD »

Code: Select all

#Include Once "md/util/mdList.bi"

mdListDeclare(String)
mdListDeclare(mdListString)

Dim As mdList(String) strings1
strings1.add("1")
strings1.add("2")
strings1.add("3")

Dim As mdList(String) strings2
strings2.add("4")
strings2.add("5")
strings2.add("6")

Dim As mdList(mdListString) lists
lists.add(strings1)
lists.add(strings2)

Print lists.get(0).get(1) 'Print second item of first stringlist: 2
Sleep
https://freebasic.net/forum/viewtopic.php?f=8&t=21614
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Dynamic list of lists

Post by badidea »

That one is only for strings right?

Some clean-up, change of class name, better out-of-bounds checking and setItem() added.
dyn_list_v05.bi :

Code: Select all

type dyn_list_type
	public:
	declare sub inc()
	declare function clr(listIndex as integer) as integer
	declare function size() as integer
	declare function addItem(byval value as data_type) as integer
	declare function numItems(listIndex as integer) as integer
	declare function getItem(listIndex as integer, itemIndex as integer) as data_type
	declare function setItem(listIndex as integer, itemIndex as integer, value as data_type) as integer
	declare sub show()
	declare destructor()
	private:
	dim as data_type ptr ptr ppList  'pointers to pointers to lists
	dim as integer nItems(any) 'array list sizes
end type

'increase list size / add a list
sub dyn_list_type.inc()
	dim as integer listSize = size()
	redim preserve nItems(listSize)
	nItems(listSize) = 0
	ppList = reallocate(pplist, (listSize + 1) * sizeof(data_type ptr))
	ppList[listSize] = 0
end sub

'increase list size / add a list
function dyn_list_type.clr(listIndex as integer) as integer
	if listIndex >= 0 and listIndex <= ubound(nItems) then
		ppList[listIndex] = reallocate(ppList[listIndex], 0)
		nItems(listIndex) = 0
		return 0
	end if
	return -1
end function

'get num lists
function dyn_list_type.size() as integer
	return ubound(nItems) + 1
end function

'add item to last list
function dyn_list_type.addItem(byval value as data_type) as integer
	dim as integer ubList = ubound(nItems)
	if ubList < 0 then
		return -1 'list not existing
	else
		ppList[ubList] = reallocate(ppList[ubList], (nItems(ubList) + 1) * sizeof(data_type))
		ppList[ubList][nItems(ubList)] = value
		nItems(ubList) += 1
		return 0
	end if
end function

'get number of items in a list
function dyn_list_type.numItems(listIndex as integer) as integer
	if listIndex >= 0 and listIndex <= ubound(nItems) then
		return nItems(listIndex)
	else
		return -1
	end if
end function

'get value, return 'zero' if out of bounds
function dyn_list_type.getItem(listIndex as integer, itemIndex as integer) as data_type
	dim as data_type value
	if listIndex >= 0 and listIndex <= ubound(nItems) then
		if itemIndex >= 0 and itemIndex <= nItems(listIndex) then
			value = ppList[listIndex][iTemIndex]
		end if
	end if
	return value
end function

'set value, return -1 if out of bounds
function dyn_list_type.setItem(listIndex as integer, itemIndex as integer, value as data_type) as integer
	if listIndex >= 0 and listIndex <= ubound(nItems) then
		if itemIndex >= 0 and itemIndex <= nItems(listIndex) then
			ppList[listIndex][iTemIndex] = value
			return 0
		end if
	end if
	return -1
end function

'print list contests
sub dyn_list_type.show()
	print "pList size: " & str(ubound(nItems) + 1)
	for i as integer = 0 to ubound(nItems)
		print "- List: " & str(i) & ", size: " & nItems(i)
		for j as integer = 0 to nItems(i) - 1
			print "  - Value: " & ppList[i][j]
		next
	next
end sub

'clean up
destructor dyn_list_type
	for i as integer = 0 to ubound(nItems)
		ppList[i] = reallocate(ppList[i], 0)
	next
	ppList = reallocate(ppList, 0)
	erase nItems
end destructor
Example use:

Code: Select all

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
#include "dyn_list_v05.bi"
#undef data_type

dim as dyn_list_type list

for iLoop as integer = 0 to 1
	print "== Make list =="
	print list.addItem(type<int2d>(1, 2)); 'should fail
	list.inc()
	print list.addItem(type<int2d>(2, 3));
	print list.addItem(type<int2d>(3, 4));
	list.inc()
	print list.addItem(type<int2d>(4, 5));
	print list.addItem(type<int2d>(5, 6));
	print list.addItem(type<int2d>(6, 7));
	print list.addItem(type<int2d>(7, 8))
	print "== Print list =="
	list.show()
	print "== Loop list, item : set value =="
	print list.setItem(-1, -1, type<int2d>(-1, -1)); 'should fail
	print list.setItem(100, 100, type<int2d>(100, 100)); 'should fail
	for iList as integer = 0 to list.size() - 1
		for iItem as integer = 0 to list.numItems(iList) - 1
			print list.setItem(iList, iItem, type<int2d>(iList, iItem));
		next
	next
	print !"\n== Print list =="
	list.show()
	print "== Kill list =="
	list.destructor()
	print "== Print list =="
	list.show()
	print
next
print "== End =="
Edit: A clear list added, destructor with reallocate size 0.
Last edited by badidea on Dec 04, 2018 23:40, edited 1 time in total.
MOD
Posts: 557
Joined: Jun 11, 2009 20:15

Re: Dynamic list of lists

Post by MOD »

As you can see, it's for all types as it's not only containing Strings but itself. Check the examples.
Post Reply