A
André Luiz Carvalho
Hi there,
I'm porting an application from Java to C++ / BREW so I can't use the
stl, therefore, I have to implement the basic structures that the app
use in Java. I'm implementing a Hashtable template, but I get a weird
error from GCC 4.0.0 (apple):
Hashtable.h:153: error: expected constructor, destructor, or type
conversion before '*' token
The code is:
#ifndef _HASHTABLE_H_
#define _HASHTABLE_H_
#include "Vector.h"
template <class K, class V>
class Hashtable
{
public:
typedef unsigned int (*HashFuncPtr)(K *);
typedef bool (*EqualsFuncPtr)(K *, K *);
Hashtable(HashFuncPtr hashFuncPtr, EqualsFuncPtr equalsFuncPtr,
bool useFreeKey = false, bool useFreeValue = false,
const int hashSize = 20);
~Hashtable();
void clear();
int size();
bool containsKey(K* key);
V* put(K* key, V* value);
V* get(K* key);
void remove(K* key);
private:
class HashItem {
private:
bool freeKey;
bool freeValue;
public:
K *key;
V *value;
HashItem( K *k, V *v, bool useFreeKey, bool useFreeValue)
{
key = k;
value = v;
freeKey = useFreeKey;
freeValue = useFreeValue;
}
~HashItem()
{
if (freeKey)
FREE(key);
else
delete key;
if (freeValue)
FREE(value);
else
delete value;
}
};
// Private method
HashItem * findItem(K* key);
// Private members
HashFuncPtr hashFunc;
EqualsFuncPtr equalsFunc;
Vector<HashItem> **hash;
int hashSize;
int curSize;
bool freeKey;
bool freeValue;
};
template <class K, class V>
Hashtable<K, V>::Hashtable(HashFuncPtr hashFuncPtr, EqualsFuncPtr
equalsFuncPtr,
bool useFreeKey, bool useFreeValue, const int
hashArraySize)
{
hashFunc = hashFuncPtr;
equalsFunc = equalsFuncPtr;
hashSize = hashArraySize;
freeKey = useFreeKey;
freeValue = useFreeValue;
hash = new Vector<HashItem>* [hashArraySize];
for (int i = 0; i < hashSize; i++)
hash = new Vector<HashItem>(4, 4);
curSize = 0;
}
template <class K, class V>
Hashtable<K, V>::~Hashtable()
{
for (int i = 0; i < hashSize; i++) {
hash->freeAll();
delete hash;
}
delete hash;
}
template <class K, class V>
void Hashtable<K, V>::clear()
{
for (int i = 0; i < hashSize; i++)
hash->freeAll();
curSize = 0;
}
template <class K, class V>
int Hashtable<K, V>::size()
{
return curSize;
}
template <class K, class V>
bool Hashtable<K, V>::containsKey(K* key)
{
if (findItem(key) != NULL)
return true;
return false;
}
template <class K, class V>
V* Hashtable<K, V>:ut(K* key, V* value)
{
HashItem *item = findItem(key);
if (item) {
V* v = item->value;
item->value = value;
return v;
} else {
unsigned int idx = hashFunc(key) % hashSize;
hash[idx]->insert(new HashItem(key, value, freeKey,
freeValue));
}
return NULL;
}
template <class K, class V>
V* Hashtable<K, V>::get(K* key)
{
HashItem *item = findItem(key);
if (item)
return item->value;
return NULL;
}
template <class K, class V>
void Hashtable<K, V>::remove(K* key)
{
unsigned int idx = hashFunc(key) % hashSize;
Vector<HashItem> *h = hash[idx];
int vsize = h->size();
for (int i = 0; i < vsize; i++) {
if ( equalsFunc( h->elementAt(i)->key, key ) ) {
delete h->removeElementAt(i);
return;
}
}
}
template <class K, class V>
Hashtable<K, V>::HashItem* Hashtable<K, V>::findItem(K* key)
{
unsigned int idx = hashFunc(key) % hashSize;
Vector<HashItem> *h = hash[idx];
int vsize = h->size();
for (int i = 0; i < vsize; i++) {
if ( equalsFunc( h->elementAt(i)->key, key ) )
return h->elementAt(i);
}
return NULL;
}
#endif
---
Can anyone help me, please?
Thank you,
André Carvalho.
I'm porting an application from Java to C++ / BREW so I can't use the
stl, therefore, I have to implement the basic structures that the app
use in Java. I'm implementing a Hashtable template, but I get a weird
error from GCC 4.0.0 (apple):
Hashtable.h:153: error: expected constructor, destructor, or type
conversion before '*' token
The code is:
#ifndef _HASHTABLE_H_
#define _HASHTABLE_H_
#include "Vector.h"
template <class K, class V>
class Hashtable
{
public:
typedef unsigned int (*HashFuncPtr)(K *);
typedef bool (*EqualsFuncPtr)(K *, K *);
Hashtable(HashFuncPtr hashFuncPtr, EqualsFuncPtr equalsFuncPtr,
bool useFreeKey = false, bool useFreeValue = false,
const int hashSize = 20);
~Hashtable();
void clear();
int size();
bool containsKey(K* key);
V* put(K* key, V* value);
V* get(K* key);
void remove(K* key);
private:
class HashItem {
private:
bool freeKey;
bool freeValue;
public:
K *key;
V *value;
HashItem( K *k, V *v, bool useFreeKey, bool useFreeValue)
{
key = k;
value = v;
freeKey = useFreeKey;
freeValue = useFreeValue;
}
~HashItem()
{
if (freeKey)
FREE(key);
else
delete key;
if (freeValue)
FREE(value);
else
delete value;
}
};
// Private method
HashItem * findItem(K* key);
// Private members
HashFuncPtr hashFunc;
EqualsFuncPtr equalsFunc;
Vector<HashItem> **hash;
int hashSize;
int curSize;
bool freeKey;
bool freeValue;
};
template <class K, class V>
Hashtable<K, V>::Hashtable(HashFuncPtr hashFuncPtr, EqualsFuncPtr
equalsFuncPtr,
bool useFreeKey, bool useFreeValue, const int
hashArraySize)
{
hashFunc = hashFuncPtr;
equalsFunc = equalsFuncPtr;
hashSize = hashArraySize;
freeKey = useFreeKey;
freeValue = useFreeValue;
hash = new Vector<HashItem>* [hashArraySize];
for (int i = 0; i < hashSize; i++)
hash = new Vector<HashItem>(4, 4);
curSize = 0;
}
template <class K, class V>
Hashtable<K, V>::~Hashtable()
{
for (int i = 0; i < hashSize; i++) {
hash->freeAll();
delete hash;
}
delete hash;
}
template <class K, class V>
void Hashtable<K, V>::clear()
{
for (int i = 0; i < hashSize; i++)
hash->freeAll();
curSize = 0;
}
template <class K, class V>
int Hashtable<K, V>::size()
{
return curSize;
}
template <class K, class V>
bool Hashtable<K, V>::containsKey(K* key)
{
if (findItem(key) != NULL)
return true;
return false;
}
template <class K, class V>
V* Hashtable<K, V>:ut(K* key, V* value)
{
HashItem *item = findItem(key);
if (item) {
V* v = item->value;
item->value = value;
return v;
} else {
unsigned int idx = hashFunc(key) % hashSize;
hash[idx]->insert(new HashItem(key, value, freeKey,
freeValue));
}
return NULL;
}
template <class K, class V>
V* Hashtable<K, V>::get(K* key)
{
HashItem *item = findItem(key);
if (item)
return item->value;
return NULL;
}
template <class K, class V>
void Hashtable<K, V>::remove(K* key)
{
unsigned int idx = hashFunc(key) % hashSize;
Vector<HashItem> *h = hash[idx];
int vsize = h->size();
for (int i = 0; i < vsize; i++) {
if ( equalsFunc( h->elementAt(i)->key, key ) ) {
delete h->removeElementAt(i);
return;
}
}
}
template <class K, class V>
Hashtable<K, V>::HashItem* Hashtable<K, V>::findItem(K* key)
{
unsigned int idx = hashFunc(key) % hashSize;
Vector<HashItem> *h = hash[idx];
int vsize = h->size();
for (int i = 0; i < vsize; i++) {
if ( equalsFunc( h->elementAt(i)->key, key ) )
return h->elementAt(i);
}
return NULL;
}
#endif
---
Can anyone help me, please?
Thank you,
André Carvalho.