性能优先的排序双向链表

说明

这是一个表头用网状的排序双向链表结构托管类
原本开发的原因是游戏服务器根据玩家战斗力排序的容器更新 & 查找效率不理想,希望有个新的方法替代
因为战斗力变化频率比较高,多个用户又可能是相同的战斗力,所以做了这个类似跳跃表的结构对数据分段

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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
enum EN_GRIDDING_SORT
{
en_grd_sort_up,
en_grd_sort_down,
};

template<class _Ty>
struct gridding_node
{
_Ty _value;
struct gridding_node* _prev;
struct gridding_node* _next;

struct gridding_node* _upper;
struct gridding_node* _downer;

gridding_node()
: _prev(NULL)
, _next(NULL)
, _upper(NULL)
, _downer(NULL)
{
}

bool is_header()
{
return _upper != NULL || _downer != NULL;
}
};

///
/// 网状双向链表托管(类似跳跃表)
/// H1 = N11 = N12 = N13....
/// ||
/// H2 = N21 = N22....
/// ||
/// H3 = N31 = N32....
///
template<class _Ty, class _Ty_rule, EN_GRIDDING_SORT en>
class gridding_list
{
typedef _Ty_rule(*FUNC_rule)(const _Ty& value);
typedef bool(*FUNC_search_filter)(const _Ty& value);
private:
gridding_node<_Ty>* m_pHeader;
FUNC_rule m_pFuncRule;
private:
static gridding_node<_Ty>* create_header(const _Ty & value)
{
gridding_node<_Ty>* pNewer = new gridding_node<_Ty>();
pNewer->_value = value;

gridding_node<_Ty>* pNewerNode = create_node(value);
pNewer->_next = pNewerNode;
pNewerNode->_prev = pNewer;

return pNewer;
}
static gridding_node<_Ty>* create_node(const _Ty & value)
{
gridding_node<_Ty>* pNewer = new gridding_node<_Ty>();
pNewer->_value = value;

return pNewer;
}

private:
gridding_list()
{

}

gridding_node<_Ty>* row_head(gridding_node<_Ty>* node)
{
gridding_node<_Ty>* head = node;
if (node)
{
while (head->_prev)
head = head->_prev;
}
return head;
}

gridding_node<_Ty>* row_end(gridding_node<_Ty>* node)
{
gridding_node<_Ty>* end = node;
if (node)
{
while (end->_next)
end = end->_next;
}
return end;
}

public:
gridding_list(FUNC_rule fnRule)
: m_pHeader(NULL)
, m_pFuncRule(fnRule)
{
}

bool insert(const _Ty & value)
{
if (m_pFuncRule == NULL)
return false;
if (m_pHeader == NULL)
{
m_pHeader = create_header(value);
return true;
}

gridding_node<_Ty>* pRow = NULL;

if (m_pFuncRule(m_pHeader->_value) == m_pFuncRule(value))
pRow = m_pHeader;
else if ((m_pFuncRule(m_pHeader->_value) > m_pFuncRule(value) && en == en_grd_sort_up)
|| (m_pFuncRule(m_pHeader->_value) < m_pFuncRule(value) && en == en_grd_sort_down))
{
pRow = create_header(value);
pRow->_downer = m_pHeader;
m_pHeader->_upper = pRow;
m_pHeader = pRow;
return true;
}
else
{
gridding_node<_Ty>* pCurr = m_pHeader;
while (pCurr->_downer)
{
if (m_pFuncRule(pCurr->_downer->_value) == m_pFuncRule(value))
{
pRow = pCurr->_downer;
break;
}
else if ((m_pFuncRule(pCurr->_downer->_value) > m_pFuncRule(value) && en == en_grd_sort_up)
|| (m_pFuncRule(pCurr->_downer->_value) < m_pFuncRule(value) && en == en_grd_sort_down))
{
pRow = create_header(value);
pRow->_downer = pCurr->_downer;
pRow->_upper = pCurr;
pCurr->_downer->_upper = pRow;
pCurr->_downer = pRow;
return true;
}
else
pCurr = pCurr->_downer;
}
if (pRow == NULL)
{
if (pCurr)
{
pRow = create_header(value);
pRow->_upper = pCurr;
pCurr->_downer = pRow;
return true;
}
}
}

if (pRow == NULL || pRow->_next == NULL)
return false;

gridding_node<_Ty>* pCurrNode = pRow->_next;

while (pCurrNode)
{
if (pCurrNode->_value == value)
{
pCurrNode->_value.merge(value);
return true;
}
else if ((pCurrNode->_value > value && en == en_grd_sort_up)
|| (pCurrNode->_value < value && en == en_grd_sort_down))
{
gridding_node<_Ty>* pCol = create_node(value);

pCol->_next = pCurrNode;
pCol->_prev = pCurrNode->_prev;
pCurrNode->_prev->_next = pCol;
pCurrNode->_prev = pCol;
return true;
}
else
{
if (pCurrNode->_next == NULL)
{
gridding_node<_Ty>* pCol = create_node(value);
pCurrNode->_next = pCol;
pCol->_prev = pCurrNode;
return true;
}
else
pCurrNode = pCurrNode->_next;
}
}
return false;
}

_Ty * search(const _Ty& value)
{
if (m_pFuncRule == NULL)
return NULL;
gridding_node<_Ty>* pCurr = m_pHeader;
while (pCurr)
{
if (pCurr)
{
if (m_pFuncRule(pCurr->_value) == m_pFuncRule(value))
break;
}
pCurr = pCurr->_downer;
}

if (pCurr)
{
pCurr = pCurr->_next;
while (pCurr)
{
if (pCurr->_value == value)
return &(pCurr->_value);
pCurr = pCurr->_next;
}
}

return NULL;
}

// 按半径搜索
bool search_radius(const _Ty& center, uint32_t dwRadius, std::vector<_Ty>& vecRes)
{
if (m_pFuncRule == NULL)
return false;

gridding_node<_Ty>* pCurr = m_pHeader;
while (pCurr)
{
if (m_pFuncRule(pCurr->_value) == m_pFuncRule(center))
break;
pCurr = pCurr->_downer;
}

if (pCurr)
{
pCurr = pCurr->_next;
while (pCurr)
{
if (pCurr->_value == center)
break;
pCurr = pCurr->_next;
}
}

if (pCurr == NULL)
return false;

vecRes.push_back(pCurr->_value);

gridding_node<_Ty>* pCenter = pCurr;

// 向前
for (uint32_t i = 1; i < dwRadius; ++i)
{
if (pCurr->_prev && pCurr->_prev->is_header() == false)
{
pCurr = pCurr->_prev;
vecRes.push_back(pCurr->_value);
}
else
{
// upper row
gridding_node<_Ty>* pRowHead = row_head(pCurr);
if (pRowHead && pRowHead->_upper)
{
// 取尾部
pCurr = row_end(pRowHead->_upper);
vecRes.push_back(pCurr->_value);
}
else
break;
}
}

pCurr = pCenter;
// 向后
for (uint32_t i = 1; i < dwRadius; ++i)
{
if (pCurr->_next)
{
pCurr = pCurr->_next;
vecRes.push_back(pCurr->_value);
}
else
{
// downer row
gridding_node<_Ty>* pRowHead = row_head(pCurr);
if (pRowHead && pRowHead->_downer)
{
pCurr = pRowHead->_downer->_next;
vecRes.push_back(pCurr->_value);
}
else
break;
}
}
return true;
}

// 按照自定规则搜索
bool search_filter(const _Ty& center, FUNC_search_filter fnFilter, std::vector<_Ty>& vecRes)
{
if (m_pFuncRule == NULL)
return false;

gridding_node<_Ty>* pRow = m_pHeader;
while (pRow)
{
if (m_pFuncRule(pRow->_value) == m_pFuncRule(center))
break;
pRow = pRow->_downer;
}

// 这里只找行,列就不找了

if (pRow == NULL)
return false;

gridding_node<_Ty>* pCurr = pRow->_next;
if (pCurr == NULL)
return false;

if(fnFilter == NULL || fnFilter(pCurr->_value))
vecRes.push_back(pCurr->_value);

// 向前
while(true)
{
if (pCurr->_prev && pCurr->_prev->is_header() == false)
{
pCurr = pCurr->_prev;

if (fnFilter == NULL || fnFilter(pCurr->_value))
vecRes.push_back(pCurr->_value);
else
break; // 因为是排序表,fnFilter认为是已知该规则的筛选,如果出现不符的,前面的都不处理了
}
else
{
// upper row
gridding_node<_Ty>* pRowHead = row_head(pCurr);
if (pRowHead && pRowHead->_upper)
{
pCurr = row_end(pRowHead->_upper);

if (fnFilter == NULL || fnFilter(pCurr->_value))
vecRes.push_back(pCurr->_value);
else
break; // 因为是排序表,fnFilter认为是已知该规则的筛选,如果出现不符的,前面的都不处理了
}
else
break;
}
}

pCurr = pRow->_next;
// 向后
while (true)
{
if (pCurr->_next)
{
pCurr = pCurr->_next;

if (fnFilter == NULL || fnFilter(pCurr->_value))
vecRes.push_back(pCurr->_value);
else
break; // 因为是排序表,fnFilter认为是已知该规则的筛选,如果出现不符的,后面的都不处理了
}
else
{
// downer row
gridding_node<_Ty>* pRowHead = row_head(pCurr);
if (pRowHead && pRowHead->_downer)
{
pCurr = pRowHead->_downer->_next;

if (fnFilter == NULL || fnFilter(pCurr->_value))
vecRes.push_back(pCurr->_value);
else
break; // 因为是排序表,fnFilter认为是已知该规则的筛选,如果出现不符的,后面的都不处理了
}
else
break;
}
}
return true;
}

uint32_t rank(const _Ty& value)
{
uint32_t dwRank = 0;
if (m_pFuncRule == NULL)
return dwRank;

gridding_node<_Ty>* pCurr = m_pHeader;
while (pCurr)
{
pCurr = pCurr->_next;
while (pCurr)
{
dwRank += pCurr->_value.size();
if (pCurr->_value == center)
break;
pCurr = pCurr->_next;
}

if (m_pFuncRule(pCurr->_value) == m_pFuncRule(center))
break;

pCurr = pCurr->_downer;
}

return dwRank;
}
};

