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

58
LINES

< > BotCompany Repo | #1002363 // PersistentList (developing)

JavaX fragment (include)

// a persistent list using a clever combination of persisting and logging
// (well, only logging as of now)
// Note: don't put in static initializer (programID not set yet)

static class PersistentList<A> extends AbstractList<A> {
  new List<A> m;
  File file;
  
  PersistentList(S fileName) {
    this(getProgramFile(fileName));
  }
  
  PersistentList(S progID, S fileName) {
    this(getProgramFile(progID, fileName));
  }
  
  *(File *file) {
    for (S s : scanLog(file)) pcall {
      L l = cast unstructure(s);
      if (eq(l.get(0), "add"))
        m.put((A) l.get(1), (B) l.get(2));
      else if (eq(l.get(0), "remove"))
        m.remove((A) l.get(1));
      else
        print("Unknown command in log: " + l.get(0));
    }
  }
  
  // just delegates
  
  public int size() {
    ret m.size();
  }
  
  public B get(int i) {
    ret m.get(i);
  }
  
  // TODO: calling remove() in the iterator will have unpersisted
  // effects.
  public Set<Map.Entry<A,B>> entrySet() {
    ret m.entrySet();
  }
  
  // delegates with logging

  public B put(A a, B b) {
    B result = m.put(a, b);
    logQuoted(file, structure(litlist("add", a, b)));
    ret result;
  }
  
  public B remove(O a) {
    B result = m.remove(a);
    logQuoted(file, structure(litlist("remove", a)));
    ret result;
  }
}

Author comment

Began life as a copy of #1002063

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002363
Snippet name: PersistentList (developing)
Eternal ID of this version: #1002363/1
Text MD5: c716b0c1c35951ffe4a0b6b8f5b92548
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-09 18:42:24
Source code size: 1358 bytes / 58 lines
Pitched / IR pitched: No / No
Views / Downloads: 616 / 538
Referenced in: [show references]