001 /*
002 * Java Genetic Algorithm Library (jenetics-3.5.0).
003 * Copyright (c) 2007-2016 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 static java.util.Objects.requireNonNull;
023
024 import java.io.Serializable;
025
026 import org.jenetics.internal.collection.Array;
027 import org.jenetics.internal.collection.ArrayISeq;
028 import org.jenetics.internal.collection.ArrayMSeq;
029 import org.jenetics.internal.util.bit;
030 import org.jenetics.internal.util.require;
031
032 import org.jenetics.util.MSeq;
033
034 /**
035 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
036 * @since 1.4
037 * @version 3.4
038 */
039 final class BitGeneMSeq extends ArrayMSeq<BitGene> {
040
041 private static final long serialVersionUID = 1L;
042
043 // Primary constructor.
044 private BitGeneMSeq(final Array<BitGene> array) {
045 super(array);
046 assert array.store() instanceof BitGeneStore;
047 }
048
049 @Override
050 public void swap(final int i, final int j) {
051 array.checkIndex(i);
052 array.checkIndex(j);
053 array.copyIfSealed();
054
055 final byte[] bytes = ((BitGeneStore)array.store()).array;
056 final boolean temp = bit.get(bytes, i);
057 bit.set(bytes, i, bit.get(bytes, j));
058 bit.set(bytes, j, temp);
059 }
060
061 @Override
062 public void swap(
063 final int start, final int end,
064 final MSeq<BitGene> other, final int otherStart
065 ) {
066 if (other instanceof BitGeneMSeq) {
067 checkIndex(start, end, otherStart, other.length());
068 final BitGeneMSeq otherMSeq = (BitGeneMSeq)other;
069 final BitGeneStore thisStore = (BitGeneStore)array.store();
070 final BitGeneStore otherStore = (BitGeneStore)otherMSeq.array.store();
071
072 array.copyIfSealed();
073 otherMSeq.array.copyIfSealed();
074 thisStore.swap(start, end, otherStore, otherStart);
075 } else {
076 super.swap(start, end, other, otherStart);
077 }
078 }
079
080 @Override
081 public BitGeneMSeq copy() {
082 return new BitGeneMSeq(array.copy());
083 }
084
085 @Override
086 public BitGeneISeq toISeq() {
087 return new BitGeneISeq(array.seal());
088 }
089
090 static BitGeneMSeq of(final byte[] genes, final int length) {
091 return new BitGeneMSeq(Array.of(BitGeneStore.of(genes, length)));
092 }
093
094 static BitGeneMSeq of(final Array<BitGene> array) {
095 return new BitGeneMSeq(array);
096 }
097
098 }
099
100 /**
101 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
102 * @since 1.4
103 * @version 3.4
104 */
105 final class BitGeneISeq extends ArrayISeq<BitGene> {
106 private static final long serialVersionUID = 1L;
107
108 // Primary constructor.
109 BitGeneISeq(final Array<BitGene> array) {
110 super(array);
111 assert array.store() instanceof BitGeneStore;
112 }
113
114 void copyTo(final byte[] array) {
115 final BitGeneStore store = (BitGeneStore)this.array.store();
116 System.arraycopy(store.array, 0, array, 0, store.array.length);
117 }
118
119 @Override
120 public BitGeneMSeq copy() {
121 return BitGeneMSeq.of(array.copy());
122 }
123
124 static BitGeneISeq of(final byte[] genes, final int length) {
125 return new BitGeneISeq(Array.of(BitGeneStore.of(genes, length)).seal());
126 }
127
128 }
129
130 /**
131 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
132 * @since 1.4
133 * @version 3.4
134 */
135 final class BitGeneStore implements Array.Store<BitGene>, Serializable {
136 private static final long serialVersionUID = 1L;
137
138 final byte[] array;
139 final int length;
140
141 // Primary constructor.
142 private BitGeneStore(final byte[] array, final int length) {
143 this.array = requireNonNull(array);
144 this.length = require.nonNegative(length);
145 }
146
147 @Override
148 public BitGene get(final int index) {
149 return BitGene.of(bit.get(array, index));
150 }
151
152 @Override
153 public void set(final int index, final BitGene value) {
154 bit.set(array, index, value.booleanValue());
155 }
156
157 void swap(
158 final int start, final int end,
159 final BitGeneStore other, final int otherStart
160 ) {
161 bit.swap(array, start, end, other.array, otherStart);
162 }
163
164 @Override
165 public BitGeneStore copy(final int from, final int until) {
166 return new BitGeneStore(bit.copy(array, from, until), until - from);
167 }
168
169 @Override
170 public BitGeneStore newInstance(final int length) {
171 return ofLength(length);
172 }
173
174 @Override
175 public int length() {
176 return length;
177 }
178
179
180 static BitGeneStore of(final byte[] array, final int length) {
181 return new BitGeneStore(array, length);
182 }
183
184 static BitGeneStore ofLength(final int length) {
185 return new BitGeneStore(bit.newArray(length), length);
186 }
187
188 }
|