예제 #1
0
파일: enumerable.go 프로젝트: GoNuuts/gods
// 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
}
예제 #2
0
파일: treemap.go 프로젝트: junan/gods
// 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
}
예제 #3
0
파일: treemap.go 프로젝트: junan/gods
// 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
}
예제 #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}
}
예제 #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)
}
예제 #6
0
파일: treemap.go 프로젝트: kebo/gods
// NewWith instantiates a tree map with the custom comparator.
func NewWith(comparator utils.Comparator) *Map {
	return &Map{tree: rbt.NewWith(comparator)}
}
예제 #7
0
파일: treeset.go 프로젝트: GoNuuts/gods
// NewWith instantiates a new empty set with the custom comparator.
func NewWith(comparator utils.Comparator) *Set {
	return &Set{tree: rbt.NewWith(comparator)}
}