From a7e5e613f86963c8bf82248ab044e0ea36e42fbc Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 16 Mar 2018 12:09:30 +0300 Subject: LIST<>::indexOf(T**) - fast index calculation for direct iterators --- protocols/JabberG/src/jabber_frame.cpp | 32 ++++++++++++------------- protocols/JabberG/src/jabber_list.cpp | 6 ++--- protocols/JabberG/src/jabber_menu.cpp | 5 ++-- protocols/JabberG/src/jabber_opttree.cpp | 12 +++++----- protocols/JabberG/src/jabber_treelist.cpp | 7 +++--- protocols/MSN/src/msn_lists.cpp | 39 +++++++++++++++---------------- protocols/VKontakte/src/misc.cpp | 6 ++--- 7 files changed, 51 insertions(+), 56 deletions(-) (limited to 'protocols') diff --git a/protocols/JabberG/src/jabber_frame.cpp b/protocols/JabberG/src/jabber_frame.cpp index 9253442816..6f15bf4780 100644 --- a/protocols/JabberG/src/jabber_frame.cpp +++ b/protocols/JabberG/src/jabber_frame.cpp @@ -190,9 +190,9 @@ LRESULT CJabberInfoFrame::WndProc(UINT msg, WPARAM wParam, LPARAM lParam) case WM_LBUTTONDOWN: { POINT pt = { LOWORD(lParam), HIWORD(lParam) }; - for (int i=0; i < m_pItems.getCount(); i++) - if (m_pItems[i].m_onEvent && PtInRect(&m_pItems[i].m_rcItem, pt)) { - m_clickedItem = i; + for (auto &it : m_pItems) + if (it->m_onEvent && PtInRect(&it->m_rcItem, pt)) { + m_clickedItem = m_pItems.indexOf(&it); return 0; } } @@ -339,19 +339,17 @@ void CJabberInfoFrame::PaintCompact(HDC hdc) int cy_icon = GetSystemMetrics(SM_CYSMICON); int cx = rc.right - cx_icon - SZ_FRAMEPADDING; - for (int i = m_pItems.getCount(); i--;) { - CJabberInfoFrameItem &item = m_pItems[i]; - - SetRect(&item.m_rcItem, 0, 0, 0, 0); - if (!item.m_bShow) continue; - if (!item.m_bCompact) continue; + for (auto &it : m_pItems.rev_iter()) { + SetRect(&it->m_rcItem, 0, 0, 0, 0); + if (!it->m_bShow) continue; + if (!it->m_bCompact) continue; int depth = 0; - for (char *p = item.m_pszName; p = strchr(p+1, '/'); ++depth) ; + for (char *p = it->m_pszName; p = strchr(p+1, '/'); ++depth) ; if (depth == 0) { - if (item.m_hIcolibIcon) { - HICON hIcon = IcoLib_GetIconByHandle(item.m_hIcolibIcon); + if (it->m_hIcolibIcon) { + HICON hIcon = IcoLib_GetIconByHandle(it->m_hIcolibIcon); if (hIcon) { DrawIconEx(hdc, SZ_FRAMEPADDING, (rc.bottom-cy_icon)/2, hIcon, cx_icon, cy_icon, 0, nullptr, DI_NORMAL); IcoLib_ReleaseIcon(hIcon); @@ -359,19 +357,19 @@ void CJabberInfoFrame::PaintCompact(HDC hdc) } RECT rcText; SetRect(&rcText, cx_icon + SZ_FRAMEPADDING + SZ_ICONSPACING, 0, rc.right - SZ_FRAMEPADDING, rc.bottom); - DrawText(hdc, item.m_pszText, -1, &rcText, DT_NOPREFIX | DT_SINGLELINE | DT_VCENTER | DT_END_ELLIPSIS); + DrawText(hdc, it->m_pszText, -1, &rcText, DT_NOPREFIX | DT_SINGLELINE | DT_VCENTER | DT_END_ELLIPSIS); } else { - if (item.m_hIcolibIcon) { - HICON hIcon = IcoLib_GetIconByHandle(item.m_hIcolibIcon); + if (it->m_hIcolibIcon) { + HICON hIcon = IcoLib_GetIconByHandle(it->m_hIcolibIcon); if (hIcon) { - SetRect(&item.m_rcItem, cx, (rc.bottom-cy_icon)/2, cx+cx_icon, (rc.bottom-cy_icon)/2+cy_icon); + SetRect(&it->m_rcItem, cx, (rc.bottom-cy_icon)/2, cx+cx_icon, (rc.bottom-cy_icon)/2+cy_icon); DrawIconEx(hdc, cx, (rc.bottom-cy_icon)/2, hIcon, cx_icon, cy_icon, 0, nullptr, DI_NORMAL); cx -= cx_icon; IcoLib_ReleaseIcon(hIcon); - SetToolTip(item.m_tooltipId, &item.m_rcItem, item.m_pszText); + SetToolTip(it->m_tooltipId, &it->m_rcItem, it->m_pszText); } } } diff --git a/protocols/JabberG/src/jabber_list.cpp b/protocols/JabberG/src/jabber_list.cpp index 0d102bf196..8a4b817965 100644 --- a/protocols/JabberG/src/jabber_list.cpp +++ b/protocols/JabberG/src/jabber_list.cpp @@ -362,13 +362,11 @@ pResourceStatus JABBER_LIST_ITEM::getBestResource() const return m_pManualResource; int nBestPos = -1, nBestPri = -200; - for (int i = 0; i < arResources.getCount(); i++) { - JABBER_RESOURCE_STATUS *r = arResources[i]; + for (auto &r : arResources) if (r->m_iPriority > nBestPri) { nBestPri = r->m_iPriority; - nBestPos = i; + nBestPos = arResources.indexOf(&r); } - } return (nBestPos != -1) ? arResources[nBestPos] : nullptr; } diff --git a/protocols/JabberG/src/jabber_menu.cpp b/protocols/JabberG/src/jabber_menu.cpp index 02fc077c4f..9e0c556ba1 100644 --- a/protocols/JabberG/src/jabber_menu.cpp +++ b/protocols/JabberG/src/jabber_menu.cpp @@ -1067,11 +1067,10 @@ CJabberProto* JabberChooseInstance(bool bIsLink) return it; int nItems = 0, lastItemId = 0; - for (int i = 0; i < g_Instances.getCount(); i++) { - CJabberProto *ppro = g_Instances[i]; + for (auto &ppro : g_Instances) { if (ppro->m_iStatus != ID_STATUS_OFFLINE && ppro->m_iStatus != ID_STATUS_CONNECTING) { ++nItems; - lastItemId = i + 1; + lastItemId = g_Instances.indexOf(&ppro) + 1; Menu_ModifyItem(ppro->m_hChooseMenuItem, nullptr, Skin_LoadProtoIcon(ppro->m_szModuleName, ppro->m_iStatus)); } else Menu_ShowItem(ppro->m_hChooseMenuItem, false); diff --git a/protocols/JabberG/src/jabber_opttree.cpp b/protocols/JabberG/src/jabber_opttree.cpp index c9ad4ba465..d4b0bfca13 100644 --- a/protocols/JabberG/src/jabber_opttree.cpp +++ b/protocols/JabberG/src/jabber_opttree.cpp @@ -107,12 +107,12 @@ void CCtrlTreeOpts::OnInit() SetImageList(hImgLst, TVSIL_NORMAL); /* build options tree. based on code from IcoLib */ - for (int i = 0; i < m_options.getCount(); i++) { + for (auto &it : m_options) { wchar_t *sectionName; int sectionLevel = 0; HTREEITEM hSection = nullptr; - mir_wstrcpy(itemName, m_options[i]->m_szOptionName); + mir_wstrcpy(itemName, it->m_szOptionName); sectionName = itemName; while (sectionName) { @@ -142,18 +142,18 @@ void CCtrlTreeOpts::OnInit() tvis.item.iImage = tvis.item.iSelectedImage = IMG_GRPOPEN; } else { - tvis.item.lParam = i; + tvis.item.lParam = m_options.indexOf(&it); - BYTE val = *m_options[i]->m_option; + BYTE val = *it->m_option; - if (m_options[i]->m_groupId == OPTTREE_CHECK) + if (it->m_groupId == OPTTREE_CHECK) tvis.item.iImage = tvis.item.iSelectedImage = val ? IMG_CHECK : IMG_NOCHECK; else tvis.item.iImage = tvis.item.iSelectedImage = val ? IMG_RCHECK : IMG_NORCHECK; } hItem = InsertItem(&tvis); if (!sectionName) - m_options[i]->m_hItem = hItem; + it->m_hItem = hItem; } } sectionLevel++; diff --git a/protocols/JabberG/src/jabber_treelist.cpp b/protocols/JabberG/src/jabber_treelist.cpp index 7a960fedd4..e701c79f60 100644 --- a/protocols/JabberG/src/jabber_treelist.cpp +++ b/protocols/JabberG/src/jabber_treelist.cpp @@ -320,8 +320,9 @@ void TreeList_Update(HWND hwnd) if (data->mode == TLM_TREE) TreeList_RecursiveApply(hItem, sttTreeList_CreateItems, (LPARAM)hwnd); else { - for (int i = data->hItemSelected->subItems.getCount(); i--;) - sttTreeList_CreateItems_List(data->hItemSelected->subItems[i], (LPARAM)hwnd); + for (auto &it : data->hItemSelected->subItems) + sttTreeList_CreateItems_List(it, (LPARAM)hwnd); + for (HTREELISTITEM p = data->hItemSelected; !(p->flags & TLIF_ROOT); p = p->parent) sttTreeList_CreateItems_List(p, (LPARAM)hwnd); } @@ -504,7 +505,7 @@ static void sttTreeList_ResetIndex(HTREELISTITEM hItem, LPARAM data) static void sttTreeList_FilterItems(HTREELISTITEM hItem, LPARAM data) { - int i = 0; + int i; for (i=0; i < hItem->text.getCount(); i++) if (JabberStrIStr(hItem->text[i], (wchar_t *)data)) break; diff --git a/protocols/MSN/src/msn_lists.cpp b/protocols/MSN/src/msn_lists.cpp index c103be86f3..393e1d085d 100644 --- a/protocols/MSN/src/msn_lists.cpp +++ b/protocols/MSN/src/msn_lists.cpp @@ -209,23 +209,22 @@ void CMsnProto::Lists_Populate(void) void CMsnProto::MSN_CleanupLists(void) { - for (int i = m_arContacts.getCount(); i--;) { - MsnContact& p = m_arContacts[i]; - if (p.list & LIST_FL) - MSN_SetContactDb(p.hContact, p.email); - - if (p.list & LIST_PL) { - if (p.list & (LIST_AL | LIST_BL)) - MSN_AddUser(NULL, p.email, p.netId, LIST_PL + LIST_REMOVE); + for (auto &it : m_arContacts.rev_iter()) { + if (it->list & LIST_FL) + MSN_SetContactDb(it->hContact, it->email); + + if (it->list & LIST_PL) { + if (it->list & (LIST_AL | LIST_BL)) + MSN_AddUser(NULL, it->email, it->netId, LIST_PL + LIST_REMOVE); else - MSN_AddAuthRequest(p.email, p.nick, p.invite); + MSN_AddAuthRequest(it->email, it->nick, it->invite); } - if (p.hContact && !(p.list & (LIST_LL | LIST_FL | LIST_PL)) && p.list != LIST_RL) { - int count = db_event_count(p.hContact); + if (it->hContact && !(it->list & (LIST_LL | LIST_FL | LIST_PL)) && it->list != LIST_RL) { + int count = db_event_count(it->hContact); if (count) { wchar_t text[256]; - wchar_t *sze = mir_a2u(p.email); + wchar_t *sze = mir_a2u(it->email); mir_snwprintf(text, TranslateT("Contact %s has been removed from the server.\nWould you like to keep it as \"Local Only\" contact to preserve history?"), sze); mir_free(sze); @@ -233,25 +232,25 @@ void CMsnProto::MSN_CleanupLists(void) mir_snwprintf(title, TranslateT("%s protocol"), m_tszUserName); if (MessageBox(nullptr, text, title, MB_YESNO | MB_ICONQUESTION | MB_SETFOREGROUND) == IDYES) { - MSN_AddUser(p.hContact, p.email, 0, LIST_LL); - setByte(p.hContact, "LocalList", 1); + MSN_AddUser(it->hContact, it->email, 0, LIST_LL); + setByte(it->hContact, "LocalList", 1); continue; } } - if (!(p.list & (LIST_LL | LIST_FL))) { - db_delete_contact(p.hContact); - p.hContact = NULL; + if (!(it->list & (LIST_LL | LIST_FL))) { + db_delete_contact(it->hContact); + it->hContact = NULL; } } - if (p.list & (LIST_LL | LIST_FL) && p.hContact) { + if (it->list & (LIST_LL | LIST_FL) && it->hContact) { wchar_t path[MAX_PATH]; - MSN_GetCustomSmileyFileName(p.hContact, path, _countof(path), "", 0); + MSN_GetCustomSmileyFileName(it->hContact, path, _countof(path), "", 0); if (path[0]) { SMADD_CONT cont; cont.cbSize = sizeof(SMADD_CONT); - cont.hContact = p.hContact; + cont.hContact = it->hContact; cont.type = 0; cont.path = path; diff --git a/protocols/VKontakte/src/misc.cpp b/protocols/VKontakte/src/misc.cpp index 551ac57da7..07bf5cd0b5 100644 --- a/protocols/VKontakte/src/misc.cpp +++ b/protocols/VKontakte/src/misc.cpp @@ -229,9 +229,9 @@ bool CVkProto::IsGroupUser(MCONTACT hContact) bool CVkProto::CheckMid(LIST &lList, int guid) { - for (int i = lList.getCount() - 1; i >= 0; i--) - if ((INT_PTR)lList[i] == guid) { - lList.remove(i); + for (auto &it : lList) + if ((INT_PTR)it == guid) { + lList.remove(lList.indexOf(&it)); return true; } -- cgit v1.2.3