/* ----------------------------------------------------------------------------- Copyright 2019-2021 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] define AutoReadIterator { @value ReadAt<#x> container @value Int position from (container) { return fromOffset<#y>(container,0) } fromOffset (container,position) { Int position2 <- position if (position < 0) { position2 <- -1 } if (position >= container.size() || container.size() == 0) { position2 <- container.size() } return AutoReadIterator<#y>{ container, position } } readCurrent () { if (pastForwardEnd() || pastReverseEnd()) { \ LazyStream.new() .append("Position ") .append(position) .append(" is out of bounds") .writeTo(SimpleOutput.error()) } return container.readAt(position) } forward () { return fromOffset<#x>(container,position+1) } pastForwardEnd () { return position >= container.size() } reverse () { return fromOffset<#x>(container,position-1) } pastReverseEnd () { return position < 0 || container.size() == 0 } } define AutoWriteIterator { @value WriteAt<#x> container @value Int position from (container) { return fromOffset<#y>(container,0) } fromOffset (container,position) { Int position2 <- position if (position < 0) { position2 <- -1 } if (position >= container.size() || container.size() == 0) { position2 <- container.size() } return AutoWriteIterator<#y>{ container, position } } writeCurrent (x) { if (pastForwardEnd() || pastReverseEnd()) { \ LazyStream.new() .append("Position ") .append(position) .append(" is out of bounds") .writeTo(SimpleOutput.error()) } \ container.writeAt(position,x) return self } forward () { return fromOffset<#x>(container,position+1) } pastForwardEnd () { return position >= container.size() } reverse () { return fromOffset<#x>(container,position-1) } pastReverseEnd () { return position < 0 || container.size() == 0 } } define AutoIterator { @value [ReadAt<#x>&WriteAt<#x>] container @value Int position from (container) { return fromOffset<#y>(container,0) } fromOffset (container,position) { Int position2 <- position if (position < 0) { position2 <- -1 } if (position >= container.size() || container.size() == 0) { position2 <- container.size() } return AutoIterator<#y>{ container, position } } readCurrent () { if (pastForwardEnd() || pastReverseEnd()) { \ LazyStream.new() .append("Position ") .append(position) .append(" is out of bounds") .writeTo(SimpleOutput.error()) } return container.readAt(position) } writeCurrent (x) { if (pastForwardEnd() || pastReverseEnd()) { \ LazyStream.new() .append("Position ") .append(position) .append(" is out of bounds") .writeTo(SimpleOutput.error()) } \ container.writeAt(position,x) return self } forward () { return fromOffset<#x>(container,position+1) } pastForwardEnd () { return position >= container.size() } reverse () { return fromOffset<#x>(container,position-1) } pastReverseEnd () { return position < 0 || container.size() == 0 } }