@@ -23,6 +23,7 @@ Base.deepcopy(x::Index) = Index(deepcopy(x.lookup), deepcopy(x.names))
23
23
Base. isequal (x:: Index , y:: Index ) = isequal (x. lookup, y. lookup) && isequal (x. names, y. names)
24
24
Base .(:(== ))(x:: Index , y:: Index ) = isequal (x, y)
25
25
26
+ # TODO : consider. 'unsafe', as in few other place allow duplicate names to corrupt index
26
27
function names! (x:: Index , nm:: Vector{Symbol} )
27
28
if length (nm) != length (x)
28
29
error (" Lengths don't match." )
@@ -63,19 +64,25 @@ Base.haskey(x::Index, key::Real) = 1 <= key <= length(x.names)
63
64
Base. keys (x:: Index ) = names (x)
64
65
65
66
# TODO : If this should stay 'unsafe', perhaps make unexported
66
- # If changing, make sure union stays fast
67
67
function Base. push! (x:: Index , nm:: Symbol )
68
68
x. lookup[nm] = length (x) + 1
69
69
push! (x. names, nm)
70
70
return x
71
71
end
72
72
73
- function Base. union (x:: Index , nm:: Index )
74
- x. lookup[nm] = length (x) + 1
75
- push! (x. names, nm)
73
+ function Base. merge! (x:: Index , y:: Index )
74
+ adds = add_names (x, y)
75
+ i = length (x)
76
+ for add in adds
77
+ i += 1
78
+ x. lookup[add] = i
79
+ end
80
+ append! (x. names, adds)
76
81
return x
77
82
end
78
83
84
+ Base. merge (x:: Index , y:: Index ) = merge! (copy (x), y)
85
+
79
86
function Base. delete! (x:: Index , idx:: Integer )
80
87
# reset the lookup's beyond the deleted item
81
88
for i in (idx + 1 ): length (x. names)
@@ -126,3 +133,32 @@ SimpleIndex() = SimpleIndex(0)
126
133
Base. length (x:: SimpleIndex ) = x. length
127
134
Base. names (x:: SimpleIndex ) = nothing
128
135
_names (x:: SimpleIndex ) = nothing
136
+
137
+ # Helpers
138
+
139
+ function add_names (ind:: Index , add_ind:: Index )
140
+ u = names (add_ind)
141
+
142
+ seen = Set (_names (ind))
143
+ dups = Int[]
144
+
145
+ for i in 1 : length (u)
146
+ name = u[i]
147
+ in (name, seen) ? push! (dups, i) : push! (seen, name)
148
+ end
149
+ for i in dups
150
+ nm = u[i]
151
+ k = 1
152
+ while true
153
+ newnm = symbol (" $(nm) _$k " )
154
+ if ! in (newnm, seen)
155
+ u[i] = newnm
156
+ push! (seen, newnm)
157
+ break
158
+ end
159
+ k += 1
160
+ end
161
+ end
162
+
163
+ return u
164
+ end
0 commit comments