Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Objects, references & values

 [1/12] from: robert::muench::robertmuench::de at: 18-Dec-2001 12:41


Hi, up to now I mostly used Rebol to handle some simple structured data. Now I want to use datastructures to handle more complext data and there I have some problems with. Please keep in mind that I'm an old C++ programmer and might not have switched my mind to see datastructures the rebolized way. Here is my test proggy: Rebol[] myobj!: make object! [ name: string! 0 note: string! 0 ] myobjects1: make hash! [] myobjects2: make hash! [] obj1: make myobj! [name: "Robert" note: "Rebol"] ; this is an obj1 reference insert myobjects1 reduce [1 obj1] ; this is the value obj1/name insert myobjects2 reduce [obj1/name 1] ; this is the value obj1/name value: obj1/name ?? obj1 ?? value ?? myobjects1 ?? myobjects2 ; changing the object obj1 and all of its references obj1/name: "robert" ?? obj1 ?? value ?? myobjects1 ?? myobjects2 halt As you can see, if I reference object-words, I get the value and not the reference to the origianl value. For the C++ developer this means: using obj1 somewhere gives me a pointer using obj1/<some-obj1-member> gives me the value How can I create datsstructures with objects if I can't get references to the original object all the time? I want to have one single object being the base and than use references to the object or parts of it all over my script. Any idea? IMO the concept of a get-path is missing. I would like to write: value: to-get-path obj1/name This should result in a reference to obj1/name and not the value. -- Robert M. Münch IT & Management Freelancer Mobile: +49 (0)177 2452 802 Fax : +49 (0)721 8408 9112 Web : http://www.robertmuench.de

 [2/12] from: rotenca:telvia:it at: 18-Dec-2001 19:40


Hi Robert,
> IMO the concept of a get-path is missing. I would like to write: > > value: to-get-path obj1/name > > This should result in a reference to obj1/name and not the value.
If i have understand well what you ask, this my answer: nameref: in obj1 'name Now 'nameref is a pointer to a word which is linked to the field called "name" in the object 'obj1. obj1/name: "robert3" == "robert3" To retrieve the changed value:
>> get nameref
== "robert3" To change it: set nameref "robert4" == "robert4" Proof:
>> get nameref
== "robert4"
>> obj1/name
== "robert4" Another method: x: bind [name] in obj1 'self Now x is a block which contains as the first item the word 'name linked to the object obj1: x: bind [name] in obj1 'self == [name]
>> get first x
== "robert4" I can reach the same result inserting the word pointed by nameref in a void block, the word always conserves its link with obj1:
>> x2: head insert copy [] nameref
== [name]
>> get first x2
== "robert4" or with reduce:
>> x3: reduce [nameref]
== [name]
>> get first x3
== "robert4" If we want to make things more difficult, we can use the third of obj1:
>> third obj1
== [name: "robert4" note: "Rebol"]
>> set first third obj1 "robert5"
== "robert5"
>> third obj1
== [name: "robert5" note: "Rebol"]
>> get nameref
== "robert5"
>> get first x
== "robert5"
>> get first x2
== "robert5"
>> get first x3
== "robert5" --- Ciao Romano

 [3/12] from: robert:muench:robertmuench at: 19-Dec-2001 13:55


> -----Original Message----- > From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]On Behalf Of
<<quoted lines omitted: 5>>
> Now 'nameref is a pointer to a word which is linked to the field called "name" > in the object 'obj1.
Hi, yeah that's right but how do you search for the value of nameref? With this you can't use the hash! datatype because you don't have a value hash! can use. That's why I would like to have a value which I can add to a hash! that's updated when the referenced value changes. I know that's not easy to achieve. Even with C++ and the STL container you have the same problem. You can either use the pointer address to your value as hash-key, or you can use the value as hash-key. What to do if a key-value changes? Well, search it, remove it and add the new key-value. The problem to solve is that you have to be sure to keep everything in sync. In C++ you can do this by using member-functions and makeing the data-members private to the object. This ensures that none will be accessed/changed directly without executing the house-keeping code.
> Another method: > x: bind [name] in obj1 'self
<<quoted lines omitted: 4>>
> >> get first x > == "robert4"
Ok. BTW: I just had an idea. When using 'bind you can't see from the word, to which it is bound. Howabout having "bind [name] in obj1 'self" return obj1/name? This would be very logical IMO. As said, the biggest problem I currently have is that I would like to have a simple way to use the well known datastructures for managing objects, values etc. and being able to search, store etc. those datastructures. BTW: What happens if those structures are stored? Will Rebol be able to resolve the linking again? I don't think so... this implies that a datastructure loader is needed to setup all those references again. Robert

 [4/12] from: rotenca:telvia:it at: 19-Dec-2001 15:28


