|
| 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 | +#> |
0 commit comments