12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118 |
|
/*******************************************************************************
copyright: Copyright (c) 2008 Kris Bell. All rights reserved
license: BSD style: $(LICENSE)
version: Apr 2008: Initial release
authors: Kris
Since: 0.99.7
Based upon Doug Lea's Java collection package
*******************************************************************************/
module tango.util.container.SortedMap;
public import tango.util.container.Container;
private import tango.util.container.RedBlack;
private import tango.util.container.model.IContainer;
private import tango.core.Exception : NoSuchElementException;
/*******************************************************************************
RedBlack trees of (key, value) pairs
---
Iterator iterator (bool forward)
Iterator iterator (K key, bool forward)
int opApply (int delegate (ref V value) dg)
int opApply (int delegate (ref K key, ref V value) dg)
bool contains (V value)
bool containsKey (K key)
bool containsPair (K key, V value)
bool keyOf (V value, ref K key)
bool get (K key, ref V value)
bool take (ref V v)
bool take (K key, ref V v)
bool removeKey (K key)
size_t remove (V value, bool all)
size_t remove (IContainer!(V) e, bool all)
bool add (K key, V value)
size_t replace (V oldElement, V newElement, bool all)
bool replacePair (K key, V oldElement, V newElement)
bool opIndexAssign (V element, K key)
K nearbyKey (K key, bool greater)
V opIndex (K key)
V* opIn_r (K key)
size_t size ()
bool isEmpty ()
V[] toArray (V[] dst)
SortedMap dup ()
SortedMap clear ()
SortedMap reset ()
SortedMap comparator (Comparator c)
---
*******************************************************************************/
class SortedMap (K, V, alias Reap = Container.reap,
alias Heap = Container.DefaultCollect)
: IContainer!(V)
{
// use this type for Allocator configuration
public alias RedBlack!(K, V) Type;
private alias Type *Ref;
private alias Heap!(Type) Alloc;
private alias Compare!(K) Comparator;
// root of the tree. Null if empty.
package Ref tree;
// configured heap manager
private Alloc heap;
// Comparators used for ordering
private Comparator cmp;
private Compare!(V) cmpElem;
private size_t count,
mutation;
/***********************************************************************
Make an empty tree, using given Comparator for ordering
***********************************************************************/
public this (Comparator c = null)
{
this (c, 0);
}
/***********************************************************************
Special version of constructor needed by dup()
***********************************************************************/
private this (Comparator c, size_t n)
{
count = n;
cmpElem = &compareElem;
cmp = (c is null) ? &compareKey : c;
}
/***********************************************************************
Clean up when deleted
***********************************************************************/
~this ()
{
reset;
}
/***********************************************************************
Return the configured allocator
***********************************************************************/
final Alloc allocator ()
{
return heap;
}
/***********************************************************************
Return a generic iterator for contained elements
***********************************************************************/
final Iterator iterator (bool forward = true)
{
Iterator i = void;
i.node = count ? (forward ? tree.leftmost : tree.rightmost) : null;
i.bump = forward ? &Iterator.fore : &Iterator.back;
i.mutation = mutation;
i.owner = this;
i.prior = null;
return i;
}
/***********************************************************************
Return an iterator which return all elements matching
or greater/lesser than the key in argument. The second
argument dictates traversal direction.
Return a generic iterator for contained elements
***********************************************************************/
final Iterator iterator (K key, bool forward)
{
Iterator i = iterator (forward);
i.node = count ? tree.findFirst(key, cmp, forward) : null;
return i;
}
/***********************************************************************
Return the number of elements contained
***********************************************************************/
final size_t size ()
{
return count;
}
/***********************************************************************
Create an independent copy. Does not clone elements
***********************************************************************/
final SortedMap dup ()
{
auto clone = new SortedMap!(K, V, Reap, Heap) (cmp, count);
if (count)
clone.tree = tree.copyTree (&clone.heap.allocate);
return clone;
}
/***********************************************************************
Time complexity: O(log n)
***********************************************************************/
final bool contains (V value)
{
if (count is 0)
return false;
return tree.findAttribute (value, cmpElem) !is null;
}
/***********************************************************************
***********************************************************************/
final int opApply (int delegate (ref V value) dg)
{
return iterator.opApply ((ref K k, ref V v) {return dg(v);});
}
/***********************************************************************
***********************************************************************/
final int opApply (int delegate (ref K key, ref V value) dg)
{
return iterator.opApply (dg);
}
/***********************************************************************
Use a new Comparator. Causes a reorganization
***********************************************************************/
final SortedMap comparator (Comparator c)
{
if (cmp !is c)
{
cmp = (c is null) ? &compareKey : c;
if (count !is 0)
{
// must rebuild tree!
mutate;
auto t = tree.leftmost;
tree = null;
count = 0;
while (t)
{
add_ (t.value, t.attribute, false);
t = t.successor;
}
}
}
return this;
}
/***********************************************************************
Time complexity: O(log n)
***********************************************************************/
final bool containsKey (K key)
{
if (count is 0)
return false;
return tree.find (key, cmp) !is null;
}
/***********************************************************************
Time complexity: O(n)
***********************************************************************/
final bool containsPair (K key, V value)
{
if (count is 0)
return false;
return tree.find (key, value, cmp) !is null;
}
/***********************************************************************
Return the value associated with Key key.
param: key a key
Returns: whether the key is contained or not
***********************************************************************/
final bool get (K key, ref V value)
{
if (count)
{
auto p = tree.find (key, cmp);
if (p)
{
value = p.attribute;
return true;
}
}
return false;
}
/***********************************************************************
Return the value of the key exactly matching the provided
key or, if none, the key just after/before it based on the
setting of the second argument
param: key a key
param: after indicates whether to look beyond or before
the given key, where there is no exact match
throws: NoSuchElementException if none found
returns: a pointer to the value, or null if not present
***********************************************************************/
K nearbyKey (K key, bool after)
{
if (count)
{
auto p = tree.findFirst (key, cmp, after);
if (p)
return p.value;
}
noSuchElement ("no such key");
assert (0);
}
/***********************************************************************
Return the first key of the map
throws: NoSuchElementException where the map is empty
***********************************************************************/
K firstKey ()
{
if (count)
return tree.leftmost.value;
noSuchElement ("no such key");
assert (0);
}
/***********************************************************************
Return the last key of the map
throws: NoSuchElementException where the map is empty
***********************************************************************/
K lastKey ()
{
if (count)
return tree.rightmost.value;
noSuchElement ("no such key");
assert (0);
}
/***********************************************************************
Return the value associated with Key key.
param: key a key
Returns: a pointer to the value, or null if not present
***********************************************************************/
final V* opIn_r (K key)
{
if (count)
{
auto p = tree.find (key, cmp);
if (p)
return &p.attribute;
}
return null;
}
/***********************************************************************
Time complexity: O(n)
***********************************************************************/
final bool keyOf (V value, ref K key)
{
if (count is 0)
return false;
auto p = tree.findAttribute (value, cmpElem);
if (p is null)
return false;
key = p.value;
return true;
}
/***********************************************************************
Time complexity: O(n)
***********************************************************************/
final SortedMap clear ()
{
return clear (false);
}
/***********************************************************************
Reset the SortedMap contents. This releases more memory
than clear() does
Time complexity: O(n)
***********************************************************************/
final SortedMap reset ()
{
return clear (true);
}
/***********************************************************************
************************************************************************/
final size_t remove (IContainer!(V) e, bool all)
{
auto c = count;
foreach (v; e)
remove (v, all);
return c - count;
}
/***********************************************************************
Time complexity: O(n
***********************************************************************/
final size_t remove (V value, bool all = false)
{
size_t i = count;
if (count)
{
auto p = tree.findAttribute (value, cmpElem);
while (p)
{
tree = p.remove (tree);
decrement (p);
if (!all || count is 0)
break;
p = tree.findAttribute (value, cmpElem);
}
}
return i - count;
}
/***********************************************************************
Time complexity: O(n)
***********************************************************************/
final size_t replace (V oldElement, V newElement, bool all = false)
{
size_t c;
if (count)
{
auto p = tree.findAttribute (oldElement, cmpElem);
while (p)
{
++c;
mutate;
p.attribute = newElement;
if (!all)
break;
p = tree.findAttribute (oldElement, cmpElem);
}
}
return c;
}
/***********************************************************************
Time complexity: O(log n)
Takes the value associated with the least key.
***********************************************************************/
final bool take (ref V v)
{
if (count)
{
auto p = tree.leftmost;
v = p.attribute;
tree = p.remove (tree);
decrement (p);
return true;
}
return false;
}
/***********************************************************************
Time complexity: O(log n)
***********************************************************************/
final bool take (K key, ref V value)
{
if (count)
{
auto p = tree.find (key, cmp);
if (p)
{
value = p.attribute;
tree = p.remove (tree);
decrement (p);
return true;
}
}
return false;
}
/***********************************************************************
Time complexity: O(log n)
Returns true if inserted, false where an existing key
exists and was updated instead
***********************************************************************/
final bool add (K key, V value)
{
return add_ (key, value, true);
}
/***********************************************************************
Time complexity: O(log n)
Returns true if inserted, false where an existing key
exists and was updated instead
***********************************************************************/
final bool opIndexAssign (V element, K key)
{
return add (key, element);
}
/***********************************************************************
Operator retreival function
Throws NoSuchElementException where key is missing
***********************************************************************/
final V opIndex (K key)
{
auto p = opIn_r (key);
if (p)
return *p;
noSuchElement ("missing or invalid key");
assert (0);
}
/***********************************************************************
Time complexity: O(log n)
***********************************************************************/
final bool removeKey (K key)
{
V value;
return take (key, value);
}
/***********************************************************************
Time complexity: O(log n)
***********************************************************************/
final bool replacePair (K key, V oldElement, V newElement)
{
if (count)
{
auto p = tree.find (key, oldElement, cmp);
if (p)
{
p.attribute = newElement;
mutate;
return true;
}
}
return false;
}
/***********************************************************************
Copy and return the contained set of values in an array,
using the optional dst as a recipient (which is resized
as necessary).
Returns a slice of dst representing the container values.
Time complexity: O(n)
***********************************************************************/
final V[] toArray (V[] dst = null)
{
if (dst.length < count)
dst.length = count;
size_t i = 0;
foreach (k, v; this)
dst[i++] = v;
return dst [0 .. count];
}
/***********************************************************************
Is this container empty?
Time complexity: O(1)
***********************************************************************/
final bool isEmpty ()
{
return count is 0;
}
/***********************************************************************
***********************************************************************/
final SortedMap check ()
{
assert(cmp !is null);
assert(((count is 0) is (tree is null)));
assert((tree is null || tree.size() is count));
if (tree)
{
tree.checkImplementation;
auto t = tree.leftmost;
K last = K.init;
while (t)
{
auto v = t.value;
assert((last is K.init || cmp(last, v) <= 0));
last = v;
t = t.successor;
}
}
return this;
}
/***********************************************************************
***********************************************************************/
private void noSuchElement (char[] msg)
{
throw new NoSuchElementException (msg);
}
/***********************************************************************
Time complexity: O(log n)
***********************************************************************/
private size_t instances (V value)
{
if (count is 0)
return 0;
return tree.countAttribute (value, cmpElem);
}
/***********************************************************************
Returns true where an element is added, false where an
existing key is found
***********************************************************************/
private final bool add_ (K key, V value, bool checkOccurrence)
{
if (tree is null)
{
tree = heap.allocate.set (key, value);
increment;
}
else
{
auto t = tree;
for (;;)
{
int diff = cmp (key, t.value);
if (diff is 0 && checkOccurrence)
{
if (t.attribute != value)
{
t.attribute = value;
mutate;
}
return false;
}
else
if (diff <= 0)
{
if (t.left)
t = t.left;
else
{
tree = t.insertLeft (heap.allocate.set(key, value), tree);
increment;
break;
}
}
else
{
if (t.right)
t = t.right;
else
{
tree = t.insertRight (heap.allocate.set(key, value), tree);
increment;
break;
}
}
}
}
return true;
}
/***********************************************************************
Time complexity: O(n)
***********************************************************************/
private SortedMap clear (bool all)
{
mutate;
// collect each node if we can't collect all at once
if (heap.collect(all) is false & count)
{
auto node = tree.leftmost;
while (node)
{
auto next = node.successor;
decrement (node);
node = next;
}
}
count = 0;
tree = null;
return this;
}
/***********************************************************************
Time complexity: O(log n)
***********************************************************************/
private void remove (Ref node)
{
tree = node.remove (tree);
decrement (node);
}
/***********************************************************************
new element was added
***********************************************************************/
private void increment ()
{
++mutation;
++count;
}
/***********************************************************************
element was removed
***********************************************************************/
private void decrement (Ref p)
{
Reap (p.value, p.attribute);
heap.collect (p);
++mutation;
--count;
}
/***********************************************************************
set was changed
***********************************************************************/
private void mutate ()
{
++mutation;
}
/***********************************************************************
The default key comparator
@param fst first argument
@param snd second argument
Returns: a negative number if fst is less than snd; a
positive number if fst is greater than snd; else 0
***********************************************************************/
private static int compareKey (ref K fst, ref K snd)
{
if (fst is snd)
return 0;
return typeid(K).compare (&fst, &snd);
}
/***********************************************************************
The default value comparator
@param fst first argument
@param snd second argument
Returns: a negative number if fst is less than snd; a
positive number if fst is greater than snd; else 0
***********************************************************************/
private static int compareElem(ref V fst, ref V snd)
{
if (fst is snd)
return 0;
return typeid(V).compare (&fst, &snd);
}
/***********************************************************************
Iterator with no filtering
***********************************************************************/
private struct Iterator
{
Ref function(Ref) bump;
Ref node,
prior;
SortedMap owner;
size_t mutation;
/***************************************************************
Did the container change underneath us?
***************************************************************/
bool valid ()
{
return owner.mutation is mutation;
}
/***************************************************************
Accesses the next value, and returns false when
there are no further values to traverse
***************************************************************/
bool next (ref K k, ref V v)
{
auto n = next (k);
return (n) ? v = *n, true : false;
}
/***************************************************************
Return a pointer to the next value, or null when
there are no further values to traverse
***************************************************************/
V* next (ref K k)
{
V* r;
if (node)
{
prior = node;
k = node.value;
r = &node.attribute;
node = bump (node);
}
return r;
}
/***************************************************************
Foreach support
***************************************************************/
int opApply (int delegate(ref K key, ref V value) dg)
{
int result;
auto n = node;
while (n)
{
prior = n;
auto next = bump (n);
if ((result = dg(n.value, n.attribute)) != 0)
break;
n = next;
}
node = n;
return result;
}
/***************************************************************
Remove value at the current iterator location
***************************************************************/
bool remove ()
{
if (prior)
{
owner.remove (prior);
// ignore this change
++mutation;
return true;
}
prior = null;
return false;
}
/***************************************************************
***************************************************************/
Iterator reverse ()
{
if (bump is &fore)
bump = &back;
else
bump = &fore;
return *this;
}
/***************************************************************
***************************************************************/
private static Ref fore (Ref p)
{
return p.successor;
}
/***************************************************************
***************************************************************/
private static Ref back (Ref p)
{
return p.predecessor;
}
}
}
/*******************************************************************************
*******************************************************************************/
debug (SortedMap)
{
import tango.io.Stdout;
import tango.core.Thread;
import tango.time.StopWatch;
import tango.math.random.Kiss;
void main()
{
// usage examples ...
auto map = new SortedMap!(char[], int);
map.add ("foo", 1);
map.add ("bar", 2);
map.add ("wumpus", 3);
// implicit generic iteration
foreach (key, value; map)
Stdout.formatln ("{}:{}", key, value);
// explicit iteration
foreach (key, value; map.iterator("foo", false))
Stdout.formatln ("{}:{}", key, value);
// generic iteration with optional remove
auto s = map.iterator;
foreach (key, value; s)
{} // s.remove;
// incremental iteration, with optional remove
char[] k;
int v;
auto iterator = map.iterator;
while (iterator.next(k, v))
{} //iterator.remove;
// incremental iteration, with optional failfast
auto it = map.iterator;
while (it.valid && it.next(k, v))
{}
// remove specific element
map.removeKey ("wumpus");
// remove first element ...
while (map.take(v))
Stdout.formatln ("taking {}, {} left", v, map.size);
// setup for benchmark, with a set of integers. We
// use a chunk allocator, and presize the bucket[]
auto test = new SortedMap!(int, int, Container.reap, Container.Chunk);
test.allocator.config (1000, 500);
const count = 500_000;
StopWatch w;
auto keys = new int[count];
foreach (ref vv; keys)
vv = Kiss.instance.toInt(int.max);
// benchmark adding
w.start;
for (int i=count; i--;)
test.add(keys[i], i);
Stdout.formatln ("{} adds: {}/s", test.size, test.size/w.stop);
// benchmark reading
w.start;
for (int i=count; i--;)
test.get(keys[i], v);
Stdout.formatln ("{} lookups: {}/s", test.size, test.size/w.stop);
// benchmark adding without allocation overhead
test.clear;
w.start;
for (int i=count; i--;)
test.add(keys[i], i);
Stdout.formatln ("{} adds (after clear): {}/s", test.size, test.size/w.stop);
// benchmark duplication
w.start;
auto dup = test.dup;
Stdout.formatln ("{} element dup: {}/s", dup.size, dup.size/w.stop);
// benchmark iteration
w.start;
foreach (key, value; test) {}
Stdout.formatln ("{} element iteration: {}/s", test.size, test.size/w.stop);
test.check;
}
}
|