Esempio n. 1
0
        public void STTest1()
        {
            // testing Get/Put semantics
            ST <string, int> st = new ST <string, int>();

            Assert.IsTrue(st.IsEmpty);

            string[] keys = { "to", "be", "or", "not", "to", "be", "is", "quest" };
            for (int i = 0; i < keys.Length; i++)
            {
                st.Put(keys[i], i);
            }
            Assert.IsTrue(!(st.IsEmpty) && (st.Count == 6));

            string key = "not";

            Assert.IsTrue(st.Contains(key));
            st.Delete(key);
            Assert.IsFalse(st.Contains(key));
            Assert.IsNull(st.Get(key));

            object value = st.Get("is");

            Assert.AreEqual(6, value);

            value = st.Get("world");
            Assert.IsNull(value);

            int dummy = (int)st.Get("hello"); // generate null exception
        }
Esempio n. 2
0
    static void Main()
    {
        var h  = Read();
        var n  = h[0];
        var qs = new int[h[1]].Select(_ => Read()).Select(v => (v[0], v[1], v[2])).ToArray();

        var a   = Enumerable.Range(0, n + 2).ToArray();
        var st  = new ST(n + 2);
        var q   = new Queue <int>();
        var set = new HashSet <int>();

        void Reverse(int x)
        {
            Swap(a, x, x + 1);
            for (int i = -1; i < 2; i++)
            {
                st.Set(x + i, a[x + i] < a[x + i + 1] ? 0 : 1);
            }
        }

        foreach (var(t, x, y) in qs)
        {
            if (t == 1)
            {
                Reverse(x);
            }
            else
            {
                foreach (var i in st.GetIndexes(x, y - 1))
                {
                    q.Enqueue(i);
                    set.Add(i);
                }

                while (q.TryDequeue(out var i))
                {
                    Reverse(i);
                    set.Remove(i);
                    if (x <= i - 1 && !set.Contains(i - 1) && st.Get(i - 1) > 0)
                    {
                        q.Enqueue(i - 1);
                        set.Add(i - 1);
                    }
                    if (i + 1 < y && !set.Contains(i + 1) && st.Get(i + 1) > 0)
                    {
                        q.Enqueue(i + 1);
                        set.Add(i + 1);
                    }
                }
            }
        }

        Console.WriteLine(string.Join(" ", a.Skip(1).Take(n)));
    }
            // ------------------------------------------------------
            //
            // Constructors
            //
            // ------------------------------------------------------

            #region Constructors

            internal RebarBandItem(IntPtr hwnd, ProxyFragment parent, int item)
                : base(hwnd, parent, item)
            {
                // Set the strings to return properly the properties.
                _sType      = ST.Get(STID.LocalizedControlTypeRebarBand);
                _fIsContent = false;
            }
Esempio n. 4
0
        // 官方解答:https://algs4.cs.princeton.edu/31elementary/GPA.java.html
        // ST.java:https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/ST.java.html
        static void Main(string[] args)
        {
            var st = new ST <string, double>();

            st.Put("A+", 4.33);
            st.Put("A", 4.00);
            st.Put("A-", 3.67);
            st.Put("B+", 3.33);
            st.Put("B", 3.00);
            st.Put("B-", 2.67);
            st.Put("C+", 2.33);
            st.Put("C", 2.00);
            st.Put("C-", 1.67);
            st.Put("D", 1.00);
            st.Put("F", 0.00);

            double total = 0;
            var    gpas  = Console.ReadLine().Split(' ');

            foreach (var gpa in gpas)
            {
                total += st.Get(gpa);
            }
            total /= gpas.Length;
            Console.WriteLine("GPA=" + total);
        }
        // Process all the Logical and Raw Element Properties
        internal override object GetElementProperty(AutomationProperty idProp)
        {
            // Normal Listview items do not have a concept of an AccessKey.  But
            // the Listview items in the Start Menu does.  This information is
            // in the IAccessible interface implemented by the Shell team.
            if (idProp == AutomationElement.AccessKeyProperty)
            {
                // The IAccessible should be valid here since it is the cached value in ProxySimple.
                System.Diagnostics.Debug.Assert(AccessibleObject != null, "Failed to get a valid IAccessible!");

                try
                {
                    string key = AccessibleObject.get_accKeyboardShortcut(_item + 1);
                    if (!string.IsNullOrEmpty(key))
                    {
                        return(ST.Get(STID.KeyAlt) + "+" + key);
                    }
                }
                catch (Exception e)
                {
                    if (Misc.IsCriticalException(e))
                    {
                        throw;
                    }
                }
            }
            else if (idProp == AutomationElement.HasKeyboardFocusProperty)
            {
                return(IsFocused());
            }


            return(base.GetElementProperty(idProp));
        }
        // ------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------

        #region Constructors

        internal ByteEditBoxOverride(IntPtr hwnd, int position) :
            base(hwnd, null, 0)
        {
            _sType                = ST.Get(STID.LocalizedControlTypeOctet);
            _position             = position;
            _sAutomationId        = "Octet " + position.ToString(CultureInfo.CurrentCulture); // This string is a non-localizable string
            _fIsKeyboardFocusable = true;
        }
        // ------------------------------------------------------
        //
        // Constructors
        //
        // ------------------------------------------------------

        #region Constructors

        WindowsRebar(IntPtr hwnd, ProxyFragment parent, int item)
            : base(hwnd, parent, item)
        {
            _sType      = ST.Get(STID.LocalizedControlTypeRebar);
            _fIsContent = false;

            // support for events
            _createOnEvent = new WinEventTracker.ProxyRaiseEvents(RaiseEvents);
        }
