Skip to content

Commit

Permalink
fix map ranging in order in dedupeInnersOuters
Browse files Browse the repository at this point in the history
  • Loading branch information
roelarents committed Feb 15, 2024
1 parent 0813b0d commit 1993bbb
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions snap/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
internalPixelResolution = 16
)

type IsOuter = bool

// SnapPolygon snaps polygons' points to a tile's internal pixel grid
// and adds points to lines to prevent intersections.
//
Expand Down Expand Up @@ -172,15 +174,15 @@ func dedupeInnersOuters(outers [][][2]float64, inners [][][2]float64) ([][][2]fl
lenOuters := len(outers)
lenInners := len(inners)
lenAll := lenOuters + lenInners
processedIndexes := make(map[int]bool) // true means outer
indexesToDelete := make(map[int]bool) // true means outer
processedIndexes := make(map[int]IsOuter)
indexesToDelete := make(map[int]IsOuter)
for i := 0; i < lenAll; i++ {
if _, processed := processedIndexes[i]; processed {
continue
}
iIsOuter := i < lenOuters
equalIndexes := make(map[int]bool) // true means outer
equalIndexes[i] = iIsOuter
equalIndexes := orderedmap.New[int, IsOuter]( // ordered for predictable outcome when ranging
orderedmap.WithInitialData(orderedmap.Pair[int, IsOuter]{Key: i, Value: iIsOuter}))
for j := i + 1; j < lenAll; j++ {
if _, processed := processedIndexes[j]; processed {
continue
Expand All @@ -201,12 +203,11 @@ func dedupeInnersOuters(outers [][][2]float64, inners [][][2]float64) ([][][2]fl
if !ringsAreEqual(ringI, ringJ, iIsOuter, jIsOuter) {
continue
}
equalIndexes[j] = jIsOuter
equalIndexes.Set(j, jIsOuter)
}
if len(equalIndexes) <= 1 {
if equalIndexes.Len() <= 1 {
continue
}
maps.Copy(processedIndexes, equalIndexes)

lenEqualOuters := countVals(equalIndexes, true)
lenEqualInners := countVals(equalIndexes, false)
Expand All @@ -221,7 +222,9 @@ func dedupeInnersOuters(outers [][][2]float64, inners [][][2]float64) ([][][2]fl
numOutersToDelete = min(lenEqualOuters, lenEqualInners)
numInnersToDelete = numOutersToDelete
}
for equalI, isOuter := range equalIndexes {
for p := equalIndexes.Oldest(); p != nil; p = p.Next() {
equalI, isOuter := p.Key, p.Value
processedIndexes[equalI] = isOuter
if isOuter && numOutersToDelete > 0 {
indexesToDelete[equalI] = isOuter
numOutersToDelete--
Expand Down Expand Up @@ -832,10 +835,10 @@ func deleteFromSliceByIndex[V any, X any](s []V, indexesToDelete map[int]X, inde
return r
}

func countVals[K, V comparable](m map[K]V, v V) int {
func countVals[K, V comparable](m *orderedmap.OrderedMap[K, V], v V) int {
n := 0
for k := range m {
if m[k] == v {
for p := m.Oldest(); p != nil; p = p.Next() {
if p.Value == v {
n++
}
}
Expand Down

0 comments on commit 1993bbb

Please sign in to comment.