Hi Robert,
> > nameref: in obj1 'name > > > > Now 'nameref is a pointer to a word which is linked to the field called
name
> > in the object 'obj1. > > Hi, yeah that's right but how do you search for the value of nameref? With
this
> you can't use the hash! datatype because you don't have a value hash! can
use.
> That's why I would like to have a value which I can add to a hash! that's > updated when the referenced value changes.
I do not understand what you want to say. I can add it to a hash! datatype:
>> x: context [a: 2] >> nameref: in x 'a
== a
>> append make hash! 1 nameref
== make hash! [a] the value of 'a in the hash datatypes changes with the value of a in the x context
> The problem to solve is that you have to be sure to keep everything in sync.
In
> C++ you can do this by using member-functions and makeing the data-members > private to the object. This ensures that none will be accessed/changed
directly
> without executing the house-keeping code.
Also in rebol you can make private field (with some tricks). Also in rebol you can make member-functions. Sorry, I continue to not understand what you want to say.
> Ok. BTW: I just had an idea. When using 'bind you can't see from the word,
to
> which it is bound. Howabout having "bind [name] in obj1 'self" return
obj1/name?
> This would be very logical IMO.
And just this happens if 'self is well defined in the obj1.
> As said, the biggest problem I currently have is that I would like to have a > simple way to use the well known datastructures for managing objects, values > etc. and being able to search, store etc. those datastructures.
I do not understand what do you want to do, that cannot be made. I don't see it, a part store, see below.
> BTW: What happens if those structures are stored? Will Rebol be able to
resolve
> the linking again? I don't think so... this implies that a datastructure
loader
> is needed to setup all those references again.
This is true. But load/save are for loading code and related data struct not for snapshot of data. This seems to me a relational database work. --- Ciao Romano

 [5/12] from: robert:muench:robertmuench at: 19-Dec-2001 16:31


> -----Original Message----- > From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]On Behalf Of
<<quoted lines omitted: 10>>
> the value of 'a in the hash datatypes changes with the value of a in the x > context
Hi, yes you can add it to the hash!, of course. But what do you want to search for in the hash!? You can only search for 'a not the value 'a is refering to. That's my problem. Example: hash!: ["romano" address-object] Now I can search for "romano" and get the address-object. In this case I have to update the hash! if your name changes. Ok, to solve this, one could say: Hey, we have an address-object that always contains the actual name: hash!: [address-object/name address-object] But now I don't want to search for 'address-object/name but for the value, which is "romano" in our case. So what I would like to have is find using reduce to get the value of address-object/name while searching. That's why I would suggested to tag the address-object/name as a to-get-path. With this you could write: index1: to-get-path address-object/name hash!: [index1 address-object] Now if find iterates through the hash! and reaches an entry that is associated with a to-get-path, it evaluates the value. However, there is an other way to be able to use find with a hash! and referenced values. You have to double reduce the hash! before sending it to find. (See attached script below). The question is what happens internally to the hash! datastructure? Is it build-up new? This would result an a temporary new hash! data-structure used for find with the actual values. I hope you now understand what I mean.
> This is true. But load/save are for loading code and related data struct not > for snapshot of data. This seems to me a relational database work.
Yep, but IMO it's unnatural to faltten rebol objects to be stored in an RDBMS. What do you do, if your objects are not all equalle structured? The RDBMS can't handle that. It always needs the table specificaction a priori and complete. Robert Rebol[] myobj!: make object! [ name: string! 0 note: string! 0 ] myobjects1: make hash! [] myobjects2: make hash! [] obj1: make myobj! [name: "Robert" note: "Rebol"] ; this is the value obj1/name value: in obj1 'name ; this is an obj1 reference insert myobjects1 [1 obj1] ; this is the value obj1/name insert myobjects2 [value 1] probe obj1 probe get value probe reduce myobjects1 probe reduce myobjects2 probe reduce reduce myobjects2 print "--- Trying find in hash!" if find/case reduce myobjects2 "Robert" [print "Found!"] ; this one fails if find/case reduce reduce myobjects2 "Robert" [print "Found!"] ; this one hits ; changing the object obj1 and all of its references obj1/name: "robert" probe obj1 probe get value probe reduce myobjects1 probe reduce reduce myobjects2 print "--- Trying find in hash!" if find/case reduce myobjects2 "robert" [print "Found!"] ; this one fails if find/case reduce reduce myobjects2 "robert" [print "Found!"] ; this one hits halt

 [6/12] from: rotenca:telvia:it at: 19-Dec-2001 17:25


