C++ 帮助将此索引优化为行-列,反之亦然

C++ 帮助将此索引优化为行-列,反之亦然,c++,algorithm,optimization,C++,Algorithm,Optimization,这是我的问题。我在游戏中为我的Gui制作了一个文本框 它所做的是,每次我调整它的大小,因为它是word换行的,我必须找出插入符号在文本字符串中的索引,然后我需要在reize之后将其转换为正确的行-列。根据我的分析器,最慢的部分是当我得到下一个要计算的unicode字符时: int AguiTextBox::indexFromColumnRow( int column, int row, bool includeUnwantedChars ) const { size_t

这是我的问题。我在游戏中为我的Gui制作了一个文本框

它所做的是,每次我调整它的大小,因为它是word换行的,我必须找出插入符号在文本字符串中的索引,然后我需要在reize之后将其转换为正确的行-列。根据我的分析器,最慢的部分是当我得到下一个要计算的unicode字符时:

int AguiTextBox::indexFromColumnRow( int column, int row, bool includeUnwantedChars ) const
    {
        size_t rowLen = 0;

        int retIndex = -1;
        int bytesSkipped = 0;
        int curCharLen = 0;
        std::string curChar;

        std::string::const_iterator it = getText().begin();
        std::string::const_iterator end = getText().end();


        //decrement column so that the lowest is -1
        column--;
        if(textRows.size() == 0 || (column == -1 && row == 0))
        {
            //not in the text
            return -1;
        }
0.01s       for(size_t i = 0; i < textRows.size(); ++i)
        {
            //get length of row
0.00s           rowLen = _unicodeFunctions.getUtf8StringLength(textRows[i]);

            //handle -1th case

            //get next character
            do 
            {
0.00s               curCharLen = _unicodeFunctions.bringToNextUnichar(it,end);
0.01s               curChar = getText().substr(bytesSkipped,curCharLen);
                bytesSkipped += curCharLen;
                if(includeUnwantedChars)
                    retIndex++;
            } while (curChar[0] >= 0 && curChar[0] < ' ' && curChar != "\n");

            if(!includeUnwantedChars)
            retIndex++;

            //only increase for newlines
0.00s           if(curChar != "\n")
            {
                bytesSkipped -= curCharLen;
                retIndex--;
                it -= curCharLen;
            }

            if((int)i == row && column == -1)
            {
                return retIndex;
            }


0.06s           for(size_t j = 0; j < rowLen; ++j)
            {
                //get next character
                do 
                {
0.10s                   curCharLen = _unicodeFunctions.bringToNextUnichar(it,end);
0.91s                   curChar = getText().substr(bytesSkipped,curCharLen);
0.03s                   bytesSkipped += curCharLen;

0.03s                   if(includeUnwantedChars)
                        retIndex++;

0.11s               } while (curChar[0] >= 0 && curChar[0] < ' ' && curChar != "\n");

0.06s               if(!includeUnwantedChars)
0.00s                   retIndex++;

0.02s               if((int)i == row && (int)j == column)
                {
                    return retIndex;
                }
            }
        }

        return retIndex;
    }
int-AguiTextBox::indexFromColumnRow(int-column,int-row,bool includeUnwantedChars)常量
{
大小\u t rowLen=0;
int-retIndex=-1;
int bytesSkipped=0;
int curCharLen=0;
std::字符串curChar;
std::string::const_迭代器it=getText().begin();
std::string::const_迭代器end=getText().end();
//减量列,使最低值为-1
列--;
if(textRows.size()==0 | |(列==-1&&row==0))
{
//文本中没有
返回-1;
}
0.01s(大小i=0;i=0&&curChar[0]<'&&curChar!=“\n”);
如果(!includeUnwantedChars)
retIndex++;
//只增加换行
0.00s如果(curChar!=“\n”)
{
bytesSkipped-=curCharLen;
视网膜指数--;
it-=curCharLen;
}
if((int)i==行和列==-1)
{
返回retIndex;
}
0.06s(尺寸j=0;j=0&&curChar[0]<'&&curChar!=“\n”);
0.06s,如果(!包括无条件)
0.00s retIndex++;
如果((int)i==行和(&(int)j==列),则为0.02s
{
返回retIndex;
}
}
}
返回retIndex;
}
我如何优化这个

谢谢


@Erik对于双端字符队列意味着什么?

您正在使用以下命令提取子字符串:

curChar = getText().substr(bytesSkipped,curCharLen);
但是您只使用第一个元素。只需提取所需的
char
即可避免字符串构造/复制


在通用算法优化方面——我会花费所需的资源来构建字符对象的
deque
,而不是使用
std::string
。这将允许您直接索引任何字符,无需反复扫描和解析相同的utf-8序列。

您正在使用以下命令提取子字符串:

curChar = getText().substr(bytesSkipped,curCharLen);
但是您只使用第一个元素。只需提取所需的
char
即可避免字符串构造/复制


在通用算法优化方面——我会花费所需的资源来构建字符对象的
deque
,而不是使用
std::string
。这将使您可以直接索引任何字符,而无需反复扫描和解析相同的utf-8序列。

您可以详细说明出列的想法吗?@Milo:这取决于您的其余代码。如果您有一个1000字节的字符串,其值为800个字符,则在调整大小期间,您当前正在反复将字节解析为字符。创建一个只解析用户键入的内容并保留这些内容的系统,例如utf8字节和字符的并行数组,这样可以避免重复解析。您可以对出列想法进行解释吗?@Milo:这取决于您的其他代码。如果您有一个1000字节的字符串,其值为800个字符,则在调整大小期间,您当前正在反复将字节解析为字符。创建一个只解析用户键入的内容并保留(例如utf8字节和字符的并行数组)的系统可以避免重复解析