vector<T> try

General FreeBASIC programming questions.
Post Reply
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

vector<T> try

Post by bluatigro »

is this even posible in FB ?
or is there a vector.bi file somwere ?

Code: Select all

''bluatigro 
''trying to create a templated vector class
''as exsist in c++ stl
''20 okt 2015 : first try

#define T integer

type vector
private :
  dim as T ptr q_ 
  dim as integer size_ 
public :
  declare constructor
  declare destructor
  declare sub push_back( in as T )
  declare function size() as integer
  declare function at( i as integer ) as T
  declare function find( in as T ) as integer
end type
constructor vector
  q_ = 0
  size_ = 0
end constructor
destructor vector
  q_ = 0
end destructor
sub vector.push_back( in as T )
  size_ += 1
  q_ = allocate( size_ * sizeof( T ) )
end sub
function vector.size() as integer
  return size_
end function
function vector.at( i as integer ) as T
  if i < 0 or i > size_ then return 0
  return q_( i )
end function
function vector.find( in as T ) as integer
  dim as integer i = 0
  while i <= size_ and q_( i ) <> in
    i += 1
  wend
  if i > size_ then return -1
  return i
end function
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: vector<T> try

Post by fxm »

Just your code very quickly corrected:

Code: Select all

''bluatigro
''trying to create a templated vector class
''as exsist in c++ stl
''20 okt 2015 : first try

#define T integer

type vector
private :
  dim as T ptr q_
  dim as integer size_
public :
  declare constructor
  declare destructor
  declare sub push_back( byval in as T )
  declare function size() as integer
  declare function at( byval i as integer ) as T
  declare function find( byval in as T ) as integer
end type
constructor vector
  q_ = 0
  size_ = 0
end constructor
destructor vector
  deallocate( q_ )
  q_ = 0
  size_ = 0
end destructor
sub vector.push_back( byval in as T )
  size_ += 1
  q_ = reallocate( q_, size_ * sizeof( T ) )
  q_[ size_-1 ] = in
end sub
function vector.size() as integer
  return size_
end function
function vector.at( byval i as integer ) as T
  if i < 0 or i > size_-1 then return 0
  return q_[ i ]
end function
function vector.find( byval in as T ) as integer
  dim as integer i = 0
  while i < size_ andalso q_[ i ] <> in
    i += 1
  wend
  if i = size_ then return -1
  return i
end function

dim as vector v
v.push_back(11)
v.push_back(22)
v.push_back(33)
print v.size()
print
print v.at(0)
print v.at(1)
print v.at(2)
print
print v.find(00)
print v.find(11)
print v.find(22)
print v.find(33)
print v.find(44)

sleep
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Re: vector<T> try

Post by MOD »

Have a look at mdTypes, it allows you to use templated classes like lists, set and maps.
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: vector<T> try

Post by bluatigro »

@ fxm :
- thanks for help
@ mod :
- where can i find the mdtype.bi's

i tryed to add a [] operator as in a string but failed

Code: Select all

''bluatigro
''trying to create a templated vector class
''as exsist in c++ stl
''20 okt 2015 : first try
''23 okt 2015 : try at [] operator

#define T integer

type vector
private :
  dim as T ptr q_
  dim as integer size_
public :
  declare constructor
  declare destructor
  declare sub push_back( byval in as T )
  declare function size() as integer
  declare function at( byval i as integer ) as T
  declare function find( byval in as T ) as integer
  declare operator []( byval i as integer ) as T
end type
constructor vector
  q_ = 0
  size_ = 0
end constructor
destructor vector
  deallocate( q_ )
  q_ = 0
  size_ = 0
end destructor
sub vector.push_back( byval in as T )
  size_ += 1
  q_ = reallocate( q_, size_ * sizeof( T ) )
  q_[ size_-1 ] = in
end sub
function vector.size() as integer
  return size_
end function
function vector.at( byval i as integer ) as T
  if i < 0 or i > size_-1 then return 0
  return q_[ i ]
end function
function vector.find( byval in as T ) as integer
  dim as integer i = 0
  while i < size_ andalso q_[ i ] <> in
    i += 1
  wend
  if i = size_ then return -1
  return i
end function
operator vector.[]( i as integer ) as T
  return vector.find( i )
end operator

dim as vector v
v.push_back(11)
v.push_back(22)
v.push_back(33)

