示例#1
0
        // TODO: Make this use same buffer and event as RpcLogWriter.
        public void OnNext(ApplyCommandRequested data, long sequence, bool endOfBatch)
        {
            var appliedDifference = data.LogIdx - _node.Data.LastApplied;
            var logsToApply       = EnumerableUtilities.Range(_node.Data.LastApplied + 1, (int)appliedDifference);

            foreach (var logIdx in logsToApply)
            {
                var command = _commandRegister.Get(_node.Data.CurrentTerm, logIdx);

                // The term may have been increased before the command was applied. In which case, rely on log matching to fix.
                if (command == null)
                {
                    continue;
                }

                command.Execute(_serviceLocator);
                _nodePublisher.PublishEvent(new NodeCommandScheduled
                {
                    Command = new ApplyEntry
                    {
                        EntryIdx = logIdx
                    }
                }).Wait();
            }
        }
 public CollectionReaderWriter(FullTypeBinaryRepresentationCache fullTypeBinaryRepresentationCache, ThisIsTotesTheRealLegitThingReaderWriterThing thingReaderWriterDispatcherThing, Type userCollectionType)
 {
     this.fullTypeBinaryRepresentationCache = fullTypeBinaryRepresentationCache;
     this.thingReaderWriterDispatcherThing  = thingReaderWriterDispatcherThing;
     this.userCollectionType       = userCollectionType;
     this.simplifiedCollectionType = TypeSimplifier.SimplifyType(userCollectionType);
     this.elementType = EnumerableUtilities.GetEnumerableElementType(simplifiedCollectionType);
 }
示例#3
0
 public LocalPublisher(ILocalPublisherSettings settings)
 {
     settings.EnsureNotNull(nameof(settings)).EnsureReadOnly().EnsureValid().EnsureNotDisabled();
     //
     _runningSettings         = settings;
     _subscriptionsSpinLock   = new PrimitiveSpinLock();
     _subscriptionsList       = new List <ILocalSubscription>();
     _subscriptionsDictionary = new Dictionary <ILocalSubscription, P_NewSubscriptionRegistrationState>();
     _postingQueue            = new Queue <LocalPostingQueueEntry>();
     _postingQueueSpinLock    = new PrimitiveSpinLock();
     _postingCts     = new CancellationTokenSource();
     _postingWorkers =
         EnumerableUtilities
         .CreateSequence(
             factory: locIndex => new LocalPostingWorker(queue: _postingQueue, queueSpinLock: _postingQueueSpinLock, cancellationToken: _postingCts.Token),
             length: _runningSettings.PostingDop)
         .ToArray();
     _runControl = new RunControl <LocalPublisher>(options: RunControlOptions.SingleStart, component: this, start: P_DoStartAsync, stop: P_DoStopAsync);
 }
