Implement PCRE Matching (prefer left-branch) #2
@@ -15,8 +15,8 @@ const (
|
|||||||
|
|
||||||
func getPriority(state *nfaState) int {
|
func getPriority(state *nfaState) int {
|
||||||
if state.isKleene {
|
if state.isKleene {
|
||||||
return kleene_priority
|
return zerostate_priority
|
||||||
} else if state.isQuestion || state.isAlternation {
|
} else if state.isAlternation {
|
||||||
return alternation_priority
|
return alternation_priority
|
||||||
} else {
|
} else {
|
||||||
if state.isEmpty {
|
if state.isEmpty {
|
||||||
@@ -33,6 +33,14 @@ type priorQueueItem struct {
|
|||||||
index int
|
index int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newPriorQueueItem(state *nfaState) *priorQueueItem {
|
||||||
|
return &priorQueueItem{
|
||||||
|
state: state,
|
||||||
|
index: -1,
|
||||||
|
priority: getPriority(state),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type priorityQueue []*priorQueueItem
|
type priorityQueue []*priorQueueItem
|
||||||
|
|
||||||
func (pq priorityQueue) Len() int {
|
func (pq priorityQueue) Len() int {
|
||||||
@@ -41,7 +49,7 @@ func (pq priorityQueue) Len() int {
|
|||||||
|
|
||||||
func (pq priorityQueue) Less(i, j int) bool {
|
func (pq priorityQueue) Less(i, j int) bool {
|
||||||
if pq[i].priority == pq[j].priority {
|
if pq[i].priority == pq[j].priority {
|
||||||
return pq[i].index > pq[j].index
|
return pq[i].index < pq[j].index
|
||||||
}
|
}
|
||||||
return pq[i].priority > pq[j].priority // We want max-heap, so we use greater-than
|
return pq[i].priority > pq[j].priority // We want max-heap, so we use greater-than
|
||||||
}
|
}
|
||||||
@@ -68,6 +76,11 @@ func (pq *priorityQueue) Pop() any {
|
|||||||
*pq = old[0 : n-1]
|
*pq = old[0 : n-1]
|
||||||
return item
|
return item
|
||||||
}
|
}
|
||||||
|
func (pq *priorityQueue) peek() any {
|
||||||
|
queue := *pq
|
||||||
|
n := len(queue)
|
||||||
|
return queue[n-1]
|
||||||
|
}
|
||||||
|
|
||||||
func (pq *priorityQueue) update(item *priorQueueItem, value *nfaState, priority int) {
|
func (pq *priorityQueue) update(item *priorQueueItem, value *nfaState, priority int) {
|
||||||
item.state = value
|
item.state = value
|
||||||
|
Reference in New Issue
Block a user