Example #1
0
        public void Remove(int indexID)
        {
            int packedIndex = GetPackedIndex(indexID);

            mIndexSet.Remove(indexID);
            mPackedValues[packedIndex] = mPackedValues[mIndexSet.Count];
        }
        IEnumerable<NeuralVectors> DoGetNextVectors(IndexSet indexes)
        {
            var privIndexes = new IndexSet(indexes);

            foreach (int index in privIndexes.ToList())
            {
                string key = CacheKeyPrefix + index;
                var foundVector = cache[key] as NeuralVectors;
                if (foundVector != null)
                {
                    yield return foundVector;
                    privIndexes.Remove(index);
                }
            }

            if (privIndexes.Count > 0)
            {
                var indexList = privIndexes.ToList();
                int listIndex = 0;
                foreach (var vector in BaseProvider.GetNextVectors(privIndexes))
                {
                    int vectorIndex = indexList[listIndex++];
                    string key = CacheKeyPrefix + vectorIndex;
                    cache[key] = vector;
                    keys.Add(key);
                    yield return vector;
                }
            }
        }
Example #3
0
        public void TestIndexSet()
        {
            var set     = new IndexSet(70);
            var set2    = new IndexSet();
            var example = new SortedSet <int>();

            void AssertEquavalent()
            {
                set.Count.Should().Be(example.Count);
                set.Should().Equal(example);
                set2.Count.Should().Be(example.Count);
                set2.Should().Equal(example);

                for (var i = 0; i < 70; i++)
                {
                    set.Contains(i).Should().Be(example.Contains(i));
                    set2.Contains(i).Should().Be(example.Contains(i));
                }
            }

            AssertEquavalent();


            void Set(int index, bool value)
            {
                if (value)
                {
                    set.Add(index).Should().Be(example.Add(index));
                    set2.Add(index);
                }
                else
                {
                    set.Remove(index).Should().Be(example.Remove(index));
                    set2.Remove(index);
                }

                AssertEquavalent();
            }

            void Clear()
            {
                set.Clear();
                set2.Clear();
                example.Clear();

                AssertEquavalent();
            }

            AssertEquavalent();


            Set(0, true);
            Set(0, true);
            Set(0, false);
            Set(1, true);
            Set(12, true);
            Set(31, true);
            Set(31, false);
            Set(32, true);
            Set(32, false);
            Set(50, true);
            Set(64, true);
            Set(64, false);

            Set(65, false);
            Set(66, true);
            Set(69, true);
            Set(69, false);


            Clear();

            Set(69, true);
            Set(68, true);

            Set(3, true);
            Set(4, true);

            for (var i = 0; i < 70; i++)
            {
                Set(i, true);
            }
        }
Example #4
0
        private void ScheduleGetNextVectors(NativeActivityContext context)
        {
            var vars = ComputationContext.GetVariables(context, this);

            var strategy = vars.Get<BatchingStrategy>(StrategyVarName);

            if (LastResult != null && !strategyHasJustInited.Get(context))
            {
                var obs = strategy as OptimizationBatchingStrategy;
                if (obs != null)
                {
                    var last = LastResult.Get(context);
                    if (!last.IsEmpty)
                    {
                        obs.SetLastResult(last);
                    }
                }
            }

            var indexSet = new IndexSet(strategy.GetNextIndexes());

            if (IsCached(context))
            {
                // Get From Cache:
                var cache = vars.Get<SerializableCache>(CacheVarName).Cache;
                // Create variable:
                var vectorsFromCache = new LinkedList<NeuralVectors>();
                cachedVectors.Set(context, vectorsFromCache);

                var newIndexSet = new IndexSet(indexSet);

                foreach (var index in indexSet)
                {
                    string key = index.ToString();
                    var cached = cache[key] as NeuralVectors;
                    if (cached != null)
                    {
                        // Cached, add to variable, and remove from indexes:
                        vectorsFromCache.AddLast(cached);
                        newIndexSet.Remove(index);
                    }
                }

                indexSet = newIndexSet;
            }

            if (indexSet.Count > 0)
            {
                // Are there any non-cached item requests? Get it!
                context.ScheduleFunc(GetNextVectors, indexSet, OnGetNextVectorsCompleted);
            }
            else
            {
                // All items was in cache, proccess'em!
                ProcessNextVectors(context, null);
            }
        }