Context:
Some code I found which implements a XOR linked list. In XOR linked list, instead of each node having a next pointer, it has a both
attribute which is the XOR of prev and next node.
import ctypes
# This is hacky. It's a data structure for C, not python.
class Node(object):
def __init__(self, val):
self.val = val
self.both = 0
class XorLinkedList(object):
def __init__(self):
self.head = self.tail = None
self.__nodes = [] # This is to prevent garbage collection
def add(self, node):
if self.head is None:
self.head = self.tail = node
else:
self.tail.both = id(node) ^ self.tail.both
node.both = id(self.tail)
self.tail = node
# Without this line, Python thinks there is no way to reach nodes between
# head and tail.
self.__nodes.append(node)
def get(self, index):
prev_id = 0
node = self.head
for i in range(index):
next_id = prev_id ^ node.both
if next_id:
prev_id = id(node)
node = _get_obj(next_id)
else:
raise IndexError('Linked list index out of range')
return node
def _get_obj(id):
return ctypes.cast(id, ctypes.py_object).value
Questions:
- Don't understand the need of
_get_obj()
function and what it is trying to do here? - How is
self.__nodes = []
useful? And how it implements garbage collection here? I have no idea what the following code is doing:
# Without this line, Python thinks there is no way to reach nodes between # head and tail. self.__nodes.append(node)`