print "size = " ; v.size()
print "v.at( 0 ) = " ; v.at( 0 )
print "v.at( 1 ) = " ; v.at( 1 )
print "v.at( 2 ) = " ; v.at( 2 )
print "v[ 0 ] = " ; *v[ 0 ]
print "v[ 1 ] = " ; *v[ 1 ]
print "v[ 2 ] = " ; *v[ 2 ]
print "v.find( 00 ) = " ; v.find( 00 )
print "v.find( 11 ) = " ; v.find( 11 )
print "v.find( 22 ) = " ; v.find( 22 )
print "v.find( 33 ) = " ; v.find( 33 )
print "v.find( 44 ) = " ; v.find( 44 )

sleep
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: vector<T> try

Post by fxm »

Code: Select all

operator vector.[]( byval i as integer ) as T
  return find( i )
end operator

Code: Select all

dim as vector v
v.push_back(11)
v.push_back(22)
v.push_back(33)

print "size = " ; v.size()
print "v.at( 0 ) = " ; v.at( 0 )
print "v.at( 1 ) = " ; v.at( 1 )
print "v.at( 2 ) = " ; v.at( 2 )
print "v[ 0 ] = " ; v[ 0 ]
print "v[ 1 ] = " ; v[ 1 ]
print "v[ 2 ] = " ; v[ 2 ]
print "v[ 11 ] = " ; v[ 11 ]
print "v[ 22 ] = " ; v[ 22 ]
print "v[ 33 ] = " ; v[ 33 ]
print "v.find( 00 ) = " ; v.find( 00 )
print "v.find( 11 ) = " ; v.find( 11 )
print "v.find( 22 ) = " ; v.find( 22 )
print "v.find( 33 ) = " ; v.find( 33 )
print "v.find( 44 ) = " ; v.find( 44 )
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: vector<T> try

Post by fxm »

But I think that similarly to a string, the operator '[]' should rather replace the member function 'at()':

Code: Select all

''bluatigro
''trying to create a templated vector class
''as exsist in c++ stl
''20 okt 2015 : first try
''23 okt 2015 : try at [] operator

#define T integer

type vector
private :
  dim as T ptr q_
  dim as integer size_
public :
  declare constructor
  declare destructor
  declare sub push_back( byval in as T )
  declare function size() as integer
  declare function find( byval in as T ) as integer
  declare operator []( byval i as integer ) as integer
end type
constructor vector
  q_ = 0
  size_ = 0
end constructor
destructor vector
  deallocate( q_ )
  q_ = 0
  size_ = 0
end destructor
sub vector.push_back( byval in as T )
  size_ += 1
  q_ = reallocate( q_, size_ * sizeof( T ) )
  q_[ size_-1 ] = in
end sub
function vector.size() as integer
  return size_
end function
function vector.find( byval in as T ) as integer
  dim as integer i = 0
  while i < size_ andalso q_[ i ] <> in
    i += 1
  wend
  if i = size_ then return -1
  return i
end function
operator vector.[]( byval i as integer ) as T
  if i < 0 or i > size_-1 then return 0
  return q_[ i ]
end operator

dim as vector v
v.push_back(11)
v.push_back(22)
v.push_back(33)

print "size = " ; v.size()
print "v[ 0 ] = " ; v[ 0 ]
print "v[ 1 ] = " ; v[ 1 ]
print "v[ 2 ] = " ; v[ 2 ]
print "v.find( 00 ) = " ; v.find( 00 )
print "v.find( 11 ) = " ; v.find( 11 )
print "v.find( 22 ) = " ; v.find( 22 )
print "v.find( 33 ) = " ; v.find( 33 )
print "v.find( 44 ) = " ; v.find( 44 )

sleep

fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: vector<T> try

Post by fxm »

By using a fbc version >= 0.90.0, the operator '[]' defined as returning by reference can also assign a value to a vector component:

Code: Select all

operator vector.[]( byval i as integer ) byref as T
  if i < 0 or i > size_-1 then error 4
  return q_[ i ]
end operator
[edit]
The operator '[]' induces now a runtime error if index 'i' is out of the valid range.
Last edited by fxm on Oct 24, 2015 6:47, edited 5 times in total.
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Re: vector<T> try

Post by MOD »

bluatigro wrote:@ mod :
- where can i find the mdtype.bi's
1. Follow the link above to the project page in this forum
2. Klick on the link below the example code which tells you ">>DOWNLOAD<<"
3. Open the zip file and have a look at the ReadMe file, which tells you, that everything you need is included in the folder "md"

That makes three easy steps...
Post Reply