func()

in s2/regioncoverer.go [502:555]


func (c *coverer) isCanonical(covering CellUnion) bool {
	trueMax := c.maxLevel
	if c.levelMod != 1 {
		trueMax = c.maxLevel - (c.maxLevel-c.minLevel)%c.levelMod
	}
	tooManyCells := len(covering) > c.maxCells
	sameParentCount := 1

	prevID := CellID(0)
	for _, id := range covering {
		if !id.IsValid() {
			return false
		}

		// Check that the CellID level is acceptable.
		level := id.Level()
		if level < c.minLevel || level > trueMax {
			return false
		}
		if c.levelMod > 1 && (level-c.minLevel)%c.levelMod != 0 {
			return false
		}

		if prevID != 0 {
			// Check that cells are sorted and non-overlapping.
			if prevID.RangeMax() >= id.RangeMin() {
				return false
			}

			lev, ok := id.CommonAncestorLevel(prevID)
			// If there are too many cells, check that no pair of adjacent cells
			// could be replaced by an ancestor.
			if tooManyCells && (ok && lev >= c.minLevel) {
				return false
			}

			// Check that there are no sequences of (4 ** level_mod) cells that all
			// have the same parent (considering only multiples of "level_mod").
			pLevel := level - c.levelMod
			if pLevel < c.minLevel || level != prevID.Level() ||
				id.Parent(pLevel) != prevID.Parent(pLevel) {
				sameParentCount = 1
			} else {
				sameParentCount++
				if sameParentCount == 1<<uint(2*c.levelMod) {
					return false
				}
			}
		}
		prevID = id
	}

	return true
}