// 测试用例
#include <vector>
struct grid_test_data
{
uint32_t _key;
std::vector<uint32_t> _value;
uint32_t _count;

grid_test_data()
: _key(0)
, _count(1)
{
}

grid_test_data(uint32_t a)
{
_key = a;
_count = 1;
}

grid_test_data& operator = (const grid_test_data& td)
{
_key = td._key;
_value.clear();
_value = td._value;
return *this;
}

bool operator == (const grid_test_data& td)
{
return _key == td._key;
}

bool operator > (const grid_test_data& td)
{
return _key > td._key;
}

bool operator < (const grid_test_data& td)
{
return _key < td._key;
}

friend std::ostream& operator << (std::ostream& out, const grid_test_data& td)
{
out << td._key << "(" << td._count << ")";
return out;
}

void merge(const grid_test_data& td)
{
_value.insert(_value.end(), td._value.begin(), td._value.end());
_count += td._count;
}

uint32_t key()
{
return _key;
}
uint32_t key() const
{
return _key;
}
uint32_t count()
{
return _count;
}
uint32_t count() const
{
return _count;
}

std::vector<uint32_t>& value()
{
return _value;
}
};

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template<class _T>  
uint32_t rule_calc_row(const _T& value)
{
return value.key() / 100;
}

random_help rh;
gridding_list<grid_test_data, uint32_t, en_grd_sort_up> g_list(rule_calc_row<grid_test_data>);
for (int i = 0; i < 1024 * 1024; ++i)
{
grid_test_data td01(rh(1, 10240));
if (g_list.insert(td01) == false)
{
std::cout << __FUNCTION__ << "," << __LINE__ << td01.key() << std::endl;
break;
}
}

测试结果

上面是测试用的规则、测试代码,random_help是之前写的一个随机函数类 centos开发环境 1024 * 1024 分散量级 10240的数据实际插入耗时是4.6s 单条查找耗时 < 1ms

------ 本文结束 ------
------ 版权声明:转载请注明出处 ------