package main

const EPSILON int = 0

type State struct {
	content     int              // Contents of current state
	isEmpty     bool             // If it is empty - Union operator states will be empty
	isLast      bool             // If it is the last state (acept state)
	output      []*State         // The outputs of the current state ie. the 'outward arrows'. A union operator state will have more than one of these.
	transitions map[int][]*State // Transitions to different states (maps a character (int representation) to a _list of states. This is useful if one character can lead multiple states eg. ab|aa)
}

type NFA struct {
	start   State
	outputs []State
}

// verifyLastStatesHelper performs the depth-first recursion needed for verifyLastStates
func verifyLastStatesHelper(state *State, visited map[*State]bool) {
	if len(state.transitions) == 0 {
		state.isLast = true
		return
	}
	if len(state.transitions) == 1 && len(state.transitions[state.content]) == 1 && state.transitions[state.content][0] == state { // Eg. a*
		state.isLast = true
		return
	}
	if visited[state] == true {
		return
	}
	visited[state] = true
	for _, states := range state.transitions {
		for i := range states {
			if states[i] != state {
				verifyLastStatesHelper(states[i], visited)
			}
		}
	}
}

// verifyLastStates penables the 'isLast' flag for the leaf nodes (last states)
func verifyLastStates(start []State) {
	verifyLastStatesHelper(&start[0], make(map[*State]bool))
}