-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.cpp
76 lines (66 loc) · 1.94 KB
/
dictionary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//--------------------------------------------------------------------------------------------------------------------------------
// Hassan Shahzad & Azka Khurram
// 18i-0441 & 18i-0461
// Algo-Project
// FAST NUCES
// Task3.cpp
//--------------------------------------------------------------------------------------------------------------------------------
//================================================================================================================================
// 1: This file is basically an implementation of header file "dictionary.h"
//================================================================================================================================
#include "dictionary.h"
// ========================================================
// class Dictionary:
// ========================================================
Dictionary::Dictionary()
{
size = FirstCode;
for (int i = 0; i < size; ++i)
{
entries[i].code = Nil;
entries[i].value = i;
}
}
int Dictionary::findIndex(const int code, const int value) const
{
if (code == Nil)
{
return value;
}
// Linear search
for (int i = 0; i < size; ++i)
{
if (entries[i].code == code && entries[i].value == value)
{
return i;
}
}
return Nil;
}
bool Dictionary::add(const int code, const int value)
{
if (size == MaxDictEntries)
{
cout<<"Dictionary overflowed!"<<endl;
return false;
}
entries[size].code = code;
entries[size].value = value;
++size;
return true;
}
bool Dictionary::flush(int & codeBitsWidth)
{
if (size == (1 << codeBitsWidth))
{
++codeBitsWidth;
if (codeBitsWidth > MaxDictBits)
{
// Clear the dictionary (except the first 256 byte entries).
codeBitsWidth = StartBits;
size = FirstCode;
return true;
}
}
return false;
}