super object try

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

super object try

Postby bluatigro » Jun 29, 2018 11:28

i dont know what to code what is missing

i need help whit this

Code: Select all

'' bluatigro 29 jun 2018
'' super object

type prop
public :
  dim as string p , t , v
  declare sub fill( a as string , b as string , c as string )
  declare function toStr() as string
end type
sub t_property.fill( a as string , b as string , c as string )
  p = a
  t = b
  v = c
end sub
function prop.toStr() as string
  return "[ type = " + t + " | property = " ; p ; " | value = " + v + " ]"
end function


type super_object
private :
  dim as t_property ptr pp = 0
  dim as integer proptel = 0
public :
  declare sub add_property( p as string , t as string )
  declare function get_property( p as string ) as prop
  declare sub remove_property( p as string )
  declare sub set_property( p as string , v as string )
  declare sub show()
end type
sub super_object.add_property( p as string , t as string )
  print "WARNING : add property : " + p
  print "that property in already in this object ."
  print "so i do not add it ."
  proptel += 1
end sub
function t_object.get_property( p as string ) as prop
  dim as prop uit
  uit.p = "ERROR : get proprty : " + p
  uit.t = "string"
  uit.v = "that property is not in this object !!"
  return uit
end function
sub super_object.remove_proprty( p as string )
end sub
sub super_object.set_property( p as string , v as string )
  print "ERROR : set property  : " + p
  print "that property is not in this object !!"
end sub 
sub super_object.show()
  dim as integer i
  for i = 0 to proptel
    print pp[ i ].toStr()
  next i
end sub
fxm
Posts: 8167
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: super object try

Postby fxm » Jun 29, 2018 11:59

Excepts some typos (";" instead of for example "+", and an "e" missing in "property"), you are referring in code to two different types "prop" and "t_property".
Which one to choose?
Therefore I added an alias at beginning of code:

Code: Select all

type t_property as prop

'' bluatigro 29 jun 2018
'' super object

type prop
public :
  dim as string p , t , v
  declare sub fill( a as string , b as string , c as string )
  declare function toStr() as string
end type
sub t_property.fill( a as string , b as string , c as string )
  p = a
  t = b
  v = c
end sub
function prop.toStr() as string
  return "[ type = " + t + " | property = " + p + " | value = " + v + " ]"
end function


type super_object
private :
  dim as t_property ptr pp = 0
  dim as integer proptel = 0
public :
  declare sub add_property( p as string , t as string )
  declare function get_property( p as string ) as prop
  declare sub remove_property( p as string )
  declare sub set_property( p as string , v as string )
  declare sub show()
end type
sub super_object.add_property( p as string , t as string )
  print "WARNING : add property : " + p
  print "that property in already in this object ."
  print "so i do not add it ."
  proptel += 1
end sub
function super_object.get_property( p as string ) as prop
  dim as prop uit
  uit.p = "ERROR : get proprty : " + p
  uit.t = "string"
  uit.v = "that property is not in this object !!"
  return uit
end function
sub super_object.remove_property( p as string )
end sub
sub super_object.set_property( p as string , v as string )
  print "ERROR : set property  : " + p
  print "that property is not in this object !!"
end sub
sub super_object.show()
  dim as integer i
  for i = 0 to proptel
    print pp[ i ].toStr()
  next i
end sub
bluatigro
Posts: 559
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: super object try

Postby bluatigro » Jul 13, 2018 3:58

update :
i got now almost al code

please help whit the missing code

Code: Select all

'' bluatigro 13 jul 2018
'' super object

type prop
public :
  dim as string p , t , v
  declare sub fill( a as string _
  , b as string , c as string )
  declare function toStr() as string
end type
sub prop.fill( a as string _
, b as string , c as string )
  p = a
  t = b
  v = c
end sub
function prop.toStr() as string
  return "[ type = " + t _
         + " | property = " + p _
         + " | value = " + v + " ]"
end function


type super_object
private :
  dim as prop ptr pp = 0
  dim as integer proptel = 0
public :
  declare sub add_property( p as string , t as string )
  declare function get_property( p as string ) as prop
  declare sub remove_property( p as string )
  declare sub set_property( p as string , v as string )
  declare sub show()
end type
sub super_object.add_property( p as string , t as string )
  dim as iunteger i , j
  j = -1
  for i = 0 to proptel
    if p = pp[ i ] then j = i
  next i
  if j <> -1 then
    print "WARNING : add property : " + p
    print "that property in already in this object ."
    print "so i do not add it ."
  else
    ''how do i ad a prop to a prop ptr ?
    ''and keep the others ?
    proptel += 1
  end if
