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

122
LINES

< > BotCompany Repo | #1028793 // IntBuffer

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

Libraryless. Click here for Pure Java version (9395L/52K).

sclass IntBuffer implements Iterable<Int> {
  int[] data;
  int size;
  
  *() {}
  *(int size) { if (size != 0) data = new int[size]; }
  *(Iterable<Int> l) {
    if (l cast Cl) allocate(l.size());
    addAll(l);
  }
  
  void add(int i) {
    if (size >= lIntArray(data)) {
      data = resizeIntArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lIntArray(data)*2L))));
      if (size >= data.length) fail("IntBuffer too large: " + size);
    }
    data[size++] = i;
  }
  
  void allocate(int n) {
    data = resizeIntArray(data, max(n, size()));
  }
  
  void setSize(int n) {
    data = resizeIntArray(data, n);
    size = min(l(data), size);
  }
  
  void addAll(Iterable<Int> l) {
    if (l != null) for (int i : l) add(i);
  }
  
  void addAll(int... l) {
    if (l != null) for (int i : l) add(i);
  }
  
  // Note: may return the internal array
  int[] toArray aka toIntArray() {
    ret size == 0 ? null : resizeIntArray(data, size);
  }
  
  int[] subArray(int from, int to) {
    ret subIntArray(data, from, min(to, size));
  }
  
  int[] toArrayNonNull() {
    ret unnull(toArray());
  }
  
  // abandoned version
  /*L<Int> toList() {
    ret intArrayToList(data, 0, size);
  }*/

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

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028793
Snippet name: IntBuffer
Eternal ID of this version: #1028793/42
Text MD5: fd6c2b2c65a573658d1ff2c927e7dc8c
Transpilation MD5: f103fdfb56c7636f912ff44ecebd8bbc
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-14 19:34:21
Source code size: 2956 bytes / 122 lines
Pitched / IR pitched: No / No
Views / Downloads: 583 / 1139
Version history: 41 change(s)
Referenced in: [show references]