BitGeneArray.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.6.0).
003  * Copyright (c) 2007-2014 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@gmx.at)
019  */
020 package org.jenetics;
021 
022 import org.jenetics.internal.util.ArrayProxy;
023 import org.jenetics.internal.util.ArrayProxyISeq;
024 import org.jenetics.internal.util.ArrayProxyMSeq;
025 
026 import org.jenetics.util.bit;
027 
028 /**
029  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
030  @since 1.4
031  @version 1.4 &mdash; <em>$Date: 2014-02-17 $</em>
032  */
033 final class BitGeneArray extends ArrayProxyMSeq<BitGene> {
034 
035     BitGeneArray(final Proxy proxy) {
036         super(proxy);
037     }
038 
039     BitGeneArray(final byte[] array, final int start, final int end) {
040         this(new Proxy(array, start, end));
041     }
042 
043     @Override
044     public BitGeneArray copy() {
045         return new BitGeneArray(((Proxy)_proxy).copy());
046     }
047 
048     @Override
049     public BitGeneISeq toISeq() {
050         return new BitGeneISeq((Proxy)_proxy.seal());
051     }
052 
053     static final class BitGeneISeq extends ArrayProxyISeq<BitGene> {
054         public BitGeneISeq(final Proxy proxy) {
055             super(proxy);
056         }
057 
058         void copyTo(final byte[] array) {
059             final Proxy proxy = (Proxy)_proxy;
060             System.arraycopy(proxy._array, 0, array, 0, proxy._array.length);
061         }
062 
063         @Override
064         public BitGeneArray copy() {
065             return new BitGeneArray(((Proxy)_proxy).copy());
066         }
067 
068     }
069 
070     static final class Proxy extends ArrayProxy<BitGene> {
071         private byte[] _array;
072         private boolean _sealed = false;
073 
074         Proxy(final byte[] array, final int start, final int end) {
075             super(start, end);
076             _array = array;
077         }
078 
079         Proxy(final int length) {
080             this(bit.newArray(length)0, length);
081         }
082 
083         @Override
084         public BitGene __get(final int absoluteIndex) {
085             return BitGene.of(bit.get(_array, absoluteIndex));
086         }
087 
088         @Override
089         public void __set(
090             final int absoluteIndex,
091             final BitGene value
092         ) {
093             bit.set(_array, absoluteIndex, value.booleanValue());
094         }
095 
096         @Override
097         public Proxy slice(final int from, final int until) {
098             return new Proxy(_array, from + _start, until + _start);
099         }
100 
101         @Override
102         public void swap(
103             final int from, final int until,
104             final ArrayProxy<BitGene> other, final int otherFrom
105         ) {
106             cloneIfSealed();
107             other.cloneIfSealed();
108 
109             if (other instanceof Proxy) {
110                 swap(from, until, (Proxy)other, otherFrom);
111             else {
112                 for (int i = (until - from); --i >= 0;) {
113                     final BitGene temp = uncheckedGet(i + from);
114                     uncheckedSet(i + from, other.uncheckedGet(otherFrom + i));
115                     other.uncheckedSet(otherFrom + i, temp);
116                 }
117             }
118         }
119 
120         private void swap(
121             final int start, final int end,
122             final Proxy other, final int otherStart
123         ) {
124             checkIndex(start, end);
125             other.checkIndex(otherStart, otherStart + (end - start));
126             cloneIfSealed();
127             other.cloneIfSealed();
128 
129             bit.swap(
130                 _array, start + _start, end + _start,
131                 other._array, otherStart + other._start
132             );
133         }
134 
135         @Override
136         public void cloneIfSealed() {
137             if (_sealed) {
138                 _array = _array.clone();
139                 _sealed = false;
140             }
141         }
142 
143         @Override
144         public Proxy seal() {
145             _sealed = true;
146             return new Proxy(_array, _start, _end);
147         }
148 
149         @Override
150         public Proxy copy() {
151             final Proxy proxy = new Proxy(_length);
152             if (_start == && _end == _length) {
153                 proxy._array = _array.clone();
154             else if (_start == 0) {
155                 System.arraycopy(
156                     _array, 0, proxy._array, 0, proxy._array.length
157                 );
158             else {
159                 for (int i = _length; --i >= 0;) {
160                     bit.set(proxy._array, i, bit.get(_array, i + _start));
161                 }
162             }
163             return proxy;
164         }
165 
166     }
167 
168 }