Example #1
0
        private RuleBuilder InitEnvironment(Dictionary<String, EnvironmentDetails> environment)
        {
            RuleBuilder rb = new RuleBuilder(_iptables, "/var/x4b/bin/bpf/nfbpf_compile", _ruleSets);
            var mappings = environment.AsParallel().Select((e) =>
            {
                if (e.Value.Language == "bash")
                {
                    e.Value.Command = rb.ExecuteBash(e.Value.Command);
                }
                else if (e.Value.Language == "bpf")
                {
                    e.Value.Command = rb.CompileBpf("RAW", e.Value.Command);
                }
                else if (e.Value.Language == "bpfl4")
                {
                    e.Value.Command = rb.CompileBpf("RAW_TRANSPORT", e.Value.Command);
                }
                else if (e.Value.Language != "text")
                {
                    throw new Exception("Invalid language: " + e.Value.Language);
                }
                return e;
            });

            foreach (var e in mappings)
            {
                rb.DefineMapping(e.Key, e.Value.Command, e.Value.Default);
            }
            return rb;
        }
Example #2
0
        static void PopulatePrimeFactors()
        {
            primeMap = new Dictionary<int, SortedSet<int>>();
            var primes = MathUtils.ESieve(3200);
            for (int i = 2; i < 10000000; i++)
            {
                primeMap.Add(i, new SortedSet<int>());

            }

            primeMap.AsParallel().ForAll(x =>
            {
                for (int j = 0; j < primes.Length; j++)
                {
                    if (primes[j] * primes[j] > x.Key)
                        break;
                    if (x.Key % primes[j] == 0)
                    {
                        x.Value.Add(primes[j]);
                        var i = x.Key/primes[j];
                        if(MathUtils.IsPrime(i))
                        {
                            x.Value.Add(i);
                        }
                    }
                }

            });


        }
        public static void Main(string[] args)
        {
            Dictionary<Address, UInt64> balances = new Dictionary<Address, UInt64>();

            UnspentTxOutList utxo = new UnspentTxOutList();
            Hash lastBlockHash;
            using (FileStream fs = new FileStream(@"D:\utxo.dat", FileMode.Open))
            {
                BinaryReader br = new BinaryReader(fs);
                lastBlockHash = br.ReadBytes(32);
                utxo = UnspentTxOutList.FromStream(fs);
            }

            foreach (KeyValuePair<TxOutId, TxOut> txo in utxo)
            {
                Address a = Address.FromScript(txo.Value.scriptPubKey);
                if (a == null)
                    continue;
                if (!balances.ContainsKey(a))
                    balances.Add(a, 0);
                balances[a] += txo.Value.value;
            }

            foreach (KeyValuePair<Address, UInt64> bal in balances.AsParallel().OrderByDescending(x=>x.Value))
            {
                Console.WriteLine(bal.Key + "\t" + bal.Value);
            }
        }
            /// <summary>
            /// Searches for duplicates or similar images and groups them into a DuplicateImage-Instance.
            /// </summary>
            /// <param name="path">The directory to search through.</param>
            protected override IEnumerable<DuplicateImages> FindDuplicates(Dictionary<FileInfo, ulong> images)
            {
                //Find identical images
                //todo: AsParallel() safe to use here?
                var __groups = images.AsParallel().GroupBy(i => i.Value).Where(grp => grp.Count() > 1).ToList();

                return __groups.Select(group => new DuplicateImages(group.Select(e => new DuplicateImage(e.Key, e.Value))));
            }
