-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgridpoint.h
More file actions
130 lines (99 loc) · 3.69 KB
/
cgridpoint.h
File metadata and controls
130 lines (99 loc) · 3.69 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//-----------------------------------------------------------------------------
//
// CGridPoint
// ==========
//
// Just a simple 2D point / vector structure. The reason for separate class
// is that it creates a hash of the x and y components. The hash is used in
// std::map< CGridPoint, WhatEver* > so that's the reason for the speedy
// and cached hash and a separate class.
//
//
// Created 21.2.2012 by Petri Purho
//
//-----------------------------------------------------------------------------
#ifndef INC_CGRIDPOINT_H
#define INC_CGRIDPOINT_H
#include <string>
#include <sstream>
#include "infinite_types.h"
namespace ceng {
//-----------------------------------------------------------------------------
struct GridPointHashString
{
template< class T >
std::string operator()( const T& x, const T& y ) const
{
std::stringstream ss;
ss << x << "," << y;
return ss.str();
}
};
//-----------------------------------------------------------------------------
struct GridPointHashToInt32
{
template< class T >
int operator()( const T& x, const T& y ) const
{
int lp1 = (int)x << 16;
int lp2 = (int)y;
int result = ( lp1 + lp2 );
return result;
}
};
//-----------------------------------------------------------------------------
struct GridPointHashToInt64
{
template< class T >
inline types::int64 operator()( const T& x, const T& y ) const
{
return ((types::int64)x << 32) + (types::int64)y;
}
};
//-----------------------------------------------------------------------------
template< class PointType = int, class HashType = types::int64, class HashFunction = GridPointHashToInt64 >
class CGridPoint
{
public:
CGridPoint() : x( 0 ), y( 0 ), hash( 0 ) { }
CGridPoint( PointType x, PointType y ) { Set( x, y ); }
CGridPoint( const CGridPoint& other ) : x( other.x ), y( other.y ), hash( other.hash ) { }
// assign operator
const CGridPoint& operator= ( const CGridPoint& other ) { x = other.x; y = other.y; hash = other.hash; return *this; }
// cmpr
inline bool operator< ( const CGridPoint& other ) const { return static_cast< bool >( hash < other.hash ); }
inline bool operator<= ( const CGridPoint& other ) const { return static_cast< bool >( hash <= other.hash ); }
inline bool operator> ( const CGridPoint& other ) const { return static_cast< bool >( hash > other.hash ); }
inline bool operator>= ( const CGridPoint& other ) const { return static_cast< bool >( hash >= other.hash ); }
inline bool operator==( const CGridPoint& other ) const { return (hash == other.hash); }
inline bool operator!=( const CGridPoint& other ) const { return !operator==(other); }
// addition operator
CGridPoint operator+ ( const CGridPoint& other ) const { CGridPoint result( *this ); result += other; return result; }
const CGridPoint& operator+= ( const CGridPoint& other ) { Set( x + other.x, y + other.y ); return *this; }
CGridPoint operator- ( const CGridPoint& other ) const { CGridPoint result( *this ); result -= other; return result; }
const CGridPoint& operator-= ( const CGridPoint& other ) { Set( x - other.x, y - other.y ); return *this; }
inline bool Equals( const CGridPoint& other ) const { return ( hash == other.hash ); }
inline PointType GetX() const { return x; }
inline PointType GetY() const { return y; }
void SetX( PointType _x ) { Set( _x, y ); }
void SetY( PointType _y ) { Set( x, _y ); }
void Set( PointType _x, PointType _y )
{
x = _x;
y = _y;
RecalculateHash();
}
private:
PointType x;
PointType y;
HashType hash;
inline HashType GetHash() const { return hash; }
inline void RecalculateHash()
{
HashFunction f;
hash = f( x, y );
}
};
//-----------------------------------------------------------------------------
} // end of namespace ceng
#endif