hash
cat hash1.cpp
// hash1.cpp
// console application
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
#include "hash1.h"
#include "myHash.h"
int main(array<System::String ^> ^args){
Console::WriteLine("Hash test\n");
myHashTable^ ht=gcnew myHashTable(100);
String^ line;
Console::Write("read key,value>");
while ( (line = Console::ReadLine()) != nullptr ) {
Console::WriteLine(line);
int index = line->IndexOf(',');
if(index <0) break;
ht->setValue(line->Substring(0,index),line);
Console::WriteLine(line->Substring(0,index));
Console::Write("read key,value>");
}
Console::Write("read key>");
while ( (line = Console::ReadLine()) != nullptr ) {
Console::WriteLine(line);
Console::Write("value=");
Console::WriteLine(ht->getValue(line));
Console::Write("read key>");
}
return EXIT_SUCCESS;
}
cat myHash.h
// myHash.h
#pragma once
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
ref class myHashItem{
public:
String^ name; // search key
String^ value; // value
myHashItem^ next; // next List item
String^ searchList( String^ key, myHashItem^ list ) ;
myHashItem(String^ name, String^ value, myHashItem^ next);
// ~myHashItem();
// !myHashItem();
};
ref class myHashTable {
public:
array<myHashItem^>^ HashTable;
int size;
myHashTable(int size);
~myHashTable();
!myHashTable();
void setValue(String^ key, String^ value);
String^ getValue(String^ key);
int getHash( String^ key );
};
cat myHash.cpp
// myHash.cpp
//
//
#include "myHash.h"
String^ myHashItem::searchList( String^ key, myHashItem^ l ) {
while( l != nullptr ) { // end of list?
if( key->CompareTo( l->name ) == 0 ) { // same?
return l->value; // yes , return its value
}
l = l->next; // move next
}
return String::Empty;
}
myHashItem::myHashItem(String^ name,String^ value,myHashItem^ next){
this->name = name;
this->value = value;
this->next = next;
}
myHashTable::myHashTable(int size){
this->size=size;
this->HashTable = gcnew array<myHashItem^>(size);
for(int i=0; i< size; i++){
this->HashTable[i] = nullptr;
}
}
myHashTable::~myHashTable(){
if(this->HashTable) delete this->HashTable;
}
myHashTable::!myHashTable(){
if(this->HashTable) delete this->HashTable;
}
int myHashTable::getHash( String^ key )
{
int len;
int ret;
// pick 3 charaacters to calculate hash
len = key->Length ;
ret = key[0];
ret += key[len-1];
ret += key[(len-1)/2];
// return hash as its modulo of hash table size
return ret % this->size;
}
void myHashTable::setValue( String^ key, String^ value )
{
int hash;
myHashItem^ add;
myHashItem^ at;
hash = this->getHash( key ); // get Hash
if( this->HashTable[hash] == nullptr ) { // check if null
add = gcnew myHashItem(key,value,nullptr); // create new item
HashTable[hash] = add; // place it
} else { // already occupied, then linear search
at = HashTable[hash]; // get current
while( at != nullptr ) {
if( key->CompareTo(at->name) == 0 ) { // already exist
at->value = value ; // update value;
return;
}
at = at->next; // check next
}
// the given key is new ..
add = gcnew myHashItem(key,value,this->HashTable[hash]); // create new item while assigning the previous item following to this new one.
HashTable[hash] = add; // place this new one to hash table
}
}
String^ myHashTable::getValue( String^ key )
{
int hash;
myHashItem^ at;
hash = this->getHash( key ); // get hash
if( this->HashTable[hash] == nullptr ) { // if not found, there is no entry
return String::Empty; // answer so.
} else { // linear search
at = HashTable[hash]; // get the first one
while( at != nullptr ) {
if( key->CompareTo(at->name) == 0 ) { // already exist
return at->value;
}
at = at->next; // next
}
return String::Empty; // not found
}
}
cat AssemblyInfo.cpp
// AssemblyInfo.cpp
//
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::CompilerServices;
using namespace System::Runtime::InteropServices;
using namespace System::Security::Permissions;
//
// General Assembly Information
//
[assembly:AssemblyCompanyAttribute("smnb")];
[assembly:AssemblyCopyrightAttribute("Copyright smnb(c) 2007")];
[assembly:AssemblyTrademarkAttribute("smnb")];
[assembly:AssemblyProductAttribute("AssemblyInfo_test")];
[assembly: AssemblyFileVersion ("2.1.1852.0")];
[assembly: AssemblyInformationalVersion("1.2")] ; // Win32 version visible in Property
[assembly:AssemblyTitleAttribute("AssemblyInfo_test")];
[assembly:AssemblyDescriptionAttribute("This is a test.")];
// Assembly Version (Major Version).(Minor Version).(Build Number).(Revision)
//[assembly:AssemblyVersionAttribute("1.0.*")]; // Build and Revision are using default.
// Build will be number of days since 2000/1/1
// Revision will be half of number of seconds since midnight of the day.
// ... doesn't work outside Visual Studio probably..
[assembly:AssemblyVersionAttribute("1.0.1.0")]; // Build and Revision are using default.
[assembly:AssemblyConfigurationAttribute("Release")]; // or debug
[assembly:AssemblyCultureAttribute("")]; // don't define this for main assembly
[assembly:ComVisible(false)]; // COM can't refer types in this Assembly
[assembly:CLSCompliantAttribute(true)];// Class marked as CLS compliant.
// study http://www.microsoft.com/japan/msdn/security/guidance/secmod81.mspx
[assembly:SecurityPermission(SecurityAction::RequestMinimum, UnmanagedCode = true)];
cat countries.txt
AFG,アフガニスタン
AGO,アンゴラ
ALB,アルバニア
AND,アンドラ
ARE,アラブ首長国連邦
ARG,アルゼンチン
ARM,アルメニア
ATG,アンティグア・バーブーダ
AUS,オーストラリア
AUT,オーストリア
AZE,アゼルバイジャン
BDI,ブルンジ
BEL,ベルギー
BEN,ベナン
BFA,ブルキナファソ
BGD,バングラディッシュ
BGR,ブルガリア
BHR,バーレーン
BHS,バハマ
BIH,ボスニア・ヘルツェゴビナ
BLR,ベラルーシ
BLZ,ベリーズ
BOL,ボリビア
BRA,ブラジル
BRB,バルバドス
BRN,ブルネイ・ダルサラーム
BTN,ブータン
BWA,ボツワナ
CAF,中央アフリカ
CAN,カナダ
CHE,スイス
CHL,チリ
CHN,中国
CIV,コートジボワール
CMR,カメルーン
COD,コンゴ民主共和国
COG,コンゴ共和国
COL,コロンビア
COM,コモロ
CPV,カーボベルデ
CRI,コスタリカ
CUB,キューバ
CYP,キプロス
CZE,チェコ
DEU,ドイツ
DJI,ジブチ
DMA,ドミニカ国
DNK,デンマーク
DOM,ドミニカ共和国
DZA,アルジェリア
ECU,エクアドル
EGY,エジプト
ERI,エリトリア
ESP,スペイン
EST,エストニア
ETH,エチオピア
FIN,フィンランド
FJI,フィジー
FRA,フランス
FSM,ミクロネシア
GAB,ガボン
GBR,英国
GEO,グルジア
GHA,ガーナ
GIN,ギニア
GMB,ガンビア
GNB,ギニアビサウ
GNQ,赤道ギニア
GRC,ギリシャ
GRD,グレナダ
GTM,グアテマラ
GUY,ガイアナ
HKG,香港(英国)(返還前に学位を取得した場合)
HND,ホンジュラス
HRV,クロアチア
HTI,ハイチ
HUN,ハンガリー
IDN,インドネシア
IND,インド
IRL,アイルランド
IRN,イラン
IRQ,イラク
ISL,アイスランド
ISR,イスラエル
ITA,イタリア
JAM,ジャマイカ
JOR,ヨルダン
JPN,日本
KAZ,カザフスタン
KEN,ケニア
KGZ,キルギス
KHM,カンボジア
KIR,キリバス
KNA,セントクリストファー・ネービス
KOR,韓国
KWT,クウェート
LAO,ラオス
LBN,レバノン
LBR,リベリア
LBY,リビア
LCA,セントルシア
LIE,リヒテンシュタイン
LKA,スリランカ
LSO,レソト
LTU,リトアニア
LUX,ルクセンブルク
LVA,ラトビア
MAR,モロッコ
MCO,モナコ
MDA,モルドバ
MDG,マダガスカル
MDV,モルディブ
MEX,メキシコ
MHL,マーシャル
MKD,マケドニア旧ユーゴスラビア共和国
MLI,マリ
MLT,マルタ
MMR,ミャンマー
MNG,モンゴル
MOZ,モザンビーク
MRT,モーリタニア
MUS,モーリシャス
MWI,マラウイ
MYS,マレーシア
NAM,ナミビア
NER,ニジェール
NGA,ナイジェリア
NIC,ニカラグア
NLD,オランダ
NOR,ノルウェー
NPL,ネパール
NRU,ナウル
NZL,ニュージーランド
OMN,オマーン
PAK,パキスタン
PAN,パナマ
PER,ペルー
PHL,フィリピン
PLW,パラオ
PNG,パプアニューギニア
POL,ポーランド
PRT,ポルトガル
PRY,パラグアイ
QAT,カタール
ROU,ルーマニア
RUS,ロシア連邦
RWA,ルワンダ
SAU,サウジアラビア
SCG,セルビア・モンテネグロ
SDN,スーダン
SEN,セネガル
SGP,シンガポール
SLB,ソロモン
SLE,シエラレオネ
SLV,エルサルバドル
SMR,サンマリノ
SOM,ソマリア
STP,サントメ・プリンシペ
SUR,スリナム
SVK,スロバキア
SVN,スロベニア
SWE,スウェーデン
SWZ,スワジランド
SYC,セーシェル
SYR,シリア
TCD,チャド
TGO,トーゴ
THA,タイ
TJK,タジキスタン
TKM,トルクメニスタン
TON,トンガ
TTO,トリニダード・トバゴ
TUN,チュニジア
TUR,トルコ
TUV,ツバル
TZA,タンザニア
UGA,ウガンダ
UKR,ウクライナ
URY,ウルグアイ
USA,米国
UZB,ウズベキスタン
VAT,バチカン
VCT,セントビンセント・グレナディーン諸島
VEN,ベネズエラ
VNM,ベトナム
VUT,バヌアツ
WSM,サモア
YEM,イエメン
ZAF,南アフリカ
ZMB,ザンビア
ZWE,ジンバブエ
ZZZ,その他の国・地域
cat query.txt
JPN
USA
FRA
SWE
cat makefile
# makefile for hash1
# console application
.PHONY: all test mmm clean
.SUFFIXES: .exe .obj .txt .resources .dll
TARGET=hash1
CULTUREDIRS=
DLLS=
all: $(TARGET).exe $(DLLS)
LOPT=
OBJS=$(TARGET).obj AssemblyInfo.obj myHash.obj
$(TARGET).obj: $(TARGET).cpp myHash.h
AssemblyInfo.obj: AssemblyInfo.cpp
meta: $(TARGET).exe
ILDasm /out:$@ /metadata $<
$(TARGET).exe: $(OBJS)
link $(LOPT) $^
myHash.obj: myHash.cpp myHash.h
.cpp.obj:
cl -c -Wall -Od -clr:safe $<
test: $(TARGET).exe
cat countries.txt query.txt | ./$(TARGET).exe
clean:
@-rm $(TARGET).exe *.obj *~ $(TARGET).exe.manifest 1>/dev/null 2>&1
@-rm *.resources 1>/dev/null 2>&1
@-rm -fr $(CULTUREDIRS)
mmm:
cat $(TARGET).cpp
cat myHash.h
cat myHash.cpp
cat AssemblyInfo.cpp
cat countries.txt
cat query.txt
@echo
cat makefile
html:
cat /dev/clipboard | code2html.exe | unix2dos