Skip to content

Commit 8b26069

Browse files
committed
Split binarysearch and interpolation into separate structures
1 parent 328647b commit 8b26069

30 files changed

Lines changed: 565 additions & 519 deletions

Src/FastData.Generator.CPlusPlus/CPlusPlusCodeGenerator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ public override string Generate()
134134
ValueCount = bsCtx.Values.Length
135135
};
136136

137+
case InterpolatedBinarySearchContext<TKey, TValue> ibsCtx:
138+
return new BinarySearchTemplateData
139+
{
140+
Keys = (ibsCtx.Keys).ToObjects(),
141+
KeyCount = ibsCtx.Keys.Length,
142+
Values = (ibsCtx.Values).ToObjects(),
143+
ValueCount = ibsCtx.Values.Length
144+
};
145+
137146
case ConditionalContext<TKey, TValue> conCtx:
138147
return new ArrayTemplateData
139148
{

Src/FastData.Generator.CPlusPlus/FastData.Generator.CPlusPlus.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@
157157
<DesignTime>True</DesignTime>
158158
<DependentUpon>SingleValueCode.t4</DependentUpon>
159159
</Compile>
160+
<Compile Update="Templates\BinarySearchInterpolationCode.cs">
161+
<AutoGen>True</AutoGen>
162+
<DesignTime>True</DesignTime>
163+
<DependentUpon>BinarySearchInterpolationCode.t4</DependentUpon>
164+
</Compile>
160165
</ItemGroup>
161166

162167
</Project>

Src/FastData.Generator.CPlusPlus/Templates/BinarySearchCode.t4

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<#@ parameter type="Genbox.FastData.Generator.CPlusPlus.TemplateModel" name="Model" #>
77
<#@ parameter type="Genbox.FastData.Generator.SharedCode" name="Shared" #>
88
<#@ parameter type="Genbox.FastData.Generator.Template.CommonDataModel" name="Common" #>
9-
<#@ parameter type="Genbox.FastData.Generators.Contexts.BinarySearchContext" name="Context" #>
109
<#@ parameter type="Genbox.FastData.Generator.CPlusPlus.TemplateData.BinarySearchTemplateData" name="Data" #>
1110

1211
<#
@@ -29,47 +28,7 @@ public:
2928
<#= Model.GetMethodModifier(true) #>bool contains(const <#= Model.KeyTypeName #> <#= Common.InputKeyName #>)<#= Model.PostMethodModifier #> {
3029
<#= Model.GetMethodHeader(MethodType.Contains) #>
3130

32-
<#
33-
if (Context.UseInterpolation)
34-
{
35-
#> int32_t lo = 0;
36-
int32_t hi = <#= (Data.KeyCount - 1).ToStringInvariant() #>;
37-
while (lo <= hi && <#= Common.LookupKeyName #> >= keys[lo] && <#= Common.LookupKeyName #> <= keys[hi]) {
38-
const <#= Model.KeyTypeName #> lo_key = keys[lo];
39-
const <#= Model.KeyTypeName #> hi_key = keys[hi];
40-
41-
if (lo_key == hi_key) {
42-
if (lo_key == <#= Common.LookupKeyName #>)
43-
return true;
44-
45-
break;
46-
}
47-
48-
const double range = static_cast<double>(hi_key) - static_cast<double>(lo_key);
49-
const double offset = static_cast<double>(<#= Common.LookupKeyName #>) - static_cast<double>(lo_key);
50-
int32_t mid = lo + static_cast<int32_t>((offset * (hi - lo)) / range);
51-
52-
if (mid < lo)
53-
mid = lo;
54-
else if (mid > hi)
55-
mid = hi;
56-
57-
const <#= Model.KeyTypeName #> mid_key = keys[mid];
58-
if (mid_key == <#= Common.LookupKeyName #>)
59-
return true;
60-
if (mid_key < <#= Common.LookupKeyName #>)
61-
lo = mid + 1;
62-
else
63-
hi = mid - 1;
64-
}
65-
66-
return false;
67-
}
68-
<#
69-
}
70-
else
71-
{
72-
#> int32_t lo = 0;
31+
int32_t lo = 0;
7332
int32_t hi = <#= (Data.KeyCount - 1).ToStringInvariant() #>;
7433
while (lo <= hi) {
7534
const int32_t mid = lo + ((hi - lo) >> 1);
@@ -87,7 +46,6 @@ public:
8746
return false;
8847
}
8948
<#
90-
}
9149
if (Data.ValueCount > 0)
9250
{
9351
Shared.Add(CodePlacement.Before, Model.ValueObjectDeclarations);
@@ -97,55 +55,7 @@ public:
9755
<#= Model.GetMethodModifier(false) #>bool try_lookup(const <#= Model.KeyTypeName #> <#= Common.InputKeyName #>, const <#= Model.ValueTypeName #>*& value)<#= Model.PostMethodModifier #> {
9856
<#= Model.GetMethodHeader(MethodType.TryLookup) #>
9957

100-
<#
101-
if (Context.UseInterpolation)
102-
{
103-
#> int32_t lo = 0;
104-
int32_t hi = <#= (Data.KeyCount - 1).ToStringInvariant() #>;
105-
while (lo <= hi && <#= Common.LookupKeyName #> >= keys[lo] && <#= Common.LookupKeyName #> <= keys[hi]) {
106-
const <#= Model.KeyTypeName #> lo_key = keys[lo];
107-
const <#= Model.KeyTypeName #> hi_key = keys[hi];
108-
109-
if (lo_key == hi_key) {
110-
if (lo_key == <#= Common.LookupKeyName #>)
111-
{
112-
value = <#= Model.IsPrimitive ? "&" : "" #>values[lo];
113-
return true;
114-
}
115-
116-
break;
117-
}
118-
119-
const double range = static_cast<double>(hi_key) - static_cast<double>(lo_key);
120-
const double offset = static_cast<double>(<#= Common.LookupKeyName #>) - static_cast<double>(lo_key);
121-
int32_t mid = lo + static_cast<int32_t>((offset * (hi - lo)) / range);
122-
123-
if (mid < lo)
124-
mid = lo;
125-
else if (mid > hi)
126-
mid = hi;
127-
128-
const <#= Model.KeyTypeName #> mid_key = keys[mid];
129-
if (mid_key == <#= Common.LookupKeyName #>)
130-
{
131-
value = <#= Model.IsPrimitive ? "&" : "" #>values[mid];
132-
return true;
133-
}
134-
135-
if (mid_key < <#= Common.LookupKeyName #>)
136-
lo = mid + 1;
137-
else
138-
hi = mid - 1;
139-
}
140-
141-
value = nullptr;
142-
return false;
143-
}
144-
<#
145-
}
146-
else
147-
{
148-
#> int32_t lo = 0;
58+
int32_t lo = 0;
14959
int32_t hi = <#= (Data.KeyCount - 1).ToStringInvariant() #>;
15060
while (lo <= hi) {
15161
const int32_t mid = lo + ((hi - lo) >> 1);
@@ -169,5 +79,4 @@ public:
16979
}
17080
<#
17181
}
172-
}
17382
#>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<#@ template language="C#" #>
2+
<#@ import namespace="Genbox.FastData.Generator.Enums" #>
3+
<#@ import namespace="Genbox.FastData.Generator.Extensions" #>
4+
<#@ import namespace="Genbox.FastData.Generator.Helpers" #>
5+
6+
<#@ parameter type="Genbox.FastData.Generator.CPlusPlus.TemplateModel" name="Model" #>
7+
<#@ parameter type="Genbox.FastData.Generator.SharedCode" name="Shared" #>
8+
<#@ parameter type="Genbox.FastData.Generator.Template.CommonDataModel" name="Common" #>
9+
<#@ parameter type="Genbox.FastData.Generator.CPlusPlus.TemplateData.BinarySearchTemplateData" name="Data" #>
10+
11+
<#
12+
if (Data.ValueCount > 0)
13+
{
14+
#>
15+
<#= Model.GetFieldModifier(false) #>std::array<<#= Model.GetValueTypeName() #>, <#= Data.ValueCount.ToStringInvariant() #>> values = {
16+
<#= FormatHelper.FormatColumns(Data.Values, Data.ValueCount, Model.ToValueLabel) #>
17+
};
18+
19+
<#
20+
}
21+
#>
22+
<#= Model.GetFieldModifier(true) #>std::array<<#= Model.KeyTypeName #>, <#= Data.KeyCount.ToStringInvariant() #>> keys = {
23+
<#= FormatHelper.FormatColumns(Data.Keys, Data.KeyCount, Model.ToValueLabel) #>
24+
};
25+
26+
public:
27+
<#= Model.MethodAttribute #>
28+
<#= Model.GetMethodModifier(true) #>bool contains(const <#= Model.KeyTypeName #> <#= Common.InputKeyName #>)<#= Model.PostMethodModifier #> {
29+
<#= Model.GetMethodHeader(MethodType.Contains) #>
30+
31+
int32_t lo = 0;
32+
int32_t hi = <#= (Data.KeyCount - 1).ToStringInvariant() #>;
33+
while (lo <= hi && <#= Common.LookupKeyName #> >= keys[lo] && <#= Common.LookupKeyName #> <= keys[hi]) {
34+
const <#= Model.KeyTypeName #> lo_key = keys[lo];
35+
const <#= Model.KeyTypeName #> hi_key = keys[hi];
36+
37+
if (lo_key == hi_key) {
38+
if (lo_key == <#= Common.LookupKeyName #>)
39+
return true;
40+
41+
break;
42+
}
43+
44+
const double range = static_cast<double>(hi_key) - static_cast<double>(lo_key);
45+
const double offset = static_cast<double>(<#= Common.LookupKeyName #>) - static_cast<double>(lo_key);
46+
int32_t mid = lo + static_cast<int32_t>((offset * (hi - lo)) / range);
47+
48+
if (mid < lo)
49+
mid = lo;
50+
else if (mid > hi)
51+
mid = hi;
52+
53+
const <#= Model.KeyTypeName #> mid_key = keys[mid];
54+
if (mid_key == <#= Common.LookupKeyName #>)
55+
return true;
56+
if (mid_key < <#= Common.LookupKeyName #>)
57+
lo = mid + 1;
58+
else
59+
hi = mid - 1;
60+
}
61+
62+
return false;
63+
}
64+
<#
65+
if (Data.ValueCount > 0)
66+
{
67+
Shared.Add(CodePlacement.Before, Model.ValueObjectDeclarations);
68+
#>
69+
70+
<#= Model.MethodAttribute #>
71+
<#= Model.GetMethodModifier(false) #>bool try_lookup(const <#= Model.KeyTypeName #> <#= Common.InputKeyName #>, const <#= Model.ValueTypeName #>*& value)<#= Model.PostMethodModifier #> {
72+
<#= Model.GetMethodHeader(MethodType.TryLookup) #>
73+
74+
int32_t lo = 0;
75+
int32_t hi = <#= (Data.KeyCount - 1).ToStringInvariant() #>;
76+
while (lo <= hi && <#= Common.LookupKeyName #> >= keys[lo] && <#= Common.LookupKeyName #> <= keys[hi]) {
77+
const <#= Model.KeyTypeName #> lo_key = keys[lo];
78+
const <#= Model.KeyTypeName #> hi_key = keys[hi];
79+
80+
if (lo_key == hi_key) {
81+
if (lo_key == <#= Common.LookupKeyName #>)
82+
{
83+
value = <#= Model.IsPrimitive ? "&" : "" #>values[lo];
84+
return true;
85+
}
86+
87+
break;
88+
}
89+
90+
const double range = static_cast<double>(hi_key) - static_cast<double>(lo_key);
91+
const double offset = static_cast<double>(<#= Common.LookupKeyName #>) - static_cast<double>(lo_key);
92+
int32_t mid = lo + static_cast<int32_t>((offset * (hi - lo)) / range);
93+
94+
if (mid < lo)
95+
mid = lo;
96+
else if (mid > hi)
97+
mid = hi;
98+
99+
const <#= Model.KeyTypeName #> mid_key = keys[mid];
100+
if (mid_key == <#= Common.LookupKeyName #>)
101+
{
102+
value = <#= Model.IsPrimitive ? "&" : "" #>values[mid];
103+
return true;
104+
}
105+
106+
if (mid_key < <#= Common.LookupKeyName #>)
107+
lo = mid + 1;
108+
else
109+
hi = mid - 1;
110+
}
111+
112+
value = nullptr;
113+
return false;
114+
}
115+
<#
116+
}
117+
#>

Src/FastData.Generator.CSharp/CSharpCodeGenerator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ public override string Generate()
144144
ValueCount = bsCtx.Values.Length
145145
};
146146

147+
case InterpolatedBinarySearchContext<TKey, TValue> ibsCtx:
148+
return new BinarySearchTemplateData
149+
{
150+
Keys = ibsCtx.Keys.ToObjects(),
151+
KeyCount = ibsCtx.Keys.Length,
152+
Values = ibsCtx.Values.ToObjects(),
153+
ValueCount = ibsCtx.Values.Length
154+
};
155+
147156
case ConditionalContext<TKey, TValue> conCtx:
148157
return new ArrayTemplateData
149158
{

Src/FastData.Generator.CSharp/FastData.Generator.CSharp.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@
9292
<Compile Include="..\..\Misc\Compat\RequiredMemberAttribute.cs">
9393
<Link>Internal\Compat\RequiredMemberAttribute.cs</Link>
9494
</Compile>
95+
<Compile Update="Templates\BinarySearchInterpolationCode.cs">
96+
<AutoGen>True</AutoGen>
97+
<DesignTime>True</DesignTime>
98+
<DependentUpon>BinarySearchInterpolationCode.t4</DependentUpon>
99+
</Compile>
95100
</ItemGroup>
96101

97102
</Project>

0 commit comments

Comments
 (0)