Hi Robert,
> Hi, yes you can add it to the hash!, of course. But what do you want to
search
> for in the hash!? You can only search for 'a not the value 'a is refering
to.
> That's my problem. Example: > > hash!: ["romano" address-object] > > Now I can search for "romano" and get the address-object. In this case I
have to
> update the hash! if your name changes. >
well, i start to understand, here it it is a solution (?) :
>> ob: context [name: "romano"] >> h: append append make hash! [] get in ob 'name in ob 'name
== make hash! ["romano" name]
>> change ob/name "dolores"
== ""
>> h
== make hash! ["dolores" name] The only problem is NOT to change name with an instruction like:
>>ob/name: "dolores"
because this changes the "fisical memory address" of the string pointed by ob/name while leaves the old string pointed by our hash series. Another problem is that this works only with series not with scalars. Another solution is writing a custom find which get the value of a word to search a string. Parse perhaps could help. I do not think that reduce is well suited, because i think you want to use a great hash database and reducing it every time is time consuming (reducing twice more).
> > This is true. But load/save are for loading code and related data struct
not
> > for snapshot of data. This seems to me a relational database work. > > Yep, but IMO it's unnatural to faltten rebol objects to be stored in an
RDBMS.
> What do you do, if your objects are not all equalle structured? The RDBMS
can't
> handle that. It always needs the table specificaction a priori and complete.
The database could be written in Rebol... What I want to say is that save/load is not the right method for this kind of work, it seems to me a more diffcult task, which requires dedicated code. I think that load/save could be better, they do not save/load neither all the normal Rebol code (unset problem, series not at head...)
> Robert
--- Ciao Romano

 [7/12] from: nitsch-lists:netcologne at: 19-Dec-2001 18:44


RE: [REBOL] Re: Objects, references & values Hi Robert, for hashs and syncing: have you checked that you need hash! permanently valid? for me most of the time i create the hash on the fly when i know i do a task with some searching. setting it up for some hundred elements seems to be not noticable, compared to rest of work. for searching a single item after a click normal find-loop works well enough. for normal storage i stay with a tree-form (series of objects with series with objects..), which can be saved/loaded pretty well. -Volker [robert--muench--robertmuench--de] wrote:

 [8/12] from: robert:muench:robertmuench at: 20-Dec-2001 10:37


