@@ -278,22 +278,6 @@ public
278278 end for ;
279279 end toArray;
280280
281- function map
282- "Maps all keys in the set."
283- input UnorderedSet < T > set;
284- input MapFn fn;
285- partial function MapFn
286- input output T key;
287- end MapFn ;
288- protected
289- array< list< T >> new_buckets = Mutable . access(set. buckets);
290- algorithm
291- for b in 1 :arrayLength(new_buckets) loop
292- new_buckets[b] := list(fn(k) for k in new_buckets[b]);
293- end for ;
294- Mutable . update(set. buckets, new_buckets);
295- end map;
296-
297281 function fold< FT >
298282 "Folds over the keys in the set."
299283 input UnorderedSet < T > set;
@@ -313,6 +297,91 @@ public
313297 end for ;
314298 end fold;
315299
300+ /*
301+ function map<OT>
302+ "Applies a function to all keys in the given set and returns a new set
303+ with the new keys."
304+ input UnorderedSet<T> set;
305+ input MapFn fn;
306+ input OutHash hash;
307+ input OutKeyEq keyEq;
308+ output UnorderedSet<OT> outSet;
309+
310+ partial function MapFn
311+ input T key;
312+ output OT outKey;
313+ end MapFn;
314+ partial function OutHash
315+ input OT key;
316+ input Integer mod;
317+ output Integer hash;
318+ end OutHash;
319+ partial function OutKeyEq
320+ input OT key1;
321+ input OT key2;
322+ output Boolean equal;
323+ end OutKeyEq;
324+ algorithm
325+ outSet := new<OT>(hash, keyEq, Util.nextPrime(Mutable.access(set.size)));
326+ for b in Mutable.access(set.buckets) loop
327+ for k in b loop
328+ add(fn(k), outSet);
329+ end for;
330+ end for;
331+ end map;
332+ */
333+
334+ function apply
335+ "Replaces all keys in the given set with the results of the given function
336+ when applied to all keys. Equivalent to a rehash."
337+ input UnorderedSet < T > set;
338+ input ApplyFn fn;
339+
340+ partial function ApplyFn
341+ input output T key;
342+ end ApplyFn ;
343+ protected
344+ Hash hashfn = set. hashFn;
345+ KeyEq eqfn = set. eqFn;
346+ Integer bucket_count, hash, size = 0 ;
347+ array< list< T >> new_buckets;
348+ T newKey;
349+ list< T > bucket;
350+ Boolean duplicate;
351+ algorithm
352+ // Make a new bucket array.
353+ bucket_count := Util . nextPrime(Mutable . access(set. size));
354+ new_buckets := arrayCreate(bucket_count, {});
355+
356+ for b in Mutable . access(set. buckets) loop
357+ for k in b loop
358+ // Apply the function to the key
359+ newKey := fn(k);
360+ hash := hashfn(newKey, bucket_count);
361+ bucket := arrayGet(new_buckets, hash + 1 );
362+
363+ // check if we have a duplicate
364+ duplicate := false ;
365+ for nk in bucket loop
366+ if eqfn(nk, newKey) then
367+ duplicate := true ;
368+ break ;
369+ end if ;
370+ end for ;
371+
372+ // Add the result to the new bucket if it is not already there.
373+ if not duplicate then
374+ arrayUpdate(new_buckets, hash + 1 , newKey :: bucket);
375+ size := size + 1 ;
376+ end if ;
377+ end for ;
378+ end for ;
379+
380+ // Replace the old bucket array with the new one and update the size of the set.
381+ Mutable . update(set. buckets, new_buckets);
382+ Mutable . update(set. size, size);
383+ end apply;
384+
316385 function all
317386 "Returns true if the given function returns true for all elements in the set,
318387 otherwise false."
0 commit comments