// subst returns a copy of pattern with values from m substituted in place
// of wildcards and pos used as the position of tokens from the pattern.
// if m == nil, subst returns a copy of pattern and doesn't change the line
// number information.
500201
func subst(m map[string]reflect.Value, pattern reflect.Value, pos reflect.Value) reflect.Value {
	if !pattern.IsValid() {
		return reflect.Value{}
	}

	// Wildcard gets replaced with map value.
	if m != nil && pattern.Type() == identType {
		name := pattern.Interface().(*ast.Ident).Name
		if isWildcard(name) {
			if old, ok := m[name]; ok {
				return subst(nil, old, reflect.Value{})
			}
		}
	}

	if pos.IsValid() && pattern.Type() == positionType {
		// use new position only if old position was valid in the first place
		if old := pattern.Interface().(token.Pos); !old.IsValid() {
			return pattern
		}
		return pos
	}

	// Otherwise copy.
	switch p := pattern; p.Kind() {
	case reflect.Slice:
		v := reflect.MakeSlice(p.Type(), p.Len(), p.Len())
		for i := 0; i < p.Len(); i++ {
			v.Index(i).Set(subst(m, p.Index(i), pos))
		}
		return v

	case reflect.Struct:
		v := reflect.New(p.Type()).Elem()
		for i := 0; i < p.NumField(); i++ {
			v.Field(i).Set(subst(m, p.Field(i), pos))
		}
		return v

	case reflect.Ptr:
		v := reflect.New(p.Type()).Elem()
		if elem := p.Elem(); elem.IsValid() {
			v.Set(subst(m, elem, pos).Addr())
		}
		return v

	case reflect.Interface:
		v := reflect.New(p.Type()).Elem()
		if elem := p.Elem(); elem.IsValid() {
			v.Set(subst(m, elem, pos))
		}
		return v
	}

	return pattern
}
554496
func Sub(v1, v2 Vect) Vect {
	return Vect{v1.X - v2.X, v1.Y - v2.Y}
}
944892
func (s *Scanner) Scan() (tok Token, pos Pos, lit string) {

	// Read next code point.
	ch0, pos := s.r.read()

	// If we see whitespace then consume all contiguous whitespace.
	// If we see a letter, or certain acceptable special characters, then consume
	// as an ident or reserved word.
	if isWhitespace(ch0) {
		return s.scanWhitespace()
	} else if isLetter(ch0) || ch0 == '_' {
		s.r.unread()
		return s.scanIdent()
	} else if isDigit(ch0) {
		return s.scanNumber()
	}

	// Otherwise parse individual characters.
	switch ch0 {
	case eof:
		return EOF, pos, ""
	case '"':
		return s.scanString()
	case '\'':
		return s.scanString()
	case '.':
		ch1, _ := s.r.read()
		s.r.unread()
		if isDigit(ch1) {
			return s.scanNumber()
		}
		return DOT, pos, ""
	case '+':
		if ch1, _ := s.r.read(); ch1 == '+' {
			return PLUSPLUS, pos, ""
		}
		s.r.unread()
		return PLUS, pos, "+"
		// return s.scanNumber()
	case '-':
		if ch1, _ := s.r.read(); ch1 == '>' {
			return ARROW, pos, ""
		} else if ch1 == '-' {
			return MINUSMINUS, pos, ""
		}
		s.r.unread()
		return MINUS, pos, "-"
		// return s.scanNumber()
	case '*':
		if ch1, _ := s.r.read(); ch1 == '*' {
			return POW, pos, ""
		}
		s.r.unread()
		return MUL, pos, ""
	case '/':
		return DIV, pos, ""
	case '=':
		if ch1, _ := s.r.read(); ch1 == '~' {
			return EQREGEX, pos, ""
		} else if ch1 == '>' {
			return EQARROW, pos, ""
		} else if ch1 == '=' {
			return EQEQ, pos, ""
		}
		s.r.unread()
		return EQ, pos, ""
	case '!':
		if ch1, _ := s.r.read(); ch1 == '=' {
			return NEQ, pos, ""
		} else if ch1 == '~' {
			return NEQREGEX, pos, ""
		}
		s.r.unread()
	case '>':
		if ch1, _ := s.r.read(); ch1 == '=' {
			return GTE, pos, ""
		} else if ch1 == '>' {
			return RSHIFT, pos, ""
		}
		s.r.unread()
		return GT, pos, ""
	case '<':
		if ch1, _ := s.r.read(); ch1 == '=' {
			return LTE, pos, ""
		} else if ch1 == '>' {
			return NEQ, pos, ""
		} else if ch1 == '<' {
			return LSHIFT, pos, ""
		}
		s.r.unread()
		return LT, pos, ""
	case '(':
		return LPAREN, pos, ""
	case ')':
		return RPAREN, pos, ""
	case '[':
		return LBRACKET, pos, ""
	case ']':
		return RBRACKET, pos, ""
	case '{':
		return LCURLY, pos, ""
	case '}':
		return RCURLY, pos, ""
	case ',':
		return COMMA, pos, ""
	case ';':
		return SEMICOLON, pos, ""
	case ':':
		return COLON, pos, ""
	case '^':
		return XOR, pos, ""
	case '|':
		return PIPE, pos, ""
	case '&':
		return AMPERSAND, pos, ""
	case '%':
		return PERCENT, pos, ``
	case '$':
		return DOLLAR, pos, ""
	case '#':
		return HASH, pos, ""
	case '@':
		return ATSIGN, pos, ""
	}

	return ILLEGAL, pos, string(ch0)
}
92644
func (s *Sublist) Match(subject string) []interface{} {
	s.RLock()
	rc, ok := s.cache[subject]
	s.RUnlock()
	if ok {
		return rc
	}

	tsa := [32]string{}
	tokens := tsa[:0]
	start := 0
	for i := 0; i < len(subject); i++ {
		if subject[i] == btsep {
			tokens = append(tokens, subject[start:i])
			start = i + 1
		}
	}
	tokens = append(tokens, subject[start:])
	result := make([]interface{}, 0, 4)

	s.Lock()
	matchLevel(s.root, tokens, &result)

	// Add to our cache
	s.cache[subject] = result
	// Bound the number of entries to sublistMaxCache
	if len(s.cache) > slCacheMax {
		for k := range s.cache {
			delete(s.cache, k)
			break
		}
	}
	s.Unlock()

	return result
}
457665
func (v1 Vec3) Sub(v2 Vec3) Vec3 {
	return Vec3{v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]}
}
45964
func (g *Graph) Substitute(s string, v interface{}) {
	if g == nil || g.Out == nil {
		return
	}
	for _, n := range g.Out {
		if _string(n.This) == s {
			n.This = v
		}
		n.Substitute(s, v)
	}

}
458086
func (z fermat) Sub(x, y fermat) fermat {
	if len(z) != len(x) {
		panic("Add: len(z) != len(x)")
	}
	n := len(y) - 1
	b := subVV(z[:n], x[:n], y[:n])
	b += y[n]
	// If b > 0, we need to subtract b<<n, which is the same as adding b.
	z[n] = x[n]
	if z[0] <= ^big.Word(0)-b {
		z[0] += b
	} else {
		addVW(z, z, b)
	}
	z.norm()
	return z
}
638832
func (s *PointScanner) Scan() (Token, string) {

	// Read the next rune
	ch := s.read()
	if isWhitespace(ch) {
		return WS, string(ch)
	} else if isLetter(ch) {
		return LETTER, string(ch)
	} else if isNumber(ch) {
		return NUMBER, string(ch)
	} else if isDelta(ch) {
		return DELTA, string(ch)
	}

	// Otherwise read the individual character.
	switch ch {
	case eof:
		return EOF, ""
	case '\n':
		return NEWLINE, string(ch)
	case '.':
		return DOT, string(ch)
	case '-':
		return MINUS_SIGN, string(ch)
	case '_':
		return UNDERSCORE, string(ch)
	case '/':
		return SLASH, string(ch)
	case '\\':
		return BACKSLASH, string(ch)
	case ',':
		return COMMA, string(ch)
	case '"':
		return QUOTES, string(ch)
	case '=':
		return EQUALS, string(ch)
	}
	return ILLEGAL, string(ch)
}
1049690
func substitute(src, sub, repl string) string {
	idx := strings.Index(src, sub)
	for idx >= 0 {
		src = src[:idx] + repl + src[idx+len(sub):]
		idx = strings.Index(src, sub)
	}
	return src
}
486459
func (g *LineString) SubLineString(start, stop int) *LineString {
	return NewLineStringFlat(g.layout, g.flatCoords[start*g.stride:stop*g.stride])
}
258593
func (f Wildcard) Regexp() string {
	const any = `%`
	// escape all meta-characters in pattern string
	pattern := regexp.QuoteMeta(f.Pattern)
	// if the pattern is anchored, add regexp analog for it
	if !strings.HasPrefix(pattern, any) {
		pattern = "^" + pattern
	} else {
		pattern = strings.TrimPrefix(pattern, any)
	}
	if !strings.HasSuffix(pattern, any) {
		pattern = pattern + "$"
	} else {
		pattern = strings.TrimSuffix(pattern, any)
	}
	// replace wildcards
	pattern = strings.NewReplacer(
		any, `.*`,
		`\?`, `.`,
	).Replace(pattern)
	return pattern
}
235436
func (a *assertion) Substring(obtained, full string, msgs ...string) bool {
	if !a.IsSubstring(obtained, full) {
		return a.failer.Fail(Substring, obtained, full, msgs...)
	}
	return true
}
195057
func (p *parser) pattern(t *Token) ([]*Token, Error) {
	// build the list of tokens that may appear after a pattern
	_tokens := []*Token{t}
	_token, _err := p.next()
	if _err != nil {
		return _tokens, _err
	}

	// what tokens are we allowed to have follow a pattern?
	var _next []*Token
	switch _token.Type {
	case SEPARATOR:
		_next, _err = p.separator(_token)
		return append(_tokens, _next...), _err

	case WHITESPACE:
		fallthrough
	case PATTERN:
		_next, _err = p.pattern(_token)
		return append(_tokens, _next...), _err

	// if we encounter end of line or file we are done
	case EOL:
		fallthrough
	case EOF:
		return _tokens, nil
	}

	// any other token is invalid
	p.undo(_token)
	return _tokens, p.err(InvalidPatternError)
}
1224595
func posString(slice []string, element string) int {
	for index, elem := range slice {
		if elem == element {
			return index
		}
	}
	return -1
}
98187
func (mat *MatMxN) Sub(dst *MatMxN, subtrahend *MatMxN) *MatMxN {
	if mat == nil || subtrahend == nil || mat.m != subtrahend.m || mat.n != subtrahend.n {
		return nil
	}

	dst = dst.Reshape(mat.m, mat.n)

	// No need to care about rows and columns
	// since it's element-wise anyway
	for i, el := range mat.dat {
		dst.dat[i] = el - subtrahend.dat[i]
	}

	return dst
}
684344
func (ss StringSet) subtract(set StringSet) {
	for k := range set {
		delete(ss, k)
	}
}
1226460
func (t Term) Sub(args ...interface{}) Term {
	return constructMethodTerm(t, "Sub", p.Term_SUB, args, map[string]interface{}{})
}