File: utility.h

package info (click to toggle)
freespace2 25.0.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 47,232 kB
  • sloc: cpp: 657,500; ansic: 22,305; sh: 293; python: 200; makefile: 198; xml: 181
file content (468 lines) | stat: -rw-r--r-- 12,630 bytes parent folder | download
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468

#ifndef _FSO_UTILITY_H
#define _FSO_UTILITY_H

#include <vector>

#include "globalincs/globals.h"
#include "globalincs/toolchain.h"


// Goober5000
// A sort for use with small or almost-sorted lists.  Iteration time is O(n) for a fully-sorted list.
// This uses a type-safe version of the function prototype for stdlib's qsort, although the size is an int rather than a size_t (for the reasons that j is an int).
// The fncompare function should return <0, 0, or >0 as the left item is less than, equal to, or greater than the right item.
template <typename array_t, typename T>
void insertion_sort(array_t& array_base, int array_size, int (*fncompare)(const T*, const T*))
{
	// NOTE: j *must* be a signed type because j reaches -1 and j+1 must be 0.
	int i, j;
	T *current, *current_buf;

	// allocate space for the element being moved
	// (Taylor says that for optimization purposes malloc/free should be used rather than vm_malloc/vm_free here)
	current_buf = new T();
	if (current_buf == nullptr)
	{
		UNREACHABLE("Malloc failed!");
		return;
	}

	// loop
	for (i = 1; i < array_size; i++)
	{
		// grab the current element
		// this does a lazy move/copy because if the array is mostly sorted,
		// there's no sense copying sorted items to their own places
		bool lazily_copied = false;
		current = &array_base[i];

		// bump other elements toward the end of the array
		for (j = i - 1; (j >= 0) && (fncompare(&array_base[j], current) > 0); j--)
		{
			if (!lazily_copied)
			{
				// this may look strange but it is just copying the data
				// into the buffer, then pointing to the buffer
				*current_buf = std::move(*current);
				current = current_buf;
				lazily_copied = true;
			}

			array_base[j + 1] = std::move(array_base[j]);
		}

		if (lazily_copied)
		{
			// insert the current element at the correct place
			array_base[j + 1] = std::move(*current);
		}
	}

	// free the allocated space
	delete current_buf;
}

//
// See https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#C++
//
template<typename T>
typename T::size_type GeneralizedLevenshteinDistance(const T &source,
	const T &target,
	typename T::size_type insert_cost = 1,
	typename T::size_type delete_cost = 1,
	typename T::size_type replace_cost = 1) {
	if (source.size() > target.size()) {
		return GeneralizedLevenshteinDistance(target, source, delete_cost, insert_cost, replace_cost);
	}

	using TSizeType = typename T::size_type;
	const TSizeType min_size = source.size(), max_size = target.size();
	std::vector<TSizeType> lev_dist(min_size + 1);

	lev_dist[0] = 0;
	for (TSizeType i = 1; i <= min_size; ++i) {
		lev_dist[i] = lev_dist[i - 1] + delete_cost;
	}

	for (TSizeType j = 1; j <= max_size; ++j) {
		TSizeType previous_diagonal = lev_dist[0], previous_diagonal_save;
		lev_dist[0] += insert_cost;

		for (TSizeType i = 1; i <= min_size; ++i) {
			previous_diagonal_save = lev_dist[i];
			if (source[i - 1] == target[j - 1]) {
				lev_dist[i] = previous_diagonal;
			}
			else {
				lev_dist[i] = std::min(std::min(lev_dist[i - 1] + delete_cost, lev_dist[i] + insert_cost), previous_diagonal + replace_cost);
			}
			previous_diagonal = previous_diagonal_save;
		}
	}

	return lev_dist[min_size];
}

template<typename T>
bool stringcost_equal(const T& a, const T& b)
{
	return a == b;
}

template<typename charT>
bool stringcost_tolower_equal(const charT& a, const charT& b)
{
	return SCP_tolower(a) == SCP_tolower(b);
}

