/* * Copyright (C) 2004-2005 Vadim Berezniker * http://www.kryptolus.com * * This Program 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 2, or (at your option) * any later version. * * This Program 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 GNU Make; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * http://www.gnu.org/copyleft/gpl.html * */ #include "stdafx.h" #include "common.h" /* * This is just a wrapper around the GHashTable except this class allows easy iteration of keys in the hash. * GHashTable only makes it possible through a callback, which I find a bit annoying. * * NOTE: Because this is just a wrapper class around GHashTable, only pointers can be stored inside. */ /* * Creates a new hash with the given hashing and comparison functions. * See glib documentation for GHashTable for more info. */ template kryHash::kryHash(GHashFunc hash_func, GEqualFunc key_equal_func) { this->m_hash = g_hash_table_new_full(hash_func, key_equal_func, NULL, NULL); this->m_equalFunc = key_equal_func; } /* * Creates a new hash with the given hashing and comparison functions and the given key/value destroy functions. * See glib documentation for GHashTable for more info. */ template kryHash::kryHash(GHashFunc hash_func, GEqualFunc key_equal_func, GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func) { this->m_hash = g_hash_table_new_full(hash_func, key_equal_func, key_destroy_func, value_destroy_func); this->m_equalFunc = key_equal_func; } /* * Destroys the data stored in the hash. * If key and/or value destroy functions were specified they are called on each key/value. */ template kryHash::~kryHash() { g_hash_table_destroy(this->m_hash); } /* * Returns an Iterator for the keys inside the hash. */ template void kryHash::GetKeysIterator(kryListIterator *iter) { this->m_keys.GetIterator(iter); } /* * Returns the number of keys in the hash. */ template int kryHash::GetKeyCount() { return this->m_keys.GetLength(); } /* * Looks up the given key and returns any associated data. * * Returns NULL if the key was not found. */ template V kryHash::Lookup(T key) { return (V) (long) g_hash_table_lookup(this->m_hash, (gconstpointer) key); } /* * Looks up a key and removes any associated data. */ template void kryHash::Remove(T key) { /* * Remove the key from the list of keys if it exists there * We must remove it from this list first because the key might * get destroyed upon removal from the hash */ if(g_hash_table_lookup(this->m_hash, (gconstpointer) key)) this->m_keys.Remove(key, this->m_equalFunc); g_hash_table_remove(this->m_hash, (gconstpointer) key); } /* * Adds a key/value pair to the hash. * If the key is already in the hash, both the key and the value within the hash are replaced * with the given ones. */ template void kryHash::Replace(T key, V value) { /* If it's a new key, we must add it to our keys list */ if(!this->Lookup(key)) { this->m_keys.Append(key); } /* If it's not, we replace the stored key with the given one. */ else { this->m_keys.Remove(key, this->m_equalFunc); this->m_keys.Append(key); } g_hash_table_replace(this->m_hash, (gpointer) key, (gpointer) value); } /* * Inserts a key/value pair into the hash. * If the key is already in the hash, the value is replaced with the given value. * The key is not replaced. */ template void kryHash::Insert(T key, V value) { /* If this is a new key, we must add it to our keys list */ if(!this->Lookup(key)) this->m_keys.Append(key); g_hash_table_insert(this->m_hash, (gpointer) key, (gpointer) value); } template class kryHash; template class kryHash; template class kryHash; template class kryHash; template class kryHash; template class kryHash; template class kryHash; template class kryHash; template class kryHash;