[Nice-commit] Nice/stdlib/nice/lang slice.nice,NONE,1.1 ForInIters.nice,1.7,1.8 collections.nice,1.7
Brought to you by:
bonniot
From: Bryn K. <xo...@us...> - 2004-08-13 06:24:49
|
Update of /cvsroot/nice/Nice/stdlib/nice/lang In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24699/stdlib/nice/lang Modified Files: ForInIters.nice collections.nice range.nice Added Files: slice.nice Log Message: Added new [] overloads for lists - slicing, relative-to-end indexing, filters, etc. Index: collections.nice =================================================================== RCS file: /cvsroot/nice/Nice/stdlib/nice/lang/collections.nice,v retrieving revision 1.71 retrieving revision 1.72 diff -C2 -d -r1.71 -r1.72 *** collections.nice 29 Jul 2004 12:36:12 -0000 1.71 --- collections.nice 13 Aug 2004 06:24:39 -0000 1.72 *************** *** 172,175 **** --- 172,176 ---- similarEmptyCollection(#HashSet c) = new HashSet(c.size()); + /**************************************************************** * Sort Index: range.nice =================================================================== RCS file: /cvsroot/nice/Nice/stdlib/nice/lang/range.nice,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** range.nice 19 Nov 2003 16:04:45 -0000 1.2 --- range.nice 13 Aug 2004 06:24:39 -0000 1.3 *************** *** 18,58 **** package nice.lang; - public class Range<T | T <: int, int <: T> extends AbstractList<T> { ! private final int begin; ! private final int end; isEmpty() = false; ! size() = 1+end-begin; get(index) { ! if (index < 0 || index >= 1+end-begin) throw new IndexOutOfBoundsException(); ! return begin + index; } add(elem) = throw new UnsupportedOperationException(); ! contains(elem) = begin <= cast(elem) <= end; - iterator() = new RangeIterator(range: this, pos: begin-1); ! listIterator() = new RangeIterator(range: this, pos: begin-1); ! listIterator(index) = new RangeIterator(range: this, pos: begin + index-1); } ! public class RangeIterator<T | T <: int, int <: T> implements ListIterator<T> { ! final Range<int> range; int pos; ! hasNext() = pos+1 <= range.end; next() = ++pos; ! hasPrevious() = pos-1 >= range.begin; previous() = --pos; ! nextIndex() = (pos-range.begin) + 1; ! previousIndex() = pos - range.begin; remove() = throw new UnsupportedOperationException(); --- 18,82 ---- package nice.lang; ! public class OpenRange<T | T <: int, int <: T> extends Slice<T> ! { ! { ! assert !begin.relativeToEnd: ! "OpenRange objects cannot be relative to a list end"; ! assert !end.relativeToEnd || end.index == 0: ! "OpenRange objects cannot be relative to a list end"; ! } ! Iterator<T> openIterator() = new OpenRangeIterator(range: this, ! pos: begin.index-1); ! } ! ! public class Range<T | T <: int, int <: T> extends OpenRange<T> ! implements List<T> { ! ! { ! assert !end.relativeToEnd: ! "Range objects must have fixed dimensions"; ! } isEmpty() = false; ! size() = 1+end.index-begin.index; get(index) { ! if (index < 0 || index >= 1+end.index-begin.index) ! throw new IndexOutOfBoundsException(); ! return int(begin.index) + int(index); } add(elem) = throw new UnsupportedOperationException(); ! contains(elem) = begin.index <= cast(elem) <= end.index; ! iterator() = new RangeIterator(range: this, pos: begin.index - 1); ! listIterator() = new RangeIterator(range: this, pos: begin.index-1); ! listIterator(index) = new RangeIterator(range: this, pos: begin.index + index-1); } ! public class OpenRangeIterator<T | T <: int, int <: T> implements Iterator<T> { ! final OpenRange<T> range; int pos; ! hasNext() = pos+1 <= range.end.index || (range.end.isInfinite); next() = ++pos; ! remove() = throw new UnsupportedOperationException(); ! } ! ! public class RangeIterator<T | T <: int, int <: T> extends OpenRangeIterator ! implements ListIterator<T> { ! ! hasNext() = pos+1 <= range.end.index; ! hasPrevious() = pos-1 >= range.begin.index; previous() = --pos; ! nextIndex() = (pos-range.begin.index) + 1; ! previousIndex() = pos - range.begin.index; remove() = throw new UnsupportedOperationException(); *************** *** 61,66 **** } - public Range<int> `..`(int begin, int end) requires begin <= end - { - return new Range(begin: begin, end: end); - } --- 85,86 ---- --- NEW FILE: slice.nice --- /**************************************************************************** * N I C E * * A high-level object-oriented research language * * (c) Daniel Bonniot 2003 * * * * This package is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * As a special exception, the copyright holders of this library give you * * permission to link this library with independent modules to produce an * * executable, regardless of the license terms of these independent * * modules, and to copy and distribute the resulting executable under * * terms of your choice. * ****************************************************************************/ package nice.lang; public class Index { final boolean relativeToEnd; final int index = 0; <U> int normalize(List<U> list) { if (relativeToEnd) return (list.size() - 1) + index; else return index; } boolean isInfinite() = relativeToEnd && index == 0; } Index `@`() = new Index(index: 0, relativeToEnd: true); public Index `-`(Index index, int offset) requires index.relativeToEnd { return new Index(index: index.index - offset, relativeToEnd: true); } public class Slice<T | T <: int, int <: T> { private final Index begin; private final Index end; <U> (int,int) normalize(List<U> list) { let normalBegin = begin.normalize(list); let normalEnd = end.normalize(list); assert normalEnd >= normalBegin : "Beginning index cannot be greater than ending index"; return (normalBegin, normalEnd); } } public OpenRange<int> `..`(int begin, ?int end) requires end == null || end >= 0, begin >= 0 { let beginIndex = new Index(index: begin, relativeToEnd: false); let endIndex = end == null ? new Index(index: 0, relativeToEnd: true) :new Index(index: end, relativeToEnd: false); if (end == null) return new OpenRange(begin: beginIndex, end: endIndex); else return new Range(begin: beginIndex, end: endIndex); } public Slice<int> `..` (int begin, Index end) requires begin >= 0 { let beginIndex = new Index(index: begin, relativeToEnd: false); return new Slice(begin: beginIndex, end: end); } public Slice<int> `..` (Index begin, ?int end) { let endIndex = new Index(index: end == null ? 0 : end, relativeToEnd: end == null); return new Slice(begin: begin, end: endIndex); } public Slice<int> `..` (Index begin, Index end) { return new Slice(begin: begin, end: end); } Index: ForInIters.nice =================================================================== RCS file: /cvsroot/nice/Nice/stdlib/nice/lang/ForInIters.nice,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** ForInIters.nice 17 Jan 2004 16:35:43 -0000 1.7 --- ForInIters.nice 13 Aug 2004 06:24:39 -0000 1.8 *************** *** 25,29 **** Iterator<char> forIterator(String s) = new StringForIterator(str: s); Iterator<char> forIterator(StringBuffer sb) = new StringBufferForIterator(strb: sb); ! Iterator<int> forIterator(Range<int> range) = range.iterator(); //The implementations of additional Iterators --- 25,31 ---- Iterator<char> forIterator(String s) = new StringForIterator(str: s); Iterator<char> forIterator(StringBuffer sb) = new StringBufferForIterator(strb: sb); ! ! <T | T <: int, int <: T> ! Iterator<T> forIterator(OpenRange<T> range) = range.openIterator(); //The implementations of additional Iterators |