#pragma once
#ifndef ___HashMap
#define ___HashMap
// HashMap.h : 해싱을 이용하는 맵 클래스
#include "HashRecord.h"
#include "hashFunctions.h"
class HashMap { // 해시 맵 클래스 선언
Record table[TABLE_SIZE]; // 해싱 테이블. 크기는 13
public:
HashMap() { reset(); } // 생성자. 모든 버켓을 비움
void reset() { // 모든 버켓을 비움
for (int i = 0; i < TABLE_SIZE; i++)
table[i].reset();
}
// 맵의 전체 내용을 출력
void display() {
for (int i = 0; i < TABLE_SIZE; i++) {
printf("[%2d] ", i);
table[i].display();
}
}
// HashMap::addLinearProb() : 선형 조사법을 이용한 삽입
void addLinearProb(const char* key, const char* value) {
int i, hashValue;
hashValue = i = hashFuntion(key);
while (table[i].isEmpty() == false) {
if (table[i].equal(key)) {
printf("[%s] 탐색키가 중복되었습니다.\n", key);
return;
}
i = (i + 1) % TABLE_SIZE;
if (i == hashValue) {
printf("[%s] 테이블이 가득찼습니다.\n", key);
return;
}
}
table[i].set(key, value);
}
// HashMap :: searchLinearProb() : 선형 조사법을 이용한 키를 탐색
Record* searchLinearProb(const char* key) {
int i, hashValue;
hashValue = i = hashFuntion(key);
while (table[i].isEmpty() == false) {
if (table[i].equal(key)) {
printf("[%s] 탐색 성공[%d]", key, i);
table[i].display();
return table + i;
}
i = (i + 1) % TABLE_SIZE;
if (i == hashValue) break;
}
printf("[%s] 탐색 실패 : 찾는 값이 테이블에 없음\n", key);
return NULL;
}
};
#endif // !___HashMap
'Programming > DS SorceCode' 카테고리의 다른 글
HashChainMap.h : 해시 체이닝으로 구현된 해시 맵 (0) | 2019.04.06 |
---|---|
HashNode.h : 해시 맵을 위한 노드 클래스 (0) | 2019.04.06 |
HashRecord.h : 해시 맵을 위한 keyed Record 클래스 (0) | 2019.04.06 |
해싱 (0) | 2019.04.06 |
AVL 트리 (0) | 2019.04.05 |