private bool Conflict(CartoFilterElement filter, Env env)
        {
            string key   = filter.Key.ToString();
            string value = filter.GetValue(env);

            // if (a=b) && (a=c)
            // if (a=b) && (a!=b)
            // or (a!=b) && (a=b)
            string op = filter.Op.ToString();

            if ((op == "=" && GetFilter(key, "=") != null &&
                 value != GetFilter(key, "=").GetValue(env)) ||
                (op == "!=" && GetFilter(key, "=") != null &&
                 value == GetFilter(key, "=").GetValue(env)) ||
                (op == "=" && GetFilter(key, "!=") != null &&
                 value == GetFilter(key, "!=").GetValue(env)))
            {
                return(false);               //filter.toString() + ' added to ' + this.toString() + ' produces an invalid filter';
            }

            return(false);
        }
        private object Addable(CartoFilterElement filter, Env env)
        {
            string key = filter.Key.ToString();
            string val = filter.GetValue(env);

            string value  = val;
            float  valueF = float.MinValue;

            if (val.Length > 0 && val[0] <= '9')
            {
                ConvertUtility.TryParseFloat(value, numStyles, numFormat, out valueF);
            }

            switch (filter.Op.ToString())
            {
            case "=":
                // if there is already foo= and we're adding foo=
                if (GetFilter(key, "=") != null)
                {
                    if (GetFilter(key, "=").GetValue(env) != value)
                    {
                        return(false);
                    }
                    else
                    {
                        return(null);
                    }
                }
                if (GetFilter(key, "!=" + value) != null)
                {
                    return(false);
                }
                if (GetFilter(key, ">") != null && GetFilter(key, ">").GetValueF(env) >= valueF)
                {
                    return(false);
                }
                if (GetFilter(key, "<") != null && GetFilter(key, "<").GetValueF(env) <= valueF)
                {
                    return(false);
                }
                if (GetFilter(key, ">=") != null && GetFilter(key, ">=").GetValueF(env) > valueF)
                {
                    return(false);
                }
                if (GetFilter(key, "<=") != null && GetFilter(key, "<=").GetValueF(env) < valueF)
                {
                    return(false);
                }
                return(true);

            case "=~":
                return(true);

            case "%":
                return(true);

            case "!=":
                if (GetFilter(key, "=") != null)
                {
                    return((GetFilter(key, "=").GetValue(env) == value) ? (object)false : null);
                }
                if (GetFilter(key, "!=" + value) != null)
                {
                    return(null);
                }
                if (GetFilter(key, ">") != null && GetFilter(key, ">").GetValueF(env) >= valueF)
                {
                    return(null);
                }
                if (GetFilter(key, "<") != null && GetFilter(key, "<").GetValueF(env) <= valueF)
                {
                    return(null);
                }
                if (GetFilter(key, ">=") != null && GetFilter(key, ">=").GetValueF(env) > valueF)
                {
                    return(null);
                }
                if (GetFilter(key, "<=") != null && GetFilter(key, "<=").GetValueF(env) < valueF)
                {
                    return(null);
                }
                return(true);

            case ">":
                if (GetFilter(key, "=") != null)
                {
                    if (GetFilter(key, "=").GetValueF(env) <= valueF)
                    {
                        return(false);
                    }
                    else
                    {
                        return(null);
                    }
                }
                if (GetFilter(key, "<") != null && GetFilter(key, "<").GetValueF(env) <= valueF)
                {
                    return(false);
                }
                if (GetFilter(key, "<=") != null && GetFilter(key, "<=").GetValueF(env) <= valueF)
                {
                    return(false);
                }
                if (GetFilter(key, ">") != null && GetFilter(key, ">").GetValueF(env) >= valueF)
                {
                    return(null);
                }
                if (GetFilter(key, ">=") != null && GetFilter(key, ">=").GetValueF(env) > valueF)
                {
                    return(null);
                }
                return(true);

            case ">=":
                if (GetFilter(key, "=") != null)
                {
                    return((GetFilter(key, "=").GetValueF(env) < valueF) ? (object)false : null);
                }
                if (GetFilter(key, "<") != null && GetFilter(key, "<").GetValueF(env) <= valueF)
                {
                    return(false);
                }
                if (GetFilter(key, "<=") != null && GetFilter(key, "<=").GetValueF(env) < valueF)
                {
                    return(false);
                }
                if (GetFilter(key, ">") != null && GetFilter(key, ">").GetValueF(env) >= valueF)
                {
                    return(null);
                }
                if (GetFilter(key, ">=") != null && GetFilter(key, ">=").GetValueF(env) >= valueF)
                {
                    return(null);
                }
                return(true);

            case "<":
                if (GetFilter(key, "=") != null)
                {
                    return((GetFilter(key, "=").GetValueF(env) >= valueF) ? (object)false : null);
                }
                if (GetFilter(key, ">") != null && GetFilter(key, ">").GetValueF(env) >= valueF)
                {
                    return(false);
                }
                if (GetFilter(key, ">=") != null && GetFilter(key, ">=").GetValueF(env) >= valueF)
                {
                    return(false);
                }
                if (GetFilter(key, "<") != null && GetFilter(key, "<").GetValueF(env) <= valueF)
                {
                    return(null);
                }
                if (GetFilter(key, "<=") != null && GetFilter(key, "<=").GetValueF(env) < valueF)
                {
                    return(null);
                }
                return(true);

            case "<=":
                if (GetFilter(key, "=") != null)
                {
                    return((GetFilter(key, "=").GetValueF(env) > valueF) ? (object)false : null);
                }
                if (GetFilter(key, ">") != null && GetFilter(key, ">").GetValueF(env) >= valueF)
                {
                    return(false);
                }
                if (GetFilter(key, ">=") != null && GetFilter(key, ">=").GetValueF(env) > valueF)
                {
                    return(false);
                }
                if (GetFilter(key, "<") != null && GetFilter(key, "<").GetValueF(env) <= valueF)
                {
                    return(null);
                }
                if (GetFilter(key, "<=") != null && GetFilter(key, "<=").GetValueF(env) <= valueF)
                {
                    return(null);
                }
                return(true);
            }

            return(null);
        }
        public void Add(CartoFilterElement filter, Env env)
        {
            bool conflict = Conflict(filter, env);

            if (conflict)
            {
                return; //conflict;
            }
            string key    = filter.Key.ToString();
            string value  = filter.Value.ToString();
            string op     = filter.Op.ToString();
            float  valueF = float.NaN;

            if (value.Length > 0 && value[0] <= '9')
            {
                ConvertUtility.TryParseFloat(value, numStyles, numFormat, out valueF);
            }

            if (op == "=")
            {
                /*foreach (var item in m_filters.Where(kvp => kvp.Key == key).ToList())
                 * {
                 * Remove(item.Key);
                 *                      }*/
                Remove(key);
                m_filters[key + "="] = filter;
            }
            else if (op == "!=")
            {
                m_filters[key + "!=" + filter.GetValue(env)] = filter;
            }
            else if (op == "=~")
            {
                m_filters[key + "=~" + filter.GetValue(env)] = filter;
            }
            else if (op == "%")
            {
                m_filters[key + "%" + filter.GetValue(env)] = filter;
            }
            else if (op == ">")
            {
                // If there are other filters that are also >
                // but are less than this one, they don't matter, so
                // remove them.

                /*	foreach (var item in m_filters.Where(kvp => kvp.Key == key && kvp.Value.GetValueF(env) <= valueF).ToList()) {
                 *  Remove(item.Key);
                 * }*/
                Remove(key, (x) => (x.GetValueF(env) <= valueF));

                m_filters[key + ">"] = filter;
            }
            else if (op == ">=")
            {
                /*foreach (var item in m_filters.Where(kvp => kvp.Key == key && kvp.Value.GetValueF(env) < valueF).ToList()) {
                 * Remove(item.Key);
                 *                      }*/
                Remove(key, (x) => (x.GetValueF(env) < valueF));
                if (GetFilter(key + "!=" + value) != null)
                {
                    Remove(key + "!=" + value);
                    filter.Op            = new dotless.Core.Parser.Infrastructure.Nodes.TextNode(">");
                    m_filters[key + ">"] = filter;
                }
                else
                {
                    m_filters[key + ">="] = filter;
                }
            }
            else if (op == "<")
            {
                /*foreach (var item in m_filters.Where(kvp => kvp.Key == key && kvp.Value.GetValueF(env) >= valueF).ToList()) {
                 * Remove(item.Key);
                 *                      }*/
                Remove(key, (x) => (x.GetValueF(env) >= valueF));
                m_filters[key + "<"] = filter;
            }
            else if (op == "<=")
            {
                /*foreach (var item in m_filters.Where(kvp => kvp.Key == key && kvp.Value.GetValueF(env) > valueF).ToList()) {
                 * Remove(item.Key);
                 *                      }*/
                Remove(key, (x) => (x.GetValueF(env) > valueF));

                if (GetFilter(key + "!=" + value) != null)
                {
                    Remove(key + "!=" + value);
                    filter.Op            = new dotless.Core.Parser.Infrastructure.Nodes.TextNode("<");
                    m_filters[key + "<"] = filter;
                }
                else
                {
                    m_filters[key + "<="] = filter;
                }
            }
        }
    private object Addable(CartoFilterElement filter, Env env)
    {
      string key = filter.Key.ToString();
      string val = filter.GetValue(env);

      string value = val;
      float valueF = float.MinValue;

      if (val.Length > 0 && val[0] <= '9')
        ConvertUtility.TryParseFloat(value, numStyles, numFormat, out valueF);

      switch (filter.Op.ToString())
      {
         case "=":
            // if there is already foo= and we're adding foo=
            if (GetFilter(key , "=") != null) {
                if (GetFilter(key , "=").GetValue(env) != value) {
                    return false;
                } else {
                    return null;
                }
            }
            if (GetFilter(key, "!=" + value) != null) return false;
            if (GetFilter(key, ">") != null && GetFilter(key, ">").GetValueF(env) >= valueF) return false;
            if (GetFilter(key, "<") != null && GetFilter(key, "<").GetValueF(env) <= valueF) return false;
            if (GetFilter(key, ">=") != null  && GetFilter(key, ">=").GetValueF(env) > valueF) return false;
            if (GetFilter(key, "<=") != null  && GetFilter(key, "<=").GetValueF(env) < valueF) return false;
            return true;
        case "=~":
            return true;
        case "%":
          return true;
        case "!=":
            if (GetFilter(key, "=") != null) return (GetFilter(key, "=").GetValue(env) == value) ? (object)false : null;
            if (GetFilter(key, "!=" + value) != null) return null;
            if (GetFilter(key, ">") != null && GetFilter(key, ">").GetValueF(env) >= valueF) return null;
            if (GetFilter(key, "<") != null && GetFilter(key, "<").GetValueF(env) <= valueF) return null;
            if (GetFilter(key, ">=") != null && GetFilter(key, ">=").GetValueF(env) > valueF) return null;
            if (GetFilter(key, "<=") != null && GetFilter(key, "<=").GetValueF(env) < valueF) return null;
            return true;
        case ">":
            if (GetFilter(key, "=") != null)
            {
                if (GetFilter(key, "=").GetValueF(env) <= valueF) {
                    return false;
                } else {
                    return null;
                }
            }
            if (GetFilter(key, "<") != null && GetFilter(key, "<").GetValueF(env) <= valueF) return false;
            if (GetFilter(key, "<=") != null  && GetFilter(key, "<=").GetValueF(env) <= valueF) return false;
            if (GetFilter(key, ">") != null && GetFilter(key, ">").GetValueF(env) >= valueF) return null;
            if (GetFilter(key, ">=") != null  && GetFilter(key, ">=").GetValueF(env) > valueF) return null;
            return true;

        case ">=":
            if (GetFilter(key, "=" ) != null) return (GetFilter(key, "=").GetValueF(env) < valueF) ? (object)false : null;
            if (GetFilter(key, "<" ) != null && GetFilter(key, "<").GetValueF(env) <= valueF) return false;
            if (GetFilter(key, "<=") != null && GetFilter(key, "<=").GetValueF(env) < valueF) return false;
            if (GetFilter(key, ">" ) != null && GetFilter(key, ">").GetValueF(env) >= valueF) return null;
            if (GetFilter(key, ">=") != null && GetFilter(key, ">=").GetValueF(env) >= valueF) return null;
            return true;

        case "<":
            if (GetFilter(key, "=" ) != null) return (GetFilter(key, "=").GetValueF(env) >= valueF) ? (object)false : null;
            if (GetFilter(key, ">" ) != null && GetFilter(key, ">").GetValueF(env) >= valueF) return false;
            if (GetFilter(key, ">=") != null && GetFilter(key, ">=").GetValueF(env) >= valueF) return false;
            if (GetFilter(key, "<" ) != null && GetFilter(key, "<").GetValueF(env) <= valueF) return null;
            if (GetFilter(key, "<=") != null && GetFilter(key, "<=").GetValueF(env) < valueF) return null;
            return true;

        case "<=":
            if (GetFilter(key, "=" ) != null) return (GetFilter(key, "=").GetValueF(env) > valueF) ? (object)false : null;
            if (GetFilter(key, ">" ) != null && GetFilter(key, ">").GetValueF(env) >= valueF) return false;
            if (GetFilter(key, ">=") != null && GetFilter(key, ">=").GetValueF(env) > valueF) return false;
            if (GetFilter(key, "<" ) != null && GetFilter(key, "<").GetValueF(env) <= valueF) return null;
            if (GetFilter(key, "<=") != null && GetFilter(key, "<=").GetValueF(env) <= valueF) return null;
            return true;
      }
      
      return null;
    }
    public void Add(CartoFilterElement filter, Env env)
    {
      bool conflict = Conflict(filter, env);

      if (conflict)
        return; //conflict;

      string key = filter.Key.ToString();
      string value = filter.Value.ToString();
      string op = filter.Op.ToString();
      float valueF = float.NaN;
      if (value.Length > 0 && value[0] <= '9')
          ConvertUtility.TryParseFloat(value, numStyles, numFormat, out valueF);

      if (op == "=")
      {
        /*foreach (var item in m_filters.Where(kvp => kvp.Key == key).ToList())
        {
          Remove(item.Key);
				}*/
        Remove(key);
        m_filters[key + "="] = filter;
      }
      else if (op == "!=")
      {
        m_filters[key + "!=" + filter.GetValue(env)] = filter;
      }
      else if (op == "=~")
      {
        m_filters[key + "=~" + filter.GetValue(env)] = filter;
      }
      else if (op == "%")
      {
        m_filters[key + "%" + filter.GetValue(env)] = filter;
      }
      else if (op == ">")
      {
        // If there are other filters that are also >
        // but are less than this one, they don't matter, so
        // remove them.
        /*	foreach (var item in m_filters.Where(kvp => kvp.Key == key && kvp.Value.GetValueF(env) <= valueF).ToList()) {
            Remove(item.Key);
          }*/
        Remove(key, (x) => (x.GetValueF(env) <= valueF));

        m_filters[key + ">"] = filter;
      }
      else if (op == ">=")
      {
        /*foreach (var item in m_filters.Where(kvp => kvp.Key == key && kvp.Value.GetValueF(env) < valueF).ToList()) {
          Remove(item.Key);
				}*/
        Remove(key, (x) => (x.GetValueF(env) < valueF));
        if (GetFilter(key + "!=" + value) != null)
        {
          Remove(key + "!=" + value);
          filter.Op = new dotless.Core.Parser.Infrastructure.Nodes.TextNode(">");
          m_filters[key + ">"] = filter;
        }
        else
        {
          m_filters[key + ">="] = filter;
        }
      }
      else if (op == "<")
      {
        /*foreach (var item in m_filters.Where(kvp => kvp.Key == key && kvp.Value.GetValueF(env) >= valueF).ToList()) {
          Remove(item.Key);
				}*/
        Remove(key, (x) => (x.GetValueF(env) >= valueF));
        m_filters[key + "<"] = filter;
      }
      else if (op == "<=")
      {
        /*foreach (var item in m_filters.Where(kvp => kvp.Key == key && kvp.Value.GetValueF(env) > valueF).ToList()) {
          Remove(item.Key);
				}*/
        Remove(key, (x) => (x.GetValueF(env) > valueF));

        if (GetFilter(key + "!=" + value) != null)
        {
          Remove(key + "!=" + value);
          filter.Op = new dotless.Core.Parser.Infrastructure.Nodes.TextNode("<");
          m_filters[key + "<"] = filter;
        }
        else
        {
          m_filters[key + "<="] = filter;
        }
      }
    }
    private bool Conflict(CartoFilterElement filter, Env env)
		{
			string key = filter.Key.ToString();
			string value = filter.GetValue(env);

      // if (a=b) && (a=c)
      // if (a=b) && (a!=b)
      // or (a!=b) && (a=b)
      string op = filter.Op.ToString();
      if ((op == "=" && GetFilter(key, "=") != null &&
			        value != GetFilter(key , "=").GetValue(env)) ||
			        (op == "!=" && GetFilter(key , "=") != null &&
			        value == GetFilter(key , "=").GetValue(env)) ||
			        (op == "=" && GetFilter(key , "!=") != null &&
			        value == GetFilter(key, "!=").GetValue(env))) {
				return false;//filter.toString() + ' added to ' + this.toString() + ' produces an invalid filter';
			}

			return false;
		}