Java Code Examples for it.unimi.dsi.fastutil.ints.Int2ObjectMap#put()

The following examples show how to use it.unimi.dsi.fastutil.ints.Int2ObjectMap#put() . 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: DimensionalConfigurationSchema.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
protected void buildNonCompositeAggregatorIDMap(String aggregatorName, FieldsDescriptor inputDescriptor,
    IntArrayList aggIDList, Int2ObjectMap<FieldsDescriptor> inputMap, Int2ObjectMap<FieldsDescriptor> outputMap)
{
  IncrementalAggregator incrementalAggregator = aggregatorRegistry.getNameToIncrementalAggregator().get(
      aggregatorName);
  //don't need to build OTF aggregate
  if (incrementalAggregator == null) {
    return;
  }
  int aggregatorID = aggregatorRegistry.getIncrementalAggregatorNameToID().get(aggregatorName);
  mergeAggregatorID(aggIDList, aggregatorID);
  inputMap.put(aggregatorID, inputDescriptor);
  outputMap.put(aggregatorID,
      AggregatorUtils.getOutputFieldsDescriptor(inputDescriptor,
      incrementalAggregator));
}
 
Example 2
Source File: SlimUDTF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static Int2ObjectMap<Int2FloatMap> kNNentries(@Nonnull final Object kNNiObj,
        @Nonnull final MapObjectInspector knnItemsOI,
        @Nonnull final PrimitiveObjectInspector knnItemsKeyOI,
        @Nonnull final MapObjectInspector knnItemsValueOI,
        @Nonnull final PrimitiveObjectInspector knnItemsValueKeyOI,
        @Nonnull final PrimitiveObjectInspector knnItemsValueValueOI,
        @Nullable Int2ObjectMap<Int2FloatMap> knnItems, @Nonnull final MutableInt nnzKNNi) {
    if (knnItems == null) {
        knnItems = new Int2ObjectOpenHashMap<>(1024);
    } else {
        knnItems.clear();
    }

    int numElementOfKNNItems = 0;
    for (Map.Entry<?, ?> entry : knnItemsOI.getMap(kNNiObj).entrySet()) {
        int user = PrimitiveObjectInspectorUtils.getInt(entry.getKey(), knnItemsKeyOI);
        Int2FloatMap ru = int2floatMap(knnItemsValueOI.getMap(entry.getValue()),
            knnItemsValueKeyOI, knnItemsValueValueOI);
        knnItems.put(user, ru);
        numElementOfKNNItems += ru.size();
    }

    nnzKNNi.setValue(numElementOfKNNItems);
    return knnItems;
}
 
Example 3
Source File: SlimUDTF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
private void replayTrain(@Nonnull final ByteBuffer buf) {
    final int itemI = buf.getInt();
    final int knnSize = buf.getInt();

    final Int2ObjectMap<Int2FloatMap> knnItems = new Int2ObjectOpenHashMap<>(1024);
    final IntSet pairItems = new IntOpenHashSet();
    for (int i = 0; i < knnSize; i++) {
        int user = buf.getInt();
        int ruSize = buf.getInt();
        Int2FloatMap ru = new Int2FloatOpenHashMap(ruSize);
        ru.defaultReturnValue(0.f);

        for (int j = 0; j < ruSize; j++) {
            int itemK = buf.getInt();
            pairItems.add(itemK);
            float ruk = buf.getFloat();
            ru.put(itemK, ruk);
        }
        knnItems.put(user, ru);
    }

    for (int itemJ : pairItems) {
        train(itemI, knnItems, itemJ);
    }
}
 
Example 4
Source File: AgreeSetGenerator.java    From metanome-algorithms with Apache License 2.0 6 votes vote down vote up
private void calculateRelationship(StrippedPartition partitions, Int2ObjectMap<TupleEquivalenceClassRelation> relationships) {

        int partitionNr = 0;
        for (IntList partition : partitions.getValues()) {
            if (this.debugSysout)
                System.out.println(".");
            for (int index : partition) {
                if (!relationships.containsKey(index)) {
                    relationships.put(index, new TupleEquivalenceClassRelation());
                }
                relationships.get(index).addNewRelationship(partitions.getAttributeID(), partitionNr);
            }
            partitionNr++;
        }

    }
 
Example 5
Source File: FixedLifespanScheduler.java    From presto with Apache License 2.0 6 votes vote down vote up
public FixedLifespanScheduler(BucketNodeMap bucketNodeMap, List<ConnectorPartitionHandle> partitionHandles, OptionalInt concurrentLifespansPerTask)
{
    checkArgument(!partitionHandles.equals(ImmutableList.of(NOT_PARTITIONED)));
    checkArgument(partitionHandles.size() == bucketNodeMap.getBucketCount());

    Map<InternalNode, IntList> nodeToDriverGroupMap = new HashMap<>();
    Int2ObjectMap<InternalNode> driverGroupToNodeMap = new Int2ObjectOpenHashMap<>();
    for (int bucket = 0; bucket < bucketNodeMap.getBucketCount(); bucket++) {
        InternalNode node = bucketNodeMap.getAssignedNode(bucket).get();
        nodeToDriverGroupMap.computeIfAbsent(node, key -> new IntArrayList()).add(bucket);
        driverGroupToNodeMap.put(bucket, node);
    }

    this.driverGroupToNodeMap = driverGroupToNodeMap;
    this.nodeToDriverGroupsMap = nodeToDriverGroupMap.entrySet().stream()
            .collect(toImmutableMap(Map.Entry::getKey, entry -> entry.getValue().iterator()));
    this.partitionHandles = requireNonNull(partitionHandles, "partitionHandles is null");
    if (concurrentLifespansPerTask.isPresent()) {
        checkArgument(concurrentLifespansPerTask.getAsInt() >= 1, "concurrentLifespansPerTask must be great or equal to 1 if present");
    }
    this.concurrentLifespansPerTask = requireNonNull(concurrentLifespansPerTask, "concurrentLifespansPerTask is null");
}
 
Example 6
Source File: WIDToCategoryMap.java    From tagme with Apache License 2.0 5 votes vote down vote up
@Override
protected Int2ObjectMap<String> parseSet() throws IOException {
	final Object2IntMap<String> categories=DatasetLoader.get(new CategoriesToWIDMap(lang));
	final Int2ObjectMap<String> map=new Int2ObjectOpenHashMap<String>();
	
	for(String s:categories.keySet()){
		map.put(categories.get(s),s);
	}
	
	return map;
}
 
Example 7
Source File: LouvainTest.java    From Neo4jSNA with Apache License 2.0 5 votes vote down vote up
@Override
    protected void initGraph() {
        Int2ObjectMap<Node> nodes = new Int2ObjectOpenHashMap<>();

        for (int i = 0; i < 9; i++) {
            Node n = db.createNode();
            n.setProperty("id", i);
            nodes.put(i, n);
        }

        for (int i = 0; i < 9; i++) {
            Node src = nodes.get(i);
            Node dst = (i + 1) % 3 != 0 ? nodes.get(i + 1) : nodes.get(i - 2);

            src.createRelationshipTo(dst, CommonsRelationshipTypes.KNOWS);
//            dst.createRelationshipTo(src, CommonsRelationshipTypes.KNOWS);
        }

        nodes.get(0).createRelationshipTo(nodes.get(3), CommonsRelationshipTypes.KNOWS);
        nodes.get(3).createRelationshipTo(nodes.get(6), CommonsRelationshipTypes.KNOWS);
        nodes.get(6).createRelationshipTo(nodes.get(0), CommonsRelationshipTypes.KNOWS);

//        for (int i = 0; i < 9; i += 3) {
//            Node src = nodes.get(i);
//            Node dst1 = nodes.get((i + 3) % 9);
//            Node dst2 = nodes.get((i + 6) % 9);
//            src.createRelationshipTo(dst1, CommonsRelationshipTypes.KNOWS);
// //            dst1.createRelationshipTo(src, CommonsRelationshipTypes.KNOWS);
//            src.createRelationshipTo(dst2, CommonsRelationshipTypes.KNOWS);
// //            dst2.createRelationshipTo(src, CommonsRelationshipTypes.KNOWS);
//        }

    }
 
Example 8
Source File: ProtocolRegistry.java    From ViaVersion with MIT License 5 votes vote down vote up
/**
 * Register a protocol
 *
 * @param protocol  The protocol to register.
 * @param supported Supported client versions.
 * @param output    The output server version it converts to.
 */
public static void registerProtocol(Protocol protocol, List<Integer> supported, int output) {
    // Clear cache as this may make new routes.
    if (!pathCache.isEmpty()) {
        pathCache.clear();
    }

    protocols.put(protocol.getClass(), protocol);

    for (int version : supported) {
        Int2ObjectMap<Protocol> protocolMap = registryMap.computeIfAbsent(version, s -> new Int2ObjectOpenHashMap<>(2));
        protocolMap.put(output, protocol);
    }

    if (Via.getPlatform().isPluginEnabled()) {
        protocol.register(Via.getManager().getProviders());
        refreshVersions();
    } else {
        registerList.add(protocol);
    }

    if (protocol.hasMappingDataToLoad()) {
        if (mappingLoaderExecutor != null) {
            // Submit mapping data loading
            addMappingLoaderFuture(protocol.getClass(), protocol::loadMappingData);
        } else {
            // Late protocol adding - just do it on the current thread
            protocol.loadMappingData();
        }
    }
}
 
Example 9
Source File: PageToCategoryIDs.java    From tagme with Apache License 2.0 5 votes vote down vote up
@Override
protected int[][] parseSet() throws IOException {
	final Int2ObjectMap<IntSet> map = new Int2ObjectOpenHashMap<IntSet>(3000000);
	final IntSet hidden= DatasetLoader.get(new HiddenCategoriesWIDs(lang));
	File input = WikipediaFiles.CAT_LINKS.getSourceFile(lang);
	final Object2IntMap<String> categories=DatasetLoader.get(new CategoriesToWIDMap(lang));
	
	SQLWikiParser parser = new SQLWikiParser(log) {
		@Override
		public boolean compute(ArrayList<String> values) throws IOException {
			String c_title=cleanPageName(values.get(SQLWikiParser.CATLINKS_TITLE_TO));
			int id=Integer.parseInt(values.get(SQLWikiParser.CATLINKS_ID_FROM));
			if(categories.containsKey(c_title) && !hidden.contains(categories.get(c_title).intValue())){
				if(map.containsKey(id)){
					map.get(id).add(categories.get(c_title).intValue());
				}else{
					IntSet set = new IntOpenHashSet();
					set.add(categories.get(c_title).intValue());
					map.put(id, set);
				}
				return true;
			} else return false;
		}
		
	};
	InputStreamReader reader = new InputStreamReader(new FileInputStream(input), Charset.forName("UTF-8"));
	parser.compute(reader);
	reader.close();
	return createDump(map);
}
 
Example 10
Source File: AgreeSetGenerator.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
private void handlePartition(IntList actuelList, int position, Int2ObjectMap<IntSet> index, Set<IntList> max) {

        if (!this.isSubset(actuelList, index)) {
            max.add(actuelList);
            for (int e : actuelList) {
                if (!index.containsKey(e)) {
                    index.put(e, new IntArraySet());
                }
                index.get(e).add(position);
            }
        }
    }
 
Example 11
Source File: CollectionUtilsTest.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
@Test
public void forEachInt2ObjectMap() {
	Int2ObjectMap<String> map = new Int2ObjectOpenHashMap<>();
	Int2ObjectMap<String> target = new Int2ObjectOpenHashMap<>();
	map.put(1, "foo");
	map.put(2, "bar");
	forEach(map, target::put);
	assertThat(target).hasSize(2);
	assertThat(target).containsEntry(1, "foo");
	assertThat(target).containsEntry(2, "bar");
}
 
Example 12
Source File: EnumUtils.java    From fastjgame with Apache License 2.0 5 votes vote down vote up
/**
 * 根据枚举的values建立索引;
 *
 * @param values    枚举数组
 * @param fastQuery 是否追求极致的查询性能
 * @param <T>       枚举类型
 * @return unmodifiable
 */
public static <T extends IndexableEnum> IndexableEnumMapper<T> mapping(final T[] values, final boolean fastQuery) {
    if (values.length == 0) {
        @SuppressWarnings("unchecked") final IndexableEnumMapper<T> mapper = (IndexableEnumMapper<T>) EmptyMapper.INSTANCE;
        return mapper;
    }

    // 结果不一定用得上,存在一定的浪费,但必须检测重复
    final Int2ObjectMap<T> result = new Int2ObjectOpenHashMap<>(values.length);
    for (T t : values) {
        if (result.containsKey(t.getNumber())) {
            throw new IllegalArgumentException(t.getClass().getSimpleName() + " number:" + t.getNumber() + " is duplicate");
        }
        result.put(t.getNumber(), t);
    }

    final int minNumber = Arrays.stream(values)
            .mapToInt(IndexableEnum::getNumber)
            .min()
            .getAsInt();

    final int maxNumber = Arrays.stream(values)
            .mapToInt(IndexableEnum::getNumber)
            .max()
            .getAsInt();

    // 保护性拷贝,避免出现并发问题 - 不确定values()是否会被修改
    final T[] copiedValues = Arrays.copyOf(values, values.length);
    if (isArrayAvailable(minNumber, maxNumber, values.length, fastQuery)) {
        return new ArrayBasedMapper<>(copiedValues, minNumber, maxNumber);
    } else {
        return new MapBasedMapper<>(copiedValues, result);
    }
}
 
Example 13
Source File: AgreeSetGenerator.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
public Set<IntList> computeMaximumSetsAlternative(List<StrippedPartition> partitions) {

        if (this.debugSysout) {
            System.out.println("\tstartet calculation of maximal partitions");
        }
        long start = System.currentTimeMillis();

        Set<IntList> sortedPartitions = new TreeSet<IntList>(new ListComparator());
        for (StrippedPartition p : partitions) {
            sortedPartitions.addAll(p.getValues());
        }
        Iterator<IntList> it = sortedPartitions.iterator();
        Int2ObjectMap<Set<IntList>> maxSets = new Int2ObjectOpenHashMap<Set<IntList>>();
        long remainingPartitions = sortedPartitions.size();
        if (this.debugSysout) {
            System.out.println("\tNumber of Partitions: " + remainingPartitions);
        }
        if (it.hasNext()) {
            IntList actuelList = it.next();
            int minSize = actuelList.size();
            Set<IntList> set = new HashSet<IntList>();
            set.add(actuelList);
            while ((actuelList = it.next()) != null && (actuelList.size() == minSize)) {
                if (this.debugSysout) {
                    System.out.println("\tremaining: " + --remainingPartitions);
                }
                set.add(actuelList);
            }
            maxSets.put(minSize, set);
            if (actuelList != null) {
                maxSets.put(actuelList.size(), new HashSet<IntList>());
                if (this.debugSysout) {
                    System.out.println("\tremaining: " + --remainingPartitions);
                }
                this.handleList(actuelList, maxSets, true);
                while (it.hasNext()) {
                    actuelList = it.next();
                    if (this.debugSysout) {
                        System.out.println("\tremaining: " + --remainingPartitions);
                    }
                    if (!maxSets.containsKey(actuelList.size()))
                        maxSets.put(actuelList.size(), new HashSet<IntList>());
                    this.handleList(actuelList, maxSets, true);
                }
            }
        }

        long end = System.currentTimeMillis();
        if (this.debugSysout)
            System.out.println("\tTime needed: " + (end - start));

        Set<IntList> max = this.mergeResult(maxSets);
        maxSets.clear();
        sortedPartitions.clear();

        return max;
    }
 
Example 14
Source File: LeftHandSideGenerator.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
/**
   * Computes the LHS
   *
   * @param maximalSets    The set of the complements of maximal sets (see Phase 2 for further information)
   * @param nrOfAttributes The number attributes in the whole relation
   * @return {@code Int2ObjectMap<List<BitSet>>} (key: dependent attribute, value: set of all lefthand sides)
   */
  public Int2ObjectMap<List<BitSet>> execute(List<CMAX_SET> maximalSets, int nrOfAttributes) {

      if (this.timeMesurement) {
          this.startTime();
      }

      Int2ObjectMap<List<BitSet>> lhs = new Int2ObjectOpenHashMap<List<BitSet>>();

/* 1: for all attributes A in R do */
      for (int attribute = 0; attribute < nrOfAttributes; attribute++) {

	/* 2: i:=1 */
          // int i = 1;

	/* 3: Li:={B | B in X, X in cmax(dep(r),A)} */
          Set<BitSet> Li = new HashSet<BitSet>();
          CMAX_SET correctSet = this.generateFirstLevelAndFindCorrectSet(maximalSets, attribute, Li);

          List<List<BitSet>> lhs_a = new LinkedList<List<BitSet>>();

	/* 4: while Li != ø do */
          while (!Li.isEmpty()) {

		/*
               * 5: LHSi[A]:={l in Li | l intersect X != ø, for all X in cmax(dep(r),A)}
		 */
              List<BitSet> lhs_i = findLHS(Li, correctSet);

		/* 6: Li:=Li/LHSi[A] */
              Li.removeAll(lhs_i);

		/*
               * 7: Li+1:={l' | |l'|=i+1 and for all l subset l' | |l|=i, l in Li}
		 */
              /*
		 * The generation of the next level is, as mentioned in the paper, done with the Apriori gen-function from the
		 * following paper: "Fast algorithms for mining association rules in large databases." - Rakesh Agrawal,
		 * Ramakrishnan Srikant
		 */
              Li = this.generateNextLevel(Li);

		/* 8: i:=i+1 */
              // i++;
              lhs_a.add(lhs_i);
          }

	/* 9: lhs(dep(r),A):= union LHSi[A] */
          if (!lhs.containsKey(attribute)) {
              lhs.put(attribute, new LinkedList<BitSet>());
          }
          for (List<BitSet> lhs_ia : lhs_a) {
              lhs.get(attribute).addAll(lhs_ia);
          }
      }

      if (this.timeMesurement) {
          this.stopTime();
      }

      return lhs;
  }
 
Example 15
Source File: OpenNlpNerRecommender.java    From inception with Apache License 2.0 4 votes vote down vote up
private Span[] extractAnnotatedSpans(CAS aCas, AnnotationFS aSentence,
                                     Collection<AnnotationFS> aTokens) {
    // Convert character offsets to token indices
    Int2ObjectMap<AnnotationFS> idxTokenOffset = new Int2ObjectOpenHashMap<>();
    Object2IntMap<AnnotationFS> idxToken = new Object2IntOpenHashMap<>();
    int idx = 0;
    for (AnnotationFS t : aTokens) {
        idxTokenOffset.put(t.getBegin(), t);
        idxTokenOffset.put(t.getEnd(), t);
        idxToken.put(t, idx);
        idx++;
    }

    // Create spans from target annotations
    Type annotationType = getType(aCas, layerName);
    Feature feature = annotationType.getFeatureByBaseName(featureName);
    List<AnnotationFS> annotations = selectCovered(annotationType, aSentence);
    int numberOfAnnotations = annotations.size();
    List<Span> result = new ArrayList<>();

    int highestEndTokenPositionObserved = 0;
    for (int i = 0; i < numberOfAnnotations; i++) {
        AnnotationFS annotation = annotations.get(i);
        String label = annotation.getFeatureValueAsString(feature);
        
        AnnotationFS beginToken = idxTokenOffset.get(annotation.getBegin());
        AnnotationFS endToken = idxTokenOffset.get(annotation.getEnd());
        if (beginToken == null || endToken == null) {
            LOG.warn("Skipping annotation not starting/ending at token boundaries: [{}-{}, {}]",
                    annotation.getBegin(), annotation.getEnd(), label);
            continue;
        }
        
        int begin = idxToken.get(beginToken);
        int end = idxToken.get(endToken);
        
        // If the begin offset of the current annotation is lower than the highest offset so far
        // observed, then it is overlapping with some annotation that we have seen before. 
        // Because OpenNLP NER does not support overlapping annotations, we skip it.
        if (begin < highestEndTokenPositionObserved) {
            LOG.debug("Skipping overlapping annotation: [{}-{}, {}]", begin, end + 1, label);
            continue;
        }
        
        if (isNotBlank(label)) {
            result.add(new Span(begin, end + 1, label));
            highestEndTokenPositionObserved = end + 1;
        }
    }
    return result.toArray(new Span[result.size()]);
}
 
Example 16
Source File: MultiSegmentReaderAccessibleInfoProvider.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
/**
 * Note that some readers might still access the oldest segment for a while -- we do NOT interrupt
 * readers accessing the old segment. The expected behavior is that after a while though, none of
 * them will reference it anymore (once their computation finishes) and the JVM can then
 * garbage-collect it. This lagging reading behavior is what makes it hard to explicitly recycle
 * the already allocated memory so we need memory for k+1 segments (where k is the number of
 * segments we'll maintain).
 *
 * @param numEdgesInLiveSegment          is the number of edges in the current live segment
 * @param numEdgesInNonLiveSegmentsMap   contains a map from segment id to number of edges in it
 * @param statsReceiver                  is where the stats are updated
 * @param bipartiteGraphSegmentProvider  provides the new segment to be added
 * @return the live segment that was added
 */