> -----Original Message----- > From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]On Behalf Of
<<quoted lines omitted: 3>>
> Subject: [REBOL] Re: Objects, references & values > well, i start to understand, here it it is a solution (?) :
Hi, :-)) as non native english speaker it's not so easy to describe a problem that detailed, that the others can follow...
> >> ob: context [name: "romano"] > >> h: append append make hash! [] get in ob 'name in ob 'name
<<quoted lines omitted: 3>>
> >> h > == make hash! ["dolores" name]
Hmm... what happens if we have several names here? Would this result in: ["dolores" name "romano" name ...] Why the 'name entry? With such a hash! I could search for a name value and get a 'name word that is linked to the address object, right? Would it be possible to use 'self instead of 'name? I used numbers as object index because these don't change if reduced twice.
> The only problem is NOT to change name with an instruction like: > > >>ob/name: "dolores"
;-)) Bingo! We need private data.
> Another solution is writing a custom find which get the value of a word to > search a string. Parse perhaps could help.
I thought about this too. The nice thing about hash! is that (at least that's what I hope) is implemented as a hash datastructure and therefore supports very fast searches. Implementing a custom made find, I might have to iterate over each object... this gives searchspeed O(n).
> I do not think that reduce is well suited, because i think you want to use a > great hash database and reducing it every time is time consuming (reducing > twice more).
I didn't checked this but this could be true. It was just a quick hack to given an example for my not-so-well-described problem ;-))
> The database could be written in Rebol...
I'm a fan of simple solutions and thought that there must be a way to do it with the given parts... anway, after solving the reference problem, I go for this one. Robert

 [9/12] from: robert:muench:robertmuench at: 20-Dec-2001 10:37


> -----Original Message----- > From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]On Behalf Of
<<quoted lines omitted: 5>>
> for me most of the time i create the hash > on the fly when i know i do a task with some searching.
Hi, well that's quite a solution. Might not be very sophisticated but perhaps is the most pragmatic way. I have to check this out. The problem is that I have a very dynamic datastructure and need to do all kind of lookups to grow it etc. What I'm currently doing is creating a graph-layouter in Rebol. You can throw a simple graph-structure at it and it will create a niche layout for you. Traversing such graphs can become quite complex.
> for normal storage i stay with a tree-form > (series of objects with series with objects..), > which can be saved/loaded pretty well.
Yes, IMO that's the way to go. I would use the same or implement a simple saver/loader that flattens the datastructure and recreates it on loading. Robert

 [10/12] from: brett:codeconscious at: 20-Dec-2001 23:42


> What I'm currently doing is creating a graph-layouter in Rebol. You can
throw a
> simple graph-structure at it and it will create a niche layout for you. > Traversing such graphs can become quite complex.
I'd love to see the result of your work. Such a tool would be handy quite often. A collegue of mine some time ago did this sort of thing in Java and it was very handy to graphically display the dependencies between stored procedures in MS SqlServer. Same for a lot of things. What sort of "access requests" does your graph layout algorithm need to make on the graph data structure? Brett.

 [11/12] from: rotenca:telvia:it at: 20-Dec-2001 14:26


Hi, Robert
> Hi, :-)) as non native english speaker it's not so easy to describe a
problem
> that detailed, that the others can follow...
I can't speak well of my english, but yours seem good to me.
> > >> ob: context [name: "romano"] > > >> h: append append make hash! [] get in ob 'name in ob 'name
<<quoted lines omitted: 6>>
> ["dolores" name "romano" name ...] > Why the 'name entry? With such a hash! I could search for a name value and
get a
> 'name word that is linked to the address object, right?
yes. So you could retrieve the object with a: probe get bind 'self select h "dolores" ; if you are sure that self is well defined in the object and "dolores" in the hash
> Would it be possible to > use 'self instead of 'name? I used numbers as object index because these
don't
> change if reduced twice.
yes
>> h: append append make hash! [] get in ob 'name in ob 'self
== make hash! ["romano" self] but if you prefer, you could put a direct pointer to the object:
>> h: append append make hash! [] get in ob 'name get in ob 'self
== make hash! ["romano" make object! [ name: "romano" ]] --- Ciao Romano

 [12/12] from: robert:muench:robertmuench at: 20-Dec-2001 14:45


> -----Original Message----- > From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]On Behalf Of
<<quoted lines omitted: 4>>
> I'd love to see the result of your work. Such a tool would be handy quite > often.
Hi, yep that's why I do it now. I have looked into several graph-layout things. Most of them need to much housekeeping stuff around the layouters, others are fixed to a specific language I don't use/want to use...
> What sort of "access requests" does your graph layout algorithm need to make > on the graph data structure?
Very simple: You just have to provide a set of nodes and a set of edges, that's it at first. You will get a set of nodes with screen coordinates set. There are other things that might come up but I want to start simple and stupid. I hope to get feedback from all of you than and than the layouter thing can be tailored to the needs of the community. Robert

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted