[Data Structure] XOR LinkedList
An XOR linked list is a type of double-linked list that uses bitwise XOR operation to store address of the previous and next address in one space (thus, reducing storage space).
In the traditional double-linked list, each node has to store a prev and next address field.
A B C D
.. n->n->n ..
.. p<-p<-p ..
An XOR linked list combines the same information into 1 address field via bitwise XOR (denoted by ⊕) of the prev and next address.
A B C D E
.. p⊕n p⊕n p⊕n ..
Getting the next address space
When traversing from left to right, suppose the cursor is at C, the address of previous item B can be XORed with the value of linked field of C (B⊕D) to get the address for D.
ie. addr(D) = link(C) ⊕ addr(B)
where
link(C) = addr(B) ⊕ addr(D)
so
so
addr(D) = addr(B) ⊕ addr(D) ⊕ addr(B)
since
X⊕X = 0
=> addr(D) = 0 ⊕ addr(D)
since
X⊕0 = X
=> addr(D) = addr(D)
The XOR operation cancels addr(B) appearing twice in the equation and all we are left is the addr(D)
Properties of XOR:
- X⊕X = 0
- X⊕0 = X
- X⊕Y = Y⊕X
- (X⊕Y)⊕Z = X⊕(Y⊕Z)
Drawbacks
- General-purpose debugging tools cannot follow the XOR chain, making debugging more difficult.
- Price for decrease in memory usage is an increase in code complexity; making maintenance expensive
- Most garbage collection schemes do not work with data structures that do not contain literal pointers.
- Computer systems have increasingly cheap and plentiful memory, therefore storage overhead is not generally a problem.
Comments
Post a Comment