Skip to content

Commit daf1263

Browse files
committed
оптимизация ОписанияТипов
1 parent 62c9fe3 commit daf1263

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

src/OneScript.StandardLibrary/TypeDescriptions/TypeComparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace OneScript.StandardLibrary.TypeDescriptions
1414
internal class TypeComparer : IComparer<BslTypeValue>
1515
{
1616
private const string TYPE_BINARYDATA_NAME = "ДвоичныеДанные";
17-
private static readonly IDictionary<TypeDescriptor, int> primitives = new Dictionary<TypeDescriptor, int>();
17+
private static readonly Dictionary<TypeDescriptor, int> primitives = new Dictionary<TypeDescriptor, int>();
1818

1919
public int Compare(BslTypeValue x, BslTypeValue y)
2020
{

src/OneScript.StandardLibrary/TypeDescriptions/TypeDescription.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,26 +154,34 @@ public IValue AdjustValue(IValue value = null)
154154

155155
public static TypeDescription StringType(int length = 0,
156156
AllowedLengthEnum allowedLength = AllowedLengthEnum.Variable)
157-
{
158-
return TypeDescriptionBuilder.OfType(BasicTypes.String)
159-
.SetStringQualifiers(new StringQualifiers(length, allowedLength))
160-
.Build();
157+
{
158+
return new TypeDescription(new[] { new BslTypeValue(BasicTypes.String) },
159+
new NumberQualifiers(),
160+
new StringQualifiers(length, allowedLength),
161+
new DateQualifiers(),
162+
new BinaryDataQualifiers());
161163
}
162164

163165
public static TypeDescription IntegerType(int length = 10,
164166
AllowedSignEnum allowedSign = AllowedSignEnum.Any)
165-
{
166-
return TypeDescriptionBuilder.OfType(BasicTypes.Number)
167-
.SetNumberQualifiers(new NumberQualifiers(length, 0, allowedSign))
168-
.Build();
167+
{
168+
return new TypeDescription(new[] { new BslTypeValue(BasicTypes.Number) },
169+
new NumberQualifiers(length, 0, allowedSign),
170+
new StringQualifiers(),
171+
new DateQualifiers(),
172+
new BinaryDataQualifiers());
169173
}
170174

171175
public static TypeDescription BooleanType()
172-
{
173-
return TypeDescriptionBuilder.OfType(BasicTypes.Boolean).Build();
174-
}
175-
176-
[ScriptConstructor]
176+
{
177+
return new TypeDescription(new[] { new BslTypeValue(BasicTypes.Boolean) },
178+
new NumberQualifiers(),
179+
new StringQualifiers(),
180+
new DateQualifiers(),
181+
new BinaryDataQualifiers());
182+
}
183+
184+
[ScriptConstructor]
177185
public static TypeDescription Constructor(
178186
TypeActivationContext context,
179187
BslValue source = null,

src/OneScript.StandardLibrary/TypeDescriptions/TypeDescriptionBuilder.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ internal class TypeDescriptionBuilder
2121

2222
private const string TYPE_BINARYDATA_NAME = "ДвоичныеДанные";
2323

24-
private List<BslTypeValue> _types = new List<BslTypeValue>();
25-
24+
private static readonly TypeComparer _comparer = new TypeComparer();
25+
private readonly SortedSet<BslTypeValue> _types = new SortedSet<BslTypeValue>(_comparer);
26+
2627
internal TypeDescriptionBuilder()
2728
{
2829
}
@@ -38,13 +39,20 @@ public TypeDescriptionBuilder SourceDescription(TypeDescription source)
3839

3940
public TypeDescriptionBuilder AddTypes(IEnumerable<BslTypeValue> types)
4041
{
41-
_types.AddRange(types);
42+
foreach (var type in types)
43+
{
44+
if (type.TypeValue.ImplementingClass != typeof(BslUndefinedValue))
45+
_types.Add(type);
46+
}
4247
return this;
4348
}
4449

4550
public TypeDescriptionBuilder RemoveTypes(IEnumerable<BslTypeValue> types)
4651
{
47-
_types.RemoveAll(types.Contains);
52+
foreach (var type in types)
53+
{
54+
_types.Remove(type);
55+
}
4856
return this;
4957
}
5058

@@ -74,14 +82,11 @@ public TypeDescriptionBuilder SetBinaryDataQualifiers(BinaryDataQualifiers bq)
7482

7583
public TypeDescription Build()
7684
{
77-
_types = new List<BslTypeValue>(_types.Distinct());
78-
_types.RemoveAll(type => type.TypeValue.ImplementingClass == typeof(BslUndefinedValue));
79-
_types.Sort(new TypeComparer());
8085
var hasNumber = _types.Any(type => type.TypeValue == BasicTypes.Number);
81-
var hasString =_types.Any(type => type.TypeValue == BasicTypes.String);
86+
var hasString = _types.Any(type => type.TypeValue == BasicTypes.String);
8287
var hasDate = _types.Any(type => type.TypeValue == BasicTypes.Date);
83-
var hasBinaryData = _types.Any(x => x.TypeValue.Name == TYPE_BINARYDATA_NAME);
84-
88+
var hasBinaryData = _types.Any(x => x.TypeValue.Name == TYPE_BINARYDATA_NAME);
89+
8590
if (!hasNumber || _numberQualifiers == null) _numberQualifiers = new NumberQualifiers();
8691
if (!hasString || _stringQualifiers == null) _stringQualifiers = new StringQualifiers();
8792
if (!hasDate || _dateQualifiers == null) _dateQualifiers = new DateQualifiers();

0 commit comments

Comments
 (0)