source: trunk/abcl/src/org/armedbear/lisp/BasicVector_CharBuffer.java

Last change on this file was 15734, checked in by Mark Evenson, 8 months ago

Implement vector-to-vector REPLACE as a primitive

File size: 9.3 KB
Line 
1/*
2 * BasicVector_CharBuffer.java
3 *
4 * Copyright (C) 2020 @easye
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 *
19 * As a special exception, the copyright holders of this library give you
20 * permission to link this library with independent modules to produce an
21 * executable, regardless of the license terms of these independent
22 * modules, and to copy and distribute the resulting executable under
23 * terms of your choice, provided that you also meet, for each linked
24 * independent module, the terms and conditions of the license of that
25 * module.  An independent module is a module which is not derived from
26 * or based on this library.  If you modify this library, you may extend
27 * this exception to your version of the library, but you are not
28 * obligated to do so.  If you do not wish to do so, delete this
29 * exception statement from your version.
30 */
31
32package org.armedbear.lisp;
33
34import static org.armedbear.lisp.Lisp.*;
35
36import java.nio.ByteBuffer;
37import java.nio.CharBuffer;
38
39/** A vector with specialized underlying storage for (unsigned-byte 16) */
40public final class BasicVector_CharBuffer 
41  extends AbstractVector
42{
43  private int capacity;
44  private CharBuffer elements;
45  private boolean directAllocation;
46
47  public BasicVector_CharBuffer(int capacity) {
48    this(capacity, false);
49  }
50 
51  public BasicVector_CharBuffer(int capacity, boolean directAllocation) {
52    this.directAllocation = directAllocation;
53    if (directAllocation) {
54      ByteBuffer b = ByteBuffer.allocateDirect(capacity * 2);
55      elements = b.asCharBuffer();
56    } else {
57      elements = CharBuffer.allocate(capacity);
58    }
59    this.capacity = capacity; 
60  }
61
62  public BasicVector_CharBuffer(LispObject[] array, boolean directAllocation) {
63    capacity = array.length;
64    this.directAllocation = directAllocation;
65    if (directAllocation) {
66      ByteBuffer b = ByteBuffer.allocateDirect(capacity * 2);
67      elements = b.asCharBuffer();
68    } else {
69      elements = CharBuffer.allocate(capacity);
70    }
71    for (int i = array.length; i-- > 0;) {
72      elements.put(i, (char)Fixnum.getValue(array[i])); // FIXME bulk copy
73    }
74  }
75
76  public BasicVector_CharBuffer(ByteBuffer buffer, boolean directAllocation) {
77    elements = buffer.asCharBuffer();
78    this.directAllocation = directAllocation;
79    capacity = ((java.nio.Buffer)buffer).limit() / 2;
80  }
81
82  public BasicVector_CharBuffer(CharBuffer buffer, boolean directAllocation) {
83    elements = buffer;
84    this.directAllocation = directAllocation;
85    capacity = ((java.nio.Buffer)buffer).limit();
86  }
87
88  @Override
89  public LispObject typeOf() {
90    return list(Symbol.SIMPLE_ARRAY, UNSIGNED_BYTE_16,
91                new Cons(Fixnum.getInstance(capacity)));
92  }
93
94  @Override
95  public LispObject classOf() {
96    return BuiltInClass.VECTOR;
97  }
98
99  @Override
100  public LispObject typep(LispObject type) {
101    if (type == Symbol.SIMPLE_ARRAY)
102      return T;
103    if (type == BuiltInClass.SIMPLE_ARRAY)
104      return T;
105    return super.typep(type);
106  }
107
108  @Override
109  public LispObject getElementType() {
110    return UNSIGNED_BYTE_16;
111  }
112
113  @Override
114  public boolean isSimpleVector() {
115    return false;
116  }
117
118  @Override
119  public boolean hasFillPointer() {
120    return false;
121  }
122
123  @Override
124  public boolean isAdjustable() {
125    return false;
126  }
127
128  @Override
129  public int capacity() {
130    return capacity;
131  }
132
133  @Override
134  public int length() {
135    return capacity;
136  }
137
138  @Override
139  public LispObject elt(int index) {
140    try {
141      return Fixnum.getInstance(elements.get(index));
142    } catch (IndexOutOfBoundsException e) {
143      badIndex(index, capacity);
144      return NIL; // Not reached.
145    }
146  }
147
148  // Ignores fill pointer.
149  @Override
150  public int aref(int index) {
151    try {
152      return elements.get(index);
153    } catch (ArrayIndexOutOfBoundsException e) {
154      badIndex(index, ((java.nio.Buffer)elements).limit()); // FIXME should implement method for length() contract
155      // Not reached.
156      return 0;
157    }
158  }
159
160  // Ignores fill pointer.
161  @Override
162  public LispObject AREF(int index) {
163    try {
164      return Fixnum.getInstance(elements.get(index));
165    } catch (IndexOutOfBoundsException e) {
166      badIndex(index, ((java.nio.Buffer)elements).limit());  // FIXME limit() --> capacity?
167      return NIL; // Not reached.
168    }
169  }
170
171  @Override
172  public void aset(int index, int n) {
173    try {
174      elements.put(index, (char)n);
175    } catch (IndexOutOfBoundsException e) {
176      badIndex(index, capacity);
177    }
178  }
179
180  @Override
181  public void aset(int index, LispObject obj) {
182    if (obj instanceof Fixnum) {
183      try {
184        elements.put(index, (char)((Fixnum)obj).value);
185      } catch (ArrayIndexOutOfBoundsException e) {
186        badIndex(index, capacity);
187      }
188    } else {
189      type_error(obj, UNSIGNED_BYTE_16);
190    }
191  }
192
193  @Override
194  public LispObject subseq(int start, int end) {
195    BasicVector_CharBuffer v = new BasicVector_CharBuffer(end - start);
196    int i = start, j = 0;
197    try {
198      while (i < end) {
199        v.elements.put(j++, elements.get(i++));
200      }
201      return v;
202    } catch (IndexOutOfBoundsException e) {
203      return error(new TypeError("Array index out of bounds: " + i + "."));
204    }
205  }
206
207  @Override
208  public void fill(LispObject obj) {
209    if (!(obj instanceof Fixnum)) {
210      type_error(obj, Symbol.FIXNUM);
211      // Not reached.
212      return;
213    }
214    int n = ((Fixnum) obj).value;
215    if (n < 0 || n > 65535) {
216      type_error(obj, UNSIGNED_BYTE_16);
217      // Not reached.
218      return;
219    }
220    char m = (char) n;
221    for (int i = capacity; i-- > 0;) {
222      elements.put(i, m); // FASTER!!!
223    }
224  }
225
226  @Override
227  public void shrink(int n) {
228    // One cannot shrink the underlying ByteBuffer physically, and
229    // the elements field may refer to malloc()d memory that we
230    // shouldn't touch, so use the java.nio.Buffer limit pointer.
231    // Not totally sure that this strategy will work out

232    if (n < length()) {
233      ((java.nio.Buffer)elements).limit(n);
234      capacity = n;
235      return;
236    }
237    if (n == capacity) {
238      return;
239    }
240    error(new LispError("End of native shrink routine:  shouldn't be reachable."));
241  }
242
243  @Override
244  public LispObject reverse() {
245    BasicVector_CharBuffer result = new BasicVector_CharBuffer(capacity);
246    int i, j;
247    for (i = 0, j = capacity - 1; i < capacity; i++, j--){
248      result.elements.put(i, elements.get(j));
249    }
250    return result;
251  }
252
253  @Override
254  public LispObject nreverse() {
255    int i = 0;
256    int j = capacity - 1;
257    while (i < j) {
258      char temp = elements.get(i);
259      elements.put(i, elements.get(j));
260      elements.put(j, temp);
261      ++i;
262      --j;
263    }
264    return this;
265  }
266
267  @Override
268  public AbstractVector adjustArray(int newCapacity,
269                                    LispObject initialElement,
270                                    LispObject initialContents) {
271    if (initialContents != null) {
272      LispObject[] newElements = new LispObject[newCapacity];
273      if (initialContents.listp()) {
274        LispObject list = initialContents;
275        for (int i = 0; i < newCapacity; i++) {
276          newElements[i] = list.car();
277          list = list.cdr();
278        }
279      } else if (initialContents.vectorp()) {
280        for (int i = 0; i < newCapacity; i++)
281          newElements[i] = initialContents.elt(i);
282      } else
283        type_error(initialContents, Symbol.SEQUENCE);
284      return new BasicVector_CharBuffer(newElements, directAllocation);
285    }
286    if (capacity != newCapacity) { // FIXME: more efficient
287      LispObject[] newElements = new LispObject[newCapacity];
288      System.arraycopy(elements.array(), 0, newElements, 0,
289                       Math.min(capacity, newCapacity));
290      if (initialElement != null) {
291        for (int i = capacity; i < newCapacity; i++)
292          newElements[i] = initialElement;
293      }
294      return new BasicVector_CharBuffer(newElements, directAllocation);
295    }
296    // No change.
297    return this;
298  }
299
300  @Override
301  public AbstractVector adjustArray(int newCapacity,
302                                    AbstractArray displacedTo,
303                                    int displacement) {
304    return new ComplexVector(newCapacity, displacedTo, displacement);
305  }
306
307  @Override
308  public AbstractVector replace(AbstractVector source,
309                                int targetStart, int targetEnd,
310                                int sourceStart, int sourceEnd)
311  {
312    if (source instanceof BasicVector_CharBuffer) {
313      CharBuffer view = ((BasicVector_CharBuffer)source).elements.asReadOnlyBuffer();
314      view.position(sourceStart);
315      view.limit(sourceEnd);
316      elements.position(targetStart);
317      elements.put(view);
318      elements.position(0);
319      return this;
320    } else {
321      return super.replace(source, targetStart, targetEnd, sourceStart, sourceEnd);
322    }
323  }
324}
325
326
327
Note: See TracBrowser for help on using the repository browser.