001/*
002 * Java Genetic Algorithm Library (jenetics-7.2.0).
003 * Copyright (c) 2007-2023 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics.internal.collection;
021
022import static java.util.Objects.requireNonNull;
023
024import java.util.Spliterator;
025import java.util.function.Consumer;
026
027import io.jenetics.util.BaseSeq;
028
029/**
030 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
031 * @since 3.0
032 * @version 5.2
033 */
034public final class BaseSeqSpliterator<T> implements Spliterator<T> {
035
036        private final BaseSeq<T> _seq;
037        private final int _fence;
038        private int _index;
039
040        public BaseSeqSpliterator(
041                final BaseSeq<T> seq,
042                final int origin,
043                final int fence
044        ) {
045                _seq = requireNonNull(seq);
046                _index = origin;
047                _fence = fence;
048        }
049
050        public BaseSeqSpliterator(final BaseSeq<T> seq) {
051                this(seq, 0, seq.length());
052        }
053
054        @Override
055        public void forEachRemaining(final Consumer<? super T> action) {
056                requireNonNull(action);
057
058                BaseSeq<T> seq;
059                int i;
060                int hi;
061
062                if ((seq = _seq).length() >= (hi = _fence) &&
063                        (i = _index) >= 0 && i < (_index = hi))
064                {
065                        do {
066                                action.accept(seq.get(i));
067                        } while (++i < hi);
068                }
069        }
070
071        @Override
072        public boolean tryAdvance(final Consumer<? super T> action) {
073                if (_index >= 0 && _index < _fence) {
074                        action.accept(_seq.get(_index++));
075                        return true;
076                }
077                return false;
078        }
079
080        @Override
081        public Spliterator<T> trySplit() {
082                final int lo = _index;
083                final int mid = (lo + _fence) >>> 1;
084
085                return (lo >= mid)
086                        ? null
087                        : new BaseSeqSpliterator<>(_seq, lo, _index = mid);
088        }
089
090        @Override
091        public long estimateSize() {
092                return _fence - _index;
093        }
094
095        @Override
096        public int characteristics() {
097                return Spliterator.NONNULL | Spliterator.SIZED | Spliterator.SUBSIZED;
098        }
099
100}