[Vala] ArrayList.equal_func Problem



hi:

I have encounter a problem when use ArrayList,  Can anyone explain below
code and its output:

vala code:
---
  1 using Gee;
  2 
  3 public class CmpTest : Object
  4 {
  5   public uint32         v1;
  6   public uint32         v2;
  7   //public uint32               v1      { get; set; }
  8   //public uint32               v2      { get; set; }
  9 }
 10 
 11 public class CmpTestList : Object
 12 {
 13   private ArrayList<CmpTest>    list;
 14   public int c = 0;
 15 
 16   public CmpTestList ()
 17     {
 18       list = new ArrayList<CmpTest> ((EqualFunc) cmp_value);
 19     }
 20 
 21   public void add (CmpTest test)
 22     {
 23       if (!list.contains (test))
 24         list.add (test);
 25     }
 26 
 27   private bool cmp_value (CmpTest a, CmpTest b)
 28     {
 29       c++;
 30       debug ("in func ... c=%d", c);
 31       return a.v1 == b.v1 && a.v2 == b.v2;
 32     }
 33 }
 34 
 35 void main ()
 36 {
 37   var l = new CmpTestList ();
 38 
 39   var t = new CmpTest ();
 40   t.v1 = 3;
 41   t.v2 = 3;
 42   l.add (t);
 43   debug ("1. c=%d", l.c);
 44 
 45   t = new CmpTest ();
 46   t.v1 = 3;
 47   t.v2 = 3;
 48   l.add (t);
 49   debug ("2. c=%d", l.c);
 50 
 51   t = new CmpTest ();
 52   t.v1 = 3;
 53   t.v2 = 3;
 54   l.add (t);
 55   debug ("3. c=%d", l.c);
 56 }

---
output:
---
** (process:10603): DEBUG: cmp.vala:43: 1. c=0
** (process:10603): DEBUG: cmp.vala:30: in func ... c=4
** (process:10603): DEBUG: cmp.vala:49: 2. c=0
** (process:10603): DEBUG: cmp.vala:30: in func ... c=5
** (process:10603): DEBUG: cmp.vala:30: in func ... c=4
** (process:10603): DEBUG: cmp.vala:55: 3. c=0
---

thanks.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]