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

106
LINES

< > BotCompany Repo | #1031946 // CompactLinkedHashSet [OK]

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

Libraryless. Click here for Pure Java version (4330L/26K).

1  
// -has fast nextElement() and prevElement()
2  
// -design allows for more functions like reordering the list
3  
// -Saves up to 34% in space over LinkedHashSet
4  
//    (e.g. 22% for a set of 1,000 Ints)
5  
sclass CompactLinkedHashSet<A> extends AbstractSet<A> {
6  
  new UnsynchronizedCompactHashSet<Entry<A>> entries;
7  
  Entry<A> head, tail;
8  
  
9  
  sclass Entry<A> {
10  
    A value;
11  
    Entry<A> prev, next;
12  
    
13  
    public int hashCode() {
14  
      ret _hashCode(value);
15  
    }
16  
    
17  
    // "magic" equals function for CompactHashSet lookup without temp object
18  
    public bool equals(O o) {
19  
      ret o == this || eq(value, o);
20  
    }
21  
  }
22  
  
23  
  public bool add(A a) {
24  
    if (entries.contains(a)) false;
25  
    new Entry<A> n;
26  
    n.value = a;
27  
    n.prev = tail;
28  
    if (tail != null) tail.next = n;
29  
    tail = n;
30  
    if (head == null) head = n;
31  
    entries.add(n);
32  
    true;
33  
  }
34  
  
35  
  public bool remove(O a) {
36  
    ret remove(entries.find(a));
37  
  }
38  
  
39  
  public bool remove(Entry<A> node) {
40  
    if (node == null) false;
41  
    if (node.next != null) node.next.prev = node.prev; else tail = node.prev;
42  
    if (node.prev != null) node.prev.next = node.next; else head = node.next;
43  
    entries.remove(node);
44  
    true;
45  
  }
46  
  
47  
  public int size() { ret entries.size(); }
48  
  
49  
  public ItIt<A> iterator() {
50  
    ret new ItIt<A> {
51  
      Entry<A> entry = head, prev = null;
52  
      public bool hasNext() { ret entry != null; }
53  
      public A next() {
54  
        A a = entry.value;
55  
        prev = entry;
56  
        entry = entry.next;
57  
        ret a;
58  
      }
59  
      
60  
      // untested
61  
      public void remove() {
62  
        if (prev == null) throw new IllegalStateException;
63  
        CompactLinkedHashSet.this.remove(prev);
64  
        prev = null;
65  
      }
66  
    };
67  
  }
68  
  
69  
  public void clear() {
70  
    entries.clear();
71  
    head = tail = null;
72  
  }
73  
  
74  
  public bool contains(O a) {
75  
    ret entries.contains(a);
76  
  }
77  
  
78  
  public A find(O o) {
79  
    Entry<A> e = entries.find(o);
80  
    ret e?.value;
81  
  }
82  
  
83  
  public A prevElement(A a) {
84  
    Entry<A> e = entries.find(a);
85  
    if (e == null || e.prev == null) null;
86  
    ret e.prev.value;
87  
  }
88  
  
89  
  public A nextElement(A a) {
90  
    Entry<A> e = entries.find(a);
91  
    if (e == null || e.next == null) null;
92  
    ret e.next.value;
93  
  }
94  
  
95  
  public A first() { ret head?.value; }
96  
  public A last() { ret tail?.value; }
97  
  
98  
  bool removeIfSame(O o) {
99  
    A value = find(o);
100  
    if (value == o) {
101  
      remove(value);
102  
      true;
103  
    }
104  
    false;
105  
  }
106  
}

Author comment

Began life as a copy of #1031507

download  show line numbers  debug dex  old transpilations   

Travelled to 5 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031946
Snippet name: CompactLinkedHashSet [OK]
Eternal ID of this version: #1031946/16
Text MD5: cbaa73f46d330710f4bfb651fab4ddf2
Transpilation MD5: b16fdec6f444e88dfd79492617f205dd
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-23 05:29:08
Source code size: 2520 bytes / 106 lines
Pitched / IR pitched: No / No
Views / Downloads: 258 / 977
Version history: 15 change(s)
Referenced in: [show references]