Esempio n. 8
0
        /// <summary>
        /// Initializes a graph from a file using the specified delimiter.
        /// Each line in the file contains
        /// the name of a vertex, followed by a list of the names
        /// of the vertices adjacent to that vertex, separated by the delimiter.
        /// </summary>
        /// <param name="lines">array of string lines</param>
        /// <param name="delimiter">delimiter the delimiter between fields</param>
        public SymbolGraph(IList <string> lines, char delimiter)
        {
            _st = new ST <string, Integer>();

            // First pass builds the index by reading strings to associate
            // distinct strings with an index
            // while (in.hasNextLine()) {
            foreach (var line in lines)
            {
                var a = line.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var word in a)
                {
                    if (!_st.Contains(word))
                    {
                        _st.Put(word, _st.Size());
                    }
                }
            }

            Console.WriteLine("Done reading");

            // inverted index to get string keys in an aray
            _keys = new string[_st.Size()];
            foreach (var name in _st.Keys())
            {
                _keys[_st.Get(name)] = name;
            }

            // second pass builds the graph by connecting first vertex on each
            // line to all others
            G = new Graph(_st.Size());
            foreach (var line in lines)
            {
                var a = line.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
                int v = _st.Get(a[0]);
                for (var i = 1; i < a.Length; i++)
                {
                    int w = _st.Get(a[i]);
                    G.AddEdge(v, w);
                }
            }
        }
        // ------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------

        #region Constructors

        WindowsIPAddress(IntPtr hwnd, ProxyFragment parent, int item)
            : base(hwnd, parent, item)
        {
            // IP Address control itself is custom so need to also return LocalizedControlType property
            _cControlType         = ControlType.Custom;
            _sType                = ST.Get(STID.LocalizedControlTypeIPAddress);;
            _fIsKeyboardFocusable = true;

            // support for events
            _createOnEvent = new WinEventTracker.ProxyRaiseEvents(RaiseEvents);
        }
Esempio n. 10
0
        private readonly ST<string, Integer> _st; // string -> index

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a graph from a file using the specified delimiter.
        /// Each line in the file contains
        /// the name of a vertex, followed by a list of the names
        /// of the vertices adjacent to that vertex, separated by the delimiter.
        /// </summary>
        /// <param name="lines">array of string lines</param>
        /// <param name="delimiter">delimiter the delimiter between fields</param>
        public SymbolGraph(IList<string> lines, char delimiter)
        {
            _st = new ST<string, Integer>();

            // First pass builds the index by reading strings to associate
            // distinct strings with an index
            // while (in.hasNextLine()) {
            foreach (var line in lines)
            {
                var a = line.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var word in a)
                {
                    if (!_st.Contains(word))
                        _st.Put(word, _st.Size());
                }
            }

            Console.WriteLine("Done reading");

            // inverted index to get string keys in an aray
            _keys = new string[_st.Size()];
            foreach (var name in _st.Keys())
            {
                _keys[_st.Get(name)] = name;
            }

            // second pass builds the graph by connecting first vertex on each
            // line to all others
            G = new Graph(_st.Size());
            foreach (var line in lines)
            {
                var a = line.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
                int v = _st.Get(a[0]);
                for (var i = 1; i < a.Length; i++)
                {
                    int w = _st.Get(a[i]);
                    G.AddEdge(v, w);
                }
            }
        }
