-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNonAllocString.cs
141 lines (122 loc) · 3.78 KB
/
NonAllocString.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
//example for(int i=0;i<1000;i++) string newZeroGcString = NonAllocString.instance+"test"+23;
using System;
using System.Collections.Generic;
using System.Text;
public class NonAllocString
{
private NonAllocString()
{
}
public static NonAllocString instance
{
get
{
if (m_instance.Count > 0)
{
var nonAllocString = m_instance.Pop();
nonAllocString.generatedString = null;
return nonAllocString;
}
else
return new NonAllocString();
}
}
static readonly Stack<NonAllocString> m_instance = new Stack<NonAllocString>();
List<AnyValue> anyValues = new List<AnyValue>();
public static NonAllocString operator +(NonAllocString a, object input)
{
a.anyValues.Add(new AnyValue() {type = input?.GetType(), obj = input is NonAllocString d ? (string)d : input is string s ? s : input});
return a;
}
public static NonAllocString operator +(NonAllocString a, int input)
{
a.anyValues.Add(input);
return a;
}
public static NonAllocString operator +(NonAllocString a, float input)
{
a.anyValues.Add(input);
return a;
}
public StringBuilder sb = new StringBuilder();
public string GetString()
{
sb.Clear();
for (int i = 0; i < anyValues.Count; i++)
{
if (anyValues[i].obj != null)
sb.Append(anyValues[i].obj);
else if (anyValues[i].type == typeof(int))
sb.Append(anyValues[i].integer);
else if (anyValues[i].type == typeof(float))
sb.Append(anyValues[i].float2);
}
return sb.ToString();
}
public override string ToString()
{
return Dispose();
}
public static implicit operator string(NonAllocString c)
{
return c.Dispose();
}
private string Dispose()
{
if (!string.IsNullOrEmpty(generatedString))
return generatedString;
var key = ArrayHash();
string s;
if (!stringcache.TryGetValue(key, out s))
s = stringcache[key] = GetString();
m_instance.Push(this);
anyValues.Clear();
return generatedString = s;
}
private string generatedString;
public static Dictionary<int, string> stringcache = new Dictionary<int, string>();
public int ArrayHash()
{
int key = 123;
for (int i = 0; i < anyValues.Count; i++)
{
AnyValue o = anyValues[i];
key = CombineHash(key, o.GetHashCode());
}
return key;
}
public static int CombineHash(int h1, int h2)
{
return ((h1 << 5) + h1) ^ h2;
}
struct AnyValue
{
public Type type;
public object obj;
public int integer;
public float float2;
public static implicit operator int(AnyValue msg)
{
return msg.integer;
}
public static implicit operator AnyValue(int c)
{
return new AnyValue() {integer = c,type = typeof(int)};
}
public static implicit operator float(AnyValue msg)
{
return msg.float2;
}
public static implicit operator AnyValue(float c)
{
return new AnyValue() {float2 = c, type = typeof(float)};
}
public override int GetHashCode()
{
if (obj != null) return CombineHash(type.GetHashCode(), obj.GetHashCode());
if (type == typeof(int)) return CombineHash(integer.GetHashCode(), 2345);
if (type == typeof(float)) return CombineHash(float2.GetHashCode(), 4933);
throw new Exception("dad");
}
}
}