Skip to content

Commit

Permalink
SortedSet support
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlano authored Mar 14, 2024
1 parent 6830581 commit ec8932a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
49 changes: 45 additions & 4 deletions libraries/Ocl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


/******************************
* Copyright (c) 2003--2023 Kevin Lano
* Copyright (c) 2003--2024 Kevin Lano
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
Expand All @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Set;
import java.util.TreeSet;
import java.util.HashSet;
Expand Down Expand Up @@ -326,6 +327,13 @@ public static <T> HashSet<T> initialiseSet(T ... args)
return result;
}

public static <T> TreeSet<T> initialiseSortedSet(T ... args)
{ TreeSet<T> result = new TreeSet<T>();
for (int i = 0; i < args.length; i++)
{ result.add(args[i]); }
return result;
}

public static <T> ArrayList<T> initialiseSequence(T ... args)
{ ArrayList<T> result = new ArrayList<T>();
for (int i = 0; i < args.length; i++)
Expand Down Expand Up @@ -366,6 +374,12 @@ public static <K,T> HashMap<K,T> copyMap(Map<K,T> s)
return result;
}

public static <K,T> TreeMap<K,T> copySortedMap(Map<K,T> s)
{ TreeMap<K,T> result = new TreeMap<K,T>();
result.putAll(s);
return result;
}

public static <T extends Comparable> T min(Collection<T> s)
{ ArrayList<T> slist = new ArrayList<T>();
slist.addAll(s);
Expand Down Expand Up @@ -473,14 +487,23 @@ public static ArrayList<Boolean> addSequence(ArrayList<Boolean> s, boolean x)
return s;
}

public static <T> HashSet<T> excludingSet(Set<T> _s, T _x)
public static <T> HashSet<T> excludingSet(HashSet<T> _s, T _x)
{ HashSet<T> result = new HashSet<T>();
result.addAll(_s);
HashSet<T> rem = new HashSet<T>();
rem.add(_x);
result.removeAll(rem);
return result;
}

public static <T> TreeSet<T> excludingSet(TreeSet<T> _s, T _x)
{ TreeSet<T> result = (TreeSet<T>) _s.clone();

TreeSet<T> rem = new TreeSet<T>();
rem.add(_x);
result.removeAll(rem);
return result;
}

public static <T> ArrayList<T> excludingSequence(List<T> _s, T _x)
{ ArrayList<T> result = new ArrayList<T>();
Expand Down Expand Up @@ -523,6 +546,12 @@ public static <T> HashSet<T> asSet(Collection<T> c)
res.addAll(c);
return res;
}

public static <T> TreeSet<T> asSortedSet(Collection<T> c)
{ TreeSet<T> res = new TreeSet<T>();
res.addAll(c);
return res;
}

public static <K,T> HashSet<Map<K,T>> asSet(Map<K,T> m)
{ Set ss = m.entrySet();
Expand Down Expand Up @@ -916,13 +945,19 @@ public static <T> ArrayList<T> includingSequence(List<T> l, T ob)
return res;
}

public static <T> HashSet<T> includingSet(Set<T> l, T ob)
public static <T> HashSet<T> includingSet(HashSet<T> l, T ob)
{ HashSet<T> res = new HashSet<T>();
res.addAll(l);
res.add(ob);
return res;
}

public static <T> TreeSet<T> includingSet(TreeSet<T> l, T ob)
{ TreeSet<T> res = (TreeSet<T>) l.clone();
res.add(ob);
return res;
}

public static <T> int count(Collection<T> l, T obj)
{ return Collections.frequency(l,obj); }

Expand Down Expand Up @@ -1368,13 +1403,19 @@ public static <D,R> HashSet<D> mapKeys(Map<D,R> m)
return res;
}

public static <D,R> HashMap<D,R> includingMap(Map<D,R> m, D src, R trg)
public static <D,R> HashMap<D,R> includingMap(HashMap<D,R> m, D src, R trg)
{ HashMap<D,R> copy = new HashMap<D,R>();
copy.putAll(m);
copy.put(src,trg);
return copy;
}

public static <D,R> TreeMap<D,R> includingMap(TreeMap<D,R> m, D src, R trg)
{ TreeMap<D,R> copy = (TreeMap<D,R>) m.clone();
copy.put(src,trg);
return copy;
}


public static <D,R> HashMap<D,R> excludeAllMap(Map<D,R> m1, Map m2)
{ // m1 - m2
Expand Down
6 changes: 5 additions & 1 deletion libraries/oclrandom.km3
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package oclrandom {

/* Twister or Sobol sequences */


class OclRandom {

Expand All @@ -10,6 +12,8 @@ package oclrandom {
attribute iy : int;
attribute iz : int;

attribute algorithm : String := "congruence";

attribute distribution : String;

attribute bernoulliP : double := 0.0;
Expand Down Expand Up @@ -83,7 +87,7 @@ package oclrandom {

operation nextFloat() : double
pre: true
post: r = self.nrandom() & result = ( r - r->floor() );
post: r = self.nextDouble();

operation nextGaussian() : double
pre: true
Expand Down

0 comments on commit ec8932a

Please sign in to comment.