2009-02-17 15:02:16 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* of the License, 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 this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
2009-02-16 09:23:58 +00:00
|
|
|
|
2009-02-24 02:59:50 +00:00
|
|
|
#include "sci/engine/intmap.h"
|
2009-02-16 09:23:58 +00:00
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
namespace Sci {
|
2009-02-16 09:23:58 +00:00
|
|
|
|
|
|
|
#define HASH(x) (x & 0xff)
|
|
|
|
|
2009-02-24 02:59:50 +00:00
|
|
|
IntMapper::IntMapper() {
|
2009-02-16 09:40:25 +00:00
|
|
|
base_value = 0;
|
|
|
|
memset(nodes, 0, sizeof(nodes));
|
|
|
|
holes = 0;
|
2009-02-16 09:23:58 +00:00
|
|
|
}
|
|
|
|
|
2009-02-24 02:59:50 +00:00
|
|
|
void IntMapper::free_node_recursive(Node *node) {
|
2009-02-16 09:23:58 +00:00
|
|
|
if (node) {
|
2009-02-16 09:40:25 +00:00
|
|
|
free_node_recursive(node->next);
|
2009-02-24 02:59:50 +00:00
|
|
|
node->next = 0;
|
2009-02-16 09:23:58 +00:00
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-24 02:59:50 +00:00
|
|
|
IntMapper::~IntMapper() {
|
2009-02-16 09:23:58 +00:00
|
|
|
int i;
|
|
|
|
|
2009-02-24 02:59:50 +00:00
|
|
|
for (i = 0; i < DCS_INT_HASH_MAX; i++)
|
2009-02-16 09:40:25 +00:00
|
|
|
free_node_recursive(nodes[i]);
|
2009-02-16 09:23:58 +00:00
|
|
|
|
2009-02-16 09:40:25 +00:00
|
|
|
free_node_recursive(holes);
|
2009-02-16 09:23:58 +00:00
|
|
|
|
2009-02-16 09:40:25 +00:00
|
|
|
// Trigger problems for people who forget to loose the reference
|
|
|
|
base_value = -42000;
|
2009-02-16 09:23:58 +00:00
|
|
|
}
|
|
|
|
|
2009-02-24 02:59:50 +00:00
|
|
|
int IntMapper::checkKey(int key, bool add, bool *was_added) {
|
|
|
|
Node **node = &(nodes[HASH(key)]);
|
2009-02-16 09:23:58 +00:00
|
|
|
|
2009-02-24 02:59:50 +00:00
|
|
|
while (*node && (key != (*node)->key))
|
2009-02-16 09:23:58 +00:00
|
|
|
node = &((*node)->next);
|
|
|
|
|
|
|
|
if (was_added)
|
2009-02-24 02:59:50 +00:00
|
|
|
*was_added = false;
|
2009-02-16 09:23:58 +00:00
|
|
|
|
|
|
|
if (*node) {
|
2009-02-24 02:59:50 +00:00
|
|
|
return (*node)->idx;
|
2009-02-16 09:23:58 +00:00
|
|
|
}
|
2009-02-19 18:11:05 +00:00
|
|
|
// Not found
|
2009-02-16 09:23:58 +00:00
|
|
|
|
|
|
|
if (!add)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (was_added)
|
2009-02-24 02:59:50 +00:00
|
|
|
*was_added = true;
|
2009-02-16 09:23:58 +00:00
|
|
|
|
2009-02-19 18:11:05 +00:00
|
|
|
if (holes) { // Re-use old node
|
2009-02-16 09:40:25 +00:00
|
|
|
(*node) = holes;
|
|
|
|
holes = (*node)->next;
|
2009-02-16 09:23:58 +00:00
|
|
|
(*node)->next = NULL;
|
2009-02-24 02:59:50 +00:00
|
|
|
(*node)->key = key;
|
2009-02-16 09:23:58 +00:00
|
|
|
} else {
|
2009-02-24 02:59:50 +00:00
|
|
|
*node = (Node*)malloc(sizeof(Node));
|
|
|
|
(*node)->key = key;
|
|
|
|
(*node)->idx = base_value++;
|
2009-02-16 09:23:58 +00:00
|
|
|
(*node)->next = NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-24 02:59:50 +00:00
|
|
|
return (*node)->idx;
|
2009-02-16 09:23:58 +00:00
|
|
|
}
|
|
|
|
|
2009-02-24 02:59:50 +00:00
|
|
|
int IntMapper::removeKey(int key) {
|
|
|
|
Node **node = &(nodes[HASH(key)]);
|
2009-02-16 09:23:58 +00:00
|
|
|
|
2009-02-24 02:59:50 +00:00
|
|
|
while (*node && (key != (*node)->key))
|
2009-02-16 09:23:58 +00:00
|
|
|
node = &((*node)->next);
|
|
|
|
|
|
|
|
if (*node) {
|
2009-02-24 02:59:50 +00:00
|
|
|
Node *oldnode = *node;
|
2009-02-16 09:23:58 +00:00
|
|
|
*node = (*node)->next;
|
|
|
|
|
2009-02-19 18:11:05 +00:00
|
|
|
oldnode->next = holes; // Old node is now a 'hole'
|
2009-02-16 09:40:25 +00:00
|
|
|
holes = oldnode;
|
2009-02-24 02:59:50 +00:00
|
|
|
return oldnode->key;
|
2009-02-16 09:40:25 +00:00
|
|
|
} else
|
2009-02-19 18:11:05 +00:00
|
|
|
return -1; // Not found
|
2009-02-16 09:23:58 +00:00
|
|
|
}
|
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
} // End of namespace Sci
|