Esempio n. 11
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - tinyST.txt"); // Prompt
            Console.WriteLine("or quit");        // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyST.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in       = new In($"Files\\Searching\\{fieName}");
            var keyValues = @in.ReadAllLines();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            //var listStrings = words.ToList();


            var st = new ST <string, string>();


            foreach (var keyValue in keyValues)
            {
                var splittedKeyValue = keyValue.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var key   = splittedKeyValue[0];
                var value = splittedKeyValue[1];
                st.Put(key, value);
            }
            // print results
            foreach (var item in st.Keys())
            {
                Console.WriteLine(st.Get(item));
            }
            Console.WriteLine(st.Max());
            Console.WriteLine(st.Min());
            Console.ReadLine();
        }
Esempio n. 12
0
        public void Run()
        {
            Console.WriteLine("Choose file:"); // Prompt
            Console.WriteLine("1 - tinyST.txt"); // Prompt
            Console.WriteLine("or quit"); // Prompt

            var fileNumber = Console.ReadLine();
            var fieName = string.Empty;
            switch (fileNumber)
            {
                case "1":
                    fieName = "tinyST.txt";
                    break;
                case "quit":
                    return;
                default:
                    return;
            }

            var @in = new In($"Files\\Searching\\{fieName}");
            var keyValues = @in.ReadAllLines();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            //var listStrings = words.ToList();

            var st = new ST<string,string>();

            foreach (var keyValue in keyValues)
            {
                var splittedKeyValue = keyValue.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                var key = splittedKeyValue[0];
                var value = splittedKeyValue[1];
                st.Put(key,value);
            }
            // print results
            foreach (var item in st.Keys())
            {
                Console.WriteLine(st.Get(item));
            }
            Console.WriteLine(st.Max());
            Console.WriteLine(st.Min());
            Console.ReadLine();
        }
Esempio n. 13
0
        // ------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------

        #region Constructors

        public WindowsContainer(IntPtr hwnd, ProxyHwnd parent, int item)
            : base(hwnd, parent, item)
        {
            string className = Misc.ProxyGetClassName(hwnd);

            if (!string.IsNullOrEmpty(className))
            {
                if (className.Equals("#32770"))
                {
                    _sType = ST.Get(STID.LocalizedControlTypeDialog);
                }
                else if (className.IndexOf("AfxControlBar", StringComparison.Ordinal) != -1)
                {
                    _sType = ST.Get(STID.LocalizedControlTypeContainer);
                }
            }

            _fIsContent           = IsTopLevelWindow();
            _fIsKeyboardFocusable = true;
        }
Esempio n. 14
0
        // Process all the Logical and Raw Element Properties
        internal override object GetElementProperty(AutomationProperty idProp)
        {
            if (idProp == AutomationElement.AccessKeyProperty)
            {
                // Special handling for forms
                if (!WindowsFormsHelper.IsWindowsFormsControl(_hwnd, ref _windowsForms) && IsStartButton())
                {
                    // Hard coded shortcut for the start button
                    return(ST.Get(STID.KeyCtrl) + " + " + ST.Get(STID.KeyEsc));
                }
                return(Misc.AccessKey(Misc.ProxyGetText(_hwnd)));
            }
            else if (idProp == AutomationElement.IsEnabledProperty)
            {
                if (InShellTray())
                {
                    return(SafeNativeMethods.IsWindowVisible(_hwnd));
                }
            }

            return(base.GetElementProperty(idProp));
        }
        // ------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------

        #region Constructors

        public WindowsGrip(IntPtr hwnd, ProxyHwnd parent, int item)
            : base(hwnd, parent, item)
        {
            _sType         = ST.Get(STID.LocalizedControlTypeGrip);
            _sAutomationId = "Window.Grip"; // This string is a non-localizable string
        }
