forked from stevemaughan/maverick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
185 lines (165 loc) · 3.51 KB
/
utils.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//===========================================================//
//
// Maverick Chess Engine
// Copyright 2013 Steve Maughan
//
//===========================================================//
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "defs.h"
#include "data.h"
#include "procs.h"
#include "bittwiddle.h"
unsigned long time_now()
{
return GetTickCount();
}
int index_of(char *substr, char *s)
{
int i, w;
w = word_count(s);
if (w == 0)
return -1;
i = 0;
do {
if (!strcmp(word_index(i,s),substr))
return i;
i++;
} while (i<w);
return -1;
}
int number_index(int index, char *s)
{
char *str;
int d, r, i;
str = word_index(index,s);
d = 1;
r = 0;
i = (int)strlen(str)-1;
while (i >= 0) {
r += d * (str[i] - '0');
d *= 10;
i--;
}
return r;
}
char *word_index(int index, char *s)
{
static char str[UCI_BUFFER_SIZE];
int j, n;
size_t l, i;
if (strlen(s)==0)
{
return 0;
}
n = 0;
j = 0;
l = strlen(s);
for (i = 0; ((n <= index) && (i < l)); i++)
{
if (i==0)
{
while ((s[i]==' ') && (s[i] != '\n')) i++;
}
if ((n == index) && (s[i]!=' '))
{
str[j] = s[i];
j++;
}
if (s[i]==' ')
{
n++;
if ((i > 0) && (s[i-1] == ' '))
{
n--;
}
}
}
if (s[strlen(s)-1] == ' ') n--;
str[j] = 0;
return str;
}
int word_count(char *s)
{
int n;
size_t l, i;
if (strlen(s)==0)
{
return 0;
}
n = 1;
l = strlen(s);
for (i=0;i<l;i++)
{
if (i==0)
{
while ((s[i]==' ') && (s[i] != '\n')) i++;
}
if (s[i]==' ')
{
n++;
if ((i>0) && (s[i-1]==' '))
{
n--;
}
}
}
if (s[strlen(s)-1]==' ') n--;
return n;
}
char *leftstr(char *s, int index)
{
static char ls[1024];
int i, c;
strcpy(ls, "");
c = word_count(s);
if (c - 1 >= index) {
strcpy(ls, word_index(index, s));
for (i = index + 1; i < c; i++) {
strcat(ls, " ");
strcat(ls, word_index(i, s));
}
}
strcat(ls, "\n");
return ls;
}
t_hash rand64()
{
t_hash i64;
i64 = rand();
i64 = (i64 << 24) ^ rand();
i64 = (i64 << 24) ^ rand();
return i64;
}
void qsort_moves(struct t_move_list *move_list, int first, int last)
{
int low, high;
signed long long midval;
struct t_move_record *temp_move;
signed long long temp_value;
low = first;
high = last;
assert(first <= last);
assert(first >= 0);
assert(last < 256);
midval = (move_list->value[low] + move_list->value[high]) >> 1;
while (low < high) {
while ((move_list->value[low] > midval) && (low < last))
low++;
while ((move_list->value[high] < midval) && (high > first))
high--;
if (low<=high) {
temp_move = move_list->move[low];
move_list->move[low] = move_list->move[high];
move_list->move[high] = temp_move;
temp_value = move_list->value[low];
move_list->value[low] = move_list->value[high];
move_list->value[high] = temp_value;
low++;
high--;
}
}
if (first < high) qsort_moves(move_list, first, high);
if (low < last) qsort_moves(move_list, low, last);
}