Guest User

Untitled

a guest
Oct 18th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 1.56 KB | None | 0 0
  1. public class LinkedHashSet<G> : Gee.HashSet<G> {
  2.     Gee.List<G> elements = new Gee.LinkedList<G> ();
  3.  
  4.     public LinkedHashSet (owned Gee.HashDataFunc? hash_func = null, owned Gee.EqualDataFunc? equal_func = null) {
  5.         base ((owned)hash_func, (owned)equal_func);
  6.     }
  7.  
  8.     public new G get (int index) {
  9.         return elements[index];
  10.     }
  11.  
  12.     public new void set (int index, G item) {
  13.         var old_item = elements[index];
  14.         if (equal_func (old_item, item)) {
  15.             return;
  16.         }
  17.  
  18.         elements[index] = item;
  19.         base.remove (old_item);
  20.         base.add (item);
  21.     }
  22.  
  23.     public override bool add (G item) {
  24.         return base.add (item) && elements.add (item);
  25.     }
  26.  
  27.     public int index_of (G item) {
  28.         return item in this ? elements.index_of (item) : -1;
  29.     }
  30.  
  31.     public void insert (int index, G item) {
  32.         if (item in this) {
  33.             return;
  34.         }
  35.         elements.insert (index, item);
  36.         base.add (item);
  37.     }
  38.  
  39.     public override bool remove (G item) {
  40.         return base.remove (item) && elements.remove (item);
  41.     }
  42.  
  43.     public G remove_at (int index) {
  44.         var item = elements.remove_at (index);
  45.         base.remove (item);
  46.  
  47.         return item;
  48.     }
  49.  
  50.     public Gee.List<G>? slice (int start, int stop) {
  51.         return elements.slice (start, stop);
  52.     }
  53.  
  54.     public void sort (owned CompareDataFunc? compare_func = null) {
  55.         elements.sort ((owned)compare_func);
  56.     }
  57.  
  58.     public override Gee.Iterator<G> iterator () {
  59.         return elements.iterator ();
  60.     }
  61. }
  62.  
  63. void main (string[] args) {
  64.     var l = new LinkedHashSet<Object> ();
  65.     l.add(new Object ());
  66.  
  67.     var o = l[0];  // this results in g_object_unref: assertion `G_IS_OBJECT (object)' failed
  68. }
Add Comment
Please, Sign In to add comment