STL Vector-alike via macros

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
duke4e
Posts: 717
Joined: Dec 04, 2005 0:16
Location: Varazdin, Croatia, Europe
Contact:

STL Vector-alike via macros

Postby duke4e » Sep 02, 2010 18:44

It's not perfect, but it works. Any improvements to this code are welcome!

Code: Select all

#define init(_list, _type)                                   Redim As _type _list(0)
#define count(_list)                                         (Abs(Lbound(_list) - Ubound(_list)))
#define add(_list, _type, _element)                          Redim Preserve As _type _list(Count(_list) + 1) : _list(Count(_list)) = _element
#define list(_list)                                          Integer = Lbound(_list) + 1 To Ubound(_list)
#define objectAtIndex(_list, _index)                         _list(_index)
#define lastObject(_list)                                    _list(Count(_list))
#define removeLastObject(_list, _type)                       Redim Preserve As _type _list(Count(_list) - 1)
#define replaceObjectAtIndexWithObject(_list, _id, _element) _list(_id) = _element

#macro insertObjectAtIndex(_list, _type, _index, _element)
If _index <= count(_list) And _index > 0 Then
    Redim Preserve As _type _list(Count(_list) + 1)
    For _i As Integer = Count(_list) To _index Step -1
        _list(_i) = _list(_i - 1)
    Next
    _list(_index) = _element
End If
#endmacro

#macro removeObjectAtIndex(_list, _type, _index)
If _index <= count(_list) And _index > 0 Then
    For _i As Integer = _index To Count(_list) - 1
        _list(_i) = _list(_i + 1)
    Next
    Redim Preserve As _type _list(Count(_list) - 1)
End If
#endmacro


/' test 1 '/

init(list1, Integer)
add(list1, Integer, 1)
add(list1, Integer, 2)
add(list1, Integer, 3)
add(list1, Integer, 4)
add(list1, Integer, 5)

Print "Size: " & Count(list1)
For i As List(list1)
    Print "Element: " & i & " Contents: " & ObjectAtIndex(list1, i)
Next

insertObjectAtIndex(list1, Integer, 3, 101)
Print "Size: " & Count(list1)
For i As List(list1)
    Print "Element: " & i & " Contents: " & ObjectAtIndex(list1, i)
Next

removeObjectAtIndex(list1, Integer, 3)
Print "Size: " & Count(list1)
For i As List(list1)
    Print "Element: " & i & " Contents: " & ObjectAtIndex(list1, i)
Next

Print "Last element: " & LastObject(list1)
removeLastObject(list1, Integer)
Print "Last element: " & LastObject(list1)
replaceObjectAtIndexWithObject(list1, count(list1), 100)
Print "Last element: " & LastObject(list1)
Print

Sleep

/' test 2 '/

Cls

init(list2, String)
add(list2, String, "a")
add(list2, String, "b")
add(list2, String, "c")


Print "Size: " & Count(list2)
For i As List(list2)
    Print "Element: " & i & " Contents: " & ObjectAtIndex(list2, i)
Next

insertObjectAtIndex(list2, String, 3, "test")
Print "Size: " & Count(list2)
For i As List(list2)
    Print "Element: " & i & " Contents: " & ObjectAtIndex(list2, i)
Next

removeObjectAtIndex(list2, String, 3)
Print "Size: " & Count(list2)
For i As List(list2)
    Print "Element: " & i & " Contents: " & ObjectAtIndex(list2, i)
Next

Print "Last element: " & LastObject(list2)
removeLastObject(list2, String)
Print "Last element: " & LastObject(list2)
replaceObjectAtIndexWithObject(list2, count(list2), "oooo")
Print "Last element: " & LastObject(list2)
Print

Sleep

AGS
Posts: 1284
Joined: Sep 25, 2007 0:26
Location: the Netherlands

Re: STL Vector-alike via macros

Postby AGS » Sep 09, 2010 22:20

I think it's a nice find. Very usable. And the code you get after preprocessing is quite readable. It's the use of Redim Resize on every insertion that looks a bit inefficient but that's only a minor issue.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 5 guests