Eliminate repeating CountItems() loop premature micro-optimization

Loop backwards if possible, if not, set a variable and use that instead.
There were a couple of instances where the loop style got changed from
    for (int32 i = CountItems(); --i >= 0;)
to
    for (int32 i = CountItems() - 1; i >= 0; i--) {
but should be functionally equivalent.
This commit is contained in:
John Scipione 2013-04-07 12:55:17 -04:00
parent 7cb974614f
commit 9f2cce2faa
6 changed files with 39 additions and 38 deletions

View File

@ -663,12 +663,13 @@ TBarView::ChangeState(int32 state, bool vertical, bool left, bool top,
void
TBarView::SaveExpandedItems()
{
if (fExpandoMenuBar == NULL || fExpandoMenuBar->CountItems() <= 0)
if (fExpandoMenuBar == NULL)
return;
// Get a list of the signatures of expanded apps. Can't use
// team_id because there can be more than one team per application
for (int32 i = 0; i < fExpandoMenuBar->CountItems(); i++) {
int32 count = fExpandoMenuBar->CountItems();
for (int32 i = 0; i < count; i++) {
TTeamMenuItem* teamItem
= dynamic_cast<TTeamMenuItem*>(fExpandoMenuBar->ItemAt(i));

View File

@ -731,7 +731,7 @@ TExpandoMenuBar::RemoveTeam(team_id team, bool partial)
{
TWindowMenuItem* windowItem = NULL;
for (int32 i = 0; i < CountItems(); i++) {
for (int32 i = CountItems() - 1; i >= 0; i--) {
if (TTeamMenuItem* item = dynamic_cast<TTeamMenuItem*>(ItemAt(i))) {
if (item->Teams()->HasItem((void*)(addr_t)team)) {
item->Teams()->RemoveItem(team);

View File

@ -253,7 +253,7 @@ namespace TResourcePrivate {
TypeItem* FindItemByID(int32 id)
{
for (int32 i = 0; i < fItems.CountItems(); i++ ) {
for (int32 i = fItems.CountItems() - 1; i >= 0; i--) {
TypeItem* it = (TypeItem*)fItems.ItemAt(i);
if (it->ID() == id)
return it;
@ -263,7 +263,7 @@ namespace TResourcePrivate {
TypeItem* FindItemByName(const char* name)
{
for (int32 i = 0; i < fItems.CountItems(); i++ ) {
for (int32 i = fItems.CountItems() - 1; i >= 0; i--) {
TypeItem* it = (TypeItem*)fItems.ItemAt(i);
if (strcmp(it->Name(), name) == 0)
return it;
@ -677,8 +677,7 @@ TResourceSet::FindTypeList(type_code type)
{
BAutolock lock(&fLock);
int32 count = fTypes.CountItems();
for (int32 i = 0; i < count; i++ ) {
for (int32 i = fTypes.CountItems() - 1; i >= 0; i--) {
TypeList* list = (TypeList*)fTypes.ItemAt(i);
if (list && list->Type() == type)
return list;
@ -731,8 +730,7 @@ TResourceSet::LoadResource(type_code type, int32 id, const char* name,
// If a named resource, first look in directories.
fLock.Lock();
int32 count = fDirectories.CountItems();
for (int32 i = 0; item == 0 && i < count; i++) {
for (int32 i = fDirectories.CountItems() - 1; i >= 0; i--) {
BPath* dir = (BPath*)fDirectories.ItemAt(i);
if (dir) {
fLock.Unlock();
@ -754,8 +752,7 @@ TResourceSet::LoadResource(type_code type, int32 id, const char* name,
if (!item) {
// Look through resource objects for data.
fLock.Lock();
int32 count = fResources.CountItems();
for (int32 i = 0; item == 0 && i < count; i++ ) {
for (int32 i = fResources.CountItems() - 1; i >= 0; i--) {
BResources* resource = (BResources*)fResources.ItemAt(i);
if (resource) {
const void* data = NULL;

View File

@ -113,17 +113,16 @@ TShowHideMenuItem::TeamShowHideCommon(int32 action, const BList* teamList,
if (teamList == NULL)
return B_BAD_VALUE;
int32 count = teamList->CountItems();
for (int32 index = 0; index < count; index++) {
team_id team = (addr_t)teamList->ItemAt(index);
for (int32 i = teamList->CountItems() - 1; i >= 0; i--) {
team_id team = (addr_t)teamList->ItemAt(i);
switch (action) {
case B_MINIMIZE_WINDOW:
do_minimize_team(zoomRect, team, doZoom && index == 0);
do_minimize_team(zoomRect, team, doZoom && i == 0);
break;
case B_BRING_TO_FRONT:
do_bring_to_front_team(zoomRect, team, doZoom && index == 0);
do_bring_to_front_team(zoomRect, team, doZoom && i == 0);
break;
case B_QUIT_REQUESTED:

View File

@ -500,7 +500,7 @@ TReplicantTray::DeleteAddOnSupport()
{
_SaveSettings();
for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
for (int32 i = fItemList->CountItems() - 1; i >= 0; i--) {
DeskbarItemInfo* item = (DeskbarItemInfo*)fItemList->RemoveItem(i);
if (item) {
if (item->isAddOn)
@ -519,7 +519,7 @@ TReplicantTray::DeleteAddOnSupport()
DeskbarItemInfo*
TReplicantTray::DeskbarItemFor(node_ref& nodeRef)
{
for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
for (int32 i = fItemList->CountItems() - 1; i >= 0; i--) {
DeskbarItemInfo* item = (DeskbarItemInfo*)fItemList->ItemAt(i);
if (item == NULL)
continue;
@ -535,7 +535,7 @@ TReplicantTray::DeskbarItemFor(node_ref& nodeRef)
DeskbarItemInfo*
TReplicantTray::DeskbarItemFor(int32 id)
{
for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
for (int32 i = fItemList->CountItems() - 1; i >= 0; i--) {
DeskbarItemInfo* item = (DeskbarItemInfo*)fItemList->ItemAt(i);
if (item == NULL)
continue;
@ -719,7 +719,7 @@ void
TReplicantTray::UnloadAddOn(node_ref* nodeRef, dev_t* device,
bool which, bool removeAll)
{
for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
for (int32 i = fItemList->CountItems() - 1; i >= 0; i--) {
DeskbarItemInfo* item = (DeskbarItemInfo*)fItemList->ItemAt(i);
if (!item)
continue;
@ -783,7 +783,7 @@ TReplicantTray::MoveItem(entry_ref* ref, ino_t toDirectory)
//
// don't need to change node info as it does not change
for (int32 i = fItemList->CountItems(); i-- > 0 ;) {
for (int32 i = fItemList->CountItems() - 1; i >= 0; i--) {
DeskbarItemInfo* item = (DeskbarItemInfo*)fItemList->ItemAt(i);
if (!item)
continue;

View File

@ -466,7 +466,7 @@ TSwitchManager::TSwitchManager(BPoint point)
TSwitchManager::~TSwitchManager()
{
for (int32 i = fGroupList.CountItems(); i-- > 0;) {
for (int32 i = fGroupList.CountItems() - 1; i >= 0; i--) {
TTeamGroup* teamInfo = static_cast<TTeamGroup*>(fGroupList.ItemAt(i));
delete teamInfo;
}
@ -551,8 +551,9 @@ TSwitchManager::MessageReceived(BMessage* message)
{
const char* signature = message->FindString("sig");
team_id team = message->FindInt32("team");
int32 count = fGroupList.CountItems();
for (int32 i = 0; i < fGroupList.CountItems(); i++) {
for (int32 i = 0; i < count; i++) {
TTeamGroup* tinfo = (TTeamGroup*)fGroupList.ItemAt(i);
if (strcasecmp(tinfo->Signature(), signature) == 0) {
if (!(tinfo->TeamList()->HasItem((void*)(addr_t)team)))
@ -566,8 +567,9 @@ TSwitchManager::MessageReceived(BMessage* message)
case kRemoveTeam:
{
team_id team = message->FindInt32("team");
int32 count = fGroupList.CountItems();
for (int32 i = 0; i < fGroupList.CountItems(); i++) {
for (int32 i = 0; i < count; i++) {
TTeamGroup* tinfo = (TTeamGroup*)fGroupList.ItemAt(i);
if (tinfo->TeamList()->HasItem((void*)(addr_t)team)) {
tinfo->TeamList()->RemoveItem((void*)(addr_t)team);
@ -813,14 +815,15 @@ int32
TSwitchManager::CountVisibleGroups()
{
int32 result = 0;
int32 count = fGroupList.CountItems();
for (int32 i = 0; i < count; i++) {
if (!OKToUse((TTeamGroup*)fGroupList.ItemAt(i)))
continue;
result++;
}
return result;
}
@ -1047,7 +1050,8 @@ TSwitchManager::QuitApp()
TTeamGroup* teamGroup;
int32 count = 0;
for (int32 i = fCurrentIndex + 1; i < fGroupList.CountItems(); i++) {
int32 groupCount = fGroupList.CountItems();
for (int32 i = fCurrentIndex + 1; i < groupCount; i++) {
teamGroup = (TTeamGroup*)fGroupList.ItemAt(i);
if (!OKToUse(teamGroup))
@ -1067,7 +1071,7 @@ TSwitchManager::QuitApp()
// send the quit request to all teams in this group
for (int32 i = teamGroup->TeamList()->CountItems(); i-- > 0;) {
for (int32 i = teamGroup->TeamList()->CountItems() - 1; i >= 0; i--) {
team_id team = (addr_t)teamGroup->TeamList()->ItemAt(i);
app_info info;
if (be_roster->GetRunningAppInfo(team, &info) == B_OK) {
@ -1090,7 +1094,7 @@ TSwitchManager::HideApp()
TTeamGroup* teamGroup = (TTeamGroup*)fGroupList.ItemAt(fCurrentIndex);
for (int32 i = teamGroup->TeamList()->CountItems(); i-- > 0;) {
for (int32 i = teamGroup->TeamList()->CountItems() - 1; i >= 0; i--) {
team_id team = (addr_t)teamGroup->TeamList()->ItemAt(i);
app_info info;
if (be_roster->GetRunningAppInfo(team, &info) == B_OK)