// Los lenguajes del capítulo, cada uno como una función de C++ que decide mirando la cadena,
// sin usar ninguna gramática. Las pruebas comparan cada gramática con su función.
#ifndef PREDICATES_H
#define PREDICATES_H

#include <algorithm>
#include <cstddef>
#include <string>

inline std::size_t count_of(const std::string& w, char c) {
    return static_cast<std::size_t>(std::count(w.begin(), w.end(), c));
}

// w = x^i y^j z^k, con las tres letras en ese orden. Devuelve false si w no tiene esa forma.
inline bool three_blocks(const std::string& w, char x, char y, char z, std::size_t& i, std::size_t& j,
                         std::size_t& k) {
    std::size_t p = 0;
    i = j = k = 0;
    while (p < w.size() && w[p] == x) {
        p++;
        i++;
    }
    while (p < w.size() && w[p] == y) {
        p++;
        j++;
    }
    while (p < w.size() && w[p] == z) {
        p++;
        k++;
    }
    return p == w.size();
}

// { 0^n 1^n : n ≥ 0 }
inline bool is_zeros_ones(const std::string& w) {
    std::size_t half = w.size() / 2;
    return w.size() % 2 == 0 && w == std::string(half, '0') + std::string(half, '1');
}

inline bool is_palindrome(const std::string& w) {
    return std::equal(w.begin(), w.end(), w.rbegin());
}

// Paréntesis balanceados: ningún prefijo cierra más de lo que abrió, y al final no queda nada abierto.
inline bool is_balanced(const std::string& w) {
    long open = 0;
    for (char c : w) {
        open += (c == '(') ? 1 : -1;
        if (open < 0) {
            return false;
        }
    }
    return open == 0;
}

inline bool has_equal_ab(const std::string& w) {
    return count_of(w, 'a') == count_of(w, 'b');
}

inline bool has_even_ones(const std::string& w) {
    return count_of(w, '1') % 2 == 0;
}

// Una lista de bloques 0^n 1^n con n ≥ 1, uno detrás de otro. La lista puede ser vacía.
inline bool is_block_list(const std::string& w) {
    std::size_t p = 0;
    while (p < w.size()) {
        std::size_t zeros = 0;
        while (p < w.size() && w[p] == '0') {
            p++;
            zeros++;
        }
        std::size_t ones = 0;
        while (p < w.size() && w[p] == '1') {
            p++;
            ones++;
        }
        if (zeros == 0 || zeros != ones) {
            return false;
        }
    }
    return true;
}

// { a^i b^j c^k : i = j o j = k }
inline bool is_abc_union(const std::string& w) {
    std::size_t i = 0, j = 0, k = 0;
    return three_blocks(w, 'a', 'b', 'c', i, j, k) && (i == j || j == k);
}

// Expresiones con a, +, *, ( y ). Se lee de izquierda a derecha sabiendo si toca un operando
// o un operador: un operando es a o un paréntesis que abre; después de un operando viene un
// operador, un paréntesis que cierra o el final.
inline bool is_expression(const std::string& w) {
    bool operand_next = true;
    long open = 0;
    for (char c : w) {
        if (c == 'a') {
            if (!operand_next) {
                return false;
            }
            operand_next = false;
        } else if (c == '(') {
            if (!operand_next) {
                return false;
            }
            open++;
        } else if (c == ')') {
            if (operand_next || open == 0) {
                return false;
            }
            open--;
        } else {  // + o *
            if (operand_next) {
                return false;
            }
            operand_next = true;
        }
    }
    return !operand_next && open == 0;
}

// Instrucciones con if y else, donde i es "if condición then", e es "else" y a es una
// instrucción simple. La cadena tiene la forma (i…i a e)…(i…i a e) i…i a, y cada e
// necesita un i anterior que todavía no tenga su else.
inline bool is_if_else(const std::string& w) {
    long free_ifs = 0;
    char previous = 'e';  // como si antes hubiera un else: lo primero es i o a
    for (char c : w) {
        if (c == 'i' || c == 'a') {
            if (previous == 'a') {
                return false;
            }
            free_ifs += (c == 'i') ? 1 : 0;
        } else {  // e
            if (previous != 'a' || free_ifs == 0) {
                return false;
            }
            free_ifs--;
        }
        previous = c;
    }
    return previous == 'a';
}

#endif
