Added NODWELL keyword

This commit is contained in:
tidwall 2018-08-22 16:26:27 -07:00
parent fa062453cd
commit 3a6f366955
3 changed files with 35 additions and 21 deletions

View File

@ -364,12 +364,13 @@ func fenceMatchRoam(
return return
} }
p := obj.CalculatedPoint() p := obj.CalculatedPoint()
var prevNearbys []roamMatch prevNearbys := fence.roam.nearbys[tid]
if fence.roam.nearbys != nil { var newNearbys map[string]bool
prevNearbys = fence.roam.nearbys[tid]
}
col.Nearby(0, p.Y, p.X, fence.roam.meters, math.Inf(-1), math.Inf(+1), col.Nearby(0, p.Y, p.X, fence.roam.meters, math.Inf(-1), math.Inf(+1),
func(id string, obj geojson.Object, fields []float64) bool { func(id string, obj geojson.Object, fields []float64) bool {
if c.hasExpired(fence.roam.key, id) {
return true
}
var idMatch bool var idMatch bool
if id == tid { if id == tid {
return true // skip self return true // skip self
@ -382,34 +383,38 @@ func fenceMatchRoam(
if !idMatch { if !idMatch {
return true return true
} }
if newNearbys == nil {
newNearbys = make(map[string]bool)
}
newNearbys[id] = true
prev := prevNearbys[id]
if prev {
delete(prevNearbys, id)
}
match := roamMatch{ match := roamMatch{
id: id, id: id,
obj: obj, obj: obj,
meters: obj.CalculatedPoint().DistanceTo(p), meters: obj.CalculatedPoint().DistanceTo(p),
} }
for i := 0; i < len(prevNearbys); i++ { if !prev || !fence.nodwell {
if prevNearbys[i].id == match.id { // brand new "nearby"
prevNearbys[i] = prevNearbys[len(prevNearbys)-1]
prevNearbys = prevNearbys[:len(prevNearbys)-1]
i--
break
}
}
nearbys = append(nearbys, match) nearbys = append(nearbys, match)
}
return true return true
}, },
) )
for i := 0; i < len(prevNearbys); i++ { for id := range prevNearbys {
obj, _, ok := col.Get(prevNearbys[i].id) obj, _, ok := col.Get(id)
if ok { if ok && !c.hasExpired(fence.roam.key, id) {
faraways = append(faraways, roamMatch{ faraways = append(faraways, roamMatch{
id: prevNearbys[i].id, id: id, obj: obj,
obj: obj,
meters: obj.CalculatedPoint().DistanceTo(p), meters: obj.CalculatedPoint().DistanceTo(p),
}) })
} }
} }
if len(nearbys) == 0 {
if len(newNearbys) == 0 {
if fence.roam.nearbys != nil { if fence.roam.nearbys != nil {
delete(fence.roam.nearbys, tid) delete(fence.roam.nearbys, tid)
if len(fence.roam.nearbys) == 0 { if len(fence.roam.nearbys) == 0 {
@ -418,9 +423,9 @@ func fenceMatchRoam(
} }
} else { } else {
if fence.roam.nearbys == nil { if fence.roam.nearbys == nil {
fence.roam.nearbys = make(map[string][]roamMatch) fence.roam.nearbys = make(map[string]map[string]bool)
} }
fence.roam.nearbys[tid] = nearbys fence.roam.nearbys[tid] = newNearbys
} }
return return
} }

View File

@ -35,7 +35,7 @@ type roamSwitches struct {
pattern bool pattern bool
meters float64 meters float64
scan string scan string
nearbys map[string][]roamMatch nearbys map[string]map[string]bool
} }
type roamMatch struct { type roamMatch struct {

View File

@ -233,6 +233,7 @@ type searchScanBaseTokens struct {
lineout string lineout string
fence bool fence bool
distance bool distance bool
nodwell bool
detect map[string]bool detect map[string]bool
accept map[string]bool accept map[string]bool
glob string glob string
@ -535,6 +536,14 @@ func (c *Controller) parseSearchScanBaseTokens(
} }
} }
continue continue
case "nodwell":
vs = nvs
if t.desc || asc {
err = errDuplicateArgument(strings.ToUpper(wtok))
return
}
t.nodwell = true
continue
case "desc": case "desc":
vs = nvs vs = nvs
if t.desc || asc { if t.desc || asc {