public T addNewSegment(
    int numEdgesInLiveSegment,
    Int2IntMap numEdgesInNonLiveSegmentsMap,
    StatsReceiver statsReceiver,
    BipartiteGraphSegmentProvider<T> bipartiteGraphSegmentProvider
) {
  final Int2ObjectMap<T> segments =
      new Int2ObjectOpenHashMap<T>(multiSegmentReaderAccessibleInfo.getSegments());
  numEdgesInNonLiveSegmentsMap.put(liveSegmentId, numEdgesInLiveSegment);
  int oldestSegmentId = multiSegmentReaderAccessibleInfo.oldestSegmentId;
  // remove a segment if we're at the limit
  if (multiSegmentReaderAccessibleInfo.getSegments().size() == maxNumSegments) {
    segments.remove(oldestSegmentId);
    numEdgesInNonLiveSegmentsMap.remove(oldestSegmentId);
    LOG.info("Removed segment " + oldestSegmentId);
    oldestSegmentId++;
  } else {
    statsReceiver.counter("numSegments").incr();
  }
  int newLiveSegmentId =  multiSegmentReaderAccessibleInfo.liveSegmentId + 1;
  // add a new segment
  T liveSegment =
      bipartiteGraphSegmentProvider.generateNewSegment(newLiveSegmentId, maxNumEdgesPerSegment);
  segments.put(newLiveSegmentId, liveSegment);
  // now make the switch for the readers -- this is immediately published and visible!
  multiSegmentReaderAccessibleInfo = new MultiSegmentReaderAccessibleInfo<T>(
          segments, oldestSegmentId, newLiveSegmentId);

  // flush the write
  liveSegmentId = newLiveSegmentId;

  numEdgesInNonLiveSegments = 0;
  for (int segmentEdgeCount : numEdgesInNonLiveSegmentsMap.values()) {
    numEdgesInNonLiveSegments += segmentEdgeCount;
  }
  LOG.info("Total number of edges in graph = " + numEdgesInNonLiveSegments);
  LOG.info("Created a new segment: oldestSegmentId = " + oldestSegmentId
      + ", and liveSegmentId = " + liveSegmentId);

  return liveSegment;
}
 
Example 17
Source File: TopSecondDegreeByCountTweetMetadataRecsGenerator.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
/**
 * Return tweet metadata recommendations, like hashtags and urls.
 *
 * @param request            topSecondDegreeByCount request
 * @param nodeInfoList       a list of node info containing engagement social proof and weights
 * @param recommendationType the recommendation type to return, like hashtag and url
 * @return a list of recommendations of the recommendation type
 */
public static List<RecommendationInfo> generateTweetMetadataRecs(
  TopSecondDegreeByCountRequestForTweet request,
  List<NodeInfo> nodeInfoList,
  RecommendationType recommendationType
) {
  Int2ObjectMap<TweetMetadataRecommendationInfo> visitedMetadata = null;
  List<RecommendationInfo> results = new ArrayList<>();

  for (NodeInfo nodeInfo : nodeInfoList) {
    // Remove unfavorited edges, and discard the nodeInfo if it no longer has social proofs
    boolean isNodeModified = NodeInfoHelper.removeUnfavoritedSocialProofs(nodeInfo);
    if (isNodeModified && !NodeInfoHelper.nodeInfoHasValidSocialProofs(nodeInfo)) {
      continue;
    }

    int[] metadata = nodeInfo.getNodeMetadata(recommendationType.getValue());
    if (metadata == null) {
      continue;
    }

    if (visitedMetadata == null) {
      visitedMetadata = new Int2ObjectOpenHashMap<>();
    }
    for (int j = 0; j < metadata.length; j++) {
      TweetMetadataRecommendationInfo recommendationInfo =
          visitedMetadata.get(metadata[j]);

      if (recommendationInfo == null) {
        recommendationInfo = new TweetMetadataRecommendationInfo(
            metadata[j],
            RecommendationType.at(recommendationType.getValue()),
            0,
            new HashMap<Byte, Map<Long, LongList>>()
        );
      }
      recommendationInfo.addToWeight(nodeInfo.getWeight());
      addToSocialProof(
          nodeInfo,
          recommendationInfo,
          request.getMaxUserSocialProofSize(),
          request.getMaxTweetSocialProofSize()
      );

      visitedMetadata.put(metadata[j], recommendationInfo);
    }
  }

  if (visitedMetadata != null) {
    int maxNumResults = GeneratorHelper.getMaxNumResults(request, recommendationType);
    int minUserSocialProofSize = GeneratorHelper.getMinUserSocialProofSize(request, recommendationType);

    List<TweetMetadataRecommendationInfo> filtered = null;

    for (Int2ObjectMap.Entry<TweetMetadataRecommendationInfo> entry
      : visitedMetadata.int2ObjectEntrySet()) {
      // handling one specific rule related to metadata recommendations.
      if (isLessThanMinUserSocialProofSize(
        entry.getValue().getSocialProof(),
        minUserSocialProofSize)) {
        continue;
      }

      if (filtered == null) {
        filtered = new ArrayList<>();
      }
      filtered.add(entry.getValue());
    }

    if (filtered != null) {
      // sort the list of TweetMetadataRecommendationInfo in ascending order
      // according to their weights
      Collections.sort(filtered);
      int toIndex = Math.min(maxNumResults, filtered.size());
      for (int j = 0; j < toIndex; j++) {
        results.add(filtered.get(j));
      }
    }
  }

  return results;
}
 
