-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALGGroupHash.cs
More file actions
62 lines (55 loc) · 1.62 KB
/
ALGGroupHash.cs
File metadata and controls
62 lines (55 loc) · 1.62 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
/********************************************************************************
** All rights reserved
** Auth: kay.yang
** E-mail: [email protected]
** Date: 6/30/2017 11:13:04 AM
** Version: v1.0.0
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace KayAlgorithm
{
public abstract class ObjectHash
{
public abstract string String();
}
// 适合 做 大量数据 的 合并; 比如 合并 顶点
// 当然,分组 也是可以的
public class GroupHash<T> where T : ObjectHash
{
public GroupHash()
{
mObjects = new List<T>();
mGroups = new Dictionary<string, List<int>>();
}
public void Add(T t)
{
int index = mObjects.Count;
mObjects.Add(t);
string str = t.String();
if (!mGroups.ContainsKey(str))
{
mGroups.Add(str, new List<int>());
}
mGroups[str].Add(index);
}
public List<List<T>> GetResult()
{
List<List<T>> groupResult = new List<List<T>>();
foreach (List<int> group in mGroups.Values)
{
List<T> tmp = new List<T>();
foreach (int idx in group)
{
tmp.Add(mObjects[idx]);
}
groupResult.Add(tmp);
}
return groupResult;
}
private List<T> mObjects;
private Dictionary<string, List<int>> mGroups;
}
}