Example #5
0
        /// <summary>
        /// 请求数据
        /// </summary>
        /// <param name="BaseUrl">请求根地址</param>
        /// <param name="Params">请求参数</param>
        /// <param name="Cookie">设置的Cookie</param>
        /// <param name="Header">设置的Header</param>
        /// <param name="Method">请求方式</param>
        /// <param name="ContentType">设置请求的ContentType</param>
        /// <param name="TimeOut">设置超时时间,默认10分钟</param>
        /// <param name="Encoding">设置请求编码</param>
        /// <param name="Accept">设置请求Accept</param>
        /// <param name="AllowReadStreamBuffering">设置是否缓存结果</param>
        /// <param name="Proxy">设置请求代理</param>
        /// <returns></returns>
        public async Task<HttpWebResponse> HttpRequestUrl(string BaseUrl, Dictionary<string, string> Params, Dictionary<string, string> Cookie = null, Dictionary<string, string> Header = null, string Method = "", string ContentType = "", int TimeOut = 60000, Encoding Encoding = null, string Accept = "", bool AllowReadStreamBuffering = false, IWebProxy Proxy = null)
        {
            try
            {
                Lazy<List<string>> paramsList = new Lazy<List<string>>();
                BaseUrl = BaseUrl.ToUpperInvariant().Contains("HTTP|HTTPS") ? BaseUrl : "http://" + BaseUrl;
                string param = string.Empty;
                if (Params != null && Params.Count > 0)
                {
                    Params.AsParallel().ForAll(p => paramsList.Value.Add("" + p.Key + "=" + p.Value));
                    param = string.Join("&", paramsList.Value);
                }
                if (Method != "POST")
                {
                    BaseUrl += "?" + param;
                }
                HttpWebRequest request = WebRequest.CreateHttp(BaseUrl);
                request.Method = Method;
                request.ContentType = ContentType;
                request.ContinueTimeout = TimeOut;
                request.Accept = Accept;
                request.AllowReadStreamBuffering = AllowReadStreamBuffering;
                if (Cookie != null && Cookie.Count > 0)
                {
                    Cookie.AsParallel().ForAll(p => request.CookieContainer.Add(new Uri(BaseUrl), new System.Net.Cookie(p.Key, p.Value)));
                }
                request.Proxy = Proxy;
                if (Header != null && Header.Count > 0)
                {
                    Header.AsParallel().ForAll(p => request.Headers[p.Key] = p.Value);
                }
                if (request.Method.ToUpperInvariant() == "POST")
                {
                    byte[] requestBytes = Encoding == null ? System.Text.Encoding.ASCII.GetBytes(param) : Encoding.GetBytes(param);
                    Stream requestStream = await request.GetRequestStreamAsync();
                    requestStream.Write(requestBytes, 0, requestBytes.Length);
                    requestStream.Dispose();
                }
                HttpWebResponse respose = (HttpWebResponse)await request.GetResponseAsync();
                return respose;
            }
            catch (Exception)
            {
                return null;
            }

        }