end sub
function t_object.get_property( p as string ) as prop
  dim as prop uit
  dim as integer i , j
  j = 0
  for i = 0 to proptel
    if p = pp[ i ] then j = 1
  next i
  if j = 0 then
    uit.p = "ERROR : get proprty : " + p
    uit.t = "string"
    uit.v = "that property is not in this object !!"
  else
    for i = 0 to proptel
      if p = pp[ i ].p then j = i
    next i
    uit = p[ j ]
  end if   
  return uit
end function
sub super_object.remove_proprty( p as string )
  dim as integer i , j
  j = -1
  for i = 0 to proptel
    if p = pp[ i ] then j = i
  next j
  if j = -1 then return
  for i = j to proptel
    pp[ i ] = pp[ i + 1 ]
  next i
  proptel -= 1
  delete pp[ j ]
end sub
sub super_object.set_property( p as string _
  , v as string )
  dim as integer i , j
  j = -1
  for i = 0 to proptel
    if p = pp[ i ] then j = i
  next i
  if j = -1 then
    print "ERROR : set property  : " + p
    print "that property is not in this object !!"
    return
  end if
  pp[ j ].v = v
end sub 
sub super_object.show()
  dim as integer i
  for i = 0 to proptel
    print pp[ i ].toStr()
  next i
end sub
fxm
Posts: 8167
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: super object try

Postby fxm » Jul 13, 2018 6:23

I tried to modify as little as possible your existing code (so I did not optimize the program):

Code: Select all

type prop
public :
  dim as string p , t , v
  declare sub fill( a as string _
  , b as string , c as string )
  declare function toStr() as string
end type
sub prop.fill( a as string _
, b as string , c as string )
  p = a
  t = b
  v = c
end sub
function prop.toStr() as string
  return "[ type = " + t _
         + " | property = " + p _
         + " | value = " + v + " ]"
end function


type super_object
private :
  dim as prop ptr pp(any)
  dim as integer proptel = 0
public :
  declare sub add_property( p as string , t as string )
  declare function get_property( p as string ) as prop
  declare sub remove_property( p as string )
  declare sub set_property( p as string , v as string )
  declare sub show()
end type
sub super_object.add_property( p as string , t as string )
  dim as integer i , j
  j = -1
  for i = 0 to proptel - 1
    if p = pp( i )->p then j = i
  next i
  if j <> -1 then
    print "WARNING : add property : " + p
    print "that property in already in this object ."
    print "so i do not add it ."
  else
    redim preserve pp( proptel )
    pp( proptel ) = new prop
    pp( proptel )->p = p
    pp( proptel )->t = t
    proptel += 1
  end if
end sub
function super_object.get_property( p as string ) as prop
  dim as prop uit
  dim as integer i , j
  j = -1
  for i = 0 to proptel - 1
    if p = pp( i )->p then j = i
  next i
  if j = -1 then
    uit.p = "ERROR : get property : " + p
    uit.t = "string"
    uit.v = "that property is not in this object !!"
  else
    uit = *pp( j )
  end if   
  return uit
end function
sub super_object.remove_property( p as string )
  dim as integer i , j
  j = -1
  for i = 0 to proptel - 1
    if p = pp( i )->p then j = i
  next i
  if j = -1 then return
  delete pp( j )
  for i = j to proptel - 2
    pp( i ) = pp( i + 1 )
  next i
  proptel -= 1
  if proptel > 0 then
    redim preserve pp( proptel - 1 )
  else
    erase pp
  end if
end sub
sub super_object.set_property( p as string _
  , v as string )
  dim as integer i , j
  j = -1
  for i = 0 to proptel - 1
    if p = pp( i )->p then j = i
  next i
  if j = -1 then
    print "ERROR : set property  : " + p
    print "that property is not in this object !!"
    return
  end if
  pp( j )->v = v
end sub
sub super_object.show()
  dim as integer i
  for i = 0 to proptel - 1
    print pp( i )->toStr()
  next i
end sub