Example 18
Source File: BinaryProtoCodecTest.java    From fastjgame with Apache License 2.0 4 votes vote down vote up
/**
 * 一个正常赋值了的对象
 */
public static ExampleMessages.FullMessage newFullMessage() {
    ExampleMessages.FullMessage fullMessage = new ExampleMessages.FullMessage();
    fullMessage.setAny("any");
    fullMessage.setaByte((byte) 25);
    fullMessage.setaChar('a');
    fullMessage.setaShort((short) 3222);
    fullMessage.setAnInt(6555895);
    fullMessage.setaLong(54654874561L);
    fullMessage.setaFloat(1.1f);
    fullMessage.setaDouble(2.0);
    fullMessage.setaBoolean(true);

    ExampleMessages.Hello hello = new ExampleMessages.Hello(65, "hello world.");
    fullMessage.setHello(hello);

    fullMessage.setaString("wjybxx");
    fullMessage.setProfession(Profession.CODER);

    fullMessage.setStringList(new ArrayList<>(Arrays.asList("张三", "李四", "王五")));
    fullMessage.setStringSet(new LinkedHashSet<>(Arrays.asList("zhangsan", "li", "wangwu")));

    Map<String, String> params = new LinkedHashMap<>();
    params.put("first", "abc");
    params.put("second", "def");
    fullMessage.setStringStringMap(params);

    fullMessage.setaByteArray(new byte[]{Byte.MIN_VALUE, 1, Byte.MAX_VALUE});
    fullMessage.setaShortArray(new short[]{Short.MIN_VALUE, 2, Short.MAX_VALUE});
    fullMessage.setaIntArray(new int[]{Integer.MIN_VALUE, 3, Integer.MAX_VALUE});
    fullMessage.setaLongArrray(new long[]{Long.MIN_VALUE, 4, Long.MAX_VALUE});
    fullMessage.setaFloatArray(new float[]{-5.5f, 0.1f, 5.5f});
    fullMessage.setaDoubleArray(new double[]{-6.6, 0.1f, 6.6});

    fullMessage.setaCharArray("hello world".toCharArray());
    fullMessage.setaStringArray(new String[]{"zhang", "wang", "zhao"});
    fullMessage.setaClassArray(new Class[]{Object.class, LinkedHashMap.class, Set.class});

    fullMessage.setTwoDimensionsStringArray(new String[][]{
            {"1,0", "1,1", "1,2"},
            {"2.0", "2,2", "2,2"}
    });

    final Int2ObjectMap<String> int2ObjectMap = new Int2ObjectOpenHashMap<>(3);
    int2ObjectMap.put(1, "a");
    int2ObjectMap.put(2, "b");
    int2ObjectMap.put(3, "c");
    fullMessage.setInt2ObjectMap(int2ObjectMap);

    fullMessage.setProfessionEnumSet(EnumSet.of(Profession.CODER, Profession.TEACHER));

    final EnumMap<Profession, String> professionEnumMap = new EnumMap<>(Profession.class);
    professionEnumMap.put(Profession.CODER, " coder");
    fullMessage.setProfessionEnumMap(professionEnumMap);

    return fullMessage;
}