Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

114
LINES

< > BotCompany Repo | #1035622 // ShortBuffer

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (9341L/51K).

sclass ShortBuffer is Iterable<Short> {
  short[] data;
  int size;
  
  *() {}
  *(int size) { if (size != 0) data = new short[size]; }
  *(Iterable<Short> l) {
    if (l cast Cl) allocate(l.size());
    addAll(l);
  }
  
  void add(short i) {
    if (size >= lShortArray(data)) {
      data = resizeShortArray(data, Math.max(1, toShort(Math.min(maximumSafeArraySize(), lShortArray(data)*2L))));
      if (size >= data.length) fail("ShortBuffer too large: " + size);
    }
    data[size++] = i;
  }
  
  void allocate(int n) {
    data = resizeShortArray(data, max(n, size()));
  }
  
  void setSize(int n) {
    data = resizeShortArray(data, n);
    size = min(l(data), size);
  }
  
  void addAll(Iterable<Short> l) {
    if (l != null) for (i : l) add(i);
  }
  
  void addAll(short... l) {
    if (l != null) for (short i : l) add(i);
  }
  
  // Note: may return the internal array
  short[] toArray aka toShortArray() {
    ret size == 0 ? null : resizeShortArray(data, size);
  }
  
  // abandoned version
  /*L<Short> toList() {
    ret intArrayToList(data, 0, size);
  }*/

  // now all these return a virtual list
  L<Short> toList aka asList aka asVirtualList() {
    ret new RandomAccessAbstractList<Short> {
      public int size() { ret size; }
      public Short get(int i) { ret ShortBuffer.this.get(i); }
      public Short set(int i, Short val) {
        Short a = get(i);
        data[i] = val;
        ret a;
      }
    };
  }
  
  void reset { size = 0; }
  void clear { reset(); }
  
  int size() { ret size; }
  bool isEmpty() { ret size == 0; }
  
  short get(int idx) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    ret data[idx];
  }
  
  void set(int idx, short value) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    data[idx] = value;
  }
  
  short popLast() {
    if (size == 0) fail("empty buffer");
    ret data[--size];
  }
  
  short last() { ret data[size-1]; }
  short nextToLast() { ret data[size-2]; }
  
  toString { ret squareBracket(joinWithSpace(toList())); }
  
  public Iterator<Short> iterator() {
    ret new ItIt<Short> {
      int i = 0;
      
      public bool hasNext() { ret i < size; }
      public Short next() {
        //if (!hasNext()) fail("Index out of bounds: " + i);
        ret data[i++];
      }
    };
  }
  
  public ShortIterator shortIterator() {
    ret new ShortIterator {
      int i = 0;
      
      public bool hasNext() { ret i < size; }
      public short next() {
        //if (!hasNext()) fail("Index out of bounds: " + i);
        ret data[i++];
      }
      toString { ret "Iterator@" + i + " over " + ShortBuffer.this; }
    };
  }
  
  void trimToSize {
    data = resizeShortArray(data, size);
  }
}

Author comment

Began life as a copy of #1028793

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035622
Snippet name: ShortBuffer
Eternal ID of this version: #1035622/5
Text MD5: d831103e68120a55ccc9df816e9c66c9
Transpilation MD5: 7b4eef29316b8613c7dc503fc7767ab2
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-26 23:11:53
Source code size: 2853 bytes / 114 lines
Pitched / IR pitched: No / No
Views / Downloads: 170 / 258
Version history: 4 change(s)
Referenced in: [show references]