16#ifndef PSTSDK_UTIL_BTREE_H
17#define PSTSDK_UTIL_BTREE_H
21#include <boost/iterator/iterator_facade.hpp>
28#pragma warning(disable:4250)
34template<
typename K,
typename V>
35struct btree_iter_impl;
37template<
typename K,
typename V>
38class const_btree_node_iter;
40template<
typename K,
typename V>
41class btree_node_nonleaf;
50template<
typename K,
typename V>
64 virtual const V&
lookup(
const K& key)
const = 0;
130template<
typename K,
typename V>
170template<
typename K,
typename V>
207template<
typename K,
typename V>
213 std::vector<std::pair<btree_node_nonleaf<K,V>*,
uint> >
m_path;
214 typedef typename std::vector<std::pair<btree_node_nonleaf<K,V>*,
uint> >::iterator
path_iter;
227template<
typename K,
typename V>
228class const_btree_node_iter :
public boost::iterator_facade<const_btree_node_iter<K,V>, const V, boost::bidirectional_traversal_tag>
243 void increment() { m_impl.m_leaf->next(m_impl); }
248 {
return ((m_impl.m_leaf ==
other.m_impl.m_leaf) && (m_impl.m_leaf_pos ==
other.m_impl.m_leaf_pos)); }
252 const V& dereference()
const
253 {
return m_impl.m_leaf->get_value(m_impl.m_leaf_pos); }
256 void decrement() { m_impl.m_leaf->prev(m_impl); }
258 mutable btree_iter_impl<K,V> m_impl;
263template<
typename K,
typename V>
291template<
typename K,
typename V>
294 int location = this->binary_search(
k);
299 if(this->get_key(location) !=
k)
302 return get_value(location);
305template<
typename K,
typename V>
310 if(
iter.m_path.size() > 0)
316 if((*piter).second + 1 < (*piter).first->num_values())
329template<
typename K,
typename V>
332 if(
iter.m_leaf_pos == 0)
334 if(
iter.m_path.size() > 0)
343 if((*piter).second != 0 && (*piter).first->num_values() > 1)
357template<
typename K,
typename V>
360 int location = this->binary_search(
k);
365 return get_child(location)->lookup(
k);
368template<
typename K,
typename V>
375template<
typename K,
typename V>
379 get_child(this->num_values()-1)->
last(
iter);
382template<
typename K,
typename V>
385 std::pair<btree_node_nonleaf<K,V>*,
uint>&
me =
iter.m_path.back();
390 if(
iter.m_path.size() > 1)
392 iter.m_path.pop_back();
403template<
typename K,
typename V>
406 std::pair<btree_node_nonleaf<K,V>*,
uint>&
me =
iter.m_path.back();
411 if(
iter.m_path.size() > 1)
413 iter.m_path.pop_back();
424template<
typename K,
typename V>
427 m_impl.m_leaf_pos = 0;
428 m_impl.m_leaf =
NULL;
431template<
typename K,
typename V>
Contains references to other bth_node allocations.
const K & get_key(uint pos) const
Returns the key at the specified position.
uint num_values() const
Returns the number of entries in this btree_node.
Represents a leaf node in a BTree structure.
virtual const V & get_value(uint pos) const =0
Returns the value at the associated position on this leaf node.
void first(btree_iter_impl< K, V > &iter) const
Positions the iterator at the first element on this tree.
void last(btree_iter_impl< K, V > &iter) const
Positions the iterator at the "end" element.
void next(btree_iter_impl< K, V > &iter) const
Moves the iterator to the next element.
virtual ~btree_node_leaf()
void prev(btree_iter_impl< K, V > &iter) const
Moves the iterator to the previous element.
const V & lookup(const K &key) const
Looks up the associated value for a given key.
Represents a non-leaf node in a BTree structure.
virtual btree_node< K, V > * get_child(uint i)=0
Returns the child btree_node at the requested location.
const V & lookup(const K &key) const
Looks up the associated value for a given key.
virtual ~btree_node_nonleaf()
void next(btree_iter_impl< K, V > &iter) const
Moves the iterator to the next element.
void prev(btree_iter_impl< K, V > &iter) const
Moves the iterator to the previous element.
void first(btree_iter_impl< K, V > &iter) const
Positions the iterator at the first element on this tree.
void last(btree_iter_impl< K, V > &iter) const
Positions the iterator at the "end" element.
virtual void next(btree_iter_impl< K, V > &iter) const =0
Moves the iterator to the next element.
virtual void last(btree_iter_impl< K, V > &iter) const =0
Positions the iterator at the "end" element.
virtual uint num_values() const =0
Returns the number of entries in this btree_node.
const_iterator end() const
Returns a STL style iterator positioned at the "end" entry.
const_btree_node_iter< K, V > const_iterator
virtual void first(btree_iter_impl< K, V > &iter) const =0
Positions the iterator at the first element on this tree.
const_iterator begin() const
Returns a STL style iterator positioned at the first entry.
virtual void prev(btree_iter_impl< K, V > &iter) const =0
Moves the iterator to the previous element.
virtual const K & get_key(uint pos) const =0
Returns the key at the specified position.
virtual const V & lookup(const K &key) const =0
Looks up the associated value for a given key.
int binary_search(const K &key) const
Performs a binary search over the keys of this btree_node.
The actual iterator type used by the btree_node class hierarchy.
friend class boost::iterator_core_access
const_btree_node_iter()
Default constructor.
The exceptions used by pstsdk.
Contains the definition of all in memory representations of disk structures.
Primitive structures defined by MS-PST and MAPI.
BTree iterator helper class.
btree_node_leaf< K, V > * m_leaf
The current leaf btree node this iterator is pointing to.
uint m_leaf_pos
The current position on that leaf.
std::vector< std::pair< btree_node_nonleaf< K, V > *, uint > > m_path
The "path" to this leaf, starting at the root of the btree.
std::vector< std::pair< btree_node_nonleaf< K, V > *, uint > >::iterator path_iter