org.apache.commons.collections4.trie.PatriciaTrie Java Examples

The following examples show how to use org.apache.commons.collections4.trie.PatriciaTrie. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EnumClassMetadata.java    From intellij-spring-assistant with MIT License 6 votes vote down vote up
private void init(@NotNull PsiClassType type) {
  if (isValidType(type)) {
    PsiField[] fields = requireNonNull(toValidPsiClass(type)).getFields();
    List<PsiField> acceptableFields = new ArrayList<>();
    for (PsiField field : fields) {
      if (field != null && field.getType().equals(type)) {
        acceptableFields.add(field);
      }
    }
    if (acceptableFields.size() != 0) {
      childLookup = new THashMap<>();
      childrenTrie = new PatriciaTrie<>();
      acceptableFields.forEach(field -> {
        childLookup.put(sanitise(requireNonNull(field.getName())), field);
        childrenTrie.put(sanitise(field.getName()), field);
      });
    }
  } else {
    childLookup = null;
    childrenTrie = null;
  }
}
 
Example #2
Source File: BinList.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
private static Trie<String, BinRecord> createTrie() {
    Trie<String, BinRecord> trie = new PatriciaTrie<>();
    Iterable<CSVRecord> records;
    try (InputStream in = BinList.class.getResourceAsStream(BINLIST);
            BOMInputStream bomStream = new BOMInputStream(in);
            InputStreamReader inStream =
                    new InputStreamReader(bomStream, StandardCharsets.UTF_8)) {
        records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(inStream).getRecords();
    } catch (NullPointerException | IOException e) {
        LOGGER.warn("Exception while loading: " + BINLIST, e);
        return trie;
    }

    for (CSVRecord record : records) {
        trie.put(
                record.get("bin"),
                new BinRecord(
                        record.get("bin"),
                        record.get("brand"),
                        record.get("category"),
                        record.get("issuer")));
    }
    return trie;
}
 
Example #3
Source File: MetaAgeOffIterator.java    From timely with Apache License 2.0 6 votes vote down vote up
@Override
public void init(SortedKeyValueIterator<Key, Value> source, Map<String, String> options, IteratorEnvironment env)
        throws IOException {
    super.init(source, options, env);
    validateOptions(options);
    ageoffs = new PatriciaTrie<>();
    options.forEach((k, v) -> {
        if (k.startsWith(AGE_OFF_PREFIX)) {
            String name = k.substring(AGE_OFF_PREFIX.length());
            LOG.trace("Adding {} to Trie with value {}", name, Long.parseLong(v));
            long ageoff = Long.parseLong(v);
            this.minAgeOff = Math.min(this.minAgeOff, ageoff);
            this.maxAgeOff = Math.max(this.maxAgeOff, ageoff);
            ageoffs.put(name, ageoff);
        }
    });
    defaultAgeOff = ageoffs.get(DEFAULT_AGEOFF_KEY);
    currentTime = System.currentTimeMillis();
}
 
Example #4
Source File: MetricAgeOffFilter.java    From timely with Apache License 2.0 6 votes vote down vote up
@Override
public void init(SortedKeyValueIterator<Key, Value> source, Map<String, String> options, IteratorEnvironment env)
        throws IOException {
    super.init(source, options, env);
    validateOptions(options);
    ageoffs = new PatriciaTrie<>();
    options.forEach((k, v) -> {
        if (k.startsWith(AGE_OFF_PREFIX)) {
            String name = k.substring(AGE_OFF_PREFIX.length());
            LOG.trace("Adding {} to Trie with value", name, Long.parseLong(v));
            long ageoff = Long.parseLong(v);
            this.minAgeOff = Math.min(this.minAgeOff, ageoff);
            this.maxAgeOff = Math.max(this.maxAgeOff, ageoff);
            ageoffs.put(name, ageoff);
        }
    });
    defaultAgeOff = ageoffs.get(DEFAULT_AGEOFF_KEY);
    currentTime = System.currentTimeMillis();
}
 
Example #5
Source File: MetricAgeOffIterator.java    From timely with Apache License 2.0 6 votes vote down vote up
@Override
public void init(SortedKeyValueIterator<Key, Value> source, Map<String, String> options, IteratorEnvironment env)
        throws IOException {
    super.init(source, options, env);
    validateOptions(options);
    ageoffs = new PatriciaTrie<>();
    options.forEach((k, v) -> {
        if (k.startsWith(AGE_OFF_PREFIX)) {
            String name = k.substring(AGE_OFF_PREFIX.length());
            LOG.trace("Adding {} to Trie with value {}", name, Long.parseLong(v));
            long ageoff = Long.parseLong(v);
            this.minAgeOff = Math.min(this.minAgeOff, ageoff);
            this.maxAgeOff = Math.max(this.maxAgeOff, ageoff);
            ageoffs.put(name, ageoff);
        }
    });
    defaultAgeOff = ageoffs.get(DEFAULT_AGEOFF_KEY);
    currentTime = System.currentTimeMillis();
}
 
Example #6
Source File: DefaultStellarAutoCompleter.java    From metron with Apache License 2.0 5 votes vote down vote up
private PatriciaTrie<AutoCompleteType> initializeIndex() {
  Map<String, AutoCompleteType> index = new HashMap<>();
  index.put("==", AutoCompleteType.TOKEN);
  index.put(">=", AutoCompleteType.TOKEN);
  index.put("<=", AutoCompleteType.TOKEN);

  return new PatriciaTrie<>(index);
}
 
Example #7
Source File: COFD2.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private COFD2(Array<Entry> entries) {
  this.entries = entries;

  trie = new PatriciaTrie<>();
  for (Entry entry : entries) {
    trie.put(entry.cofName.toLowerCase(), entry.cof);
  }
}
 
Example #8
Source File: MetricCompactionStrategy.java    From timely with Apache License 2.0 5 votes vote down vote up
public static MetricAgeOffConfiguration newFromRequest(MajorCompactionRequest request,
        String majcIteratorName) {
    Configuration config = new MapConfiguration(request.getTableProperties());
    String majcIteratorKey = Property.TABLE_ITERATOR_MAJC_PREFIX.getKey() + majcIteratorName;

    if (LOG.isTraceEnabled()) {
        LOG.trace("Using key lookup for iterator: {}", majcIteratorKey);
    }

    Configuration configAgeOff = config.subset((majcIteratorKey + ".opt"));
    if (null == configAgeOff.getString(DEFAULT_AGEOFF_KEY)) {
        throw new IllegalArgumentException(
                DEFAULT_AGEOFF_KEY_SUFFIX + " must be configured for  " + majcIteratorKey);
    }

    PatriciaTrie<Long> ageoffs = new PatriciaTrie<>();
    long configureMinAgeOff = Long.MAX_VALUE;
    long configureMaxAgeOff = Long.MIN_VALUE;
    @SuppressWarnings("unchecked")
    Iterator<String> keys = configAgeOff.getKeys();
    while (keys.hasNext()) {
        String k = keys.next();
        String v = configAgeOff.getString(k);
        if (k.startsWith((AGE_OFF_PREFIX))) {
            String name = k.substring(AGE_OFF_PREFIX.length());
            if (LOG.isTraceEnabled()) {
                LOG.trace("Adding {} to Trie with value {}", name, Long.parseLong(v));
            }
            long ageoff = Long.parseLong(v);
            configureMinAgeOff = Math.min(configureMinAgeOff, ageoff);
            configureMaxAgeOff = Math.max(configureMaxAgeOff, ageoff);
            ageoffs.put(name, ageoff);
        }
    }
    long defaultAgeOff = ageoffs.get(DEFAULT_AGEOFF_KEY_SUFFIX);
    return new MetricAgeOffConfiguration(ageoffs, defaultAgeOff);
}
 
Example #9
Source File: OrgMinimalPrefixGenerator.java    From act with GNU General Public License v3.0 5 votes vote down vote up
public OrgMinimalPrefixGenerator(Iterator<Organism> orgIterator) {
  Map<String, Long> orgMap = new HashMap<>();

  while (orgIterator.hasNext()) {
    Organism org = orgIterator.next();
    orgMap.put(org.getName(), 1L);
  }

  PatriciaTrie orgPrefixTrie = new PatriciaTrie<>(orgMap);
  orgNameToMinimalPrefix = new HashMap<>();

  while (orgPrefixTrie.size() != 0) {
    String firstKey = (String) orgPrefixTrie.firstKey();
    orgNameToMinimalPrefix.put(firstKey, firstKey);
    orgPrefixTrie.remove(firstKey);

    SortedMap<String, Long> keyPrefixMap = orgPrefixTrie.prefixMap(firstKey);

    List<String> namesToRemove = new ArrayList<>();

    for (String orgWithPrefix : keyPrefixMap.keySet()) {
      orgNameToMinimalPrefix.put(orgWithPrefix, firstKey);
      namesToRemove.add(orgWithPrefix);
    }

    for (String nameToRemove : namesToRemove) {
      orgPrefixTrie.remove(nameToRemove);
    }
  }
}
 
Example #10
Source File: PatriciaTrieTest.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Fuzz
public void testCopy(Map<String, Integer> map, String key) {
    assumeTrue(map.containsKey(key));
    // Create new trie with input `map`
    Trie trie = new PatriciaTrie(map);
    // The key should exist in the trie as well
    assertTrue(trie.containsKey(key));
}
 
Example #11
Source File: PatriciaTrieTest.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Fuzz
public void testPrefixMap(HashMap<String, Integer> map, String prefix) {
    assumeTrue(prefix.length() > 0);
    // Create new trie with input `map`
    PatriciaTrie trie = new PatriciaTrie(map);
    // Get sub-map whose keys start with `prefix`
    Map prefixMap = trie.prefixMap(prefix);
    // Ensure that it contains all keys that start with `prefix`
    for (String key : map.keySet()) {
        if (key.startsWith(prefix)) {
            assertTrue(prefixMap.containsKey(key));
        }
    }
}
 
Example #12
Source File: SpringConfigurationMetadataHint.java    From intellij-spring-assistant with MIT License 5 votes vote down vote up
@Override
public void doOnGsonDeserialization() {
  if (hasPredefinedValues()) {
    valueLookup = new THashMap<>();
    valueTrie = new PatriciaTrie<>();
    for (SpringConfigurationMetadataHintValue value : requireNonNull(values)) {
      // The default value can be array (if property is of type array) as per documentation, we dont support those usecases as of now
      if (value.representsSingleValue()) {
        String suggestion = value.toString();
        valueLookup.put(sanitise(suggestion), value);
        valueTrie.put(sanitise(suggestion), value);
      }
    }
  }
}
 
Example #13
Source File: SuggestionServiceImpl.java    From intellij-spring-assistant with MIT License 5 votes vote down vote up
private void reindexModule(List<MetadataContainerInfo> newProjectSourcesToProcess,
    List<MetadataContainerInfo> projectContainersToRemove, Module module) {
  Map<String, MetadataContainerInfo> moduleSeenContainerPathToSeenContainerInfo =
      moduleNameToSeenContainerPathToContainerInfo
          .computeIfAbsent(module.getName(), k -> new THashMap<>());

  Trie<String, MetadataSuggestionNode> moduleRootSearchIndex =
      moduleNameToRootSearchIndex.get(module.getName());
  if (moduleRootSearchIndex == null) {
    moduleRootSearchIndex = new PatriciaTrie<>();
    moduleNameToRootSearchIndex.put(module.getName(), moduleRootSearchIndex);
  }

  OrderEnumerator moduleOrderEnumerator = OrderEnumerator.orderEntries(module);

  List<MetadataContainerInfo> newModuleContainersToProcess =
      computeNewContainersToProcess(moduleOrderEnumerator,
          moduleSeenContainerPathToSeenContainerInfo);
  newModuleContainersToProcess.addAll(newProjectSourcesToProcess);

  List<MetadataContainerInfo> moduleContainersToRemove =
      computeContainersToRemove(moduleOrderEnumerator,
          moduleSeenContainerPathToSeenContainerInfo);
  moduleContainersToRemove.addAll(projectContainersToRemove);

  processContainers(module, newModuleContainersToProcess, moduleContainersToRemove,
      moduleSeenContainerPathToSeenContainerInfo, moduleRootSearchIndex);
}
 
