ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
XorList.h File Reference

The XOR list of the discussion of chapter 3 of Morin's book, beyond the book: a doubly linked list whose cells keep one word of links instead of two, the exclusive or \( \mathit{prev} \oplus \mathit{next} \) of the addresses of their two neighbors (nil at the ends being 0), so that a walk that knows the cell it comes from finds the next one by an exclusive or, \( \mathit{next} = \mathit{link} \oplus \mathit{prev} \), in either direction. More...

#include "ADS/Contract.h"
#include "ADS/memory/ObjectPool.h"
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <iterator>
#include <type_traits>
#include <utility>
#include "ADS/sequence/impl/XorList.tpp"

Go to the source code of this file.

Classes

class  ADS::XorList< T >
 a doubly linked list with one word of links per cell, the exclusive or of the two neighbors More...
class  ADS::XorList< T >::Iterator< Const >
 the bidirectional iterator, the cell and the one before it, on items const if Const More...

Namespaces

namespace  ADS

Functions

template<std::unsigned_integral U>
constexpr void ADS::xorSwap (U &a, U &b)
 \( a \) and \( b \) swapped by three exclusive ors and no third variable (exercise 3.19 of Morin's book)

Detailed description

The XOR list of the discussion of chapter 3 of Morin's book, beyond the book: a doubly linked list whose cells keep one word of links instead of two, the exclusive or \( \mathit{prev} \oplus \mathit{next} \) of the addresses of their two neighbors (nil at the ends being 0), so that a walk that knows the cell it comes from finds the next one by an exclusive or, \( \mathit{next} = \mathit{link} \oplus \mathit{prev} \), in either direction.

A cell takes one pointer less than in a List [see List.h], and reverse() takes \( O( 1 ) \) time, by exchanging the first and the last cell, since the links read the same both ways. The price is that a cell alone is no position: an iterator holds the cell and the one before it, and no cell can be removed from the middle given only its address. The four operations of a double ended queue take \( O( 1 ) \) time; the cells come from an ObjectPool [see ObjectPool.h], as those of LinkedQueue. The addresses are turned into integers of the type std::uintptr_t and back, which C++ defines for the addresses of live objects.

The exclusive or that links the cells also swaps two integers without a third (exercise 3.19 of Morin's book): \( a \gets a \oplus b \), \( b \gets a \oplus b \), \( a \gets a \oplus b \) leaves \( b = ( a \oplus b ) \oplus b = a \) and then \( a = ( a \oplus b ) \oplus a = b \), since \( \oplus \) is associative and \( x \oplus x = 0 \); xorSwap() does it, and it fails (zeroes both) on two references to the same integer.

The contracts [see Contract.h]: the removals and front() and back() need a list that is not empty; the invariant is that the walk from the first cell ends at the last one after size() cells, an \( O( n ) \) check. The operations are defined in XorList.tpp.

See also
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 3.4 and exercise 3.19
P. Sinha, A memory-efficient doubly linked list, Linux Journal 129, 2004
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa