// 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 }
// 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 }
// 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 }
// 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} }
// 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) }
// NewWith instantiates a tree map with the custom comparator. func NewWith(comparator utils.Comparator) *Map { return &Map{tree: rbt.NewWith(comparator)} }
// NewWith instantiates a new empty set with the custom comparator. func NewWith(comparator utils.Comparator) *Set { return &Set{tree: rbt.NewWith(comparator)} }