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

102
LINES

< > BotCompany Repo | #1031661 // SortedStringMap_managed [dev.]

JavaX fragment (include)

sclass SortedStringMap_managed extends AbstractManagedObject {
  replace Addr with int.
  
  new TreeMap<S, Entry> map;
  int physicalLength; // total array length is twice this. data is: String *key, String *value, ...
  int size; // our actual length
  
  // it's actually a union
  class Entry {
    int index; // also when not loaded
    S value;   // only when loaded
    
    *(int *index) {}
    *(int *index, S *value) {}
    
    S value() {
      if (value == null)
        value = mem.readString(valueAddr(index));
      ret value;
    }
  }
  
  int size() { ret l(map); }

  int arrayStart() { ret addr+2; }
  int keyAddr(int i) { ret arrayStart()+i*2; }
  int valueAddr(int i) { ret arrayStart()+i*2+1; }
  
  // read from memory
  *(ManagedIntObjects_v1 *mem, Addr *addr) {
    physicalLength = mem.get(addr);
    size = mem.get(addr+1);
    for i to size:
      map.put(mem.readString(keyAddr(i)), new Entry(valueAddr(i)));
  }
  
  *(ManagedIntObjects_v1 *mem, SS initialData) {
    SS sorted;
    if (initialData instanceof TreeMap) {
      map.setComparator(initialData.comparator());
      sorted = initialData;
    } else
      sorted = new TreeMap(unnull(initialData), map.comparator());
      
    physicalLength = l(sorted);
    alloc(objectSize());
    mem.set(addr, physicalLength);
    
    int i = 0;
    for (S key, S value : sorted)
      map.put(key, new Entry(i++, value));
      
    flush();
  }
  
  void flush {
    ensureCapacityInternal(size());
    mem.set(addr+1, size());
    int i = 0;
    for (S key, Entry entry : map) {
      int keyPtr = mem.get(keyAddr(i));
      if (!eq(mem.readString(keyPtr), key)) {
        mem.freeString(keyAddr(i)); // strings belong to map
        mem.set(keyAddr(i), mem.newString(key));
      }
      
      int valuePtr = mem.get(valueAddr(i));
      if (entry.index != i
        || entry.value != null && !eq(entry.value
      if (!eq(mem.readString(valuePtr), entry.value())) {
        mem.freeString(valueAddr(i)); // strings belong to map
        mem.set(valueAddr(i), mem.newString(key));
      }
      map.put(key, new Entry(i++, value));
    }
  }
  
  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;
    long oldAddr = addr, oldSize = objectSize();
    physicalLength = newCapacity;
    setAddr(mem.alloc(objectSize()));
    for (int i = 1; i < oldSize; i++)
      mem.set(addr+i, mem.get(oldAddr+i));
    mem.free(oldAddr, oldSize);
    mem.set(addr, physicalLength);
  }
  
  // GC handling
  public void scanForCollection(IManagedObjectCollector gc) {
    gc.noteObject(addr, objectSize());
  } 
  
  int objectSize() { ret 2+physicalLength*2; }
}

Author comment

Began life as a copy of #1031653

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1031661
Snippet name: SortedStringMap_managed [dev.]
Eternal ID of this version: #1031661/3
Text MD5: 3a79d5de0b5b54aac42266729022f9b4
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-25 07:44:07
Source code size: 3002 bytes / 102 lines
Pitched / IR pitched: No / No
Views / Downloads: 61 / 84
Version history: 2 change(s)
Referenced in: [show references]