示例#4
0
        //Do not delete or edit this method, you can only modify the block
        public int GetMaximumPairOfSocks(int noOfWashes, int[] cleanPile, int[] dirtyPile)
        {
            int pair_count = 0;

            // get pairs count already in C


            HashSet <int> distinct_vals = new HashSet <int>(cleanPile);
            var           remaining     = new List <int>();

            foreach (var v in distinct_vals)
            {
                if (cleanPile.Count(c => c == v) % 2 == 0)
                {
                    pair_count += Convert.ToInt32(cleanPile.Count(c => c == v) / 2);
                }
                else
                {
                    pair_count += Convert.ToInt32(cleanPile.Count(c => c == v) / 2);
                    remaining.Add(v);
                }
            }

            if (noOfWashes == 0)
            {
                return(pair_count);
            }

            var lr = remaining.Count;

            foreach (var i in EnumerableUtilities.RangePython(lr - 1, -1, -1))
            {
                if (dirtyPile.Count(c => c == remaining[i]) > 0)
                {
                    var index = Array.FindIndex(dirtyPile, row => row == remaining[i]);
                    dirtyPile = dirtyPile.Where((d, idx) => idx != index).ToArray();

                    pair_count += 1;
                    noOfWashes -= 1;
                    if (noOfWashes == 0)
                    {
                        return(pair_count);
                    }
                }
            }
            distinct_vals = new HashSet <int>(dirtyPile);


            foreach (var v in distinct_vals)
            {
                var matching_pairs = dirtyPile.Count(c => c == v) / 2;
                //var sequence = Enumerable.Range(matching_pairs,0);
                foreach (var i in Enumerable.Range(0, matching_pairs))
                {
                    noOfWashes -= 1;
                    if (noOfWashes == 0)
                    {
                        return(pair_count);
                    }
                    pair_count += 1;
                    noOfWashes -= 1;
                    if (noOfWashes == 0)
                    {
                        return(pair_count);
                    }
                }
            }
            return(pair_count);
        }
        public static object ConvertCollectionToHintType(object collection, Type hintType)
        {
            // fast fail on non-collections, since we aren't converting them.
            if (!typeof(IEnumerable).IsAssignableFrom(hintType))
            {
                return(collection);
            }

            // TODO: This will fail on nulls right now
            // shortcut for if collection already matches hinttype. Handles strings as well.
            if (hintType == collection?.GetType())
            {
                return(collection);
            }

            // Don't attempt conversion if collection extends from hintType interface.
            if (hintType.IsInterface && hintType.IsInstanceOfType(collection))
            {
                return(collection);
            }

            // If hintType is IROD/IROS, up that to the Dictionary default, since we can't
            // construct an interface.
            // TODO: All sets are IROS, all dicts are IROD, so is this an impossible code path?
            if (hintType.IsGenericType && hintType.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IReadOnlyDictionary <,>))
            {
                hintType = typeof(System.Collections.Generic.Dictionary <,>).MakeGenericType(hintType.GetGenericArguments());
            }
            if (hintType.IsGenericType && hintType.GetGenericTypeDefinition() == typeof(IReadOnlySet <>))
            {
                hintType = typeof(HashSet <>).MakeGenericType(hintType.GetGenericArguments());
            }

            var enumerableType = EnumerableUtilities.GetGenericIEnumerableInterfaceTypeOrNull(hintType);
            var elementType    = EnumerableUtilities.GetEnumerableElementType(enumerableType);

            if (EnumerableUtilities.IsDictionaryLikeType(hintType))
            {
                // e.g. KeyValuePair<TKey, TValue>[]
                var subjectKvpVectorLike        = (IEnumerable)collection;
                var shallowClonedCastedElements = ShallowCloneDictionaryToType(subjectKvpVectorLike, elementType);

                // invoke .ctor(IEnumerable<KVP<TKey, TVal>>)
                var constructor = hintType.GetConstructor(new[] { enumerableType });
                if (constructor != null)
                {
                    return(constructor.Invoke(new object[] { shallowClonedCastedElements }));
                }

                // invoke .ctor(), then Add/Enqueue of collection type
                var instance = Activator.CreateInstance(hintType);
                var add      = hintType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                               .FirstOrDefault(m => m.Name.Contains("Add") && m.GetParameters().Length == 2);

                foreach (var kvpObject in shallowClonedCastedElements)
                {
                    dynamic kvp = kvpObject;
                    add.Invoke(instance, new object[] { kvp.Key, kvp.Value });
                }
                return(instance);
            }
            else
            {
                // Vector-like case
                var subjectElements = (Array)collection;

                if (hintType.IsArray)
                {
                    return(ShallowCloneArrayToType(subjectElements, hintType.GetElementType()));
                }
                else
                {
                    // Invoke .ctor(IEnumerable<TElement>)
                    var constructor = hintType.GetConstructor(new[] { enumerableType });
                    if (constructor != null)
                    {
                        var shallowClonedCastedElements = ShallowCloneArrayToType(subjectElements, elementType);
                        return(constructor.Invoke(new object[] { shallowClonedCastedElements }));
                    }

                    // Invoke .ctor(), then Add/Enqueue(TElement)
                    var instance = Activator.CreateInstance(hintType);
                    var add      = hintType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                                   .FirstOrDefault(m => m.Name.Contains("Add") && m.GetParameters().Length == 1) ?? hintType.GetMethod("Enqueue");
                    foreach (var element in subjectElements)
                    {
                        add.Invoke(instance, new object[] { element });
                    }
                    return(instance);
                }
            }
        }
