Exemplo n.º 1
0
 public void TestRangeSetIntList()
 {
     RangeSet rangeSet = new RangeSet( new int[] { 1, 2, 3, 4, 5, 7, 8, 9, 10 } );
     Assert.AreEqual( 2, rangeSet.Count );
     Assert.AreEqual( new Range() { Start = 1, Stop = 5 }, rangeSet[0] );
     Assert.AreEqual( new Range() { Start = 7, Stop = 10 }, rangeSet[1] );
 }
Exemplo n.º 2
0
            public IAcceptInputTransition <string> ActivateRegexNFATransitionFromRegexCondition(RegexCondition <string> regex)
            {
                if (regex == null)
                {
                    throw new ArgumentNullException(nameof(regex));
                }

                RangeSet <string> set = new RangeSet <string>(this.rangeInfo);

                if (regex is RegexConst <string> regexConst)
                {
                    set.Add(regexConst.ConstValue);
                }
                else if (regex is RegexRange <string> regexRange)
                {
                    set.AddRange(regexRange.Minimum, regexRange.Maximum, regexRange.CanTakeMinimum, regexRange.CanTakeMaximum);
                }
                else if (regex is IRange <string> range)
                {
                    set.AddRange(range.Minimum, range.Maximum, range.CanTakeMinimum, range.CanTakeMaximum);
                }
                else
                {
                    throw new NotSupportedException();
                }

                return(new RangeSetRegexFATransition <string, BasicRegexNFAState <string> >(set));
            }
Exemplo n.º 3
0
        public void UnionComplement1()
        {
            var charSetA = new RangeSet <Codepoint>('a');
            var charSet  = RangeOperations <Codepoint> .Union(charSetA, RangeOperations <Codepoint> .Negate(charSetA));

            Assert.Equal(new[] { Range <Codepoint> .Create(Codepoint.MinValue, Codepoint.MaxValue) }, charSet);
        }
Exemplo n.º 4
0
        private double processRate(int size, string queue)
        {
            CircularBuffer <IMessage> buffer   = new CircularBuffer <IMessage>(100);
            IMessageListener          listener = new SyncListener(buffer);
            string localQueue = "queue-" + UUID.RandomUuid();

            Session.QueueDeclare(localQueue, null, null, Option.AUTO_DELETE);
            Session.ExchangeBind(localQueue, "amq.direct", queue);
            Session.AttachMessageListener(listener, localQueue);
            Session.MessageSubscribe(localQueue);
            double   rate  = 0;
            RangeSet range = new RangeSet();

            for (int i = 0; i < size; ++i)
            {
                IMessage m = buffer.Dequeue();
                range.Add(m.Id);
                BinaryReader reader = new BinaryReader(m.Body, Encoding.UTF8);
                byte[]       body   = new byte[m.Body.Length - m.Body.Position];
                reader.Read(body, 0, body.Length);
                rate += BitConverter.ToDouble(body, 0);
            }
            Session.MessageAccept(range);
            return(rate);
        }
        /// <summary>
        /// モーションオブジェクトの欠損/被欠損の情報をフレーム単位で取得する
        /// </summary>
        /// <param name="id">情報を取得するさきのオブジェクトのId</param>
        /// <returns></returns>
        private RangeSet <int> getExistenceList(uint id)
        {
            MotionDataSet dataSet = _dataSet;

            if (dataSet == null)
            {
                return(null);
            }
            RangeSet <int> ret;

            if (_existenceList.TryGetValue(id, out ret))
            {
                return(ret);
            }
            try {
                ret = new RangeSet <int>();
                int index = 0;
                foreach (var frame in dataSet.EnumerateFrame())
                {
                    if (frame[id] != null)
                    {
                        ret.Add(new RangeSet <int> .Range(index, index + 1));
                    }
                    index++;
                }
                _existenceList[id] = ret;
                return(ret);
            } catch { return(null); }
        }
        public AlphabetBuilder(RxNode <TLetter> expression, TLetter?eof = null, RangeSet <TLetter>?validRanges = default)
        {
            var eofRange = eof.HasValue ? new RangeSet <TLetter>(eof.Value) : RangeSet <TLetter> .Empty;
            // Step 1: Find all charset-generating regular expression parts
            var visitor = new AlphabetBuilderVisitor <TLetter>();

            expression.Visit(visitor, (letters, negate) => letters - eofRange);
            var charsets = visitor.Charsets;
            // Step 2: Get all ranges of all used charsets and register their "users"
            var ranges = MakeRanges(charsets, (validRanges ?? RangeSet <TLetter> .All) - eofRange);
            // Step 3: Group the information into alphabet entries
            var alphabetByKey = ranges
                                .GroupBy <UsedLetterRange <TLetter>, string, Range <TLetter> >(r => r.GetUsersKey(), r => r.Range)
                                .Select((g, ix) => new KeyValuePair <string, AlphabetLetter <TLetter> >(g.Key, new AlphabetLetter <TLetter>(new LetterId(ix + 1), new RangeSet <TLetter>(g))))
                                .ToDictionary(p => p.Key, p => p.Value);

            // Step 4: Store alphabet entries for each regex part
            foreach (var range in ranges)
            {
                var alphabetEntry = alphabetByKey[range.GetUsersKey()];
                for (var rangeUser = range.Users; rangeUser != null; rangeUser = rangeUser.Next)
                {
                    charsets[rangeUser.User].Value.Add(alphabetEntry.Id);
                }
            }
            // Step 5: store alphabet information
            this.AlphabetById = alphabetByKey.Values.ToDictionary(e => e.Id, e => e.Ranges);
            this.AlphabetById.Add(LetterId.Eof, eofRange);             // EOF
            // Step 6: rebuild expression
            this.Expression = expression.Visit(new AlphabetLetterVisitor <TLetter>(), node => charsets[visitor.GetId(node)].Value);
        }
        public override void SessionCompleted(Session ssn, SessionCompleted cmp)
        {
            RangeSet ranges = cmp.GetCommands();
            RangeSet known  = null;

            if (cmp.GetTimelyReply())
            {
                known = new RangeSet();
            }

            if (ranges != null)
            {
                foreach (Range range in ranges)
                {
                    bool advanced = ssn.Complete(range.Lower, range.Upper);
                    if (advanced && known != null)
                    {
                        known.Add(range);
                    }
                }
            }

            if (known != null)
            {
                ssn.SessionKnownCompleted(known);
            }
        }
        public static RangeSet Split(this IWordSplitter splitter, ByteBlock text)
        {
            RangeSet words = new RangeSet();

            splitter.Split(text.Array, new Range(text.Index, text.Length), words);
            return(words);
        }
