Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/20/16 22:57:11 (7 years ago)
Author:
pkimmesw
Message:

#2665 Added Problem.ProgramSynthesis Project, Fixed Expression Issues, Fixed Code Generation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Stack/PushGPStack.cs

    r14398 r14513  
    1 using System;
    2 using System.Collections;
    3 using System.Collections.Generic;
    4 
    5 namespace HeuristicLab.Algorithms.PushGP.Stack
    6 {
    7     /// <summary>
    8     /// While Push's stacks are generally treated as genuine stacks---that is, inclassions take their arguments from the tops of
    9     /// the stacks and push their results onto the tops of the stacks---a few inclassions (like YANK and SHOVE) do allow direct access
    10     /// to "deep" stack elements by means of integer indices. To this extent the stacks can be used as general, random access memory
    11     /// classures. This is one of the features that ensures the Turing-completeness of Push (another being the arbitrary name/value
    12     /// bindings supported by the NAME data type and DEFINE methods; see below).
    13     /// </summary>
    14     /// <typeparam name="T">The item type of the collection.</typeparam>
    15     public class PushGPStack<T> : IStack<T>
    16     {
    17         private readonly List<T> data;
    18         private const string Delimiter = " ";
    19 
    20         public PushGPStack(int capacity = 0)
    21         {
    22             data = new List<T>();
    23         }
    24 
    25         public T Top { get { return data[Count - 1]; } }
    26         public T TopOrDefault { get { return Count > 0 ? data[Count - 1] : default(T); } }
    27         public T Bottom { get { return data[0]; } }
    28         public T BottomOrDefault { get { return Count > 0 ? data[0] : default(T); } }
    29         public int Count { get { return data.Count; } }
    30         public int Capacity { get { return data.Capacity; } }
    31         public bool IsEmpty { get { return Count == 0; } }
    32 
    33         public bool IsReadOnly { get { return false; } }
    34 
    35         public void Add(T item)
    36         {
    37             Push(item);
    38         }
    39 
    40         public void Clear()
    41         {
    42             data.Clear();
    43         }
    44 
    45         public bool Contains(T item)
    46         {
    47             return data.Contains(item);
    48         }
    49 
    50         public void CopyTo(T[] array, int arrayIndex)
    51         {
    52             data.CopyTo(array, arrayIndex);
    53         }
    54 
    55         public IEnumerator<T> GetEnumerator()
    56         {
    57             return data.GetEnumerator();
    58         }
    59 
    60         public T ElementAt(int index)
    61         {
    62             return data[index];
    63         }
    64 
    65 
    66         public T ReverseElementAt(int offset)
    67         {
    68             return data[Count - 1 - offset];
    69         }
    70 
    71         public void SetTop(T value)
    72         {
    73             data[Count - 1] = value;
    74         }
    75 
    76         public T this[int key]
    77         {
    78             get { return data[key]; }
    79             set { data[key] = value; }
    80         }
    81 
    82         public void Swap(int count)
    83         {
    84             var top = Top;
    85             var bottomIndex = Count - count;
    86 
    87             for (var i = Count - 1; i > bottomIndex; i--)
    88             {
    89                 data[i] = data[i - 1];
    90             }
    91 
    92             data[bottomIndex] = top;
    93         }
    94 
    95         public void Yank(int index)
    96         {
    97             var item = ElementAt(index);
    98             data.RemoveAt(index);
    99             data.Add(item);
    100         }
    101 
    102         public T Pop()
    103         {
    104             var value = data[Count - 1];
    105             data.RemoveAt(Count - 1);
    106 
    107             return value;
    108         }
    109 
    110         public T[] Pop(int count)
    111         {
    112             var startIndex = Count - count;
    113 
    114             var items = new T[count];
    115             data.CopyTo(startIndex, items, 0, count);
    116             Remove(count);
    117 
    118             return items;
    119         }
    120 
    121         public void Push(T item)
    122         {
    123             data.Add(item);
    124         }
    125 
    126         public void Push(object[] items)
    127         {
    128             for (var i = 0; i < items.Length; i++)
    129             {
    130                 data.Add((T)items[i]);
    131             }
    132         }
    133 
    134         public void PushResult(int count, Func<T[], T> templateFunc)
    135         {
    136             var startIndex = Count - count;
    137             var items = new T[count];
    138 
    139             data.CopyTo(startIndex, items, 0, count);
    140             Remove(count - 1);
    141 
    142             data[Count - 1] = templateFunc(items);
    143         }
    144 
    145         public void Push(params T[] items)
    146         {
    147             for (var i = 0; i < items.Length; i++)
    148             {
    149                 data.Add(items[i]);
    150             }
    151         }
    152 
    153         public void Push(IEnumerable<T> items)
    154         {
    155             data.AddRange(items);
    156         }
    157 
    158         public void Insert(int index, T item)
    159         {
    160             data.Insert(index, item);
    161         }
    162 
    163         public void Insert(int index, params T[] items)
    164         {
    165             data.InsertRange(index, items);
    166         }
    167 
    168         public void Insert(int index, IEnumerable<T> items)
    169         {
    170             data.InsertRange(index, items);
    171         }
    172 
    173         public bool Remove(T item)
    174         {
    175             var index = Count - 1;
    176             if (Count > 0 && data[index].Equals(item))
    177             {
    178                 data.RemoveAt(index);
    179                 return true;
    180             }
    181             else
    182             {
    183                 return false;
    184             }
    185         }
    186 
    187         public void RemoveTop()
    188         {
    189             data.RemoveAt(Count - 1);
    190         }
    191 
    192         public void Remove(int count)
    193         {
    194             for (var i = 0; i < count; i++)
    195             {
    196                 data.RemoveAt(Count - 1);
    197             }
    198         }
    199 
    200         public void RemoveAt(int index)
    201         {
    202             data.RemoveAt(index);
    203         }
    204 
    205         public void RemoveAt(int index, int count)
    206         {
    207             data.RemoveRange(index, count);
    208         }
    209 
    210         public bool TryPop(out T item)
    211         {
    212             if (Count > 0)
    213             {
    214                 item = Pop();
    215                 return true;
    216             }
    217             else
    218             {
    219                 item = default(T);
    220                 return false;
    221             }
    222         }
    223 
    224         IEnumerator IEnumerable.GetEnumerator()
    225         {
    226             return (data as IEnumerable).GetEnumerator();
    227         }
    228 
    229         public override string ToString()
    230         {
    231             return string.Join(Delimiter, data);
    232         }
    233     }
     1namespace HeuristicLab.Algorithms.PushGP.Stack {
     2  using System;
     3  using System.Collections;
     4  using System.Collections.Generic;
     5
     6  /// <summary>
     7  ///     While Push's stacks are generally treated as genuine stacks---that is, inclassions take their arguments from the
     8  ///     tops of
     9  ///     the stacks and push their results onto the tops of the stacks---a few inclassions (like YANK and SHOVE) do allow
     10  ///     direct access
     11  ///     to "deep" stack elements by means of integer indices. To this extent the stacks can be used as general, random
     12  ///     access memory
     13  ///     classures. This is one of the features that ensures the Turing-completeness of Push (another being the arbitrary
     14  ///     name/value
     15  ///     bindings supported by the NAME data type and DEFINE methods; see below).
     16  /// </summary>
     17  /// <typeparam name="T">The item type of the collection.</typeparam>
     18  public class PushGPStack<T> : IStack<T> {
     19    private const string Delimiter = " ";
     20
     21    private readonly List<T> data;
     22
     23    public PushGPStack(int capacity = 0) {
     24      this.data = new List<T>();
     25    }
     26
     27    public int Capacity
     28    {
     29      get
     30      {
     31        return this.data.Capacity;
     32      }
     33    }
     34
     35    public T Top
     36    {
     37      get
     38      {
     39        return this.data[this.Count - 1];
     40      }
     41    }
     42
     43    public T TopOrDefault
     44    {
     45      get
     46      {
     47        return this.Count > 0 ? this.data[this.Count - 1] : default(T);
     48      }
     49    }
     50
     51    public T Bottom
     52    {
     53      get
     54      {
     55        return this.data[0];
     56      }
     57    }
     58
     59    public T BottomOrDefault
     60    {
     61      get
     62      {
     63        return this.Count > 0 ? this.data[0] : default(T);
     64      }
     65    }
     66
     67    public int Count
     68    {
     69      get
     70      {
     71        return this.data.Count;
     72      }
     73    }
     74
     75    public bool IsEmpty
     76    {
     77      get
     78      {
     79        return this.Count == 0;
     80      }
     81    }
     82
     83    public bool IsReadOnly
     84    {
     85      get
     86      {
     87        return false;
     88      }
     89    }
     90
     91    public void Add(T item) {
     92      this.Push(item);
     93    }
     94
     95    public void Clear() {
     96      this.data.Clear();
     97    }
     98
     99    public bool Contains(T item) {
     100      return this.data.Contains(item);
     101    }
     102
     103    public void CopyTo(T[] array, int arrayIndex) {
     104      this.data.CopyTo(array, arrayIndex);
     105    }
     106
     107    public IEnumerator<T> GetEnumerator() {
     108      return this.data.GetEnumerator();
     109    }
     110
     111    public T ElementAt(int index) {
     112      return this.data[index];
     113    }
     114
     115    public T ReverseElementAt(int offset) {
     116      return this.data[this.data.Count - 1 - offset];
     117    }
     118
     119    public void SetTop(T value) {
     120      this.data[this.Count - 1] = value;
     121    }
     122
     123    public T this[int key]
     124    {
     125      get
     126      {
     127        return this.data[key];
     128      }
     129      set
     130      {
     131        this.data[key] = value;
     132      }
     133    }
     134
     135    public void Swap(int count) {
     136      var top = this.Top;
     137      var bottomIndex = this.Count - count;
     138
     139      for (var i = this.Count - 1; i > bottomIndex; i--) this.data[i] = this.data[i - 1];
     140
     141      this.data[bottomIndex] = top;
     142    }
     143
     144    public void Yank(int index) {
     145      if (index == this.Count - 1) return;
     146
     147      var item = this.ElementAt(index);
     148      this.data.RemoveAt(index);
     149      this.data.Add(item);
     150    }
     151
     152    public T Pop() {
     153      var value = this.data[this.Count - 1];
     154      this.data.RemoveAt(this.Count - 1);
     155
     156      return value;
     157    }
     158
     159    public T[] Pop(int count) {
     160      var startIndex = this.Count - count;
     161
     162      var items = new T[count];
     163      this.data.CopyTo(startIndex, items, 0, count);
     164      this.Remove(count);
     165
     166      return items;
     167    }
     168
     169    public void Push(T item) {
     170      this.data.Add(item);
     171    }
     172
     173    public void PushResult(int count, Func<T[], T> templateFunc) {
     174      var startIndex = this.Count - count;
     175      var items = new T[count];
     176
     177      this.data.CopyTo(startIndex, items, 0, count);
     178      this.Remove(count - 1);
     179
     180      this.data[this.Count - 1] = templateFunc(items);
     181    }
     182
     183    public void Push(params T[] items) {
     184      for (var i = 0; i < items.Length; i++) this.data.Add(items[i]);
     185    }
     186
     187    public void Push(IEnumerable<T> items) {
     188      this.data.AddRange(items);
     189    }
     190
     191    public void Insert(int index, T item) {
     192      this.data.Insert(index, item);
     193    }
     194
     195    public void Insert(int index, params T[] items) {
     196      this.data.InsertRange(index, items);
     197    }
     198
     199    public void Insert(int index, IEnumerable<T> items) {
     200      this.data.InsertRange(index, items);
     201    }
     202
     203    public bool Remove(T item) {
     204      var index = this.Count - 1;
     205      if ((this.Count > 0) && this.data[index].Equals(item)) {
     206        this.data.RemoveAt(index);
     207        return true;
     208      }
     209      return false;
     210    }
     211
     212    public void RemoveTop() {
     213      this.data.RemoveAt(this.Count - 1);
     214    }
     215
     216    public void Remove(int count) {
     217      for (var i = 0; i < count; i++) this.data.RemoveAt(this.Count - 1);
     218    }
     219
     220    public void RemoveAt(int index) {
     221      this.data.RemoveAt(index);
     222    }
     223
     224    public void RemoveAt(int index, int count) {
     225      this.data.RemoveRange(index, count);
     226    }
     227
     228    public bool TryPop(out T item) {
     229      if (this.Count > 0) {
     230        item = this.Pop();
     231        return true;
     232      }
     233      item = default(T);
     234      return false;
     235    }
     236
     237    IEnumerator IEnumerable.GetEnumerator() {
     238      return (this.data as IEnumerable).GetEnumerator();
     239    }
     240
     241    public override string ToString() {
     242      return string.Join(Delimiter, this.data);
     243    }
     244  }
    234245}
Note: See TracChangeset for help on using the changeset viewer.