-
Notifications
You must be signed in to change notification settings - Fork 147
/
ReGoapState.cs
288 lines (261 loc) · 8.68 KB
/
ReGoapState.cs
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
namespace ReGoap.Core
{
public class ReGoapState<T, W>
{
// can change to object
private ConcurrentDictionary<T, W> values;
private readonly ConcurrentDictionary<T, W> bufferA;
private readonly ConcurrentDictionary<T, W> bufferB;
public static int DefaultSize = 20;
private ReGoapState()
{
int concurrencyLevel = 5; // No idea.
bufferA = new ConcurrentDictionary<T, W>(concurrencyLevel, DefaultSize);
bufferB = new ConcurrentDictionary<T, W>(concurrencyLevel, DefaultSize);
values = bufferA;
}
private void Init(ReGoapState<T, W> old)
{
values.Clear();
if (old != null)
{
lock (old.values)
{
foreach (var pair in old.values)
{
values[pair.Key] = pair.Value;
}
}
}
}
public static ReGoapState<T, W> operator +(ReGoapState<T, W> a, ReGoapState<T, W> b)
{
ReGoapState<T, W> result;
lock (a.values)
{
result = Instantiate(a);
}
lock (b.values)
{
foreach (var pair in b.values)
result.values[pair.Key] = pair.Value;
return result;
}
}
public void AddFromState(ReGoapState<T, W> b)
{
lock (values) lock (b.values)
{
foreach (var pair in b.values)
values[pair.Key] = pair.Value;
}
}
public int Count
{
get { return values.Count; }
}
public bool HasAny(ReGoapState<T, W> other)
{
lock (values) lock (other.values)
{
foreach (var pair in other.values)
{
W thisValue;
values.TryGetValue(pair.Key, out thisValue);
if (Equals(thisValue, pair.Value))
return true;
}
return false;
}
}
public bool HasAnyConflict(ReGoapState<T, W> other) // used only in backward for now
{
lock (values) lock (other.values)
{
foreach (var pair in other.values)
{
var otherValue = pair.Value;
// not here, ignore this check
W thisValue;
if (!values.TryGetValue(pair.Key, out thisValue))
continue;
if (!Equals(otherValue, thisValue))
return true;
}
return false;
}
}
// this method is more relaxed than the other, also accepts conflits that are fixed by "changes"
public bool HasAnyConflict(ReGoapState<T, W> changes, ReGoapState<T, W> other)
{
lock (values) lock (other.values)
{
foreach (var pair in other.values)
{
var otherValue = pair.Value;
// not here, ignore this check
W thisValue;
if (!values.TryGetValue(pair.Key, out thisValue))
continue;
W effectValue;
changes.values.TryGetValue(pair.Key, out effectValue);
if (!Equals(otherValue, thisValue) && !Equals(effectValue, thisValue))
return true;
}
return false;
}
}
public int MissingDifference(ReGoapState<T, W> other, int stopAt = int.MaxValue)
{
lock (values)
{
var count = 0;
foreach (var pair in values)
{
W otherValue;
other.values.TryGetValue(pair.Key, out otherValue);
if (!Equals(pair.Value, otherValue))
{
count++;
if (count >= stopAt)
break;
}
}
return count;
}
}
// write differences in "difference"
public int MissingDifference(ReGoapState<T, W> other, ref ReGoapState<T, W> difference, int stopAt = int.MaxValue, Func<KeyValuePair<T, W>, W, bool> predicate = null, bool test = false)
{
lock (values)
{
var count = 0;
foreach (var pair in values)
{
W otherValue;
other.values.TryGetValue(pair.Key, out otherValue);
if (!Equals(pair.Value, otherValue) && (predicate == null || predicate(pair, otherValue)))
{
count++;
if (difference != null)
difference.values[pair.Key] = pair.Value;
if (count >= stopAt)
break;
}
}
return count;
}
}
// keep only missing differences in values
public int ReplaceWithMissingDifference(ReGoapState<T, W> other, int stopAt = int.MaxValue, Func<KeyValuePair<T, W>, W, bool> predicate = null, bool test = false)
{
lock (values)
{
var count = 0;
var buffer = values;
values = values == bufferA ? bufferB : bufferA;
values.Clear();
foreach (var pair in buffer)
{
W otherValue;
other.values.TryGetValue(pair.Key, out otherValue);
if (!Equals(pair.Value, otherValue) && (predicate == null || predicate(pair, otherValue)))
{
count++;
values[pair.Key] = pair.Value;
if (count >= stopAt)
break;
}
}
return count;
}
}
public ReGoapState<T, W> Clone()
{
return Instantiate(this);
}
#region StateFactory
private static Stack<ReGoapState<T, W>> cachedStates;
public static void Warmup(int count)
{
cachedStates = new Stack<ReGoapState<T, W>>(count);
for (int i = 0; i < count; i++)
{
cachedStates.Push(new ReGoapState<T, W>());
}
}
public void Recycle()
{
lock (cachedStates)
{
cachedStates.Push(this);
}
}
public static ReGoapState<T, W> Instantiate(ReGoapState<T, W> old = null)
{
ReGoapState<T, W> state;
if (cachedStates == null)
{
cachedStates = new Stack<ReGoapState<T, W>>();
}
lock (cachedStates)
{
state = cachedStates.Count > 0 ? cachedStates.Pop() : new ReGoapState<T, W>();
}
state.Init(old);
return state;
}
#endregion
public override string ToString()
{
lock (values)
{
var result = "";
foreach (var pair in values)
result += string.Format("'{0}': {1}, ", pair.Key, pair.Value);
return result;
}
}
public W Get(T key)
{
lock (values)
{
if (!values.ContainsKey(key))
return default(W);
return values[key];
}
}
public void Set(T key, W value)
{
lock (values)
{
values[key] = value;
}
}
public void Remove(T key)
{
values.TryRemove(key, out _);
}
public ConcurrentDictionary<T, W> GetValues()
{
lock (values)
return values;
}
public bool TryGetValue(T key, out W value) {
return values.TryGetValue(key, out value);
}
public bool HasKey(T key)
{
lock (values)
return values.ContainsKey(key);
}
public void Clear()
{
lock (values)
values.Clear();
}
}
}