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

114
LINES

< > BotCompany Repo | #1028793 // IntBuffer

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

Transpiled version (5360L) is out of date.

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);
  }
  
  // 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/39
Text MD5: 26a0002cd0865474f7c8e41b65dd4497
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-25 21:25:15
Source code size: 2795 bytes / 114 lines
Pitched / IR pitched: No / No
Views / Downloads: 304 / 770
Version history: 38 change(s)
Referenced in: [show references]