Пример #1
0
// Set sets the map entry for key to val,
// and returns the previous entry, if any.
func (m *Map) Set(key types.Type, value interface{}) (prev interface{}) {
	if m.table != nil {
		hash := m.hasher.Hash(key)
		bucket := m.table[hash]
		var hole *entry
		for i, e := range bucket {
			if e.key == nil {
				hole = &bucket[i]
			} else if types.Identical(key, e.key) {
				prev = e.value
				bucket[i].value = value
				return
			}
		}

		if hole != nil {
			*hole = entry{key, value} // overwrite deleted entry
		} else {
			m.table[hash] = append(bucket, entry{key, value})
		}
	} else {
		if m.hasher.memo == nil {
			m.hasher = MakeHasher()
		}
		hash := m.hasher.Hash(key)
		m.table = map[uint32][]entry{hash: {entry{key, value}}}
	}

	m.length++
	return
}
Пример #2
0
func checkEqualButNotIdentical(t *testing.T, x, y types.Type, comment string) {
	if !types.Identical(x, y) {
		t.Errorf("%s: not equal: %s, %s", comment, x, y)
	}
	if x == y {
		t.Errorf("%s: identical: %v, %v", comment, x, y)
	}
}
Пример #3
0
// At returns the map entry for the given key.
// The result is nil if the entry is not present.
//
func (m *Map) At(key types.Type) interface{} {
	if m != nil && m.table != nil {
		for _, e := range m.table[m.hasher.Hash(key)] {
			if e.key != nil && types.Identical(key, e.key) {
				return e.value
			}
		}
	}
	return nil
}
Пример #4
0
// Delete removes the entry with the given key, if any.
// It returns true if the entry was found.
//
func (m *Map) Delete(key types.Type) bool {
	if m != nil && m.table != nil {
		hash := m.hasher.Hash(key)
		bucket := m.table[hash]
		for i, e := range bucket {
			if e.key != nil && types.Identical(key, e.key) {
				// We can't compact the bucket as it
				// would disturb iterators.
				bucket[i] = entry{}
				m.length--
				return true
			}
		}
	}
	return false
}
Пример #5
0
//ranges through config file and checks all expressions.
// prints result messages to stdout
func (c *checker) CheckAll() ([]CheckResult, error) {
	result := []CheckResult{}
	for _, section := range c.config.GetSections() {
		if section == "default" {
			continue
		}
		expr, err := c.config.GetString(section, "expr")
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			continue
		}
		fset := token.NewFileSet()
		fset.AddFile("", fset.Base(), len(expr)).SetLinesForContent([]byte(expr))
		tv, err := types.Eval(fset, c.pkg, 0, expr)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			fmt.Fprintln(os.Stderr, expr)
			continue
		}
		cr := &CheckResult{
			Name: section,
		}
		var m string
		if exact.BoolVal(tv.Value) {
			m, err = c.config.GetString(section, "true")
			if err != nil {
				continue
			}
		} else {
			m, err = c.config.GetString(section, "false")
			if err != nil {
				continue
			}
		}
		val, err := c.config.GetString(section, "val")
		if err == nil {
			fset := token.NewFileSet()
			fset.AddFile("", fset.Base(), len(val)).SetLinesForContent([]byte(val))
			tv, err := types.Eval(fset, c.pkg, 0, val)
			if err == nil {
				if types.Identical(tv.Type, types.Typ[types.UntypedFloat]) || types.Identical(tv.Type, types.Typ[types.Float64]) {
					x, _ := exact.Float64Val(tv.Value)
					cr.Value = x
				}
			}
		}
		tags, err := c.config.GetString(section, "tags")
		if err == nil {
			cr.Tags = tags
		} else {
			cr.Tags = "unknown"
		}

		fset = token.NewFileSet()
		fset.AddFile("", fset.Base(), len(m)).SetLinesForContent([]byte(m))
		tv, err = types.Eval(fset, c.pkg, 0, m)
		if err != nil {
			cr.Message = m
		} else {
			cr.Message = exact.StringVal(tv.Value)
		}
		result = append(result, *cr)
	}
	return result, nil
}