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).

1  
sclass ShortBuffer is Iterable<Short> {
2  
  short[] data;
3  
  int size;
4  
  
5  
  *() {}
6  
  *(int size) { if (size != 0) data = new short[size]; }
7  
  *(Iterable<Short> l) {
8  
    if (l cast Cl) allocate(l.size());
9  
    addAll(l);
10  
  }
11  
  
12  
  void add(short i) {
13  
    if (size >= lShortArray(data)) {
14  
      data = resizeShortArray(data, Math.max(1, toShort(Math.min(maximumSafeArraySize(), lShortArray(data)*2L))));
15  
      if (size >= data.length) fail("ShortBuffer too large: " + size);
16  
    }
17  
    data[size++] = i;
18  
  }
19  
  
20  
  void allocate(int n) {
21  
    data = resizeShortArray(data, max(n, size()));
22  
  }
23  
  
24  
  void setSize(int n) {
25  
    data = resizeShortArray(data, n);
26  
    size = min(l(data), size);
27  
  }
28  
  
29  
  void addAll(Iterable<Short> l) {
30  
    if (l != null) for (i : l) add(i);
31  
  }
32  
  
33  
  void addAll(short... l) {
34  
    if (l != null) for (short i : l) add(i);
35  
  }
36  
  
37  
  // Note: may return the internal array
38  
  short[] toArray aka toShortArray() {
39  
    ret size == 0 ? null : resizeShortArray(data, size);
40  
  }
41  
  
42  
  // abandoned version
43  
  /*L<Short> toList() {
44  
    ret intArrayToList(data, 0, size);
45  
  }*/
46  
47  
  // now all these return a virtual list
48  
  L<Short> toList aka asList aka asVirtualList() {
49  
    ret new RandomAccessAbstractList<Short> {
50  
      public int size() { ret size; }
51  
      public Short get(int i) { ret ShortBuffer.this.get(i); }
52  
      public Short set(int i, Short val) {
53  
        Short a = get(i);
54  
        data[i] = val;
55  
        ret a;
56  
      }
57  
    };
58  
  }
59  
  
60  
  void reset { size = 0; }
61  
  void clear { reset(); }
62  
  
63  
  int size() { ret size; }
64  
  bool isEmpty() { ret size == 0; }
65  
  
66  
  short get(int idx) {
67  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
68  
    ret data[idx];
69  
  }
70  
  
71  
  void set(int idx, short value) {
72  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
73  
    data[idx] = value;
74  
  }
75  
  
76  
  short popLast() {
77  
    if (size == 0) fail("empty buffer");
78  
    ret data[--size];
79  
  }
80  
  
81  
  short last() { ret data[size-1]; }
82  
  short nextToLast() { ret data[size-2]; }
83  
  
84  
  toString { ret squareBracket(joinWithSpace(toList())); }
85  
  
86  
  public Iterator<Short> iterator() {
87  
    ret new ItIt<Short> {
88  
      int i = 0;
89  
      
90  
      public bool hasNext() { ret i < size; }
91  
      public Short next() {
92  
        //if (!hasNext()) fail("Index out of bounds: " + i);
93  
        ret data[i++];
94  
      }
95  
    };
96  
  }
97  
  
98  
  public ShortIterator shortIterator() {
99  
    ret new ShortIterator {
100  
      int i = 0;
101  
      
102  
      public bool hasNext() { ret i < size; }
103  
      public short next() {
104  
        //if (!hasNext()) fail("Index out of bounds: " + i);
105  
        ret data[i++];
106  
      }
107  
      toString { ret "Iterator@" + i + " over " + ShortBuffer.this; }
108  
    };
109  
  }
110  
  
111  
  void trimToSize {
112  
    data = resizeShortArray(data, size);
113  
  }
114  
}

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: 92 / 150
Version history: 4 change(s)
Referenced in: [show references]