예제 #1
0
        protected bool code_pressed_once(input_code code, bool moved)
        {
            // look for the code in the memory
            bool pressed = m_manager.code_pressed(code);
            var  found   = std.lower_bound(m_switch_memory, code); //auto const found(std::lower_bound(m_switch_memory.begin(), m_switch_memory.end(), code));

            if ((-1 != found) && (m_switch_memory[found] == code)) //if ((m_switch_memory.end() != found) && (*found == code))
            {
                // if no longer pressed, clear entry
                if (!pressed)
                {
                    m_switch_memory.erase(found);
                }

                // always return false
                return(false);
            }

            // if we get here, we were not previously pressed; if still not pressed, return false
            if (!pressed || !moved)
            {
                return(false);
            }

            // otherwise, add the code to the memory and return true
            m_switch_memory.emplace(found, code);
            return(true);
        }
예제 #2
0
        // general helpers

        //void set_current(UInt32 index) { assert(index >= -1 && index <= s_driver_count); m_current = index; }

        //-------------------------------------------------
        //  driver_sort_callback - compare two items in
        //  an array of game_driver pointers
        //-------------------------------------------------
        public void find_approximate_matches(string string_, int count, out int [] results)
        {
            //#undef rand

            results = new int [count];

            // if no name, pick random entries
            if (string_.empty())
            {
                // seed the RNG first
                //srand(osd_ticks());
                Random r = new Random((int)m_osdcore.osd_ticks());

                // allocate a temporary list
                std.vector <int> templist = new std.vector <int>(m_filtered_count);
                int arrayindex            = 0;
                for (int index = 0; index < (int)s_driver_count; index++)
                {
                    if (m_included[index])
                    {
                        templist[arrayindex++] = index;
                    }
                }

                assert(arrayindex == (int)m_filtered_count);

                // shuffle
                for (int shufnum = 0; shufnum < (int)(4 * s_driver_count); shufnum++)
                {
                    int item1 = r.Next() % (int)m_filtered_count;
                    int item2 = r.Next() % (int)m_filtered_count;
                    int temp  = templist[item1];
                    templist[item1] = templist[item2];
                    templist[item2] = temp;
                }

                // copy out the first few entries
                for (int matchnum = 0; matchnum < count; matchnum++)
                {
                    results[matchnum] = templist[matchnum % (int)m_filtered_count];
                }
            }
            else
            {
                // allocate memory to track the penalty value
                std.vector <std.pair <double, int> > penalty = new std.vector <std.pair <double, int> >();
                penalty.reserve((size_t)count);
                string search = ustr_from_utf8(normalize_unicode(string_, unicode_normalization_form.D, true));
                string composed;
                string candidate;

                // scan the entire drivers array
                for (int index = 0; index < (int)s_driver_count; index++)
                {
                    if (m_included[index])
                    {
                        // cheat on the shortname as it's always lowercase ASCII
                        game_driver drv     = s_drivers_sorted[index];
                        size_t      namelen = std.strlen(drv.name);
                        //candidate.resize(namelen);
                        candidate = drv.name;  //std.copy_n(drv.name, namelen, candidate.begin());
                        double curpenalty = util.edit_distance(search, candidate);

                        // if it's not a perfect match, try the description
                        if (curpenalty != 0)
                        {
                            candidate = ustr_from_utf8(normalize_unicode(drv.type.fullname(), unicode_normalization_form.D, true));
                            double p = util.edit_distance(search, candidate);
                            if (p < curpenalty)
                            {
                                curpenalty = p;
                            }
                        }

                        // also check "<manufacturer> <description>"
                        if (curpenalty != 0)
                        {
                            composed  = drv.manufacturer;
                            composed += ' ';
                            composed += drv.type.fullname();
                            candidate = ustr_from_utf8(normalize_unicode(composed, unicode_normalization_form.D, true));
                            double p = util.edit_distance(search, candidate);
                            if (p < curpenalty)
                            {
                                curpenalty = p;
                            }
                        }

                        // insert into the sorted table of matches
                        //var it = std.upper_bound(penalty.begin(), penalty.end(), std.make_pair(curpenalty, index));
                        int it;
                        for (it = 0; it < penalty.Count; it++)
                        {
                            if (penalty[it].first > curpenalty)
                            {
                                break;
                            }
                        }

                        if (penalty.Count != it)
                        {
                            if ((int)penalty.size() >= count)
                            {
                                penalty.resize((size_t)count - 1);
                            }

                            penalty.emplace(it, new std.pair <double, int>(curpenalty, index));
                        }
                        else if ((int)penalty.size() < count)
                        {
                            penalty.emplace(it, new std.pair <double, int>(curpenalty, index));
                        }
                    }
                }

                // copy to output and pad with -1
                //std::fill(
                //        std::transform(
                //            penalty.begin(),
                //            penalty.end(),
                //            results,
                //            [] (std::pair<double, int> const &x) { return x.second; }),
                //        results + count,
                //        -1);
                results = new int [penalty.Count];
                for (int i = 0; i < results.Length; i++)
                {
                    results[i] = penalty[i].second;
                }
            }
        }