Esempio n. 16
0
        private string GetTitleBarToolTipTextHitTest()
        {
            NativeMethods.Win32Point pt = new NativeMethods.Win32Point();
            if (!Misc.GetCursorPos(ref pt))
            {
                return("");
            }

            IntPtr hwnd = UnsafeNativeMethods.WindowFromPhysicalPoint(pt.x, pt.y);

            if (hwnd == IntPtr.Zero)
            {
                return("");
            }

            int hit = Misc.ProxySendMessageInt(hwnd, NativeMethods.WM_NCHITTEST, IntPtr.Zero, NativeMethods.Util.MAKELPARAM(pt.x, pt.y));

            switch (hit)
            {
            case NativeMethods.HTMINBUTTON:
                if (Misc.IsBitSet(Misc.GetWindowStyle(hwnd), NativeMethods.WS_MINIMIZE))
                {
                    return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonRestore));
                }
                else
                {
                    return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonMinimize));
                }

            case NativeMethods.HTMAXBUTTON:
                if (Misc.IsBitSet(Misc.GetWindowStyle(hwnd), NativeMethods.WS_MAXIMIZE))
                {
                    return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonRestore));
                }
                else
                {
                    return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonMaximize));
                }

            case NativeMethods.HTCLOSE:
            case NativeMethods.HTMDICLOSE:
                return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonClose));

            case NativeMethods.HTHELP:
                return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonContextHelp));

            case NativeMethods.HTMDIMINBUTTON:
                return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonMinimize));

            case NativeMethods.HTMDIMAXBUTTON:
                return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonMaximize));

            case NativeMethods.HTCAPTION:
                return(Misc.ProxyGetText(hwnd));

            default:
                break;
            }

            return("");
        }
Esempio n. 17
0
 /// <summary>
 /// Returns the integer associated with the vertex named <tt>s</tt>.
 /// </summary>
 /// <param name="s">s the name of a vertex</param>
 /// <returns>the integer (between 0 and <em>V</em> - 1) associated with the vertex named <tt>s</tt></returns>
 public int Index(string s)
 {
     return(_st.Get(s));
 }
Esempio n. 18
0
        // For Vista getting the part of the titlebar that a tooltip belongs to is more
        // reliable across themes
        private string GetTitleBarToolTipTextForDWMEnabled()
        {
            // The mouse is over the titlebar item so get that point on the screen
            NativeMethods.Win32Point pt = new NativeMethods.Win32Point();
            if (!Misc.GetCursorPos(ref pt))
            {
                return("");
            }

            // Find the titlebar hwnd
            IntPtr hwnd = UnsafeNativeMethods.WindowFromPhysicalPoint(pt.x, pt.y);

            if (hwnd == IntPtr.Zero)
            {
                return("");
            }

            // Get the rects for each titlbar part
            Rect[] rects = Misc.GetTitlebarRects(hwnd);

            // Look from back to front - front is entire titlebar rect
            int scan;

            for (scan = rects.Length - 1; scan >= 0; scan--)
            {
                // Not using Misc.PtInRect because including the bounding pixels all the way around gives
                // better results; tooltips may appear when the mouse is one or two pixels outside of the
                // bounding rect so even this technique may miss.
                if (pt.x >= rects[scan].Left && pt.x <= rects[scan].Right && pt.y >= rects[scan].Top && pt.y <= rects[scan].Bottom)
                {
                    break;
                }
            }

            switch (scan)
            {
            case NativeMethods.INDEX_TITLEBAR_MINBUTTON:
                if (Misc.IsBitSet(WindowStyle, NativeMethods.WS_MINIMIZE))
                {
                    return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonRestore));
                }
                else
                {
                    return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonMinimize));
                }

            case NativeMethods.INDEX_TITLEBAR_HELPBUTTON:
                return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonContextHelp));

            case NativeMethods.INDEX_TITLEBAR_MAXBUTTON:
                if (Misc.IsBitSet(WindowStyle, NativeMethods.WS_MAXIMIZE))
                {
                    return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonRestore));
                }
                else
                {
                    return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonMaximize));
                }

            case NativeMethods.INDEX_TITLEBAR_CLOSEBUTTON:
                return(ST.Get(STID.LocalizedNameWindowsTitleBarButtonClose));

            case NativeMethods.INDEX_TITLEBAR_SELF:
                return(Misc.ProxyGetText(hwnd));

            default:
                return("");
            }
        }