Example #6
0
        private void ProcessingCompactByYear()
        {
            this.Invoke((MethodInvoker)delegate
            {
                this.Cursor = Cursors.WaitCursor;
            });
            var sw = Stopwatch.StartNew();
            DirectoryInfo dInfo = new DirectoryInfo(Path.GetFullPath(txtFolder.Text));
            fileArticles = dInfo.GetFiles(txtArticlesPrefix.Text + "*", SearchOption.TopDirectoryOnly).ToList();
            fileInproceedings = dInfo.GetFiles(txtInproceedingsPrefix.Text + "*", SearchOption.TopDirectoryOnly).ToList();
            fileProceedings = dInfo.GetFiles(txtProceedingsPrefix.Text + "*", SearchOption.TopDirectoryOnly).ToList();
            filePhdthesis = dInfo.GetFiles(txtPhdThesisPrefix.Text + "*", SearchOption.TopDirectoryOnly).ToList();
            fileAuthors = dInfo.GetFiles(txtAuthorPrefix.Text + "*", SearchOption.TopDirectoryOnly).ToList();
            fileConferences = dInfo.GetFiles(txtConferencePrefix.Text + "*", SearchOption.TopDirectoryOnly).ToList();
            GC.Collect();
            GC.WaitForPendingFinalizers();

            ParseCompactAuthors();
            ParseCompactInproceedings(Convert.ToInt32(txtYearTo.Text));
            ParseConferences();
            Dictionary<int, compactInproceedingsDBLP> inproCals = diccompactInproceedings.Where(
                x => x.Value.Year <= Convert.ToInt32(txtYearTo.Text)).ToDictionary(x => x.Key, x => x.Value);
            diccompactInproceedings.Clear();

            Dictionary<int, compactAuthorDBLP> authorCals = new Dictionary<int, compactAuthorDBLP>();
            Dictionary<string, ConferenceDBLP> confCals = new Dictionary<string, ConferenceDBLP>();

            Parallel.ForEach(inproCals, inproceeding =>
            {
                Parallel.ForEach(inproceeding.Value.Authors, ai =>
                {

                    compactAuthorDBLP authorDblp = diccompactAuthors[ai];
                    lock (lock_acal)
                    {
                        if (!authorCals.Keys.Contains(ai))
                        {
                            authorCals.Add(ai, diccompactAuthors[ai]);
                            authorCals[ai].Count = 1;
                            authorCals[ai].Papers.Clear();
                            authorCals[ai].Papers.Add(inproceeding.Key);
                            authorCals[ai].CurrentValue = 1000;
                            authorCals[ai].OldValue = 1000;
                        }
                        else
                        {
                            authorCals[ai].Count++;
                            authorCals[ai].Papers.Add(inproceeding.Key);
                        }
                    }
                });
                lock (lock_ccal)
                {
                    if (!confCals.Keys.Contains(inproceeding.Value.Conference))
                    {
                        confCals.Add(inproceeding.Value.Conference, dicStringConferences[inproceeding.Value.Conference]);
                        confCals[inproceeding.Value.Conference].CountInproceedings = 1;
                        confCals[inproceeding.Value.Conference].InproceedingsID = inproceeding.Value.Key.ToString();
                    }
                    else
                    {
                        confCals[inproceeding.Value.Conference].CountInproceedings++;
                        confCals[inproceeding.Value.Conference].InproceedingsID += "|" + inproceeding.Value.Key.ToString();
                    }
                }
            });
            dicAuthors.Clear();
            dicStringConferences.Clear();
            dicConferences.Clear();
            this.Invoke((MethodInvoker)delegate
            {
                txtMsgLog.Text = "Begin...";
                Cursor = Cursors.WaitCursor;
            });
            double sumAs1 = 0, sumIs1 = 0, sumCs = 0, sumIs2 = 0, sumAs2 = 0, min, ratio, max, max_old = 0, sum0;
            max = 1000;

            ratio = 1;
            sum0 = authorCals.AsParallel().Sum(x => x.Value.CurrentValue);

            while (max > Convert.ToDouble(txtMinDelta.Text))
            {
                sw.Start();

                sumAs1 = authorCals.AsParallel().Sum(x => x.Value.CurrentValue);
                inproCals.AsParallel().ForAll(x => x.Value.SetValueFromAuthors(authorCals));
                sumIs1 = inproCals.AsParallel().Sum(x => x.Value.CurrentValue);
                confCals.AsParallel().ForAll(x => x.Value.SetValueFromInproceedings(inproCals));
                sumCs = confCals.AsParallel().Sum(x => x.Value.CurrentValue);
                inproCals.AsParallel().ForAll(x => x.Value.SetValueFromConferences(confCals));
                sumIs2 = inproCals.AsParallel().Sum(x => x.Value.CurrentValue);
                authorCals.AsParallel().ForAll(x => x.Value.SetValueFromInproceedings(inproCals));
                sumAs2 = authorCals.AsParallel().Sum(x => x.Value.CurrentValue);
                ratio = sum0 / sumAs2;
                min = authorCals.AsParallel().Min(x => Math.Abs(x.Value.CurrentValue - x.Value.OldValue));
                max = authorCals.AsParallel().Max(x => Math.Abs(x.Value.CurrentValue - x.Value.OldValue));
                if (Math.Abs(ratio - 1) > 0.009)
                {
                    authorCals.AsParallel().ForAll(x => x.Value.OldValue = x.Value.CurrentValue);
                    //authorCals.AsParallel().ForAll(x => x.Value.CurrentValue = x.Value.CurrentValue * ratio);
                    authorCals.AsParallel().ForAll(x => x.Value.CurrentValue = x.Value.CurrentValue * ratio * 0.8 + 200);
                }
                if (Math.Abs(max_old - max) > 50)
                {
                    max_old = max;
                    WriteResultToFile(max, txtResultPrefix.Text, authorCals, inproCals, confCals);
                }
                sw.Stop();
                if (this.IsHandleCreated)
                {
                    this.Invoke((MethodInvoker)delegate
                    {
                        txtMsgLog.Text += string.Format("Total: {0}:{1}:{2} Max={3} Ratio={12}(Sum AS1:{4}-SumIS1:{5}-sumCs:{6}-sumIs2:{7}- sumAs2{8} (A:{9}-P:{10}-C{11})\r\n",
                            sw.Elapsed.Hours, sw.Elapsed.Minutes, sw.Elapsed.Seconds, max, sumAs1, sumIs1, sumCs, sumIs2, sumAs2, authorCals.Count, inproCals.Count, confCals.Count, ratio);
                        txtMsgLog.SelectionStart = txtMsgLog.Text.Length;
                        txtMsgLog.ScrollToCaret();
                    });
                }
                else
                {
                    Thread.CurrentThread.Abort();
                }
            }
            WriteResultToFile(max, txtResultPrefix.Text, authorCals, inproCals, confCals);
            File.WriteAllText(Path.GetFullPath(txtFolder.Text + "\\" + txtResultPrefix.Text + "=" + txtYearTo.Text + "-" + ((int)max).ToString() +
                    "-log.csv"), txtMsgLog.Text);
            this.Invoke((MethodInvoker)delegate
            {
                txtMsgLog.Text += "Done!!!";
                this.Cursor = Cursors.Default;
            });
        }
