Разбираюсь с шаблонами, сделал шаблон для хэширования по произвольной таблице:
Code: Select all
#ifndef _TEMPLATE_HASH_H_
#define _TEMPLATE_HASH_H_
template <int HASH_SIZE, typename HASH_TYPE,typename VALUE_TYPE>
class HashTable{
private:
const HASH_TYPE* const table;
public:
HashTable(const void* const table_ptr);
const HASH_TYPE CalcHash(const VALUE_TYPE& val)const;
};
template <int HASH_SIZE, typename HASH_TYPE,typename VALUE_TYPE>
HashTable<HASH_SIZE,HASH_TYPE,VALUE_TYPE>::HashTable(const void* const table_ptr):table((HASH_TYPE*)table_ptr){}
template <int HASH_SIZE, typename HASH_TYPE,typename VALUE_TYPE>
const HASH_TYPE HashTable<HASH_SIZE,HASH_TYPE,VALUE_TYPE>::CalcHash(const VALUE_TYPE& val)const{
if(val<HASH_SIZE) return table[val];
else return table[0];
}
#endif
а когода весь код в одном хидере все окD:\My Projects\PoliTetst\main.cpp|113|instantiated from here|
D:\My Projects\PoliTetst\main.cpp|44|warning: `class TelemetryData<80, 8, ushort, HASH6BIT>' has virtual functions but non-virtual destructor|
D:\My Projects\PoliTetst\main.cpp||In function `int main()':|
D:\My Projects\PoliTetst\main.cpp|108|warning: unused variable 'MAX_VAL'|
obj\Debug\main.o||In function `main':|
D:\My Projects\PoliTetst\main.cpp|111|undefined reference to `HashTable<64, unsigned char, unsigned char>::HashTable(void const*)'|
obj\Debug\main.o||In function `_ZN13TelemetryDataILi80ELi8Et9HashTableILi64EhhEE7SetDataERKtS4_':|
unsigned short)]+0x1e)||undefined reference to `HashTable<64, unsigned char, unsigned char>::HashTable(void const*)'|
)]+0x4a)||undefined reference to `HashTable<64, unsigned char, unsigned char>::CalcHash(unsigned char const&) const'|
||=== Build finished: 3 errors, 2 warnings ===|
для ясности main()
Code: Select all
#include <iostream>
#include <vector>
#include "HashTemplate.h"
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef unsigned int uint;
typedef unsigned char byte;
typedef unsigned short ushort;
typedef HashTable<64,byte,byte>HASH6BIT;
const byte table[64]={0x1,0x41,0x21,0x61,0x11,0x51,0x31,0x71,0x9,0x49,
0x29,0x69,0x19,0x59,0x39,0x79,0x5,0x45,0x25,0x65
,0x15,0x55,0x35,0x75,0xD,0x4D,0x2D,0x6D,0x1D,
0x5D,0x3D,0x7D,0x3,0x43,0x23,0x63,0x13,0x53,0x33,
0x73,0xB,0x4B,0x2B,0x6B,0x1B,0x5B,0x3B,0x7B,
0x7,0x47,0x27,0x67,0x17,0x57,0x37,0x77,0xF,0x4F,
0x2F,0x6F,0x1F,0x5F,0x3F,0x7F};
int main(){
//прогнать разными значениями
srand( time(NULL) );
const uint MAX_VAL=64;
HASH6BIT hash(table);
//какой убогий итератор получился =(
bool stop=false;
for(frame.Rewind();!stop;stop=frame.Next()){
for(uint i=0;i<80;i++){
frame.SetData(i,rand()%MAX_VAL);
}
}
return 0;
}