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

147
LINES

< > BotCompany Repo | #1031663 // ManagedStringList

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

Uses 11335K of libraries. Click here for Pure Java version (6988L/44K).

1  
/* layout:
2  
-physicalSize
3  
-actual size
4  
-one string pointer per entry
5  
*/
6  
sclass ManagedStringList extends AbstractRandomAccessList<S> implements IManagedObject {
7  
  replace Addr with int.
8  
  !include #1031662 // IManagedObject mix-in
9  
  
10  
  new L<Entry> list;
11  
  int physicalLength;
12  
13  
  class Entry {
14  
    int ptr;
15  
    S value;
16  
    bool changed;
17  
    
18  
    *(int *ptr) {}
19  
    *(S *value) { changed = true; }
20  
    
21  
    S value() {
22  
      if (value == null && ptr != 0) {
23  
        //printVars("Reading " + this);
24  
        value = mem.readString(ptr);
25  
      }
26  
      ret value;
27  
    }
28  
    
29  
    void setValue(S s) {
30  
      value = s;
31  
      changed = true;
32  
    }
33  
    
34  
    toString { ret "Entry(" + renderVars(+ptr, +value, +changed) + ")"; }
35  
  }
36  
  
37  
  public int size() { ret l(list); }
38  
39  
  int arrayStart() { ret addr+2; }
40  
  int elementAddr(int i) { ret arrayStart()+i; }
41  
42  
  // read from memory
43  
  *(ManagedIntObjects_v1 *mem, Addr *addr) {
44  
    physicalLength = mem.get(addr);
45  
    int size = mem.get(addr+1);
46  
    list = emptyList(size);
47  
    for i to size:
48  
      list.add(new Entry(mem.get(elementAddr(i))));
49  
  }
50  
  
51  
  *(ManagedIntObjects_v1 *mem) {
52  
    flush();
53  
  }
54  
  
55  
  *(ManagedIntObjects_v1 *mem, LS initialData) {
56  
    fOr (S s : initialData)
57  
      add(s);
58  
    flush();
59  
  }
60  
  
61  
  public S set(int i, S s) {
62  
    Entry e = list.get(i);
63  
    S old = e.value();
64  
    e.setValue(s);
65  
    ret old;
66  
  }
67  
  
68  
  public S get(int i) {
69  
    ret list.get(i).value();
70  
  }
71  
  
72  
  @Override
73  
  public void add(int i, S s) {
74  
    list.add(i, new Entry(s));
75  
  }
76  
  
77  
  public bool add(S s) {
78  
    ret list.add(new Entry(s));
79  
  }
80  
  
81  
  public S remove(int i) {
82  
    ret list.remove(i).value();
83  
  }
84  
  
85  
  void flush {
86  
    ensureCapacityInternal(2+size());
87  
    mem.set(addr+1, size());
88  
    int n = size();
89  
    for i to n: {
90  
      Entry e = list.get(i);
91  
      if (e.changed) {
92  
        // release strings here if they are owned
93  
        e.ptr = mem.newString(e.value);
94  
        e.changed = false;
95  
        deb()?.printVars("flushed entry", +e);
96  
      }
97  
      mem.set(elementAddr(i), e.ptr);
98  
    }
99  
    deb()?.printVars(ptrs := map(list, e -> e.ptr));
100  
  }
101  
  
102  
  void ensureCapacityInternal(int minCapacity) {
103  
    if (physicalLength < minCapacity) grow(minCapacity);
104  
  }
105  
  
106  
  private void grow(int minCapacity) {
107  
    int oldCapacity = physicalLength;
108  
    int newCapacity = oldCapacity + (oldCapacity >> 1);
109  
    if (newCapacity - minCapacity < 0)
110  
      newCapacity = minCapacity;
111  
    int oldAddr = addr, oldSize = objectSize();
112  
    physicalLength = newCapacity;
113  
    setAddr(mem.realloc(oldAddr, oldSize, objectSize()));
114  
    mem.set(addr, physicalLength);
115  
  }
116  
  
117  
  // GC handling
118  
  public void scanForCollection(IManagedObjectCollector gc) {
119  
    deb()?.printVars("scanForCollection", +this, +addr);
120  
    gc.noteObject(addr, objectSize());
121  
    /*int n = size();
122  
    for i to n:
123  
      gc.noteString(mem.get(elementAddr(i)));*/
124  
    int i = 0;
125  
    for (Entry e : list) {
126  
      int elAddr = elementAddr(i++);
127  
      int actualPtr = mem.read(elAddr);
128  
      deb()?.printVars("noteEntry", +e, +i, +elAddr, +actualPtr);
129  
      gc.noteString(actualPtr, null);
130  
      gc.notePointer(elAddr, addr -> {
131  
        deb()?.print("Moving entry " + e + " to " + addr);
132  
        e.ptr = addr;
133  
      });
134  
    }
135  
  } 
136  
  
137  
  int objectSize() { ret 2+physicalLength; }
138  
  
139  
  static IF2<ManagedIntObjects_v1, Int, ManagedStringList> factory() {
140  
    ret (mem, addr) -> new ManagedStringList(mem, addr);
141  
  }
142  
  
143  
  protected void removeRange(int fromIndex, int toIndex) {
144  
    if (fromIndex == toIndex) ret;
145  
    list.subList(fromIndex, toIndex).clear();
146  
  }
147  
}

Author comment

Began life as a copy of #1031661

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031663
Snippet name: ManagedStringList
Eternal ID of this version: #1031663/35
Text MD5: 21227938bdcf1f0dceeaef82d5ab49a9
Transpilation MD5: 34bed7fbd3da98e88ca8f7557e150f0b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-25 13:30:35
Source code size: 3694 bytes / 147 lines
Pitched / IR pitched: No / No
Views / Downloads: 226 / 516
Version history: 34 change(s)
Referenced in: [show references]