Exemplo n.º 9
0
        public void Split(byte[] text, Range withinRange, RangeSet result)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }
            if (text == null)
            {
                return;
            }

            int lastGroup = withinRange.Index;
            int end       = withinRange.Index + withinRange.Length;
            int i;

            for (i = withinRange.Index; i < end; ++i)
            {
                byte c = text[i];

                if (c == UTF8.Semicolon)
                {
                    result.Add(lastGroup, i - lastGroup);
                    lastGroup = i + 1;
                    if (lastGroup < end && text[lastGroup] == UTF8.Space)
                    {
                        lastGroup++;
                    }
                }
            }

            // Include the last value
            result.Add(lastGroup, i - lastGroup);
        }
Exemplo n.º 10
0
 internal void Load(RangeSet stationRanges, RangeSet spatialZones, SparseArray <float> capacity, int[] closestStation)
 {
     if (CapacityFactor == null || ReloadCapacityFactors)
     {
         LoadCapacityFactors();
     }
     CalculateUtilities(stationRanges, spatialZones, capacity.GetFlatData(), closestStation);
 }
Exemplo n.º 11
0
 private static IEnumerable <RxNode <char> > MakeSingleRange(RangeSet <Codepoint> ranges)
 {
     if (ranges.Count > 0)
     {
         return(new RxMatch <char>(new RangeSet <char>(ranges.Select(r => Range <char> .Create((char)r.From, (char)r.To)))).Yield());
     }
     return(Enumerable.Empty <RxNode <char> >());
 }
Exemplo n.º 12
0
        public void TestRangeSetIntList()
        {
            RangeSet rangeSet = new RangeSet(new[] { 1, 2, 3, 4, 5, 7, 8, 9, 10 });

            Assert.AreEqual(2, rangeSet.Count);
            Assert.AreEqual(new Range(1, 5), rangeSet[0]);
            Assert.AreEqual(new Range(7, 10), rangeSet[1]);
        }
 private static IEnumerable <RxNode <byte> > MakeSingleRange(RangeSet <int> ranges)
 {
     if (ranges.Count > 0)
     {
         return(new RxMatch <byte>(new RangeSet <byte>(ranges.Select(range => Range <byte> .Create((byte)range.From, (byte)range.To)))).Yield());
     }
     return(Enumerable.Empty <RxMatch <byte> >());
 }
 protected override RxNode <byte> MapCodepoints(RangeSet <Codepoint> codepointRanges)
 {
     return(MakeSingleRange(Intersect(codepointRanges, SingleByte))
            .Concat(Intersect(codepointRanges, DoubleByte).SelectMany(r => MakeTwoRanges(r, 0xC0)))
            .Concat(Intersect(codepointRanges, TripleByte).SelectMany(r => MakeThreeRanges(r, 0xE0)))
            .Concat(Intersect(codepointRanges, QuadripleByte).SelectMany(r => MakeFourRanges(r, 0xF0)))
            .JoinAlternation());
 }
Exemplo n.º 15
0
            private void CalculateUtilities(RangeSet stationRanges, RangeSet spatialZones, float[] capacity, int[] closestStation)
            {
                INetworkData       autoNetwork    = GetNetwork(AutoNetworkName);
                ITripComponentData transitNetwork = GetNetwork(TransitNetworkName) as ITripComponentData;

                EnsureNetworks(autoNetwork, transitNetwork);
                var zoneArray = Root.ZoneSystem.ZoneArray;

                IZone[] zones              = zoneArray.GetFlatData();
                int[]   stationZones       = GetStationZones(stationRanges, capacity, zones);
                var     flatCapacityFactor = CapacityFactor.GetFlatData();

                if (AutoFromOriginToAccessStation == null || TransitFromAccessStationToDestination.Length != stationZones.Length * zones.Length)
                {
                    TransitFromAccessStationToDestination = new float[stationZones.Length * zones.Length];
                    AutoFromOriginToAccessStation         = new float[stationZones.Length * zones.Length];
                    TransitFromDestinationToAccessStation = new float[stationZones.Length * zones.Length];
                    AutoFromAccessStationToDestination    = new float[stationZones.Length * zones.Length];
                }
                // compute the toAccess utilities
                Parallel.For(0, zones.Length, (int originIndex) =>
                {
                    var zoneNumber = zones[originIndex].ZoneNumber;
                    if (spatialZones.Contains(zoneNumber))
                    {
                        for (int i = 0; i < stationZones.Length; i++)
                        {
                            var accessIndex = stationZones[i];
                            var factor      = (float)Math.Pow(flatCapacityFactor[accessIndex], CapacityFactorExp);
                            // calculate access' to access station this will include more factors
                            AutoFromOriginToAccessStation[originIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(autoNetwork, originIndex, accessIndex)
                                                                                                                   + (Capacity * capacity[accessIndex]
                                                                                                                      + ParkingCost * zones[accessIndex].ParkingCost
                                                                                                                      + (closestStation[originIndex] == accessIndex ? ClosestStationFactor : 0))) * factor;
                            // calculate egress' from access station
                            AutoFromAccessStationToDestination[originIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(autoNetwork, accessIndex, originIndex)) * factor;
                        }
                    }
                });

                // compute the toDesinstination utilities
                Parallel.For(0, zones.Length, (int destIndex) =>
                {
                    var zoneNumber = zones[destIndex].ZoneNumber;
                    if (spatialZones.Contains(zoneNumber))
                    {
                        for (int i = 0; i < stationZones.Length; i++)
                        {
                            var accessIndex = stationZones[i];
                            var factor      = (float)Math.Pow(flatCapacityFactor[accessIndex], CapacityFactorExp);
                            // calculate access' to destination
                            TransitFromAccessStationToDestination[destIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(transitNetwork, accessIndex, destIndex)) * factor;
                            // calculate egress' to access station
                            TransitFromDestinationToAccessStation[destIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(transitNetwork, destIndex, accessIndex)) * factor;
                        }
                    }
                });
            }
        public RangeSetRegexStateMachineActivationContextInfo(RangeSet <T> rangeSet)
        {
            if (rangeSet == null)
            {
                throw new ArgumentNullException(nameof(rangeSet));
            }

            this.rangeSet = rangeSet;
        }
