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

95
LINES

< > BotCompany Repo | #1028793 - IntBuffer

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

Libraryless. Click here for Pure Java version (2888L/18K).

sclass IntBuffer implements Iterable<Int> {
  int[] data;
  int size;
  
  *() {}
  *(int size) { if (size != 0) data = new int[size]; }
  *(Iterable<Int> l) { this(l(l)); 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 addAll(Iterable<Int> l) {
    if (l != null) for (int i : l) add(i);
  }
  
  int[] toArray() {
    ret size == 0 ? null : resizeIntArray(data, size);
  }
  
  L<Int> toList() {
    ret intArrayToList(data, 0, size);
  }

  L<Int> 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   

Travelled to 5 computer(s): bhatertpkbcr, mqqgnosmbjvj, pzhvpgtvlbxg, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028793
Snippet name: IntBuffer
Eternal ID of this version: #1028793/31
Text MD5: eba0f94a89c4c31913355a1c7ab43b26
Transpilation MD5: a09946eece57f3873076e6f6363d4fcd
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-06 18:26:45
Source code size: 2342 bytes / 95 lines
Pitched / IR pitched: No / No
Views / Downloads: 124 / 317
Version history: 30 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1028793 & http://1028793.tinybrain.de