[Nice-commit] Nice/stdlib/nice/lang array.nice,1.25,1.26 collections.nice,1.51,1.52
Brought to you by:
bonniot
From: <ar...@us...> - 2003-08-24 20:48:00
|
Update of /cvsroot/nice/Nice/stdlib/nice/lang In directory sc8-pr-cvs1:/tmp/cvs-serv1668/F:/nice/stdlib/nice/lang Modified Files: array.nice collections.nice Log Message: First part of updates to the stdlib. Index: array.nice =================================================================== RCS file: /cvsroot/nice/Nice/stdlib/nice/lang/array.nice,v retrieving revision 1.25 retrieving revision 1.26 diff -C2 -d -r1.25 -r1.26 *** array.nice 21 Jul 2003 17:24:14 -0000 1.25 --- array.nice 24 Aug 2003 20:47:57 -0000 1.26 *************** *** 24,30 **** = native; ! <Any T> !T[] elementsNotNull(?T[] arr) { ! assert(!arr.has(?T elem => elem == null)); return cast(arr); } --- 24,30 ---- = native; ! <T> !T[] elementsNotNull(?T[] arr) { ! assert(arr.all(?T elem => elem != null)); return cast(arr); } *************** *** 34,43 **** The result can be the same array as the argument. */ ! <Any T> T[] resize(T[], int newSize) = native Object Native.resize(Object,int); ! <Any T> int length(T[]) = inline nice.lang.inline.ArrayLength(); ! <Any T> T get(T[], int) = inline nice.lang.inline.ArrayGetOp("o"); ! <Any T> void set(T[], int, T) = inline nice.lang.inline.ArraySetOp("o"); --- 34,43 ---- The result can be the same array as the argument. */ ! <T> T[] resize(T[], int newSize) = native Object Native.resize(Object,int); ! <T> int length(T[]) = inline nice.lang.inline.ArrayLength(); ! <T> T get(T[], int) = inline nice.lang.inline.ArrayGetOp("o"); ! <T> void set(T[], int, T) = inline nice.lang.inline.ArraySetOp("o"); *************** *** 89,104 **** // Define collection methods ! <C,T,U> map(a@Array, f) = fill(new U[a.length], int i => f(a[i])); ! <C,T> filter(a@Array, test) = a.filter(test); // Specialized versions for arrays. ! <T> T[] filter(T[] a, T -> boolean test) { int l = a.length; int found = 0; ! T[] res = cast(new T[l]); a.foreach(T elem => --- 89,104 ---- // Define collection methods ! <C,T,U> map(a@Array, func) = fill(new U[a.length], int i => func(a[i])); ! <C,T,U> filter(a@Array, test) = a.filter(test); // Specialized versions for arrays. ! <T, U | T <: U> U[] filter(T[] a, T -> boolean test) { int l = a.length; int found = 0; ! U[] res = cast(new U[l]); a.foreach(T elem => *************** *** 108,112 **** } ! <Any T> void foreach(T[] a, (T)->void f) { for (int i = 0; i < a.length; i++) --- 108,112 ---- } ! <T> void foreach(T[] a, (T)->void f) { for (int i = 0; i < a.length; i++) *************** *** 114,118 **** } ! <Any T> ?T find(!T[] a, (!T)->boolean test) { for (int i = 0; i < a.length; i++) --- 114,118 ---- } ! <T> ?T find(!T[] a, (!T)->boolean test) { for (int i = 0; i < a.length; i++) *************** *** 143,147 **** like in the above example. */ ! <Any T, Any U | U <: T> U[] fill(T[] array, int->U value) // In particular, this is useful with T = ?X and U = !X. // The above type is more general, and useful when the component type is a type variable. --- 143,147 ---- like in the above example. */ ! <T, U | U <: T> U[] fill(T[] array, int->U value) // In particular, this is useful with T = ?X and U = !X. // The above type is more general, and useful when the component type is a type variable. Index: collections.nice =================================================================== RCS file: /cvsroot/nice/Nice/stdlib/nice/lang/collections.nice,v retrieving revision 1.51 retrieving revision 1.52 diff -C2 -d -r1.51 -r1.52 *** collections.nice 21 Jul 2003 17:24:14 -0000 1.51 --- collections.nice 24 Aug 2003 20:47:57 -0000 1.52 *************** *** 19,35 **** ****************************************************************/ ! <Any T> void foreach(java.util.Collection<T>, T->void); // Backward compatibility. `iter` will probably be removed sometime. ! <Any T> void iter(java.util.Collection<T> c, T->void f) = c.foreach(f); ! ! <java.util.Collection C, T, U> C<U> similarEmptyCollection(C<T>); ! <java.util.Collection C, Any T, Any U> C<U> map(C<T>, T->U); ! // A more precise type for filter would be: ! // <Collection C, T, U | T <: U> C<U> filter(C<T>, T -> boolean); ! // This would allow List<A> lA = lB.filter(...) where B is a subclass of A. ! <java.util.Collection C, Any T> C<T> filter(C<T>, T->boolean); /** Return a collection containing all elements for which converter --- 19,31 ---- ****************************************************************/ ! <T> void foreach(Collection<T> coll, T->void func); // Backward compatibility. `iter` will probably be removed sometime. ! <T> void iter(Collection<T> coll, T->void func) = coll.foreach(func); ! <Collection C, T, U> C<U> similarEmptyCollection(C<T>); ! <Collection C, T, U> C<U> map(C<T> coll, T->U func); ! <Collection C, T, U | T <: U> C<U> filter(C<T>, T->boolean); /** Return a collection containing all elements for which converter *************** *** 42,61 **** { C<!U> res = source.similarEmptyCollection(); ! source.foreach(T elem => { ! ?U converted = converter(elem); ! if (converted != null) ! res.add(converted); ! }); return res; } - /** @deprecated */ ! <T> void keep (Collection<T> c, T->boolean test) = c.retain(test); /** Modifies c, only keeping the elements for which test returns true. */ ! <T> void retain (Collection<T> c, T->boolean test) { ! Iterator<T> i = c.iterator(); while (i.hasNext()) { --- 38,57 ---- { C<!U> res = source.similarEmptyCollection(); ! for(T elem : source) ! { ! ?U converted = converter(elem); ! if (converted != null) ! res.add(converted); ! } return res; } /** @deprecated */ ! <T> void keep(Collection<T> coll, T->boolean test) = coll.retain(test); /** Modifies c, only keeping the elements for which test returns true. */ ! <T> void retain(Collection<T> coll, T->boolean test) { ! Iterator<T> i = coll.iterator(); while (i.hasNext()) { *************** *** 67,73 **** /** Modifies c, removing the elements for which test returns true. */ ! <T> void remove (Collection<T> c, T->boolean test) { ! Iterator<T> i = c.iterator(); while (i.hasNext()) { --- 63,69 ---- /** Modifies c, removing the elements for which test returns true. */ ! <T> void remove(Collection<T> coll, T->boolean test) { ! Iterator<T> i = coll.iterator(); while (i.hasNext()) { *************** *** 78,102 **** } ! ! <Any T, Any U> U foldLeft(java.util.List<T> l, (U, T)->U f, U init) { U res = init; ! l.foreach(T elem => { res = f(res, elem); }); return res; } ! <Any T, Any U> U foldRight(java.util.List<T> l, (T, U)->U f, U init) = ! foldRight(l, f, init, 0); ! private <Any T, Any U> ! U foldRight(java.util.List<T> l, (T, U)->U f, U init, int from) { ! if (from < l.size()) ! return f(l[from], l.foldRight(f, init, from + 1)); else return init; } ! <Any T> List<T> slice(List<T> list, int from = 0, int to = -1) { if (to == -1) --- 74,99 ---- } ! <T, U> U foldLeft(List<T> list, (U, T)->U func, U init) { U res = init; ! for (T elem : list) ! res = func(res, elem); ! return res; } ! <T, U> U foldRight(List<T> list, (T, U)->U func, U init) = ! foldRight(list, func, init, 0); ! private <T, U> ! U foldRight(List<T> list, (T, U)->U func, U init, int from) { ! if (from < list.size()) ! return func(list[from], list.foldRight(func, init, from + 1)); else return init; } ! <T> List<T> slice(List<T> list, int from = 0, int to = -1) { if (to == -1) *************** *** 106,109 **** --- 103,107 ---- for (int i = from; i <= to; i++) res.add(list[i]); + return res; } *************** *** 113,145 **** ****************************************************************/ ! /** ! Default implementation, using an Iterator. ! */ ! <T> foreach(c...@ja...llection, f) { ! for (java.util.Iterator<T> i = c.iterator(); i.hasNext();) ! f(i.next()); } ! /** ! A more efficient implementation for lists: doesn't allocate an Iterator. ! */ ! foreach(l...@ja...st, f) { ! for (int i = 0; i < l.size; i++) ! f(l.get(i)); ! } - <C,T,U> map(c, f) - { - C<U> res = c.similarEmptyCollection(); - c.foreach(T elem => { res.add(f(elem)); }); return res; } ! <C,T> filter(c, test) { ! C<T> res = c.similarEmptyCollection(); ! c.foreach(T elem => { if(test(elem)) res.add(elem); }); return res; } --- 111,136 ---- ****************************************************************/ ! <T> foreach(co...@ja...llection, func) { ! for (T elem : coll) ! func(elem); } ! <C,T,U> map(coll, func) { ! C<U> res = coll.similarEmptyCollection(); ! for (T elem : coll) ! res.add(func(elem)); return res; } ! <C,T,U> filter(coll, test) { ! C<U> res = coll.similarEmptyCollection(); ! for (T elem : coll) ! if (test(elem)) ! res.add(elem); ! return res; } *************** *** 183,192 **** /** @deprecated */ ! <Any T> boolean has (java.util.List<T> s, T->boolean test) = s.contains(test); ! <Any T> boolean contains (java.util.List<T> s, T->boolean test) { ! for (int i = 0; i < s.size(); i++) ! if (test(s[i])) return true; --- 174,210 ---- /** @deprecated */ ! <T> boolean has (Collection<T> coll, T->boolean test) = coll.contains(test); ! <T> boolean contains (Collection<T> coll, T->boolean test) { ! for (T elem : coll) ! if (test(elem)) ! return true; ! ! return false; ! } ! ! <T, U, V | T <: V, U <: V> boolean containsAny (Collection<T> coll1, Collection<U> coll2) ! { ! for (T elem : coll1) ! if (coll2.contains(elem)) ! return true; ! ! return false; ! } ! ! <T> int count (Collection<T> coll, T->boolean test) ! { ! int res = 0; ! for (T elem : coll) ! if (test(elem)) res++; ! ! return res; ! } ! ! <T> boolean any (Collection<T> coll, T->boolean test) ! { ! for (T elem : coll) ! if (test(elem)) return true; *************** *** 194,205 **** } /** Find the first element that passes the given test. @throw java.util.NoSuchElementException if there is no such element. */ ! <Any T> T find (java.util.List<T> s, T->boolean test) { ! for (int i = 0; i < s.size(); i++) ! if (test(s[i])) ! return s[i]; throw new java.util.NoSuchElementException(); --- 212,241 ---- } + <T> boolean all (Collection<T> coll, T->boolean test) + { + for (T elem : coll) + if (!test(elem)) + return false; + + return true; + } + + <T> boolean none (Collection<T> coll, T->boolean test) + { + for (T elem : coll) + if (test(elem)) + return false; + + return true; + } + /** Find the first element that passes the given test. @throw java.util.NoSuchElementException if there is no such element. */ ! <T> T find (List<T> list, T->boolean test) { ! for (T elem : list) ! if (test(elem)) ! return elem; throw new java.util.NoSuchElementException(); *************** *** 209,217 **** @throw java.util.NoSuchElementException if there is no such element. */ ! <Any T> T findLast (java.util.List<T> s, T->boolean test) { ! for (int i = s.size(); --i >= 0;) ! if (test(s[i])) ! return s[i]; throw new java.util.NoSuchElementException(); --- 245,253 ---- @throw java.util.NoSuchElementException if there is no such element. */ ! <T> T findLast (List<T> list, T->boolean test) { ! for (int i = list.size(); --i >= 0;) ! if (test(list[i])) ! return list[i]; throw new java.util.NoSuchElementException(); *************** *** 221,229 **** Returns <code>null</code> if there is no such element. */ ! <Any T> ?T search (java.util.List<!T> s, !T->boolean test) { ! for (int i = 0; i < s.size(); i++) ! if (test(s[i])) ! return s[i]; return null; --- 257,265 ---- Returns <code>null</code> if there is no such element. */ ! <Any T> ?T search (java.util.List<!T> list, !T->boolean test) { ! for (!T elem : list) ! if (test(elem)) ! return elem; return null; *************** *** 233,237 **** Returns <code>null</code> if there is no such element. */ ! <Any T> ?T searchLast (java.util.List<!T> s, !T->boolean test) { for (int i = s.size(); --i >= 0;) --- 269,273 ---- Returns <code>null</code> if there is no such element. */ ! <T> ?T searchLast (List<!T> s, !T->boolean test) { for (int i = s.size(); --i >= 0;) *************** *** 243,261 **** /** @deprecated */ ! <Any T> int findIndex (List<T> s, T->boolean test) = s.findIndex(test); ! <Any T> int indexOf (List<T> s, T->boolean test) { ! for (int i = 0; i < s.size(); i++) ! if (test(s[i])) return i; ! throw new java.util.NoSuchElementException(); } ! boolean or(java.util.List<boolean> s) { ! for (int i = 0; i < s.size(); i++) ! if (s[i]) return true; --- 279,297 ---- /** @deprecated */ ! <T> int findIndex (List<T> list, T->boolean test) = list.findIndex(test); ! <T> int indexOf (List<T> list, T->boolean test) { ! for (int i = 0; i < list.size(); i++) ! if (test(list[i])) return i; ! throw new NoSuchElementException(); } ! boolean or(List<boolean> list) { ! for (boolean elem : list) ! if (elem) return true; *************** *** 263,273 **** } ! long max(java.util.List<long> s) requires !s.isEmpty() { long res = Long.MIN_VALUE; ! for(int i = 0; i < s.size(); i++) ! if (s[i] > res) ! res = s[i]; return res; --- 299,351 ---- } ! boolean and(List<boolean> list) ! { ! for (boolean elem : list) ! if (!elem) ! return false; ! ! return true; ! } ! ! long max(List<long> list) requires !list.isEmpty() { long res = Long.MIN_VALUE; ! for(long elem : list) ! if (elem > res) ! res = elem; ! ! return res; ! } ! ! long min(List<long> list) requires !list.isEmpty() ! { ! long res = Long.MAX_VALUE; ! ! for(long elem : list) ! if (elem < res) ! res = elem; ! ! return res; ! } ! ! int max(List<int> list) requires !list.isEmpty() ! { ! int res = Integer.MIN_VALUE; ! ! for(int elem : list) ! if (elem > res) ! res = elem; ! ! return res; ! } ! ! int min(List<int> list) requires !list.isEmpty() ! { ! int res = Integer.MAX_VALUE; ! ! for(int elem : list) ! if (elem < res) ! res = elem; return res; *************** *** 284,320 **** /**************************************************************** ! * Printing ****************************************************************/ ! /* ! We need overriding of native methods for this ! toString(s@List) { ! if (s.size() == 0) ! return "[]"; ! StringBuffer buf = new StringBuffer(); ! buf.append("["); ! buf.append(s[0].toString()); ! for (int i=1; i<s.size(); i++) ! { ! buf.append(", "); ! buf.append(s[i].toString()); ! } ! buf.append("]"); ! return buf.toString(); } - */ /**************************************************************** ! * toArray ****************************************************************/ ! <Any T> T[] toArray(java.util.Collection<T> c) { ! T[] res = cast(new T[c.size()]); - int i = 0; - c.foreach(T elem => res[i++] = elem); return res; } --- 362,439 ---- /**************************************************************** ! * toArray ****************************************************************/ ! <T, U | T <: U> U[] toArray(Collection<T> coll) { ! U[] res = cast(new U[coll.size()]); ! int i = 0; ! for (T elem : coll) ! res[i++] = elem; ! return res; ! } ! ! <T, U> U[] mapToArray(List<T> list, T->U func) ! { ! U[] res = cast(new U[list.size()]); ! int i = 0; ! for (T elem : list) ! res[i++] = func(elem); ! ! return res; } /**************************************************************** ! * Set operations ****************************************************************/ ! <T, U, V | U <: T, V <: T> Set<T> union(Set<U> set1, Set<V> set2) { ! let Set<T> res = new HashSet(set1); ! res.addAll(set2); ! return res; ! } ! ! <T, U, V | U <: T, V <: T> Set<T> intersection(Set<U> set1, Set<V> set2) ! { ! Set<T> res = new HashSet(); ! if (set1.size() > set2.size()) ! { ! for (V elem : set2) ! if (set1.contains(elem)) ! res.add(elem); ! } ! else ! { ! for (U elem : set1) ! if (set2.contains(elem)) ! res.add(elem); ! } ! ! return res; ! } ! ! <T, U, V | U <: T, V <: T> Set<T> disjunction(Set<U> set1, Set<V> set2) ! { ! Set<T> res = new HashSet(); ! for (U elem : set1) ! if (! set2.contains(elem)) ! res.add(elem); ! ! for (V elem : set2) ! if (! set1.contains(elem)) ! res.add(elem); ! ! return res; ! } ! ! <T, U, V, W | U <: T, V <: W, W <: U> Set<T> difference(Set<U> set1, Set<V> set2) ! { ! Set<T> res = new HashSet(); ! for (U elem : set1) ! if (! set2.contains(elem)) ! res.add(elem); return res; } |