Example #14
Source File: GenericClassMetadata.java    From intellij-spring-assistant with MIT License 5 votes vote down vote up
private void init(@NotNull PsiClassType type) {
  if (isValidType(type)) {
    childLookup = getSanitisedPropertyToPsiMemberWrapper(toValidPsiClass(type));
    if (childLookup != null) {
      childrenTrie = new PatriciaTrie<>();
      childLookup.forEach((k, v) -> childrenTrie.put(k, v));
    }
  } else {
    childLookup = null;
    childrenTrie = null;
  }
}
 
Example #15
Source File: MetricCompactionStrategy.java    From timely with Apache License 2.0 4 votes vote down vote up
private MetricAgeOffConfiguration(PatriciaTrie<Long> ageoffs, long defaultAgeOff) {
    this.ageoffs = ageoffs;
    this.defaultAgeOff = defaultAgeOff;
}
 
Example #16
Source File: MetricCompactionStrategy.java    From timely with Apache License 2.0 4 votes vote down vote up
public static MetricAgeOffConfiguration newFromDefaultingMinimum(long minAgeOff) {
    return new MetricAgeOffConfiguration(new PatriciaTrie<>(), minAgeOff);
}
 
Example #17
Source File: MPQViewer.java    From riiablo with Apache License 2.0 4 votes vote down vote up
private void readMPQs() {
  if (fileTreeNodes == null) {
    fileTreeNodes = new PatriciaTrie<>();
    fileTreeCofNodes = new PatriciaTrie<>();
  } else {
    fileTreeNodes.clear();
    fileTreeCofNodes.clear();
  }

  BufferedReader reader = null;
  try {
    //if (options_useExternalList.isChecked()) {
      reader = Gdx.files.internal(ASSETS + "(listfile)").reader(4096);
    //} else {
    //  try {
    //    reader = new BufferedReader(new InputStreamReader((new ByteArrayInputStream(mpq.readBytes("(listfile)")))));
    //  } catch (Throwable t) {
    //    reader = Gdx.files.internal(ASSETS + "(listfile)").reader(4096);
    //  }
    //}

    Node<Node, Object, Actor> root = new BaseNode(new VisLabel("root"));
    final boolean checkExisting = options_checkExisting.isChecked();

    String fileName;
    while ((fileName = reader.readLine()) != null) {
      if (checkExisting && !Riiablo.mpqs.contains(fileName)) {
        continue;
      }

      String path = FilenameUtils.getPathNoEndSeparator(fileName).toLowerCase();
      treeify(fileTreeNodes, root, path);

      final MPQFileHandle handle = (MPQFileHandle) Riiablo.mpqs.resolve(fileName);
      VisLabel label = new VisLabel(FilenameUtils.getName(fileName));
      final Node node = new BaseNode(label);
      node.setValue(handle);
      label.addListener(new ClickListener(Input.Buttons.RIGHT) {
        @Override
        public void clicked(InputEvent event, float x, float y) {
          showPopmenu(node, handle);
        }
      });

      String key = fileName.toLowerCase();
      fileTreeNodes.put(key, node);
      if (FilenameUtils.isExtension(key, "cof")) {
        key = FilenameUtils.getBaseName(key);
        fileTreeCofNodes.put(key, node);
      }
      if (path.isEmpty()) {
        root.add(node);
      } else {
        fileTreeNodes.get(path + "\\").add(node);
      }
    }

    sort(root);
    fileTree.clearChildren();
    for (Node child : root.getChildren()) {
      fileTree.add(child);
    }

    fileTree.layout();
    fileTreeFilter.clearText();
  } catch (IOException e) {
    throw new GdxRuntimeException("Failed to read list file.", e);
  } finally {
    StreamUtils.closeQuietly(reader);
  }
}
 
Example #18
Source File: BooleanClassMetadata.java    From intellij-spring-assistant with MIT License 4 votes vote down vote up
@Override
protected void init(Module module) {
  childrenTrie = new PatriciaTrie<>();
  childrenTrie.put("true", TRUE);
  childrenTrie.put("false", FALSE);
}