Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8219


Ignore:
Timestamp:
07/04/12 21:14:26 (12 years ago)
Author:
bburlacu
Message:

#1265: Group.cs: Replaced primitive list with a linked list as it better fits the requirements (random insertion of primitives in the beginning or middle of the list) and gives better performance.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/Primitives/Group.cs

    r8215 r8219  
    2424using System.Collections.ObjectModel;
    2525using System.Drawing;
     26using System.Linq;
    2627using System.Windows.Forms;
    2728
    2829namespace HeuristicLab.Visualization {
    2930  public class Group : PrimitiveBase, IGroup {
    30     private IList<IPrimitive> myPrimitives;
     31    private LinkedList<IPrimitive> myPrimitives;
    3132    private HashSet<IPrimitive> myPrimitivesLookup;
    3233    public virtual ReadOnlyCollection<IPrimitive> Primitives {
    33       get { return new ReadOnlyCollection<IPrimitive>(myPrimitives); }
     34      get { return new ReadOnlyCollection<IPrimitive>(new List<IPrimitive>(myPrimitives)); }
    3435    }
    3536    public virtual ReadOnlyCollection<IPrimitive> SelectedPrimitives {
     
    5657    public Group(IChart chart)
    5758      : base(chart) {
    58       myPrimitives = new List<IPrimitive>();
     59      myPrimitives = new LinkedList<IPrimitive>();
    5960      myPrimitivesLookup = new HashSet<IPrimitive>();
    6061    }
     
    6465        throw new ArgumentException("Primitive already added");
    6566
    66       myPrimitives.Insert(0, primitive);
     67      myPrimitives.AddFirst(primitive);
    6768      myPrimitivesLookup.Add(primitive);
    6869      primitive.Group = this;
     
    7576          throw new ArgumentException("Primitive already added");
    7677
    77         myPrimitives.Insert(0, primitive);
     78        myPrimitives.AddFirst(primitive);
    7879        myPrimitivesLookup.Add(primitive);
    7980        primitive.Group = this;
     
    106107
    107108    public virtual IPrimitive GetPrimitive(PointD point) {
    108       int i = 0;
    109       while ((i < myPrimitives.Count) && (!myPrimitives[i].ContainsPoint(point)))
    110         i++;
    111       if (i == myPrimitives.Count) return null;
    112       else return myPrimitives[i];
    113     }
     109      return myPrimitives.FirstOrDefault(primitive => primitive.ContainsPoint(point));
     110    }
     111
    114112    public IPrimitive GetPrimitive(double x, double y) {
    115113      return GetPrimitive(new PointD(x, y));
    116114    }
    117115    public virtual IList<IPrimitive> GetAllPrimitives(PointD point) {
    118       List<IPrimitive> primitives = new List<IPrimitive>();
    119       for (int i = 0; i < myPrimitives.Count; i++) {
    120         if (myPrimitives[i].ContainsPoint(point)) {
    121           primitives.Add(myPrimitives[i]);
    122           if (myPrimitives[i] is IGroup) {
    123             primitives.AddRange(((IGroup)myPrimitives[i]).Primitives);
    124           }
    125         }
     116      var primitives = new List<IPrimitive>();
     117      foreach (var primitive in myPrimitives.Where(primitive => primitive.ContainsPoint(point))) {
     118        primitives.Add(primitive);
     119        var @group = primitive as IGroup;
     120        if (@group != null)
     121          primitives.AddRange((@group).Primitives);
    126122      }
    127123      return primitives;
     
    159155        throw new ArgumentException("Primitive not found");
    160156
    161       int index = myPrimitives.IndexOf(primitive);
    162       if (index > 0) {
    163         myPrimitives.Remove(primitive);
    164         myPrimitives.Insert(index - 1, primitive);
    165         OnUpdate();
    166       }
    167     }
     157      var curr = myPrimitives.Find(primitive);
     158      if (curr == null) return;
     159      var prev = curr.Previous;
     160      var temp = curr.Value;
     161      curr.Value = prev.Value;
     162      prev.Value = temp;
     163
     164      OnUpdate();
     165    }
     166
    168167    public void OneLayerDown(IPrimitive primitive) {
    169168      if (!Contains(primitive))
    170169        throw new ArgumentException("Primitive not found");
    171170
    172       int index = myPrimitives.IndexOf(primitive);
    173       if (index < myPrimitives.Count - 1) {
    174         myPrimitives.Remove(primitive);
    175         myPrimitives.Insert(index + 1, primitive);
    176         OnUpdate();
    177       }
     171      var curr = myPrimitives.Find(primitive);
     172      if (curr == null) return;
     173      var next = curr.Next;
     174      var temp = curr.Value;
     175      curr.Value = next.Value;
     176      next.Value = temp;
    178177    }
    179178    public void IntoForeground(IPrimitive primitive) {
     
    182181
    183182      myPrimitives.Remove(primitive);
    184       myPrimitives.Insert(0, primitive);
     183      myPrimitives.AddFirst(primitive);
    185184      OnUpdate();
    186185    }
     
    190189
    191190      myPrimitives.Remove(primitive);
    192       myPrimitives.Add(primitive);
     191      myPrimitives.AddLast(primitive);
    193192      OnUpdate();
    194193    }
    195194
    196195    public override void Draw(Graphics graphics) {
    197       for (int i = myPrimitives.Count - 1; i >= 0; i--) {
    198         myPrimitives[i].PreDraw(graphics);
    199         myPrimitives[i].Draw(graphics);
    200         myPrimitives[i].PostDraw(graphics);
     196      foreach (var primitive in myPrimitives) {
     197        primitive.PreDraw(graphics);
     198        primitive.Draw(graphics);
     199        primitive.PostDraw(graphics);
    201200      }
    202201    }
Note: See TracChangeset for help on using the changeset viewer.