示例#6
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            string output = "";

            try
            {
                GdalConfiguration.ConfigureOgr();

                GdalConfiguration.ConfigureGdal();

                output = "It works!";
            }

            catch (Exception e)
            {
                output = "{0} Exception caught. " + e;
            }

            string input = "";

            DA.GetData(0, ref input);
            string file = input;

            bool flag = false;

            DA.GetData(1, ref flag);

            double minLat = 0.0;

            DA.GetData(2, ref minLat);

            double maxLat = 0.0;

            DA.GetData(3, ref maxLat);

            double minLon = 0.0;

            DA.GetData(4, ref minLon);

            double maxLon = 0.0;

            DA.GetData(5, ref maxLon);

            var boolListX = new List <bool>();
            var boolListY = new List <bool>();
            var boolOut   = new List <bool>();

            OSGeo.GDAL.Dataset ds = OSGeo.GDAL.Gdal.Open(file, OSGeo.GDAL.Access.GA_ReadOnly);

            double[] gt = new double[6];

            ds.GetGeoTransform(gt);

            var xres = gt[1];
            var yres = gt[5];

            var xsize = ds.RasterXSize;
            var ysize = ds.RasterYSize;

            var xmin = gt[0] + xres * 0.5;
            var xmax = gt[0] + (xres * xsize) - xres * 0.5;
            var ymin = gt[3] + (yres * ysize) + yres * 0.5;
            var ymax = gt[3] - yres * 0.5;

            var xx = EnumerableUtilities.RangePython(xmin, xmax + xres, xres).ToArray();
            var yy = EnumerableUtilities.RangePython(ymax + yres, ymin, yres).ToArray();

            var inMX = new List <double>().ToArray();
            var inMY = new List <double>().ToArray();

            var xNew = new List <double>();
            var yNew = new List <double>();

            var M = Acc.Matrix.MeshGrid(inMX, inMY);

            bool xFirst = false;

            if (flag == false)
            {
                M      = Acc.Matrix.MeshGrid(yy, xx);
                xFirst = false;
            }

            else if (flag == true)
            {
                for (int i = 0; i < xx.Length; ++i)
                {
                    if (xx[i] > minLat && xx[i] < maxLat)
                    {
                        xNew.Add(xx[i]);
                    }
                }

                for (int i = 0; i < yy.Length; ++i)
                {
                    if (yy[i] > minLon && yy[i] < maxLon)
                    {
                        yNew.Add(yy[i]);
                    }
                }

                if (yNew.Count() < xNew.Count())
                {
                    M      = Acc.Matrix.MeshGrid(yNew.ToArray(), xNew.ToArray());
                    xFirst = false;
                }

                else if (yNew.Count() > xNew.Count() || yNew.Count() == xNew.Count())
                {
                    M      = Acc.Matrix.MeshGrid(xNew.ToArray(), yNew.ToArray());
                    xFirst = true;
                }
            }

            int xSize = 0, ySize = 0;

            if (xFirst == true)
            {
                xSize = M.Item1.GetLength(0);
                ySize = M.Item2.GetLength(0);
            }

            else
            {
                xSize = M.Item2.GetLength(0);
                ySize = M.Item1.GetLength(0);
            }

            var x = new double[xSize, ySize];
            var y = new double[ySize, xSize];

            if (xFirst == true)
            {
                x = M.Item1;
                y = M.Item2;
            }

            else
            {
                x = M.Item2;
                y = M.Item1;
            }

            x = M.Item2;
            y = M.Item1;

            ds = null;

            DA.SetData(0, output);
            DA.SetDataList(1, x);
            DA.SetDataList(2, y);
        }