Index: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfigurationBase.cs
===================================================================
--- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfigurationBase.cs	(revision 15017)
+++ branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfigurationBase.cs	(revision 15032)
@@ -8,4 +8,5 @@
   using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
   using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
+  using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
   using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
 
@@ -17,7 +18,6 @@
 
       enabledExpressions = ExpressionTable.ExpressionNames.ToList();
-      enabledStacks = new Dictionary<StackTypes, bool>();
-
-      InitEnabledStacks();
+      expressionsPerStackCount = new Dictionary<StackTypes, int>();
+      InitExpressionsPerStackCount();
     }
 
@@ -27,11 +27,9 @@
     protected PushConfigurationBase(PushConfigurationBase origin, Cloner cloner) : base(origin, cloner) {
       enabledExpressions = origin.EnabledExpressions.ToList();
-      enabledStacks = origin.EnabledStacks.ToDictionary(x => x.Key, x => x.Value);
+      expressionsPerStackCount = origin.expressionsPerStackCount.ToDictionary(x => x.Key, x => x.Value);
     }
 
     [Storable]
-    private readonly Dictionary<StackTypes, bool> enabledStacks;
-
-    public IReadOnlyDictionary<StackTypes, bool> EnabledStacks { get { return enabledStacks; } }
+    private readonly Dictionary<StackTypes, int> expressionsPerStackCount;
 
     public event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged;
@@ -40,5 +38,10 @@
     protected readonly List<string> enabledExpressions;
 
-    public IList<string> EnabledExpressions
+    public bool IsStackEnabled(StackTypes type) {
+      int count;
+      return expressionsPerStackCount.TryGetValue(type, out count) && count > 0;
+    }
+
+    public IReadOnlyList<string> EnabledExpressions
     {
       get { return enabledExpressions; }
@@ -55,8 +58,18 @@
     }
 
-    private void InitEnabledStacks(bool state = true) {
+    private void InitExpressionsPerStackCount() {
       foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {
-        if (!enabledStacks.ContainsKey(type) && type != StackTypes.None)
-          enabledStacks.Add(type, state);
+        if (type == StackTypes.None) continue;
+        expressionsPerStackCount.Add(type, 0);
+      }
+
+      foreach (var expressionName in ExpressionTable.ExpressionNames) {
+        var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key;
+        var attribute = ExpressionTable.TypeToAttributeTable[type];
+
+        expressionsPerStackCount[attribute.StackType]++;
+
+        foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues())
+          expressionsPerStackCount[additionalStackType]++;
       }
     }
@@ -64,18 +77,16 @@
     public void SetEnabledStacks(StackTypes types) {
       // Disable all
-      EnabledExpressions.Clear();
-
-      foreach (StackTypes type in Enum.GetValues(types.GetType())) {
+      enabledExpressions.Clear();
+
+      foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {
         if (type == StackTypes.None) continue;
-        enabledStacks[type] = false;
-      }
-
-      foreach (var pair in ExpressionTable.StackDependencyToNamesTable) {
-        if (types.HasFlag(pair.Key)) {
-          foreach (var name in pair.Value) {
-            EnableExpression(name, true);
-          }
-        }
-      }
+        expressionsPerStackCount[type] = 0;
+      }
+
+      var expressionNames = ExpressionTable.StackDependencyToNamesTable
+        .Where(pair => types.HasFlag(pair.Key))
+        .SelectMany(pair => pair.Value);
+
+      EnableExpressions(expressionNames);
     }
 
@@ -92,5 +103,5 @@
           return (attribute.StackType | attribute.AdditionalStackDependencies)
             .ToEnumerable()
-            .All(st => enabledStacks.ContainsKey(st) && enabledStacks[st]);
+            .All(IsStackEnabled);
         });
 
@@ -98,10 +109,10 @@
     }
 
-    private void EnableExpressions(IEnumerable<string> names) {
-      foreach (var name in names.Except(EnabledExpressions))
-        EnabledExpressions.Add(name);
+    private void EnableExpressions(IEnumerable<string> expressionNames) {
+      foreach (var expressionName in expressionNames.Except(EnabledExpressions))
+        EnableExpression(expressionName, false);
 
       if (EnabledExpressionsChanged != null) {
-        EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(names, new string[0]));
+        EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(expressionNames, new string[0]));
       }
     }
@@ -119,86 +130,94 @@
     }
 
