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

/* layout:
-physicalSize
-actual size
-one string pointer per entry
*/
sclass ManagedStringList extends AbstractRandomAccessList<S> implements IManagedObject {
  replace Addr with int.
  !include #1031662 // IManagedObject mix-in
  
  new L<Entry> list;
  int physicalLength;

  class Entry {
    int ptr;
    S value;
    bool changed;
    
    *(int *ptr) {}
    *(S *value) { changed = true; }
    
    S value() {
      if (value == null && ptr != 0) {
        //printVars("Reading " + this);
        value = mem.readString(ptr);
      }
      ret value;
    }
    
    void setValue(S s) {
      value = s;
      changed = true;
    }
    
    toString { ret "Entry(" + renderVars(+ptr, +value, +changed) + ")"; }
  }
  
  public int size() { ret l(list); }

  int arrayStart() { ret addr+2; }
  int elementAddr(int i) { ret arrayStart()+i; }

  // read from memory
  *(ManagedIntObjects_v1 *mem, Addr *addr) {
    physicalLength = mem.get(addr);
    int size = mem.get(addr+1);
    list = emptyList(size);
    for i to size:
      list.add(new Entry(mem.get(elementAddr(i))));
  }
  
  *(ManagedIntObjects_v1 *mem) {
    flush();
  }
  
  *(ManagedIntObjects_v1 *mem, LS initialData) {
    fOr (S s : initialData)
      add(s);
    flush();
  }
  
  public S set(int i, S s) {
    Entry e = list.get(i);
    S old = e.value();
    e.setValue(s);
    ret old;
  }
  
  public S get(int i) {
    ret list.get(i).value();
  }
  
  @Override
  public void add(int i, S s) {
    list.add(i, new Entry(s));
  }
  
  public bool add(S s) {
    ret list.add(new Entry(s));
  }
  
  public S remove(int i) {
    ret list.remove(i).value();
  }
  
  void flush {
    ensureCapacityInternal(2+size());
    mem.set(addr+1, size());
    int n = size();
    for i to n: {
      Entry e = list.get(i);
      if (e.changed) {
        // release strings here if they are owned
        e.ptr = mem.newString(e.value);
        e.changed = false;
        deb()?.printVars("flushed entry", +e);
      }
      mem.set(elementAddr(i), e.ptr);
    }
    deb()?.printVars(ptrs := map(list, e -> e.ptr));
  }
  
  void ensureCapacityInternal(int minCapacity) {
    if (physicalLength < minCapacity) grow(minCapacity);
  }
  
  private void grow(int minCapacity) {
    int oldCapacity = physicalLength;
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    if (newCapacity - minCapacity < 0)
      newCapacity = minCapacity;
    int oldAddr = addr, oldSize = objectSize();
    physicalLength = newCapacity;
    setAddr(mem.realloc(oldAddr, oldSize, objectSize()));
    mem.set(addr, physicalLength);
  }
  
  // GC handling
  public void scanForCollection(IManagedObjectCollector gc) {
    deb()?.printVars("scanForCollection", +this, +addr);
    gc.noteObject(addr, objectSize());
    /*int n = size();
    for i to n:
      gc.noteString(mem.get(elementAddr(i)));*/
    int i = 0;
    for (Entry e : list) {
      int elAddr = elementAddr(i++);
      int actualPtr = mem.read(elAddr);
      deb()?.printVars("noteEntry", +e, +i, +elAddr, +actualPtr);
      gc.noteString(actualPtr, null);
      gc.notePointer(elAddr, addr -> {
        deb()?.print("Moving entry " + e + " to " + addr);
        e.ptr = addr;
      });
    }
  } 
  
  int objectSize() { ret 2+physicalLength; }
  
  static IF2<ManagedIntObjects_v1, Int, ManagedStringList> factory() {
    ret (mem, addr) -> new ManagedStringList(mem, addr);
  }
  
  protected void removeRange(int fromIndex, int toIndex) {
    if (fromIndex == toIndex) ret;
    list.subList(fromIndex, toIndex).clear();
  }
}

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: 224 / 512
Version history: 34 change(s)
Referenced in: [show references]