public void TestCaptureOrder() { // post source ints var intSource = new BufferBlock <int>(); intSource.Post(1); intSource.Post(2); intSource.Post(99); intSource.Post(98); intSource.Complete(); // capture source order var orderedInts = OrderingBlock.CaptureOrder <int, long, long>( intSource, intValue => (long)intValue); // post longs to combine, in reverse of original order var longSource = new BufferBlock <long>(); longSource.Post(99); longSource.Post(98); longSource.Post(2); longSource.Post(1); longSource.Complete(); // apply source order var orderedLongs = orderedInts.ApplyOrder(longSource, longValue => longValue); // verify the original order was preserved CollectionAssert.AreEqual(new long[] { 1, 2, 99, 98 }, orderedLongs.ToEnumerable().ToList()); }
private IEnumerable <QuizInfoForDb> CreateQuizInfoForDb(OrderingBlock orderingBlock, IGrouping <string, QuizAnswer> answers) { var ans = answers.Select(x => x.ItemId).ToList() .Select(x => new QuizInfoForDb { QuizId = orderingBlock.Id, IsRightAnswer = true, ItemId = x, Text = null, QuizType = typeof(OrderingBlock), QuizBlockScore = 0, QuizBlockMaxScore = orderingBlock.MaxScore }).ToList(); var isRightQuizBlock = answers.Count() == orderingBlock.Items.Length && answers.Zip(orderingBlock.Items, (answer, item) => answer.ItemId == item.GetHash()).All(x => x); var blockScore = isRightQuizBlock ? orderingBlock.MaxScore : 0; foreach (var info in ans) { info.QuizBlockScore = blockScore; } return(ans); }
public static ISourceBlock <DecodedBlockTx> LookAhead(ISourceBlock <DecodedBlockTx> blockTxes, IDeferredChainStateCursor deferredChainStateCursor, CancellationToken cancelToken = default(CancellationToken)) { // capture the original block txes order var orderedBlockTxes = OrderingBlock.CaptureOrder <DecodedBlockTx, DecodedBlockTx, int>( blockTxes, blockTx => blockTx.Index, cancelToken); // queue each utxo entry to be warmed up: each input's previous transaction, and each new transaction var queueUnspentTxLookup = InitQueueUnspentTxLookup(cancelToken); orderedBlockTxes.LinkTo(queueUnspentTxLookup, new DataflowLinkOptions { PropagateCompletion = true }); // warm up each uxto entry var warmupUtxo = InitWarmupUtxo(deferredChainStateCursor, cancelToken); queueUnspentTxLookup.LinkTo(warmupUtxo, new DataflowLinkOptions { PropagateCompletion = true }); // return the block txes with warmed utxo entries in original order return(orderedBlockTxes.ApplyOrder(warmupUtxo, blockTx => blockTx.Index, cancelToken)); }
private static QuizAnswerInfo GetOrderingBlockAnswerInfo(IReadOnlyDictionary <string, List <UserQuiz> > answers, OrderingBlock block, int questionIndex) { IEnumerable <UserQuiz> userAnswers = new List <UserQuiz>(); UserQuiz firstAnswer = null; if (answers.ContainsKey(block.Id)) { userAnswers = answers[block.Id].Where(q => q.ItemId != null); firstAnswer = userAnswers.FirstOrDefault(); } var answersPositions = userAnswers.Select( userAnswer => block.Items.FindIndex(i => i.GetHash() == userAnswer.ItemId) ).ToList(); return(new OrderingBlockAnswerInfo { Id = questionIndex.ToString(), AnswersPositions = answersPositions, Score = firstAnswer?.QuizBlockScore ?? 0, MaxScore = firstAnswer?.QuizBlockMaxScore ?? 0, }); }