Shop OBEX P1 Docs P2 Docs Learn Events
Searching an array of objects — Parallax Forums

Searching an array of objects

TLTL Posts: 25
edited 2008-01-23 18:29 in Propeller 1
I have an object called "Message." Each instance of Message contains multiple variables - an ID#, some data, a string.

Messenger.spin

VAR

  long _ID         'ID associated with the message
  long _data      'Data block associated with the message 
  byte _str[noparse][[/noparse]32]  'String of text message associated with this ID

DAT
  arraysize long 1 

PUB setsize(myarraysize)
   if myarraysize > 255
       return -1
   longmove(@arraysize,@myarraysize,1)

PUB set(myID,mydata,mystrptr)   
'EXAMPLE CALL:  mymsg.set(1,2,@somestring) OR mymsg.set(1,2,string("Some text"))  

  _ID   := myID
  _data := mydata
  strcopy(@_str,mystrptr)

etc





In the calling routine, I declare an array of "message" as follows:


Another_program.spin

CON
  MSG_NUM = 4  'Allow up to 4 messages in array

OBJ
  message[noparse][[/noparse]MSG_NUM]: "Messenger"

PUB Main  | i

   message.setsize(MSG_NUM)  'set size of array for object
 
   repeat i from 0 to 3
      message[noparse][[/noparse] i ].set (i,i*2, string("This is a test"))





So what I want to do is have a method that does this:

     myindex := message.find(myID)




So how would I do that? I have the array size stored in a DAT field, and I can see how to access the array elements (ID, Data, etc) using low-level byte offsets, given a pointer to the array. But is there a way to do this using higher level routines? I guess I'm asking is there is any equivalent to the "this" object in other object-oriented languages.

-TL

(Darn, I keep finding more typos to fix...)

Post Edited (TL) : 1/22/2008 10:46:40 PM GMT

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2008-01-22 23:04
    There is very little you can do that would not look extremely clumsy.. The static character of what SPIN calls object will conterfight most of your strive for OO

    You can generally use the DAT section as class attribute space (as you already do), and allocate an array associating IDs to addresses... This is not so good as you have to know in advance how large this must be, and there is no way to send compile time constants "downstream" from the main object....

    You can however create a linked chain using one cell in object space... This chain has always to be searched when doing something like "find".

    VAR
      LONG chain, _ID, _DATA  ...
    
    
    DAT
     chainEnd  LONG 0 
    ...
    
    PUB set
       IFNOT chain
          chain:= chainEnd
          chainEnd := @chain
       
    ...
    
    PUB find_data(id):  theChain
        theChain := endChain
         REPEAT WHILE theChain
              IF LONG[noparse][[/noparse]theChain][noparse][[/noparse] 1] == id      ' painstakingly count the locations of your "structure"
                  return @LONG[noparse][[/noparse]theChain][noparse][[/noparse] 2]  ' painstakingly count the locations of your "structure"
              theChain := LONG[noparse][[/noparse]theChain]
    
    






    PUB find_str(id)
    ... similiar...
  • TLTL Posts: 25
    edited 2008-01-23 01:16
    So what you're saying is that I can make a singly-linked list by saving the address of the next cell in each cell.

    It's doable but inflates my array by one LONG per cell. No biggie.

    I'm having trouble following your code though. It's very dense. (I'm sure you're aware that's a 'C' compliment. As opposed to saying the programmer is dense...)

    I tried doing it with byte offsets and discovered (ha!) that I couldn't get the base address of the object array. This actually makes sense, now that I think of it, because although the VAR elements have a base address, there are also a common DAT and code elements in the object. Hmmm...

    All kind of "yooglie."

    -TL
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-01-23 01:26
    Can't you use a pseudo-id which is in fact the index
    in the array? Using the pseudo-id you can grab
    the ID# and its message directly.

    regards peter
  • TLTL Posts: 25
    edited 2008-01-23 03:28
    Peter-
    If I understand what you're suggesting, it wouldn't let you do the search with an object method.

    Unless someone more experienced can show me otherwise, it appears that you can't access an object array from within that object.

    -TL
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-23 18:29
    TL said...
    it appears that you can't access an object array from within that object.

    It does not only appear so smile.gif
    Despite the fancy words you use ("object array") this is something quite different as you have in true OO languages.
    SPIN has no datastructures, thus there is nothing you can access. SPIN also has only local access to object attributes in the first place

    So you have to think of workarounds, based on the fact that only pointers to memory cells can be freely used throughout all "objects".
Sign In or Register to comment.