// Lafiel
template<typename T, typename charT = typename T::value_type>
typename T::size_type stringcost(const T& op, const T& input, typename T::size_type max_expected_length = NAME_LENGTH,
	bool (*equal_check)(const charT&a, const charT&b) = stringcost_equal<charT>)
{
	using TSizeType = typename T::size_type;

    if(input.empty())
        return T::npos;

    struct string_search_it {
		TSizeType count;
		TSizeType lastpos;
		TSizeType cost;
    };
    std::vector<string_search_it> iterators;

    //Go through the input. If we find it split up into parts, prefer things that are least split, and within this prefer things that are closer together
    for (TSizeType i = 0; i < op.length(); i++) {
		std::vector<string_search_it> insert;
        for (auto& it : iterators) {
            if (it.count < input.length() && equal_check(op[i], input[it.count])) {
                //We found something. There may be a better match for this later, so only make a copy.
                insert.emplace_back(string_search_it{it.count + 1, i, i - it.lastpos <= 1 ? it.cost : max_expected_length + i - it.lastpos - 1});
            }
        }

        iterators.insert(iterators.end(), insert.begin(), insert.end());

        if (equal_check(op[i], input[0]))
            iterators.emplace_back(string_search_it{1, i, i});
    }

    auto cost = T::npos;

    for (const auto& it : iterators) {
        //Things that are missing letters are considered worse by default
        auto localcost = (input.length() - it.count) * (max_expected_length * max_expected_length) + it.cost;
        if (localcost < cost)
            cost = localcost;
    }

    return cost;
}

template <typename VECTOR_T, typename ITEM_T, typename FIELD_T>
int count_items_with_string(const VECTOR_T& item_vector, FIELD_T* ITEM_T::* field, const char* str)
{
	int count = 0;
	for (const ITEM_T& item : item_vector)
	{
		if (str == nullptr && item.*field == nullptr)
			++count;
		else if (str == nullptr || item.*field == nullptr)
			;
		else if (!stricmp(item.*field, str))
			++count;
	}

	return count;
}

template <typename VECTOR_T, typename ITEM_T, typename FIELD_T>
int count_items_with_ptr_string(const VECTOR_T& item_vector, FIELD_T ITEM_T::* field, const char* str)
{
	int count = 0;
	for (const ITEM_T& item : item_vector)
	{
		if (str == nullptr && (item.*field).get() == nullptr)
			++count;
		else if (str == nullptr || (item.*field).get() == nullptr)
			;
		else if (!stricmp((item.*field).get(), str))
			++count;
	}

	return count;
}

template <typename VECTOR_T, typename ITEM_T, typename FIELD_T>
int count_items_with_string(const VECTOR_T& item_vector, FIELD_T ITEM_T::* field, const SCP_string& str)
{
	int count = 0;
	for (const ITEM_T& item : item_vector)
	{
		if (lcase_equal(item.*field, str))
			++count;
	}

	return count;
}

template <typename VECTOR_T, typename ITEM_T>
int count_items_with_string(const VECTOR_T& item_vector, SCP_string ITEM_T::* field, const char* str)
{
	if (str == nullptr)
		return 0;

	return count_items_with_string(item_vector, field, SCP_string(str));
}

template <typename VECTOR_T, typename ITEM_T, typename FIELD_T>
int count_items_with_field(const VECTOR_T& item_vector, FIELD_T ITEM_T::* field, const FIELD_T& search)
{
	int count = 0;
	for (const ITEM_T& item : item_vector)
	{
		if (item.*field == search)
			++count;
	}

	return count;
}

template <typename ITEM_T, typename FIELD_T>
int count_items_with_string(const ITEM_T* item_array, int num_items, FIELD_T* ITEM_T::* field, const char* str)
{
	if (item_array == nullptr)
		return 0;

	int count = 0;
	for (int i = 0; i < num_items; ++i)
	{
		if (str == nullptr && item_array[i].*field == nullptr)
			++count;
		else if (str == nullptr || item_array[i].*field == nullptr)
			;
		else if (!stricmp(item_array[i].*field, str))
			++count;
	}

	return count;
}

template <typename ITEM_T, typename FIELD_T>
int count_items_with_ptr_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const char* str)
{
	if (item_array == nullptr)
		return 0;

	int count = 0;
	for (int i = 0; i < num_items; ++i)
	{
		if (str == nullptr && (item_array[i].*field).get() == nullptr)
			++count;
		else if (str == nullptr || (item_array[i].*field).get() == nullptr)
			;
		else if (!stricmp((item_array[i].*field).get(), str))
			++count;
	}

	return count;
}

template <typename ITEM_T, typename FIELD_T>
int count_items_with_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const SCP_string& str)
{
	if (item_array == nullptr)
		return 0;

	int count = 0;
	for (int i = 0; i < num_items; ++i)
		if (lcase_equal(item_array[i].*field, str))
			++count;

	return count;
}

template <typename ITEM_T>
int count_items_with_string(const ITEM_T* item_array, int num_items, SCP_string ITEM_T::* field, const char* str)
{
	if (item_array == nullptr || str == nullptr)
		return 0;

	return count_items_with_string(item_array, num_items, field, SCP_string(str));
}