dim as super_object so
print "starting"
print "showing properties:"
so.show()
print
so.add_property( "property #1" , "type #1" )
print "adding " + so.get_property( "property #1" ).p
print "showing properties:"
so.show()
print
so.add_property( "property #2" , "type #2" )
print "adding " + so.get_property( "property #2" ).p
print "showing properties:"
so.show()
print
so.set_property( "property #2" , "value #2" )
print "setting " + so.get_property( "property #2" ).p
print "showing properties:"
so.show()
print
so.set_property( "property #1" , "value #1" )
print "setting " + so.get_property( "property #1" ).p
print "showing properties:"
so.show()
print
print "removing " + so.get_property( "property #1" ).p
so.remove_property( "property #1" )
print "showing properties:"
so.show()
print
print "removing " + so.get_property( "property #2" ).p
so.remove_property( "property #2" )
print "showing properties:"
so.show()
print

sleep
(+ main code for test cases)
fxm
Posts: 8167
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: super object try

Postby fxm » Jul 13, 2018 8:36

But we can also simplify the above code by replacing the array of 'prop ptr' by an array of 'prop' (so suppressing calls to 'new' and 'delete'):

Code: Select all

type prop
public :
  dim as string p , t , v
  declare sub fill( a as string _
  , b as string , c as string )
  declare function toStr() as string
end type
sub prop.fill( a as string _
, b as string , c as string )
  p = a
  t = b
  v = c
end sub
function prop.toStr() as string
  return "[ type = " + t _
         + " | property = " + p _
         + " | value = " + v + " ]"
end function


type super_object
private :
  dim as prop pp(any)
  dim as integer proptel = 0
public :
  declare sub add_property( p as string , t as string )
  declare function get_property( p as string ) as prop
  declare sub remove_property( p as string )
  declare sub set_property( p as string , v as string )
  declare sub show()
end type
sub super_object.add_property( p as string , t as string )
  dim as integer i , j
  j = -1
  for i = 0 to proptel - 1
    if p = pp( i ).p then j = i
  next i
  if j <> -1 then
    print "WARNING : add property : " + p
    print "that property in already in this object ."
    print "so i do not add it ."
  else
    redim preserve pp( proptel )
    pp( proptel ).p = p
    pp( proptel ).t = t
    proptel += 1
  end if
end sub
function super_object.get_property( p as string ) as prop
  dim as prop uit
  dim as integer i , j
  j = -1
  for i = 0 to proptel - 1
    if p = pp( i ).p then j = i
  next i
  if j = -1 then
    uit.p = "ERROR : get property : " + p
    uit.t = "string"
    uit.v = "that property is not in this object !!"
  else
    uit = pp( j )
  end if   
  return uit
end function
sub super_object.remove_property( p as string )
  dim as integer i , j
  j = -1
  for i = 0 to proptel - 1
    if p = pp( i ).p then j = i
  next i
  if j = -1 then return
  for i = j to proptel - 2
    pp( i ) = pp( i + 1 )
  next i
  proptel -= 1
  if proptel > 0 then
    redim preserve pp( proptel - 1 )
  else
    erase pp
  end if
end sub
sub super_object.set_property( p as string _
  , v as string )
  dim as integer i , j
  j = -1
  for i = 0 to proptel - 1
    if p = pp( i ).p then j = i
  next i
  if j = -1 then
    print "ERROR : set property  : " + p
    print "that property is not in this object !!"
    return
  end if
  pp( j ).v = v
end sub
sub super_object.show()
  dim as integer i
  for i = 0 to proptel - 1
    print pp( i ).toStr()
  next i
end sub


dim as super_object so
print "starting"
print "showing properties:"
so.show()
print
so.add_property( "property #1" , "type #1" )
print "adding " + so.get_property( "property #1" ).p
print "showing properties:"
so.show()
print
so.add_property( "property #2" , "type #2" )
print "adding " + so.get_property( "property #2" ).p
print "showing properties:"
so.show()
print
so.set_property( "property #2" , "value #2" )
print "setting " + so.get_property( "property #2" ).p
print "showing properties:"
so.show()
print
so.set_property( "property #1" , "value #1" )
print "setting " + so.get_property( "property #1" ).p
print "showing properties:"
so.show()
print
print "removing " + so.get_property( "property #1" ).p
so.remove_property( "property #1" )
print "showing properties:"
so.show()
print
print "removing " + so.get_property( "property #2" ).p
so.remove_property( "property #2" )
print "showing properties:"
so.show()
print

sleep
bluatigro
Posts: 559
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: super object try

Postby bluatigro » Jul 14, 2018 4:12

@ fxm :
thanks for help

i got a question :
what is faster : prop pp( any ) or prop ptr pp ?

i m very curious :
can this be used in a game ?
i m not good in thingking up new games
if you do menion me and fxm in the credits