Example #7
0
        private void ProcessingByYear()
        {
            Dictionary<int, InproceedingsDBLP> inproCals = dicInproceedings.Where(
                x =>
                {
                    int year;
                    if (int.TryParse(x.Value.Year, out year))
                        return Convert.ToInt32(x.Value.Year) <= Convert.ToInt32(txtYearTo.Text) &&
                            Convert.ToInt32(x.Value.Year) >= Convert.ToInt32(txtYearFrom.Text);
                    else
                        return false;
                }).ToDictionary(x => x.Key, x => x.Value);
            dicInproceedings.Clear();

            Dictionary<int, AuthorDBLP> authorCals = new Dictionary<int, AuthorDBLP>();
            Dictionary<string, ConferenceDBLP> confCals = new Dictionary<string, ConferenceDBLP>();

            Parallel.ForEach(inproCals, inproceeding =>
            {
                string[] AuthorsIDs = inproceeding.Value.AuthorsID.Split('|');
                Parallel.For(0, AuthorsIDs.Length, ai =>
                {
                    int akey = 0;
                    if (int.TryParse(AuthorsIDs[ai], out akey))
                    {
                        AuthorDBLP authorDblp = dicAuthors[akey];
                        lock (lock_acal)
                        {
                            if (!authorCals.Keys.Contains(akey))
                            {
                                authorCals.Add(akey, dicAuthors[akey]);
                                authorCals[akey].CountInproceedings = 1;
                                authorCals[akey].InproceedingsID = inproceeding.Value.Id.ToString();
                                authorCals[akey].CurrentValue = 1000;
                                authorCals[akey].OldValue = 1000;
                            }
                            else
                            {
                                authorCals[akey].CountInproceedings++;
                                authorCals[akey].InproceedingsID += "|" + inproceeding.Value.Id.ToString();
                            }
                        }
                    }
                });
                lock (lock_ccal)
                {
                    string[] keys = inproceeding.Value.Key.Split('/');
                    string conf_key = keys.Length > 1 ? keys[1] : keys[0];
                    if (!confCals.Keys.Contains(conf_key))
                    {
                        confCals.Add(conf_key, dicStringConferences[conf_key]);
                        confCals[conf_key].CountInproceedings = 1;
                        confCals[conf_key].InproceedingsID = inproceeding.Value.Id.ToString();
                    }
                    else
                    {
                        confCals[conf_key].CountInproceedings++;
                        confCals[conf_key].InproceedingsID += "|" + inproceeding.Value.Id.ToString();
                    }
                }
            });
            dicAuthors.Clear();
            dicStringConferences.Clear();
            dicConferences.Clear();
            this.Invoke((MethodInvoker)delegate
            {
                txtMsgLog.Text = "Begin...";
                Cursor = Cursors.WaitCursor;
            });
            double sumAs1 = 0, sumIs1 = 0, sumCs = 0, sumIs2 = 0, sumAs2 = 0, min, ratio, max, max_old = 0, sum0;
            max = 1000;
            var sw = Stopwatch.StartNew();
            ratio = 1;
            sum0 = authorCals.AsParallel().Sum(x => x.Value.CurrentValue);

            while (max > Convert.ToDouble(txtMinDelta.Text))
            {
                sw.Start();

                sumAs1 = authorCals.AsParallel().Sum(x => x.Value.CurrentValue);
                inproCals.AsParallel().ForAll(x => x.Value.SetValueFromAuthors(authorCals));
                sumIs1 = inproCals.AsParallel().Sum(x => x.Value.CurrentValue);
                confCals.AsParallel().ForAll(x => x.Value.SetValueFromInproceedings(inproCals));
                sumCs = confCals.AsParallel().Sum(x => x.Value.CurrentValue);
                inproCals.AsParallel().ForAll(x => x.Value.SetValueFromConferences(confCals));
                sumIs2 = inproCals.AsParallel().Sum(x => x.Value.CurrentValue);
                authorCals.AsParallel().ForAll(x => x.Value.SetValueFromInproceedings(inproCals));
                sumAs2 = authorCals.AsParallel().Sum(x => x.Value.CurrentValue);
                ratio = sum0 / sumAs2;
                min = authorCals.AsParallel().Min(x => Math.Abs(x.Value.CurrentValue - x.Value.OldValue));
                max = authorCals.AsParallel().Max(x => Math.Abs(x.Value.CurrentValue - x.Value.OldValue));
                if ((ratio - 1) > 0.009)
                {
                    authorCals.AsParallel().ForAll(x => x.Value.OldValue = x.Value.CurrentValue);
                    authorCals.AsParallel().ForAll(x => x.Value.CurrentValue = x.Value.CurrentValue * ratio);
                    //          authorCals.AsParallel().ForAll(x => x.Value.CurrentValue = x.Value.CurrentValue * ratio * 0.8 + 200);
                }
                if (Math.Abs(max_old - max) > 50)
                {
                    max_old = max;
                    WriteResultToFile(max, txtResultPrefix.Text, authorCals, inproCals, confCals);
                }
                sw.Stop();
                this.Invoke((MethodInvoker)delegate
                {
                    txtMsgLog.Text += string.Format("Total: {0}:{1}:{2} Max={3} Ratio={12}(Sum AS1:{4}-SumIS1:{5}-sumCs:{6}-sumIs2:{7}- sumAs2{8} (A:{9}-P:{10}-C{11})\r\n",
                        sw.Elapsed.Hours, sw.Elapsed.Minutes, sw.Elapsed.Seconds, max, sumAs1, sumIs1, sumCs, sumIs2, sumAs2, authorCals.Count, inproCals.Count, confCals.Count, ratio);
                    txtMsgLog.SelectionStart = txtMsgLog.Text.Length;
                    txtMsgLog.ScrollToCaret();
                });
            }
            WriteResultToFile(max, txtResultPrefix.Text, authorCals, inproCals, confCals);
            File.WriteAllText(Path.GetFullPath(txtFolder.Text + "\\" + txtResultPrefix.Text + "=" + txtYearTo.Text + "-" + ((int)max).ToString() +
                    "-log.csv"), txtMsgLog.Text);
            this.Invoke((MethodInvoker)delegate
            {
                txtMsgLog.Text += "Done!!!";
                this.Cursor = Cursors.Default;
            });
        }