template <typename ITEM_T, typename FIELD_T>
int count_items_with_field(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const FIELD_T& search)
{
	if (item_array == nullptr)
		return 0;

	int count = 0;
	for (int i = 0; i < num_items; ++i)
		if (item_array[i].*field == search)
			++count;

	return count;
}

template <typename VECTOR_T, typename ITEM_T, typename FIELD_T>
int find_item_with_string(const VECTOR_T& item_vector, FIELD_T* ITEM_T::* field, const char* str)
{
	int index = 0;
	for (const ITEM_T& item : item_vector)
	{
		if (str == nullptr && item.*field == nullptr)
			return index;
		else if (str == nullptr || item.*field == nullptr)
			++index;
		else if (!stricmp(item.*field, str))
			return index;
		else
			++index;
	}

	return -1;
}

template <typename VECTOR_T, typename ITEM_T, typename FIELD_T>
int find_item_with_ptr_string(const VECTOR_T& item_vector, FIELD_T ITEM_T::* field, const char* str)
{
	int index = 0;
	for (const ITEM_T& item : item_vector)
	{
		if (str == nullptr && (item.*field).get() == nullptr)
			return index;
		else if (str == nullptr || (item.*field).get() == nullptr)
			++index;
		else if (!stricmp((item.*field).get(), str))
			return index;
		else
			++index;
	}

	return -1;
}

template <typename VECTOR_T, typename ITEM_T, typename FIELD_T>
int find_item_with_string(const VECTOR_T& item_vector, FIELD_T ITEM_T::* field, const SCP_string& str)
{
	int index = 0;
	for (const ITEM_T& item : item_vector)
	{
		if (lcase_equal(item.*field, str))
			return index;
		else
			++index;
	}

	return -1;
}

template <typename VECTOR_T, typename ITEM_T>
int find_item_with_string(const VECTOR_T& item_vector, SCP_string ITEM_T::* field, const char* str)
{
	if (str == nullptr)
		return -1;

	return find_item_with_string(item_vector, field, SCP_string(str));
}

template <typename VECTOR_T, typename ITEM_T, typename FIELD_T>
int find_item_with_field(const VECTOR_T& item_vector, FIELD_T ITEM_T::* field, const FIELD_T& search)
{
	int index = 0;
	for (const ITEM_T& item : item_vector)
	{
		if (item.*field == search)
			return index;
		else
			++index;
	}

	return -1;
}

template <typename ITEM_T, typename FIELD_T>
int find_item_with_string(const ITEM_T* item_array, int num_items, FIELD_T* ITEM_T::* field, const char* str)
{
	if (item_array == nullptr)
		return -1;

	for (int i = 0; i < num_items; ++i)
	{
		if (str == nullptr && item_array[i].*field == nullptr)
			return i;
		else if (str == nullptr || item_array[i].*field == nullptr)
			;
		else if (!stricmp(item_array[i].*field, str))
			return i;
	}

	return -1;
}

template <typename ITEM_T, typename FIELD_T>
int find_item_with_ptr_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const char* str)
{
	if (item_array == nullptr)
		return -1;

	for (int i = 0; i < num_items; ++i)
	{
		if (str == nullptr && (item_array[i].*field).get() == nullptr)
			return i;
		else if (str == nullptr || (item_array[i].*field).get() == nullptr)
			;
		else if (!stricmp((item_array[i].*field).get(), str))
			return i;
	}

	return -1;
}

template <typename ITEM_T, typename FIELD_T>
int find_item_with_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const SCP_string& str)
{
	if (item_array == nullptr)
		return -1;

	for (int i = 0; i < num_items; ++i)
		if (lcase_equal(item_array[i].*field, str))
			return i;

	return -1;
}

template <typename ITEM_T>
int find_item_with_string(const ITEM_T* item_array, int num_items, SCP_string ITEM_T::* field, const char* str)
{
	if (item_array == nullptr || str == nullptr)
		return -1;

	return find_item_with_string(item_array, num_items, field, str);
}

template <typename ITEM_T, typename FIELD_T>
int find_item_with_field(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const FIELD_T& search)
{
	if (item_array == nullptr)
		return -1;

	for (int i = 0; i < num_items; ++i)
		if (item_array[i].*field == search)
			return i;

	return -1;
}

template <typename T>
const T* coalesce(const T* possibly_null, const T* value_if_null)
{
	Assertion(value_if_null != nullptr, "value_if_null can never be null itself!");

	return (possibly_null != nullptr) ? possibly_null : value_if_null;
}

#endif