Exemplo n.º 17
0
        public Dfa <LetterId> ComputeDfa(out AlphabetBuilder <TLetter> alphabet, RangeSet <TLetter> validRanges = default)
        {
            var regex = this.ComputeRx();

            alphabet = new AlphabetBuilder <TLetter>(regex, this.eof, validRanges);
            var nfa = NfaBuilder <LetterId> .Build(alphabet.Expression);

            return(DfaBuilder <LetterId> .Build(nfa, LetterId.Eof, true));
        }
        public override void SessionKnownCompleted(Session ssn, SessionKnownCompleted kcmp)
        {
            RangeSet kc = kcmp.GetCommands();

            if (kc != null)
            {
                ssn.KnownComplete(kc);
            }
        }
Exemplo n.º 19
0
        public void UnionComplement2()
        {
            var charSetA    = new RangeSet <Codepoint>('a');
            var charSetNotA = RangeOperations <Codepoint> .Difference(RangeSet <Codepoint> .All, charSetA);

            var charSet = RangeOperations <Codepoint> .Union(charSetNotA, charSetA);

            Assert.Equal(new[] { Range <Codepoint> .Create(Codepoint.MinValue, Codepoint.MaxValue) }, charSet);
        }
Exemplo n.º 20
0
 public RangeSetView ToDto(RangeSet rangeSet)
 {
     return(new RangeSetView()
     {
         Id = rangeSet.Id,
         Name = rangeSet.Name,
         Ranges = rangeSet.Ranges.ToDictionary(s => Ranges.Find(c => c.Id == s.Key), s => s.Value)
     });
 }
Exemplo n.º 21
0
        /// <summary>
        /// Updates all of the specified ListView entries.  This is called after minor changes,
        /// such as editing a comment or renaming a label, that can be handled by regenerating
        /// selected parts of the DisplayList.
        /// </summary>
        /// <param name="offsetSet"></param>
        private void RefreshCodeListViewEntries(RangeSet offsetSet)
        {
            IEnumerator <RangeSet.Range> iter = offsetSet.RangeListIterator;

            while (iter.MoveNext())
            {
                RangeSet.Range range = iter.Current;
                mDisplayList.GenerateRange(range.Low, range.High);
            }
        }
Exemplo n.º 22
0
        public BasicRegexStateMachineActivationContextInfo(RangeSet <T> accreditedSet)
        {
            if (accreditedSet == null)
            {
                throw new ArgumentNullException(nameof(accreditedSet));
            }

            this.rangeInfo     = accreditedSet.RangeInfo;
            this.accreditedSet = accreditedSet;
        }
Exemplo n.º 23
0
        private bool[] CreateMask(RangeSet range)
        {
            var zones = Root.ZoneSystem.ZoneArray.GetFlatData();

            bool[] ret = new bool[zones.Length];
            for (int i = 0; i < zones.Length; i++)
            {
                ret[i] = range.Contains(zones[i].ZoneNumber);
            }
            return(ret);
        }
Exemplo n.º 24
0
 private static bool IsContained(IZone zone, RangeSet validRanges, bool contained, bool useZoneRegion, bool useZonePlanningDistrict)
 {
     if (useZoneRegion)
     {
         return((!contained) ^ validRanges.Contains(zone.RegionNumber));
     }
     if (useZonePlanningDistrict)
     {
         return((!contained) ^ validRanges.Contains(zone.PlanningDistrict));
     }
     return((!contained) ^ validRanges.Contains(zone.ZoneNumber));
 }
Exemplo n.º 25
0
        internal void FromRangeSet(RangeSet rs)
        {
            this.SetAllNull();

            if (rs.FieldItemIndexPage1 != null) this.FieldItemIndexPage1 = rs.FieldItemIndexPage1.Value;
            if (rs.FieldItemIndexPage2 != null) this.FieldItemIndexPage2 = rs.FieldItemIndexPage2.Value;
            if (rs.FieldItemIndexPage3 != null) this.FieldItemIndexPage3 = rs.FieldItemIndexPage3.Value;
            if (rs.FieldItemIndexPage4 != null) this.FieldItemIndexPage4 = rs.FieldItemIndexPage4.Value;
            if (rs.Reference != null) this.Reference = rs.Reference.Value;
            if (rs.Name != null) this.Name = rs.Name.Value;
            if (rs.Sheet != null) this.Sheet = rs.Sheet.Value;
            if (rs.Id != null) this.Id = rs.Id.Value;
        }
Exemplo n.º 26
0
        internal static int[] GetStationZones(RangeSet stationRanges, float[] capacity, IZone[] zones)
        {
            List <int> validStationIndexes = new List <int>();

            for (int i = 0; i < zones.Length; i++)
            {
                if (capacity[i] > 0 && stationRanges.Contains(zones[i].ZoneNumber))
                {
                    validStationIndexes.Add(i);
                }
            }
            return(validStationIndexes.ToArray());
        }