Example #8
0
        public static string GenerateCanonicalizedResource(Uri uri)
        {
            StringBuilder sb = new StringBuilder("/");

            sb.AppendFormat("{0:S}", GetAccountNameFromUri(uri));

            string[] tokens = uri.AbsolutePath.Split(new char[] { '/' });
            bool fAdded = false;

            for (int i = 0; i < tokens.Length; i++)
            {
                if (!string.IsNullOrEmpty(tokens[i]))
                {
                    sb.AppendFormat("/{0:S}", tokens[i]);
                    fAdded = true;
                }
            }
            if (!fAdded)
                sb.Append("/");

            Dictionary<string, List<string>> dParams = new Dictionary<string, List<string>>();

            if (!string.IsNullOrEmpty(uri.Query))
            {
                foreach (string sParam in uri.Query.Substring(1).Split(new char[] { '&' }))
                {
                    int idx = sParam.IndexOf('=');
                    string sKey = Uri.UnescapeDataString(sParam.Substring(0, idx)).ToLowerInvariant();
                    string sValue = Uri.UnescapeDataString(sParam.Substring(idx + 1, sParam.Length - idx - 1));

                    if (!dParams.ContainsKey(sKey))
                    {
                        if ((sKey == "nextpartitionkey") || (sKey == "nextrowkey"))
                            continue;
                        dParams[sKey] = new List<string>();
                    }
                    dParams[sKey].Add(sValue);
                }
            }

            KeyValuePair<string, List<string>>[] kvpairs = dParams.AsParallel().OrderBy(item => item.Key).ToArray();

            if (kvpairs.Length > 0)
                sb.Append("\n");

            for (int iPair = 0; iPair < kvpairs.Length; iPair++)
            {
                StringBuilder sbParam = new StringBuilder();
                sbParam.AppendFormat("{0:S}:", kvpairs[iPair].Key);
                string[] vals = kvpairs[iPair].Value.AsParallel().OrderBy(item => item).ToArray();
                for (int i = 0; i < vals.Length; i++)
                {
                    sbParam.Append(vals[i]);
                    if (i + 1 < vals.Length)
                        sbParam.Append(",");
                }

                if ((iPair + 1) < kvpairs.Length)
                    sbParam.Append("\n");

                sb.Append(sbParam);
            }

            //System.Diagnostics.Trace.TraceInformation("GenerateCanonicalizedResource: Generated string: " + sb.ToString());
            return sb.ToString();//.Replace("%82", new string(new char[] { (char)0xEF, (char)0xBF, (char)0xBD }));
        }
        private void SaveRunningSprintData(Dictionary<string, double> resultOfAnalysis, Dictionary<string, uint> sectionPositions, uint columnPosition)
        {
            if (!resultOfAnalysis.Any(r => r.Key.Equals("--Indefinido--")))
                resultOfAnalysis.Add("--Indefinido--", 0);

            foreach (var keyValue in resultOfAnalysis.AsParallel())
            {
                SetSprintValueByResponsible(_worksheet, sectionPositions, columnPosition, keyValue.Key, keyValue.Value);
                Console.WriteLine("\t\t> {0} - {1}", keyValue.Key, keyValue.Value);
            }
        }
Example #10
0
        /// <summary>
        /// Make a step in the game
        /// </summary>
        private void GameStep(ref List<PointULong> alive)
        {
            var aliveNeighbors = new Dictionary<PointULong, int>();

            foreach (var i in _aliveCells.Concat(_aroundAliveCells).AsParallel())
            {
                var nearMeCells = 0;
                foreach (var aliveCell in _aliveCells.AsParallel())
                {
                    if (i.Value.IsNearMe(aliveCell.Value))
                        nearMeCells++;
                }
                //_aliveCells.AsParallel().Count(j => i.Value.IsNearMe(j.Value));
                aliveNeighbors[i.Key] = nearMeCells;
            }

            alive = new List<PointULong>();

            foreach (var i in aliveNeighbors.AsParallel())
            {
                var isAlive = _aliveCells.ContainsKey(i.Key);
                var willBorn = Algorithm.NextState(isAlive, i.Value);

                if (willBorn)
                    alive.Add(i.Key);
            }

            _aliveCells.Clear();
            _aroundAliveCells.Clear();
            SetAlive(alive);
        }