Address-Of operator equivalent, PAVL search tree

C

castironpi

Hi,

I am translating the GNU library's PAVL search tree implementation
into Python. I can't use it directly because a delete function I need
uses a different method of finding the node to delete.

It contains this line:

q = (struct pavl_node *) &tree->pavl_root;

line 276 in http://www.sfr-fresh.com/unix/misc/avl-2.0.3.tar.gz:a/avl-2.0.3/pavl.c
for definition

struct pavl_node *q; /* Parent of |p|. */

I want to know the equivalent in Python. I observe that

q = &tree->pavl_root;

is a type error and

q = tree->pavl_root;

gives wrong results. Is there any way I can use the existing code, or
will it require heavy modification? It is possible to wrap it in a
DLL, (which I am considering for other reasons too), but I would
prefer a pure Python implementation.

Thanks in advance.
 
C

castironpi

Hi,

I am translating the GNU library's PAVL search tree implementation
into Python.  I can't use it directly because a delete function I need
uses a different method of finding the node to delete.

It contains this line:

      q = (struct pavl_node *) &tree->pavl_root;

line 276 in http://www.sfr-fresh.com/unix/misc/avl-2.0.3.tar.gz:a/avl-2.0..3/pavl.c

It seems the only property that's accessed is q->pavl_link[ 0 ], done
in order to generically set the tree's root along with other nodes. I
will use a proxy class to reroute set operations on q.link[ 0 ] to
tree.root. It is also accessible via q.left. Here is a tentative
implementation:

class RootProxy:
__slots__= '_tree'
class RootLink:
__slots__= '_tree'
def __init__( self, tree ):
self._tree= tree
def __getitem__( self, key ):
assert key== 0
return self._tree.root
def __setitem__( self, key, val ):
assert key== 0
self._tree.root= val
def __init__( self, tree ):
self._tree= tree
def _getleft( self ):
return self._tree.root
def _setleft( self, val ):
self._tree.root= val
left= property( _getleft, _setleft )
def _getlink( self ):
return self.RootLink( self._tree )
link= property( _getlink )

The line then becomes:
q = (struct pavl_node *) &tree->pavl_root;
q= RootProxy( tree )

Thanks for your attention. Any questions or comments please share.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top