Exemplo n.º 27
0
        public void CompareToTestEquals()
        {
            // Arrange
            var rangeSet1 = new RangeSet(0, long.MaxValue);
            var rangeSet2 = new RangeSet(0, long.MaxValue);

            // Act
            var actual = rangeSet1.CompareTo(rangeSet2);

            // Assert
            var expected = 0;

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 28
0
        public void CompareToTestPureSuperset()
        {
            // Arrange
            var rangeSet1 = new RangeSet(0, 5_000_000);
            var rangeSet2 = new RangeSet(0, long.MaxValue);

            // Act
            var actual = rangeSet2.CompareTo(rangeSet1);

            // Assert
            var expected = 1;

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 29
0
        public void TestRangeSetIntList()
        {
            RangeSet rangeSet = new RangeSet(new int[] { 1, 2, 3, 4, 5, 7, 8, 9, 10 });

            Assert.AreEqual(2, rangeSet.Count);
            Assert.AreEqual(new Range()
            {
                Start = 1, Stop = 5
            }, rangeSet[0]);
            Assert.AreEqual(new Range()
            {
                Start = 7, Stop = 10
            }, rangeSet[1]);
        }
Exemplo n.º 30
0
        public ByteBlock Highlight(ByteBlock value, IWordSplitter splitter, List <HighlightTerm> terms)
        {
            if (terms == null)
            {
                throw new ArgumentNullException("terms");
            }
            if (splitter == null)
            {
                throw new ArgumentNullException("splitter");
            }

            ByteBlockAppender appender = new ByteBlockAppender(value);

            // Make a lowercase value copy to compare
            ByteBlock valueLower = value.Copy();

            valueLower.ToLowerInvariant();

            // Split the value and compare words to highlight terms
            RangeSet ranges = splitter.Split(valueLower);

            for (int rangeIndex = 0; rangeIndex < ranges.Count; ++rangeIndex)
            {
                Range     r         = ranges.Ranges[rangeIndex];
                ByteBlock valueWord = new ByteBlock(valueLower.Array, r.Index, r.Length);

                foreach (HighlightTerm term in terms)
                {
                    // If this word in the value starts with a search term...
                    if (term.Matches(valueWord))
                    {
                        // Append to the beginning of the word, if not already past this point
                        if (appender.AppendTo(valueWord.Index))
                        {
                            // Wrap and append the *prefix* of the term from the query
                            appender.Append(_wrapPrefix);
                            appender.AppendTo(valueWord.Index + term.Value.Length);
                            appender.Append(_wrapSuffix);
                        }

                        // If we matched, do not check other HighlightTerms against this word
                        break;
                    }
                }
            }

            // Append the remaining content and return
            appender.AppendRemainder();
            return(appender.Value());
        }
Exemplo n.º 31
0
        public void CompareToIndeterminableTest()
        {
            // Assemble
            var hs1 = new HashedSet(new long[] { -1, 0, 1, 2, 3 });
            var hs2 = new HashedSet(new long[] { 4, 5, 6, 7, 8 });
            var us  = new UnionSet(hs1, hs2);

            var rs3 = new RangeSet(long.MinValue, long.MaxValue);

            // Act
            var expected = 2;
            var actual   = us.CompareTo(rs3);

            //Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 32
0
        public void CompareToUnionSetIsEqualToRangedSetTest()
        {
            // Assemble
            var hs1 = new HashedSet(new long[] { -1, 0, 1, 2, 3 });
            var hs2 = new HashedSet(new long[] { -1, 0, 1, 2, 3, 4, 5 });
            var us  = new UnionSet(hs1, hs2);

            var rs3 = new RangeSet(-1, 5);

            // Act
            var expected = 0;
            var actual   = us.CompareTo(rs3);

            //Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 33
0
        public override System.ServiceModel.Channels.Message Receive(TimeSpan timeout)
        {
            _session.messageFlow("myDest", MessageCreditUnit.MESSAGE, 1);
            _session.sync();
            IMessage m = _queue.Dequeue();
            System.ServiceModel.Channels.Message result = null;
            if (m != null)
            {              
                var reader = new BinaryReader(m.Body, Encoding.UTF8);
                var body = new byte[m.Body.Length - m.Body.Position];
                reader.Read(body, 0, body.Length);
                try
                {
                    result = _encoder.ReadMessage(new MemoryStream(body),
                                                  (int) _bindingElement.MaxReceivedMessageSize);
                }
                catch(Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }
                result.Headers.To = LocalAddress.Uri;

                var ack = new RangeSet();
                // ack this message 
                ack.add(m.Id);
                _session.messageAccept(ack);
                _session.sync();
            }
            else
            {
                if(! _closed )
                {
                   return Receive(timeout); 
                }
            }
            return result;
        }
Exemplo n.º 34
0
 private bool GenerateChildren(ref string error)
 {
     this.Children = new List<AccessStation>();
     List<Range> rangeList = new List<Range>();
     Range currentRange = new Range();
     int current = 0;
     bool first = true;
     var zoneArray = this.Root.ZoneSystem.ZoneArray;
     foreach ( var record in StationInformationReader.Read() )
     {
         // make sure the station is actually inside of the zone system
         if ( zoneArray.ContainsIndex( record.O ) )
         {
             var parkingSpots = record.D;
             if ( first )
             {
                 current = currentRange.Start = record.O;
                 first = false;
             }
             else if ( current + 1 != record.O )
             {
                 currentRange.Stop = current;
                 rangeList.Add( currentRange );
                 currentRange.Start = record.O;
             }
             current = record.O;
             CreateChild( record.O, parkingSpots, record.Data );
         }
     }
     if ( !first )
     {
         currentRange.Stop = current;
         rangeList.Add( currentRange );
         var set = new RangeSet( rangeList );
         foreach ( var child in this.Children )
         {
             ( child as AccessStation ).StationRanges = set;
         }
     }
     return true;
 }
Exemplo n.º 35
0
		/// <summary>
		/// Starts the file download.
		/// </summary>
		public void StartDownload()
		{
			Trace.TraceInformation(String.Format("[{0}] Downloading.", m_uriURL.ToString()));
			if (!FileExists)
				throw new FileNotFoundException("The file to download does not exist.", m_uriURL.ToString());

			Int32 intConnectionsToUse = m_fmdInfo.SupportsResume ? m_intMaxConnections : 1;
			if (ServicePointManager.DefaultConnectionLimit < 1)
				throw new Exception(String.Format("Only {0} connections can be created to the same file; {1} are wanted.", ServicePointManager.DefaultConnectionLimit, 1));
			else if (ServicePointManager.DefaultConnectionLimit < intConnectionsToUse)
				intConnectionsToUse = ServicePointManager.DefaultConnectionLimit;

			//get the list of ranges we have not already downloaded
			RangeSet rgsMissingRanges = new RangeSet();
			rgsMissingRanges.AddRange(new Range(0, m_fmdInfo.Length - 1));
			if (File.Exists(m_strFileMetadataPath))
			{
				string[] strRanges = File.ReadAllLines(m_strFileMetadataPath);
				foreach (string strRange in strRanges)
				{
					string strCleanRange = strRange.Trim().Trim('\0');
					if (String.IsNullOrEmpty(strCleanRange))
						continue;
					rgsMissingRanges.RemoveRange(Range.Parse(strCleanRange));
				}
			}
			else if (File.Exists(m_strSavePath))
				File.Delete(m_strSavePath);

			Int32 intMinBlockSize = (Int32)Math.Min((UInt64)m_intMinBlockSize, rgsMissingRanges.TotalSize);
			Int32 intBaseBlockSize = (Int32)Math.Max(rgsMissingRanges.TotalSize / (UInt64)intConnectionsToUse, (UInt64)intMinBlockSize);
			if (intConnectionsToUse > 1)
				intBaseBlockSize = Math.Min(intBaseBlockSize, m_intMaxBlockSize);

			//break the ranges into blocks to be downloaded
			foreach (Range rngNeeded in rgsMissingRanges)
			{
				//find out how many blocks will fit into the range
				Int32 intBlockCount = (Int32)(rngNeeded.Size / (UInt64)intBaseBlockSize);
				if (intBlockCount == 0)
					intBlockCount = 1;
				//there is likely to be some remainder (there are likely a fractional number of blocks
				// in the range), so lets distrubute the remainder amongst all of the blocks
				// we do this by elarging our blocksize
				UInt64 intBlockSize = (UInt64)Math.Ceiling(rngNeeded.Size / (double)intBlockCount);
				UInt64 intBlockStart = rngNeeded.StartByte;
				for (; intBlockStart + intBlockSize < rngNeeded.EndByte; intBlockStart += intBlockSize)
					m_queRequiredBlocks.Enqueue(new Range(intBlockStart, intBlockStart + intBlockSize - 1));
				m_queRequiredBlocks.Enqueue(new Range(intBlockStart, rngNeeded.EndByte));
			}

			m_fwrWriter = new FileWriter(m_strSavePath, m_strFileMetadataPath);

			m_dteStartTime = DateTime.Now;
			//spawn the downloading threads
			Int32 intRequiredBlocks = m_queRequiredBlocks.Count;
			lock (m_lstDownloaders)
			{
				for (Int32 i = 0; i < (intRequiredBlocks < intConnectionsToUse ? intRequiredBlocks : intConnectionsToUse); i++)
				{
					BlockDownloader bdrDownloader = new BlockDownloader(this, m_fmdInfo, m_fwrWriter, m_intWriteBufferSize, m_strUserAgent);
					bdrDownloader.FinishedDownloading += new EventHandler(Downloader_FinishedDownloading);
					bdrDownloader.Start();
					m_lstDownloaders.Add(bdrDownloader);
				}
			}
		}
Exemplo n.º 36
0
		/// <summary>
		/// Sets up the initial values of the downloader.
		/// </summary>
		/// <param name="p_strSavePath">The path to which to save the file.
		/// If <paramref name="p_booUseDefaultFileName"/> is <c>false</c>, this value should be a complete
		/// path, including filename. If <paramref name="p_booUseDefaultFileName"/> is <c>true</c>,
		/// this value should be the directory in which to save the file.</param>
		/// <param name="p_booUseDefaultFileName">Whether to use the file name suggested by the server.</param>
		private void Initialize(string p_strSavePath, bool p_booUseDefaultFileName)
		{
			m_fmdInfo = GetMetadata();

			string strFilename = p_booUseDefaultFileName ? m_fmdInfo.SuggestedFileName : Path.GetFileName(p_strSavePath);

			strFilename = Uri.UnescapeDataString(strFilename);

			foreach (char chrInvalid in Path.GetInvalidFileNameChars())
				strFilename = strFilename.Replace(chrInvalid, '_');
			p_strSavePath = Path.Combine(p_strSavePath, strFilename);

			m_strSavePath = p_strSavePath + ".partial";
			m_strFileMetadataPath = p_strSavePath + ".parts";

			if (!m_fmdInfo.SupportsResume)
			{
				File.Delete(m_strFileMetadataPath);
				File.Delete(m_strSavePath);
			}

			//get the list of ranges we have already downloaded
			RangeSet rgsRanges = new RangeSet();
			if (File.Exists(m_strFileMetadataPath))
			{
				string[] strRanges = File.ReadAllLines(m_strFileMetadataPath);
				foreach (string strRange in strRanges)
				{
					string strCleanRange = strRange.Trim().Trim('\0');
					if (String.IsNullOrEmpty(strCleanRange))
						continue;
					rgsRanges.AddRange(Range.Parse(strCleanRange));
				}
			}
			m_intInitialByteCount = rgsRanges.TotalSize;
			m_intInitialDownloadedByteCount = rgsRanges.TotalSize / 1024;
		}
Exemplo n.º 37
0
        public override void Start()
        {
            if (Options.Tx > 0)
            {
                Session.TxSelect();
                Session.Sync();
            }
            CircularBuffer<IMessage> buffer = new CircularBuffer<IMessage>(100);
            // Create a listener and subscribe it to the queue named "message_queue"
            IMessageListener listener = new SyncListener(buffer);

            string dest = "dest" + UUID.RandomUuid();
            Session.AttachMessageListener(listener, dest);
            Session.MessageSubscribe(_queue, dest,
                                     Options.Tx > 0 || Options.SubAck > 0
                                         ? MessageAcceptMode.EXPLICIT
                                         : MessageAcceptMode.NONE,
                                     MessageAcquireMode.PRE_ACQUIRED, null, 0, null);
            // issue credits     
            Session.MessageSetFlowMode(dest, MessageFlowMode.WINDOW);
            Session.MessageFlow(dest, MessageCreditUnit.BYTE, ClientSession.MESSAGE_FLOW_MAX_BYTES);

            // Notify controller we are ready.
            IMessage message = new Message();
            message.DeliveryProperties.SetRoutingKey("sub_ready");

            message.AppendData(Encoding.UTF8.GetBytes("ready"));
            Session.MessageTransfer("amq.direct", message);

            if (Options.Tx > 0)
            {
                Session.TxCommit();
                Session.Sync();
            }


            for (int j = 0; j < Options.Iterations; ++j)
            {
               
                //need to allocate some more credit
                Session.MessageFlow(dest, MessageCreditUnit.MESSAGE, (long)Options.SubQuota);
                
                RangeSet range = new RangeSet();
                IMessage msg;
                DateTime start = DateTime.Now;
                for (long i = 0; i < Options.SubQuota; ++i)
                {                   
                    msg = buffer.Dequeue();
                    if (Options.Tx > 0 && ((i + 1)%Options.Tx == 0))
                    {
                        Session.TxCommit();
                        Session.Sync();
                    }
                    if (Options.IntervalSub > 0)
                    {
                        Thread.Sleep((int) Options.IntervalSub*1000);
                    }
                    range.Add(msg.Id);
                }
                if (Options.Tx > 0 || Options.SubAck > 0)
                    Session.MessageAccept(range);
                range.Clear();
                if (Options.Tx > 0)
                {
                    Session.TxSelect();
                    Session.Sync();
                }
                DateTime end = DateTime.Now;

                // Report to publisher.
                message.DeliveryProperties.SetRoutingKey("sub_done");
                message.ClearData();
                message.AppendData(BitConverter.GetBytes(Options.SubQuota / end.Subtract(start).TotalMilliseconds ));
                Session.MessageTransfer("amq.direct", message);
                if (Options.Tx > 0)
                {
                    Session.TxSelect();
                    Session.Sync();
                }
            }
            Session.Close();
        }
Exemplo n.º 38
0
            private void CalculateUtilities(RangeSet stationRanges, RangeSet spatialZones, float[] capacity, int[] closestStation)
            {
                INetworkData autoNetwork = GetNetwork(AutoNetworkName);
                ITripComponentData transitNetwork = GetNetwork(TransitNetworkName) as ITripComponentData;
                EnsureNetworks(autoNetwork, transitNetwork);
                var zoneArray = Root.ZoneSystem.ZoneArray;
                IZone[] zones = zoneArray.GetFlatData();
                int[] stationZones = GetStationZones(stationRanges, capacity, zones);
                var flatCapacityFactor = CapacityFactor.GetFlatData();
                if(AutoFromOriginToAccessStation == null || TransitFromAccessStationToDestination.Length != stationZones.Length * zones.Length)
                {
                    TransitFromAccessStationToDestination = new float[stationZones.Length * zones.Length];
                    AutoFromOriginToAccessStation = new float[stationZones.Length * zones.Length];
                    TransitFromDestinationToAccessStation = new float[stationZones.Length * zones.Length];
                    AutoFromAccessStationToDestination = new float[stationZones.Length * zones.Length];
                }
                // compute the toAccess utilities
                Parallel.For(0, zones.Length, (int originIndex) =>
                {
                    var zoneNumber = zones[originIndex].ZoneNumber;
                    if(spatialZones.Contains(zoneNumber))
                    {
                        for(int i = 0; i < stationZones.Length; i++)
                        {
                            var accessIndex = stationZones[i];
                            var factor = (float)Math.Pow(flatCapacityFactor[accessIndex], CapacityFactorExp);
                            // calculate access' to access station this will include more factors
                            AutoFromOriginToAccessStation[originIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(autoNetwork, originIndex, accessIndex)
                                + (Capacity * capacity[accessIndex]
                                + ParkingCost * zones[accessIndex].ParkingCost
                                + (closestStation[originIndex] == accessIndex ? ClosestStationFactor : 0))) * factor;
                            // calculate egress' from access station
                            AutoFromAccessStationToDestination[originIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(autoNetwork, accessIndex, originIndex)) * factor;
                        }
                    }
                });

                // compute the toDesinstination utilities
                Parallel.For(0, zones.Length, (int destIndex) =>
                {
                    var zoneNumber = zones[destIndex].ZoneNumber;
                    if(spatialZones.Contains(zoneNumber))
                    {
                        for(int i = 0; i < stationZones.Length; i++)
                        {
                            var accessIndex = stationZones[i];
                            var factor = (float)Math.Pow(flatCapacityFactor[accessIndex], CapacityFactorExp);
                            // calculate access' to destination
                            TransitFromAccessStationToDestination[destIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(transitNetwork, accessIndex, destIndex)) * factor;
                            // calculate egress' to access station
                            TransitFromDestinationToAccessStation[destIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(transitNetwork, destIndex, accessIndex)) * factor;
                        }
                    }
                });
            }
Exemplo n.º 39
0
 public void SessionKnownCompleted(RangeSet Commands, params Option[] options) {
     Invoke(new SessionKnownCompleted(Commands, options));
 }
Exemplo n.º 40
0
 private static bool IsContained(IZone zone, RangeSet validRanges, bool contained, bool useZoneRegion, bool useZonePlanningDistrict)
 {
     if ( useZoneRegion )
     {
         return ( !contained ) ^ validRanges.Contains( zone.RegionNumber );
     }
     else if ( useZonePlanningDistrict )
     {
         return ( !contained ) ^ validRanges.Contains( zone.PlanningDistrict );
     }
     else
     {
         return ( !contained ) ^ validRanges.Contains( zone.ZoneNumber );
     }
 }
Exemplo n.º 41
0
 public void WriteSequenceSet(RangeSet ranges)
 {
     if (ranges == null)
     {
         WriteUint16(0);
     }
     else
     {
         WriteUint16(ranges.Size()*8);
         foreach (Range range in ranges)
         {
             WriteSequenceNo(range.Lower);
             WriteSequenceNo(range.Upper);
         }
     }
 }
Exemplo n.º 42
0
 public void WriteByteRanges(RangeSet ranges)
 {
     throw new Exception("not implemented");
 }
Exemplo n.º 43
0
 public static void TestInit(TestContext testContext)
 {
     string json = GetTextFromEmbededFile("Smi.RangeSet.Lib.Tests.RangeMap.json");
     _listOfRanges = JsonConvert.DeserializeObject<List<KeyValuePair<Range, int>>>(json);
     _rangeSet = new RangeSet(_listOfRanges);
 }
Exemplo n.º 44
0
 private bool GenerateChildren(ref string error)
 {
     this.Children = new List<SubwayAccessStation>();
     List<Range> rangeList = new List<Range>();
     Range currentRange = new Range();
     int current = 0;
     bool first = true;
     foreach ( var record in StationZoneData.Read() )
     {
         var zoneNumber = (int)Math.Round( record[0] );
         var parkingSpots = zoneNumber;
         if ( this.RequireParking && parkingSpots <= 0 )
         {
             // skip zones without parking spots
             continue;
         }
         if ( first )
         {
             current = currentRange.Start = zoneNumber;
             first = false;
         }
         else if ( current + 1 != zoneNumber )
         {
             currentRange.Stop = current;
             rangeList.Add( currentRange );
             currentRange.Start = zoneNumber;
         }
         current = zoneNumber;
         CreateChild( zoneNumber, (int)record[1] );
     }
     if ( !first )
     {
         currentRange.Stop = current;
         rangeList.Add( currentRange );
         var set = new RangeSet( rangeList );
         foreach ( var child in this.Children )
         {
             ( child as SubwayAccessStation ).StationRanges = set;
         }
     }
     return true;
 }
Exemplo n.º 45
0
 public void KnownComplete(RangeSet kc)
 {
     lock (_processedLock)
     {
         RangeSet newProcessed = new RangeSet();
         foreach (Range pr in _processed)
         {
             foreach (Range kr in kc)
             {
                 foreach (Range r in pr.Subtract(kr))
                 {
                     newProcessed.Add(r);
                 }
             }
         }
         _processed = newProcessed;
     }
 }
Exemplo n.º 46
0
 public RangeSet ReadSequenceSet()
 {
     int count = ReadUint16()/8;
     if (count == 0)
     {
         return null;
     }
     RangeSet ranges = new RangeSet();
     for (int i = 0; i < count; i++)
     {
         ranges.Add(ReadSequenceNo(), ReadSequenceNo());
     }
     return ranges;
 }
Exemplo n.º 47
0
 public void MessageAccept(RangeSet Transfers, params Option[] options) {
     Invoke(new MessageAccept(Transfers, options));
 }
Exemplo n.º 48
0
 public void SessionConfirmed(RangeSet Commands, List<Object> Fragments, params Option[] options) {
     Invoke(new SessionConfirmed(Commands, Fragments, options));
 }
Exemplo n.º 49
0
 public void MessageRelease(RangeSet Transfers, params Option[] options) {
     Invoke(new MessageRelease(Transfers, options));
 }
Exemplo n.º 50
0
 public void SessionGap(RangeSet Commands, params Option[] options) {
     Invoke(new SessionGap(Commands, options));
 }
Exemplo n.º 51
0
 internal static int[] GetStationZones(RangeSet stationRanges, float[] capacity, IZone[] zones)
 {
     List<int> validStationIndexes = new List<int>();
     for (int i = 0; i < zones.Length; i++)
     {
         if (capacity[i] > 0 && stationRanges.Contains(zones[i].ZoneNumber))
         {
             validStationIndexes.Add(i);
         }
     }
     return validStationIndexes.ToArray();
 }
Exemplo n.º 52
0
 public void MessageReject(RangeSet Transfers, MessageRejectCode Code, String Text, params Option[] options) {
     Invoke(new MessageReject(Transfers, Code, Text, options));
 }
Exemplo n.º 53
0
        internal RangeSet ToRangeSet()
        {
            RangeSet rs = new RangeSet();
            if (this.FieldItemIndexPage1 != null) rs.FieldItemIndexPage1 = this.FieldItemIndexPage1.Value;
            if (this.FieldItemIndexPage2 != null) rs.FieldItemIndexPage2 = this.FieldItemIndexPage2.Value;
            if (this.FieldItemIndexPage3 != null) rs.FieldItemIndexPage3 = this.FieldItemIndexPage3.Value;
            if (this.FieldItemIndexPage4 != null) rs.FieldItemIndexPage4 = this.FieldItemIndexPage4.Value;
            if (this.Reference != null && this.Reference.Length > 0) rs.Reference = this.Reference;
            if (this.Name != null && this.Name.Length > 0) rs.Name = this.Name;
            if (this.Sheet != null && this.Sheet.Length > 0) rs.Sheet = this.Sheet;
            if (this.Id != null && this.Id.Length > 0) rs.Id = this.Id;

            return rs;
        }
Exemplo n.º 54
0
 public IFuture MessageAcquire(RangeSet Transfers, params Option[] options) {
     return Invoke(new MessageAcquire(Transfers, options), new ResultFuture());
 }
Exemplo n.º 55
0
		public void MessageTransfer(IMessage msg) {
			MSDecoder decoder = new MSDecoder() ;
			decoder.Init(msg.Body) ;
			RangeSet rangeSet = new RangeSet() ;
			rangeSet.Add(msg.Id) ;
			char opcode = 'x' ;
			long seq = -1 ;
			while (this.CheckHeader(decoder, out opcode, out seq)) {
				//log.Debug("Message recieved with opcode " + opcode + " and sequence " + seq) ;
				//log.Debug(System.Text.Encoding.UTF8.GetString(msg.Body.ToArray())) ;
				switch (opcode) {
					case 'b':
						consoleSession.HandleBrokerResponse(this, decoder, seq) ;
						break ;
					case 'p':
						consoleSession.HandlePackageIndicator(this, decoder, seq) ;
						break ;		
					case 'z':
						consoleSession.HandleCommandComplete(this, decoder, seq) ;
						break ;		
					case 'q':
						consoleSession.HandleClassIndicator(this, decoder, seq) ;
						break ;			
					case 'm':
						consoleSession.HandleMethodResponse(this, decoder, seq) ;
						break ;							
					case 'h':
						consoleSession.HandleHeartbeatIndicator(this, decoder, seq, msg) ;
						break ;						
					case 'e':
						consoleSession.HandleEventIndicator(this, decoder, seq) ;
						break ;							
					case 's':
						consoleSession.HandleSchemaResponse(this, decoder, seq) ;
						break ;			
					case 'c':
						consoleSession.HandleContentIndicator(this, decoder, seq, true, false) ;
						break ;	
					case 'i':
						consoleSession.HandleContentIndicator(this, decoder, seq, false, true) ;
						break ;		
					case 'g':
						consoleSession.HandleContentIndicator(this, decoder, seq, true, true) ;
						break ;			
					default:
						log.Error("Invalid message type recieved with opcode " + opcode) ;
						break ;
				}		
			} 
			lock (lockObject) {
				outSession.MessageAccept(rangeSet) ;
			}
		}
Exemplo n.º 56
0
 public void MessageTransfer(IMessage m)
 {            
     foreach (int i in _topics)
     {
         if (_excel.TopicMessages.ContainsKey(i))
         {
             _excel.TopicMessages[i] = m;
         }
     }
     // ack this message 
     RangeSet rs = new RangeSet();
     rs.Add(m.Id);
     _excel.Session.MessageAccept(rs);        
     _excel.OnMessage.UpdateNotify();
 }
Exemplo n.º 57
0
 internal void Load(RangeSet stationRanges, RangeSet spatialZones, SparseArray<float> capacity, int[] closestStation)
 {
     LoadCapacityFactors();
     CalculateUtilities(stationRanges, spatialZones, capacity.GetFlatData(), closestStation);
 }
Exemplo n.º 58
0
 private double processRate(int size, string queue)
 {
     CircularBuffer<IMessage> buffer = new CircularBuffer<IMessage>(100);
     IMessageListener listener = new SyncListener(buffer);
     string localQueue = "queue-" + UUID.RandomUuid();
     Session.QueueDeclare(localQueue, null, null, Option.AUTO_DELETE);
     Session.ExchangeBind(localQueue, "amq.direct", queue);
     Session.AttachMessageListener(listener, localQueue);
     Session.MessageSubscribe(localQueue);
     double rate = 0;
     RangeSet range = new RangeSet();
     for (int i = 0; i < size; ++i)
     {
         IMessage m = buffer.Dequeue();
         range.Add(m.Id);
         BinaryReader reader = new BinaryReader(m.Body, Encoding.UTF8);
         byte[] body = new byte[m.Body.Length - m.Body.Position];
         reader.Read(body, 0, body.Length);
         rate += BitConverter.ToDouble(body,0);
     }
     Session.MessageAccept(range);
     return rate;
 }
Exemplo n.º 59
0
 public IEnumerable<SparseTwinIndex<float>> Distribute(IEnumerable<SparseArray<float>> productions, IEnumerable<SparseArray<float>> attractions, IEnumerable<IDemographicCategory> category)
 {
     if ( this.SaveDistributionSeries != null )
     {
         this.SaveDistributionSeries.Reset();
     }
     IDemographicCategory cat = null;
     var ret = this.Root.ZoneSystem.ZoneArray.CreateSquareTwinArray<float>();
     var linkages = this.Root.ZoneSystem.ZoneArray.CreateSquareTwinArray<float>();
     var catEnum = category.GetEnumerator();
     var productionEnum = productions.GetEnumerator();
     int catIndex = 0;
     var rangeSet = new RangeSet( new int[] { 0 } );
     // just multiply by the number of occupations in v2 since this is a throw away module
     this.NumberOfGenerations = NumberOfCategoriesPerOccupation * 4;
     while ( catEnum.MoveNext() & productionEnum.MoveNext() )
     {
         cat = catEnum.Current;
         var ageRate = productionEnum.Current;
         Parallel.Invoke(
             () => SetupMobilityInformation( catIndex ),
             () => LoadLinkages( catIndex, linkages.GetFlatData() )
         );
         foreach ( var mobilitySet in this.MobilityRanges )
         {
             for ( int mobility = mobilitySet.Start; mobility <= mobilitySet.Stop; mobility++ )
             {
                 var realCat = cat as DemographicCategoryGeneration;
                 if ( realCat == null )
                 {
                     throw new XTMFRuntimeException( "In '" + this.Name + "' it is required that all generates be of the type DemographicCategoryGeneration!" );
                 }
                 rangeSet[0] = new Range() { Start = mobility, Stop = mobility };
                 realCat.Mobility = rangeSet;
                 DistributePopulation( realCat, ageRate.GetFlatData(), linkages.GetFlatData(), catIndex, ret.GetFlatData() );
                 Task save = null;
                 if ( this.SaveDistributionSeries != null )
                 {
                     save = Task.Factory.StartNew( () =>
                         {
                             SaveDistribution( ret, this.Root.ZoneSystem.ZoneArray.GetFlatData(), catIndex );
                         } );
                 }
                 yield return ret;
                 if ( save != null )
                 {
                     save.Wait();
                 }
                 catIndex++;
             }
         }
     }
     // Free up our memory
     this.MobilityCache = null;
     this.UnloadRates();
 }