-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathUniqueSetValue.cs
More file actions
138 lines (121 loc) · 4.49 KB
/
Copy pathUniqueSetValue.cs
File metadata and controls
138 lines (121 loc) · 4.49 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using kOS.Safe.Encapsulation.Suffixes;
using kOS.Safe.Exceptions;
using kOS.Safe.Serialization;
using kOS.Safe.Function;
namespace kOS.Safe.Encapsulation
{
[kOS.Safe.Utilities.KOSNomenclature("UniqueSet")]
public class UniqueSetValue<T> : CollectionValue<T, HashSet<T>>
where T : Structure
{
public UniqueSetValue()
: this(new HashSet<T>())
{
}
public UniqueSetValue(IEnumerable<T> setValue) : base("UNIQUESET", new HashSet<T>(setValue))
{
SetInitializeSuffixes();
}
// Required for all IDumpers for them to work, but can't enforced by the interface because it's static:
public static UniqueSetValue<T> CreateFromDump(SafeSharedObjects shared, Dump d)
{
var newObj = new UniqueSetValue<T>();
newObj.LoadDump(d);
return newObj;
}
public void Add(T item)
{
Collection.Add(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
Collection.CopyTo(array, arrayIndex);
}
public bool Remove(T item)
{
return Collection.Remove(item);
}
public void Clear()
{
Collection.Clear();
}
public override void LoadDump(Dump dump)
{
Collection.Clear();
List<object> values = (List<object>)dump[kOS.Safe.Dump.Items];
foreach (object item in values)
{
Collection.Add((T)FromPrimitive(item));
}
}
private void SetInitializeSuffixes()
{
AddSuffix("COPY", new NoArgsSuffix<UniqueSetValue<T>> (() => new UniqueSetValue<T>(this)));
AddSuffix("ADD", new OneArgsSuffix<T> (toAdd => Collection.Add(toAdd)));
AddSuffix("REMOVE", new OneArgsSuffix<BooleanValue, T> (toRemove => Collection.Remove(toRemove)));
}
public override string ToStringItems(int level)
{
StringBuilder sb = new StringBuilder();
string pad = string.Empty.PadRight(level * TerminalFormatter.INDENT_SPACES, ' ');
var asArray = InnerEnumerable.ToArray();
foreach (object item in asArray)
{
Structure asStructure = item as Structure;
if (asStructure != null)
{
sb.Append(string.Format("{0}{1}\n",
pad,
asStructure.ToStringIndented(level)
));
}
else // Hypothetically this case should not happen, but if we screwed up somewhere so it does, at least you can see something.
{
sb.Append(item.ToString());
}
}
return sb.ToString();
}
}
[kOS.Safe.Utilities.KOSNomenclature("UniqueSet", KOSToCSharp = false)] // one-way because the generic templated UniqueSetValue<T> is the canonical one.
public class UniqueSetValue : UniqueSetValue<Structure>
{
[Function("uniqueset")]
public class FunctionSet : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
{
Structure[] argArray = new Structure[CountRemainingArgs(shared)];
for (int i = argArray.Length - 1; i >= 0; --i)
argArray[i] = PopStructureAssertEncapsulated(shared); // fill array in reverse order because .. stack args.
AssertArgBottomAndConsume(shared);
var setValue = new UniqueSetValue(argArray.ToList());
ReturnValue = setValue;
}
}
public UniqueSetValue()
{
InitializeSuffixes();
}
public UniqueSetValue(IEnumerable<Structure> toCopy)
: base(toCopy)
{
InitializeSuffixes();
}
// Required for all IDumpers for them to work, but can't enforced by the interface because it's static:
public static new UniqueSetValue CreateFromDump(SafeSharedObjects shared, Dump d)
{
var newObj = new UniqueSetValue();
newObj.LoadDump(d);
return newObj;
}
private void InitializeSuffixes()
{
AddSuffix("COPY", new NoArgsSuffix<UniqueSetValue>(() => new UniqueSetValue(this)));
}
}
}