-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdirection.cc
More file actions
120 lines (100 loc) · 2.65 KB
/
direction.cc
File metadata and controls
120 lines (100 loc) · 2.65 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
/* Package : libhex
* direction.cc Created : 2007/10/11
* Author : Alex Tingle
*
* Copyright (C) 2007-2008, Alex Tingle.
*
* This file is part of the libhex application.
*
* libhex is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* libhex is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "hex.h"
namespace hex {
Direction to_direction(char c) throw(hex::invalid_argument)
{
if(c<'A' || c>'F')
throw hex::invalid_argument(std::string("to_direction:")+c);
return A + static_cast<int>(c-'A');
}
char to_char(Direction d)
{
static const char root =static_cast<char>( A );
return static_cast<char>( 'A' + (static_cast<char>(d) - root) );
}
Direction& operator+=(Direction& d, int i)
{
int d_ =static_cast<int>( d );
d_= (d_+i) % DIRECTIONS;
while(d_<0)
d_ += DIRECTIONS;
d=static_cast<Direction>( d_ );
return d;
}
Direction operator+(const Direction& d, int i)
{
Direction result =d;
result+=i;
return result;
}
/** Prefix increment */
Direction& operator++(Direction& d)
{
return d+=1;
}
/** Postfix increment */
Direction operator++(const Direction& d,int)
{
Direction result =d;
++result;
return result;
}
Direction& operator-=(Direction& d, int i)
{
return d += (-i);
}
Direction operator-(const Direction& d, int i)
{
return d + (-i);
}
/** Prefix decrement */
Direction& operator--(Direction& d)
{
return d-=1;
}
/** Postfix decrement */
Direction operator--(const Direction& d,int)
{
Direction result =d;
--result;
return result;
}
std::ostream& operator<<(std::ostream& os, const Direction& d)
{
os << to_char(d);
return os;
}
std::string rotate(const std::string& steps, int i)
{
// Rotate all letters A-F, but leave other characters alone.
std::string result =steps;
for(std::string::iterator c=result.begin(); c!=result.end(); ++c)
if('A'<=*c && *c<='F')
{
Direction d =to_direction(*c);
*c = to_char(d+i);
}
return result;
}
} // end namespace hex