1 | using HeuristicLab.Common;
|
---|
2 | using HeuristicLab.Core;
|
---|
3 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
4 | using System;
|
---|
5 | using System.Collections.Generic;
|
---|
6 | using System.Drawing;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Data.MoveVectorData
|
---|
9 | {
|
---|
10 | [Item("Stack", "Represents a stack of containers.")]
|
---|
11 | [StorableClass]
|
---|
12 | public class Stack : IntArray
|
---|
13 | {
|
---|
14 | public static new Image StaticItemImage
|
---|
15 | {
|
---|
16 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
|
---|
17 | }
|
---|
18 |
|
---|
19 | public int Id { get; set; }
|
---|
20 | public int CurrentHeight
|
---|
21 | {
|
---|
22 | get
|
---|
23 | {
|
---|
24 | int ch = 0;
|
---|
25 | for(int i = 0; i < Length; i++)
|
---|
26 | {
|
---|
27 | if(array[i] > 0)
|
---|
28 | {
|
---|
29 | ch = i + 1;
|
---|
30 | }
|
---|
31 | }
|
---|
32 | return ch;
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | public virtual int Capacity
|
---|
37 | {
|
---|
38 | get
|
---|
39 | {
|
---|
40 | return array.Length;
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public Stack(int id) : base() { Id = id; }
|
---|
45 | public Stack(int id, int length) : base(length) { Id = id; }
|
---|
46 | public Stack(int id, int[] elements) : base(elements) { Id = id; }
|
---|
47 | public Stack(int id, IntArray elements) : this(elements.Length) {
|
---|
48 | Id = id;
|
---|
49 | for (int i = 0; i < array.Length; i++)
|
---|
50 | {
|
---|
51 | array[i] = elements[i];
|
---|
52 | }
|
---|
53 |
|
---|
54 | }
|
---|
55 |
|
---|
56 | [StorableConstructor]
|
---|
57 | protected Stack(bool deserializing) : base(deserializing) { }
|
---|
58 | protected Stack(Stack original, Cloner cloner) : base(original, cloner) { }
|
---|
59 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
60 | {
|
---|
61 | return new Stack(this, cloner);
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected override bool Validate(string value, out string errorMessage)
|
---|
65 | {
|
---|
66 | if (value == null) //TODO
|
---|
67 | {
|
---|
68 | errorMessage = "Invalid Value (string must not be null)";
|
---|
69 | return false;
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | errorMessage = string.Empty;
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected override string GetValue(int index)
|
---|
78 | {
|
---|
79 | return this[index].ToString();
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected override bool SetValue(string value, int index)
|
---|
83 | {
|
---|
84 | int result;
|
---|
85 | if (Int32.TryParse(value, out result))
|
---|
86 | {
|
---|
87 | this[index] = result;
|
---|
88 | return true;
|
---|
89 | }
|
---|
90 | else
|
---|
91 | {
|
---|
92 | return false;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void Push(int value)
|
---|
97 | {
|
---|
98 | if (CurrentHeight < Length)
|
---|
99 | {
|
---|
100 | //currentHeight++;
|
---|
101 | array[CurrentHeight] = value;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public int Pop()
|
---|
106 | {
|
---|
107 | if (CurrentHeight > 0)
|
---|
108 | {
|
---|
109 | //currentHeight--;
|
---|
110 | int val = array[CurrentHeight - 1];
|
---|
111 | array[CurrentHeight - 1] = 0;
|
---|
112 | return val;
|
---|
113 | } else {
|
---|
114 | return 0;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void Clear()
|
---|
119 | {
|
---|
120 | for (int i = 0; i < array.Length; i++)
|
---|
121 | {
|
---|
122 | array[i] = 0;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void Randomize(double fillRate, int seed)
|
---|
127 | {
|
---|
128 | Random random = new Random(seed);
|
---|
129 | Clear();
|
---|
130 | int containerCount = Capacity;
|
---|
131 | var range = new List<int>(containerCount + 1);
|
---|
132 | for (int i = 0; i < containerCount; i++)
|
---|
133 | {
|
---|
134 | range.Add(i + 1);
|
---|
135 | }
|
---|
136 | range.Shuffle(random);
|
---|
137 |
|
---|
138 | for (int i = 0; i < containerCount; i++)
|
---|
139 | {
|
---|
140 | Push(range[i]);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|