i think that if you had a array of super_object's
they can every one have a different 'shape'
what do you thiunk about that !
you only need then 1 array of super_object's !
you can put a array in a super_object
fxm
Posts: 8167
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: super object try

Postby fxm » Jul 14, 2018 5:44

In addition to the 2 previous codes using:
- a dynamic array of pointers to the objects,
- a dynamic array of objects,
we can also use:
- an own dynamic buffer of pointers to the objects:

Code: Select all

type prop
public :dim as string p , t , v
  declare sub fill( a as string _
  , b as string , c as string )
  declare function toStr() as string
end type
sub prop.fill( a as string _
, b as string , c as string )
  p = a
  t = b
  v = c
end sub
function prop.toStr() as string
  return "[ type = " + t _
         + " | property = " + p _
         + " | value = " + v + " ]"
end function


type super_object
private :
  dim as prop ptr ptr pp
  dim as integer proptel = 0
public :
  declare sub add_property( p as string , t as string )
  declare function get_property( p as string ) as prop
  declare sub remove_property( p as string )
  declare sub set_property( p as string , v as string )
  declare sub show()
end type
sub super_object.add_property( p as string , t as string )
  dim as integer i , j
  j = -1
  for i = 0 to proptel - 1
    if p = pp[ i ]->p then j = i
  next i
  if j <> -1 then
    print "WARNING : add property : " + p
    print "that property in already in this object ."
    print "so i do not add it ."
  else
    pp = reallocate( pp, (proptel+1)*sizeof(prop ptr ptr) )
    pp[ proptel ] = new prop
    pp[ proptel ]->p = p
    pp[ proptel ]->t = t
    proptel += 1
  end if
end sub
function super_object.get_property( p as string ) as prop
  dim as prop uit
  dim as integer i , j
  j = -1
  for i = 0 to proptel - 1
    if p = pp[ i ]->p then j = i
  next i
  if j = -1 then
    uit.p = "ERROR : get property : " + p
    uit.t = "string"
    uit.v = "that property is not in this object !!"
  else
    uit = *pp[ j ]
  end if   
  return uit
end function
sub super_object.remove_property( p as string )
  dim as integer i , j
  j = -1
  for i = 0 to proptel - 1
    if p = pp[ i ]->p then j = i
  next i
  if j = -1 then return
  delete pp[ j ]
  for i = j to proptel - 2
    pp[ i ] = pp[ i + 1 ]
  next i
  proptel -= 1
  pp = reallocate( pp, proptel*sizeof(prop ptr ptr) )
end sub
sub super_object.set_property( p as string _
  , v as string )
  dim as integer i , j
  j = -1
  for i = 0 to proptel - 1
    if p = pp[ i ]->p then j = i
  next i
  if j = -1 then
    print "ERROR : set property  : " + p
    print "that property is not in this object !!"
    return
  end if
  pp[ j ]->v = v
end sub
sub super_object.show()
  dim as integer i
  for i = 0 to proptel - 1
    print pp[ i ]->toStr()
  next i
end sub


dim as super_object so
print "starting"
print "showing properties:"
so.show()
print
so.add_property( "property #1" , "type #1" )
print "adding " + so.get_property( "property #1" ).p
print "showing properties:"
so.show()
print
so.add_property( "property #2" , "type #2" )
print "adding " + so.get_property( "property #2" ).p
print "showing properties:"
so.show()
print
so.set_property( "property #2" , "value #2" )
print "setting " + so.get_property( "property #2" ).p
print "showing properties:"
so.show()
print
so.set_property( "property #1" , "value #1" )
print "setting " + so.get_property( "property #1" ).p
print "showing properties:"
so.show()
print
print "removing " + so.get_property( "property #1" ).p
so.remove_property( "property #1" )
print "showing properties:"
so.show()
print
print "removing " + so.get_property( "property #2" ).p
so.remove_property( "property #2" )
print "showing properties:"
so.show()
print

sleep
Last edited by fxm on Jul 14, 2018 14:41, edited 2 times in total.
fxm
Posts: 8167
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: super object try

Postby fxm » Jul 14, 2018 6:14

I think if we compare the 3 above solutions using:
- dim as prop ptr pp1(any)
- dim as prop pp2(any)
- dim as prop ptr ptr pp3
the 3 access types respectively:
- pp1( i )->p
- pp2( i ).p
- pp3[ i ]->p
are equivalent for the execution time if we compile without '- exx' option.

Return to “General”

Who is online

Users browsing this forum: Bing [Bot] and 4 guests