如何跳过此循环? [英] How to skip out of this loop?

查看:96
本文介绍了如何跳过此循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个排序的列表视图,在delphi中具有50000个项目(字符串)。如何快速搜索带有相同前缀词的项目然后跳过循环?

This is a sorted listview with 50000 items(strings) in delphi. How to fast search items with same prefixed words and then skip out loop?

列表类似于:

aa.....
ab cd//from here
ab kk
ab li
ab mn
ab xy// to here
ac xz
...

我的意思是如何禁食查找并复制带有ab前缀的项目,然后跳过循环。假设ab项之一的索引在二分查找中获得。 ab cd到ab xy的索引是通过二进制搜索获得的。

I mean how to fast find and copy items with prefixes of ab and skip out loop. Suppose index of one of ab items is got in a binarysearch. The index of ab cd to ab xy is got through a binary search.

非常感谢。

编辑:我们感谢大家的回答。

We thank all for your answers.

推荐答案

如果您想快速获取内容,请不要将数据存储在TListView中。

If you want something fast, don't store your data in a TListView.

使用TStringList存储列表,然后在虚拟模式下使用TListView。

Use a TStringList to store your list, then use a TListView in virtual mode.

从TStringList.Items []读取的速度比从TListView.Items []属性读取。

Reading from a TStringList.Items[] is many times faster than reading from a TListView.Items[] property.

如果您确定列表中没有空项,请使用以下方法:

If you're sure that no void item exist in the list, uses this:

procedure Extract(List, Dest: TStrings; Char1, Char2: char);
var i,j: integer;
    V: cardinal;
type PC = {$ifdef UNICODE}PCardinal{$else}PWord{$endif};
begin
  V := ord(Char1)+ord(Char2) shl (8*sizeof(char));
  Dest.BeginUpdate;
  Dest.Clear;
  for i := 0 to List.Count-1 do begin
  if PC(pointer(List[i]))^=V then begin
    for j := i to List.Count-1 do begin
      Dest.Add(List[j]);
      if PC(pointer(List[j]))^<>V then
        break; // end the for j := loop
     end;
     break; // end the for i := loop
  end;
  Dest.EndUpdate;
end;

您可以使用二进制搜索使其更快。但是使用PWord()技巧,在50000个项目列表上,您不会注意到它。

You can use binary search to get it even faster. But with the PWord() trick, on a 50000 items list, you won't notice it.

请注意,PC(pointer(List [i]))^ = V是copy(List [i],1,2)= Char1 + Char2的更快版本,因为在比较期间没有创建临时字符串。但是它仅在没有List [i] ='',即没有指针(List [i])= nil的情况下起作用。

Note that PC(pointer(List[i]))^=V is a faster version of copy(List[i],1,2)=Char1+Char2, because no temporary string is created during the comparison. But it works only if no List[i]='', i.e. no pointer(List[i])=nil.

我添加了{$ ifdef UNICODE }和sizeof(char),以使此代码可以与所有版本的Delphi(在Delphi 2009之前和之后)一起编译。

I've added a {$ifdef UNICODE} and sizeof(char) in order to have this code compile with all version of Delphi (before and after Delphi 2009).

这篇关于如何跳过此循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