summaryrefslogtreecommitdiffstats
path: root/labb8/src/types.cpp
blob: 001b67ed042a7df49cc00ff89263e60664bc5a5e (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * TDDB86 Trailblazer
 * This file implements fundamental types used by the Trailblazer assignment.
 *
 * Please do not modify this provided file. Your turned-in files should work
 * with an unmodified version of all provided code files.
 *
 * Author: Marty Stepp, Keith Schwarz, et al
 * Slight modifications by Tommy Farnqvist
 */

#include "types.h"

/*
 * A large prime number used in hash code computation.
 */
const int kLargePrime = 78979871;

/*
 * A value that can be bitwise-ANDed with an integer to force it to be nonnegative,
 * which is useful when writing hash functions.
 */
const int kHashMask = 0x7FFFFFF;

/*
 * Utility constructor functions.
 */
TBLoc makeLoc(int row, int col) {
    TBLoc result = { row, col };
    return result;
}
TBEdge makeEdge(TBLoc start, TBLoc end) {
    TBEdge result = { start, end };
    return result;
}

/*
 * Overloaded comparison operators.
 */
bool operator < (TBLoc lhs, TBLoc rhs) {
        if (lhs.row != rhs.row)
                return lhs.row < rhs.row;
        return lhs.col < rhs.col;
}

bool operator > (TBLoc lhs, TBLoc rhs) {
    return rhs < lhs;
}

bool operator <= (TBLoc lhs, TBLoc rhs) {
    return !(rhs < lhs);
}

bool operator >= (TBLoc lhs, TBLoc rhs) {
    return !(lhs < rhs);
}

bool operator == (TBLoc lhs, TBLoc rhs) {
    return lhs.row == rhs.row && lhs.col == rhs.col;
}

bool operator != (TBLoc lhs, TBLoc rhs) {
    return !(lhs == rhs);
}

/*
 * Hash code function to make it possible to store nodes in a hash set/map.
 */
int hashCode(TBLoc l) {
    return (l.row + kLargePrime * l.col) & kHashMask;
}

/*
 * Overloaded comparison operators.
 */
bool operator < (TBEdge lhs, TBEdge rhs) {
    if (lhs.start != rhs.start) return lhs.start < rhs.start;
    return lhs.end < rhs.end;
}

bool operator > (TBEdge lhs, TBEdge rhs) {
    return rhs < lhs;
}

bool operator <= (TBEdge lhs, TBEdge rhs) {
    return !(rhs < lhs);
}

bool operator >= (TBEdge lhs, TBEdge rhs) {
    return !(lhs < rhs);
}

bool operator == (TBEdge lhs, TBEdge rhs) {
    return lhs.start == rhs.start && lhs.end == rhs.end;
}

bool operator != (TBEdge lhs, TBEdge rhs) {
    return !(lhs == rhs);
}

/*
 * Hash code function to make it possible to store arcs in a hash set/map.
 */
int hashCode(TBEdge e) {
    return (hashCode(e.start) + kLargePrime * hashCode(e.end)) & kHashMask;
}