-    private void DisableExpressions(IEnumerable<string> names) {
-      foreach (var name in names.Intersect(EnabledExpressions))
-        EnabledExpressions.Remove(name);
+    private void DisableExpressions(IEnumerable<string> expressionNames) {
+      foreach (var expressionName in expressionNames.Intersect(EnabledExpressions))
+        DisableExpression(expressionName, false);
 
       if (EnabledExpressionsChanged != null) {
-        EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], names));
-      }
-    }
-
-    public void EnableExpression(string name, bool enableStackIfDisabled = false) {
-      if (EnabledExpressions.Contains(name)) return;
-
-      EnabledExpressions.Add(name);
-
-      if (enableStackIfDisabled) {
-        var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key;
-        var attribute = ExpressionTable.TypeToAttributeTable[type];
-        enabledStacks[attribute.StackType] = true;
-      }
-
-      if (EnabledExpressionsChanged != null) {
+        EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], expressionNames));
+      }
+    }
+
+    public void EnableExpression(string expressionName) {
+      EnableExpression(expressionName, true);
+    }
+
+    private void EnableExpression(string expressionName, bool triggerExpressionChanged) {
+      if (enabledExpressions.Contains(expressionName)) return;
+
+      var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key;
+      var attribute = ExpressionTable.TypeToAttributeTable[type];
+
+      enabledExpressions.Add(expressionName);
+      expressionsPerStackCount[attribute.StackType]++;
+      foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues())
+        expressionsPerStackCount[additionalStackType]++;
+
+      if (EnabledExpressionsChanged != null && triggerExpressionChanged) {
         EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
-          new[] { name },
+          new[] { expressionName },
           new string[0]));
       }
     }
 
-    public void DisableExpression(string name, bool disableStackIfEnabled = false) {
-      if (!EnabledExpressions.Contains(name)) return;
-
-      EnabledExpressions.Remove(name);
-
-      if (disableStackIfEnabled) {
-        var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key;
-        var attribute = ExpressionTable.TypeToAttributeTable[type];
-        enabledStacks[attribute.StackType] = false;
-      }
-
-      if (EnabledExpressionsChanged != null) {
+    public void DisableExpression(string expressionName) {
+      DisableExpression(expressionName, true);
+    }
+
+    public void DisableExpression(string expressionName, bool triggerExpressionChanged) {
+      if (!EnabledExpressions.Contains(expressionName)) return;
+
+      var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key;
+      var attribute = ExpressionTable.TypeToAttributeTable[type];
+
+      enabledExpressions.Remove(expressionName);
+      expressionsPerStackCount[attribute.StackType]--;
+      foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues())
+        expressionsPerStackCount[additionalStackType]--;
+
+      if (EnabledExpressionsChanged != null && triggerExpressionChanged) {
         EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
           new string[0],
-          new[] { name }));
-      }
-    }
-
-    public void EnableExpression<T>(bool enableStackIfDisabled = false) where T : Expression {
+          new[] { expressionName }));
+      }
+    }
+
+    public void EnableExpression<T>() where T : Expression {
       var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];
-      EnableExpression(attribute.ExpressionName, enableStackIfDisabled);
-    }
-
-    public void DisableExpression<T>(bool disableStackIfEnabled = false) where T : Expression {
+      EnableExpression(attribute.Name);
+    }
+
+    public void DisableExpression<T>() where T : Expression {
       var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];
-      DisableExpression(attribute.ExpressionName, disableStackIfEnabled);
-    }
-
-    public void SetExpression(string name, bool state, bool cascadeForStack = false) {
-      if (state) EnableExpression(name, cascadeForStack);
-      else DisableExpression(name, cascadeForStack);
-    }
-
-    public void SetExpression<T>(bool state, bool cascadeForStack = false) where T : Expression {
-      if (state) EnableExpression<T>(cascadeForStack);
-      else DisableExpression<T>(cascadeForStack);
-    }
-
-    public void EnableStack(StackTypes type, bool enableExpressions = true, bool enableDependencies = true) {
-      enabledStacks[type] = true;
-
-      if (enableExpressions) EnableExpressionOfStack(type);
-      if (enableDependencies) EnableExpressionDependentOnStack(type);
-    }
-
-    public void DisableStack(StackTypes type, bool disableExpressions = true, bool disableDependencies = true) {
-      enabledStacks[type] = false;
-
-      if (disableExpressions) DisableExpressionsOfStack(type);
-      if (disableDependencies) DisableExpressionsDependentOnStack(type);
-    }
-
-    public void SetStack(StackTypes type, bool state, bool setExpressions = true, bool setDependencies = true) {
-      if (state) EnableStack(type, setExpressions, setDependencies);
-      else DisableStack(type, setExpressions, setDependencies);
+      DisableExpression(attribute.Name);
+    }
+
+    public void SetExpression(string name, bool state) {
+      if (state) EnableExpression(name);
+      else DisableExpression(name);
+    }
+
+    public void SetExpression<T>(bool state) where T : Expression {
+      if (state) EnableExpression<T>();
+      else DisableExpression<T>();
+    }
+
+    public void EnableStack(StackTypes type, bool enableDependencies = false) {
+      EnableExpressionOfStack(type);
+
+      if (enableDependencies)
+        EnableExpressionDependentOnStack(type);
+    }
+
+    public void DisableStack(StackTypes type, bool disableDependencies = false) {
+      DisableExpressionsOfStack(type);
+
+      if (disableDependencies)
+        DisableExpressionsDependentOnStack(type);
+    }
+
+    public void SetStack(StackTypes type, bool state, bool setDependencies = false) {
+      if (state) EnableStack(type, setDependencies);
+      else DisableStack(type, setDependencies);
     }
   }
