Ejemplo n.º 1
0
// Map invokes the given function once for each element and returns a
// container containing the values returned by the given function.
func (set *Set) Map(f func(index int, value interface{}) interface{}) *Set {
	newSet := &Set{tree: rbt.NewWith(set.tree.Comparator)}
	iterator := set.Iterator()
	for iterator.Next() {
		newSet.Add(f(iterator.Index(), iterator.Value()))
	}
	return newSet
}
Ejemplo n.º 2
0
// Invokes the given function once for each element and returns a container
// containing the values returned by the given function as key/value pairs.
func (m *Map) Map(f func(key1 interface{}, value1 interface{}) (interface{}, interface{})) *Map {
	newMap := &Map{tree: rbt.NewWith(m.tree.Comparator)}
	iterator := m.Iterator()
	for iterator.Next() {
		key2, value2 := f(iterator.Key(), iterator.Value())
		newMap.Put(key2, value2)
	}
	return newMap
}
Ejemplo n.º 3
0
// Returns a new container containing all elements for which the given function returns a true value.
func (m *Map) Select(f func(key interface{}, value interface{}) bool) *Map {
	newMap := &Map{tree: rbt.NewWith(m.tree.Comparator)}
	iterator := m.Iterator()
	for iterator.Next() {
		if f(iterator.Key(), iterator.Value()) {
			newMap.Put(iterator.Key(), iterator.Value())
		}
	}
	return newMap
}
Ejemplo n.º 4
0
// CreateSourcePositionMapper returns a source position mapper for the contents of a source file.
func CreateSourcePositionMapper(contents []byte) SourcePositionMapper {
	lines := strings.Split(string(contents), "\n")
	rangeTree := redblacktree.NewWith(inclusiveComparator)

	var currentStart = 0
	for index, line := range lines {
		lineEnd := currentStart + len(line)
		rangeTree.Put(inclusiveRange{currentStart, lineEnd}, lineAndStart{index, currentStart})
		currentStart = lineEnd + 1
	}

	return SourcePositionMapper{rangeTree}
}
Ejemplo n.º 5
0
// Get returns the data associated with the given key and range. If the data for that range
// has not yet been calculated, the calculator method is called to do so lazily.
func (rmt *RangeMapTree) Get(key string, current IntRange) interface{} {
	rmt.globalLock.RLock()
	currentEntry, hasKey := rmt.rangeTreeMap[key]
	rmt.globalLock.RUnlock()

	if !hasKey {
		currentEntry = rangeTreeEntry{
			key:          key,
			internalTree: redblacktree.NewWith(rangeComparator),
			entryLock:    &sync.RWMutex{},
		}

		rmt.globalLock.Lock()
		rmt.rangeTreeMap[key] = currentEntry
		rmt.globalLock.Unlock()
	}

	return currentEntry.Get(current, rmt.calculator)
}
Ejemplo n.º 6
0
Archivo: treemap.go Proyecto: kebo/gods
// NewWith instantiates a tree map with the custom comparator.
func NewWith(comparator utils.Comparator) *Map {
	return &Map{tree: rbt.NewWith(comparator)}
}
Ejemplo n.º 7
0
// NewWith instantiates a new empty set with the custom comparator.
func NewWith(comparator utils.Comparator) *Set {
	return &Set{tree: rbt.NewWith(comparator)}
}