/* *************************************************************************** * Copyright (C) 2007 Jason House * * * * This file is part of HouseBot. * * * * HouseBot is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 3 of the License, or * * (at your option) any later version. * * * * HouseBot is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * * * *****************************************************************************/ /// Provides transposition table class module search.transposition; private{ import tangoBind/*: bind*/; alias void delegate(Object) DisposeEvent; extern(C) void rt_attachDisposeEvent(Object obj, DisposeEvent event); extern(C) void rt_detachDisposeEvent(Object obj, DisposeEvent event); typedef size_t weakPointer; struct valueHolder(content_t){ ulong key; weakPointer value; transpositionTable!(content_t) parent; void remove_(Object obj){ parent.lookup.remove(key); } } } class transpositionTable(content_t){ private: valueHolder!(content_t)[ulong] lookup; content_t* castRealPointer(weakPointer p){ return cast(content_t*)cast(void*)p; } weakPointer castWeakPointer(content_t* p){ return cast(weakPointer)cast(void*)p; } public: content_t* opIn(ulong key){ valueHolder!(content_t)* ptr_ptr = (key in lookup); if (ptr_ptr is null) return null; else return castRealPointer((*ptr_ptr).value); } content_t opIndex(ulong key){ return *castRealPointer(lookup[key].value); } void opIndexAssign(content_t value, ulong key){ lookup[key] = valueHolder!(content_t)(key,castWeakPointer(&value),this); rt_attachDisposeEvent(value, &lookup[key].remove_); } void remove(ulong key){ rt_detachDisposeEvent(*castRealPointer(lookup[key].value), &lookup[key].remove_); lookup.remove(key); } }