Author Topic: Classes, Unions and Arrays Help Please  (Read 20504 times)

0 Members and 1 Guest are viewing this topic.

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #30 on: June 13, 2012, 04:56:41 PM »
I was thinking about doing that, but then wanted to know if could do it with private. Probably the easiest solution. Will think about it some more.

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #31 on: June 13, 2012, 09:13:07 PM »
Hi Kent,

Private members are quite well guarded.

When dealing with compound math types that use operators, such as vectors, quaternions and matrices, you need efficient access to the individual members, at least for input and output of scalar values.

Oxygen performs automatic conversion between integers, floats and strings, which in many cases, eliminates the need for accessor methods.

Code: OxygenBasic
  1. class vec
  2.   double x,y,z
  3. end class
  4.  
  5. vec v
  6. v<="1.1","2.2","3.3" 'these are converted to doubles automatically
  7.  
  8. print str(v.x+v.y+v.z,4) 'answer 6.6 (integrity check)
  9.  

Charles

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #32 on: June 13, 2012, 10:37:06 PM »
I forget that Oxygen is no ordinary language and compiler, it is Smart++. That will be super then, a lot easier than having to write all the methods to get and set!!

kryton9

  • Guest
This could lead to some neat things.
« Reply #33 on: June 14, 2012, 10:49:13 PM »
I need your help oh master of oxygen.

Unzip the attachment to the projects folder. There is a readme.txt file to read and the comments in the code.

Getting this to work could lead to some powerful stuff, at least that is my hope!!
« Last Edit: June 15, 2012, 12:41:52 AM by kryton9 »

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #34 on: June 15, 2012, 12:56:19 AM »

I made a few minor changes, Kent, but the one to scrutinize is how to hook dynamic objects (of csqlite)into a new class. The new object has to be instantiated inside a method, in this case your constructor.

I've include to techniques for doing it. Solution d is the simplest


new cSqlite lDB ()
@mDB=@lDB 'link to this member


Solution d
Code: OxygenBasic
  1.  
  2. class cArray
  3.     'member variables
  4.    private
  5.  
  6.         string  mName
  7.         cSqlite *mdb
  8.  
  9.     public
  10.  
  11.         method Constructor( string aName = "AutoTableName" )
  12.             new cSqlite lDB ()
  13.             @mDB=@lDB 'link to this member
  14.            mname=aname
  15.             mDB.Exec "create table "mName" ( t text );"
  16.         end method
  17.        
  18.         method Destructor()
  19.             del mDB
  20.         end method
  21.        
  22. ....
  23.  

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #35 on: June 15, 2012, 01:03:19 AM »
Thanks Charles, it was driving me crazy. I couldn't sleep thinking about it. It is 5 a.m. already. So thanks now I can have some peace and sleep!

One question in solution D, which I agree, I like the best.  What about lDB?  shouldn't that be deleted also in the destructor along with mDB?

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #36 on: June 15, 2012, 01:47:09 AM »

No. In reality, lDB is just a pointer. Once its value is transferred to the mDB pointer, it is redundant.

then mDB takes on the role of the new object.

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #37 on: June 15, 2012, 10:57:10 AM »
Thanks for the explanation. That is a very elegant solution.

kryton9

  • Guest
Dynamic Array Start
« Reply #38 on: June 15, 2012, 06:58:32 PM »
Here is the start of a pretty slick dynamic array using sqlite3 as the backend. Right now it is only in the beginning stages. I am not flushing out any of the classes till I know that the whole idea I have in my mind can be done.

For now it is a single dimensional dynamic array that can hold numbers and strings. You can sort easily in either direction and go through the array with minimal code.

Unzip the attachment into your projects directory.
« Last Edit: June 15, 2012, 07:06:21 PM by kryton9 »

kryton9

  • Guest
The snag for tonight. Step 2
« Reply #39 on: June 15, 2012, 11:11:41 PM »
Charles, just download, unzip into the projects folder and please read the "Read Me.txt".
Another one I am at a loss.

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #40 on: June 16, 2012, 02:14:03 AM »

Hi Kent, I see a problem with self-referencing. That is: when a member of the class is the class.

If this was a direct member, you would get a recursive explosion, but it should be possible to have a pointered member, as you have in cVec3

class cVec3 
    public 'member vars
        has tVec3
       
    private 'member vars
        cVec3 *mTemp
       


Anyway this will keep me busy for a while.

Charles

kryton9

  • Guest
Re: I see a problem with self-referencing
« Reply #41 on: June 16, 2012, 01:55:34 PM »
I knew that was going to be tough one. I hate to hit you with it, good luck!

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #42 on: June 17, 2012, 03:05:08 AM »
Hi Kent,

It's been an interesting journey through the hall of mirrors :)

I've marked up 2 infinite recursions!

Below your program files, a new Oxygen, and an example tree class, which I trust is reasonably proof against unintended recursion.

new tree trunk
let branchA = trunk.CreateBranch 1
let twigA1 = branchA.CreateBranch 1
...
del trunk


Charles

[attachment deleted by admin]

kryton9

  • Guest
RE: New oxygen class update
« Reply #43 on: June 17, 2012, 12:00:39 PM »
Charles, I love it. Since we are not using New, then no Del, we are using getmemory and then freememory, so that eliminates confusion on when to use which.

I don't understand this:
Code: OxygenBasic
  1.   method ShowAll() as string
  2.   tree*tr
  3.   sys i
  4.   method+=s  ": Coords "  x ", " y ", " z chr(13) chr(10)
  5.   for i=1 to branches
  6.     if t[i] then
  7.       @tr=t[i]
  8.       method+=tr.ShowAll() 'RECURSE
  9.    end if
  10.   next
  11.   end method

method+=s  ": Coords "  x ", " y ", " z chr(13) chr(10)
method+=tr.ShowAll() 'RECURSE

I thought method used like that was like return. How can you have +=?  Also on the recurse, won't it exit when it sees the first method? I don't see a conditional to prevent it.

Anyways, I know this must have been a nightmare to solve. But it is making for a tighter better oxygen.

The last 2 days I have been working on 1 million cVec3's, creating, inserting, resorting and getting back the data. Here is the moral of the story:
With a small test, as we have in the cArray that you were using, it was just a few records hand entered. It was no problem. Once I got a good RandDouble function to generate random cVec3's, I started bumping it up. One thousand, no problem. Ten Thousand, no problem. 100 Thousand, about 5 seconds total time, not bad at all. 1 Million about a minute, too slow and that was after optimizing. The original took about 4 minutes at 1 Million. In reading Sqlite3 documentation, they say I should be able to do about 50,000 inserts per second on an average computer. So I am working to get it down to around 20 seconds, with 1 million inserts. With your improvements to the classes, it will help get data in and out faster now.

Thanks again!


« Last Edit: June 17, 2012, 12:17:34 PM by kryton9 »

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #44 on: June 17, 2012, 12:26:41 PM »
Hi Kent,

it works like function= in PowerBasic. Method is the variable that will be returned at the end of the procedure. In this case it behaves like any other string variable.

No problem with recursion, the string just accumulates the recursed output of levels below. Though it is not the most efficient way of doing it, and a large tree would be better served by using a buffering techniques rather than simple concatenation.

I wouldn't worry too much about speed. Once you have a working system, the bottlenecks will become obvious, and if necessary, we unleash the assembler! But once optimisations are in place, it becomes more difficult to change your program design, so they are best done later in the project.

Charles
« Last Edit: June 17, 2012, 12:39:40 PM by Charles Pegge »