Fixed the behaviour of DoForEach() functions (from Isaac Yonemoto implementation)
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2216 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+48
-12
@@ -21,6 +21,7 @@
|
|||||||
//
|
//
|
||||||
// File Name: List.cpp
|
// File Name: List.cpp
|
||||||
// Author(s): The Storage kit Team
|
// Author(s): The Storage kit Team
|
||||||
|
// Isaac Yonemoto
|
||||||
// Description: BList class provides storage for pointers.
|
// Description: BList class provides storage for pointers.
|
||||||
// Not thread safe.
|
// Not thread safe.
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
@@ -96,6 +97,7 @@ BList::AddItem(void *item, int32 index)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// AddItem
|
// AddItem
|
||||||
bool
|
bool
|
||||||
BList::AddItem(void *item)
|
BList::AddItem(void *item)
|
||||||
@@ -111,6 +113,7 @@ BList::AddItem(void *item)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// AddList
|
// AddList
|
||||||
bool
|
bool
|
||||||
BList::AddList(BList *list, int32 index)
|
BList::AddList(BList *list, int32 index)
|
||||||
@@ -128,6 +131,7 @@ BList::AddList(BList *list, int32 index)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// AddList
|
// AddList
|
||||||
bool
|
bool
|
||||||
BList::AddList(BList *list)
|
BList::AddList(BList *list)
|
||||||
@@ -244,11 +248,30 @@ BList::SwapItems(int32 indexA, int32 indexB)
|
|||||||
|
|
||||||
|
|
||||||
//MoveItem
|
//MoveItem
|
||||||
|
//This moves a list item from posititon a to position b, moving the appropriate block of
|
||||||
|
// list elements to make up for the move. For example, in the array:
|
||||||
|
// A B C D E F G H I J
|
||||||
|
// Moveing 1(B)->6(G) would result in this:
|
||||||
|
// A C D E F G B H I J
|
||||||
bool
|
bool
|
||||||
BList::MoveItem(int32 fromIndex, int32 toIndex)
|
BList::MoveItem(int32 fromIndex, int32 toIndex)
|
||||||
{
|
{
|
||||||
//TODO: Implement
|
if ((fromIndex >= fItemCount) || (toIndex >= fItemCount) || (fromIndex < 0) || (toIndex < 0))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if (fromIndex < toIndex)
|
||||||
|
{
|
||||||
|
void * tmp_mover = fObjectList[fromIndex];
|
||||||
|
memmove(fObjectList + fromIndex + 1, fObjectList + fromIndex, (toIndex - fromIndex) * sizeof(void *));
|
||||||
|
fObjectList[toIndex] = tmp_mover;
|
||||||
|
}
|
||||||
|
else if (fromIndex > toIndex)
|
||||||
|
{
|
||||||
|
void * tmp_mover = fObjectList[fromIndex];
|
||||||
|
memmove(fObjectList + toIndex + 1, fObjectList + toIndex, (fromIndex - toIndex) * sizeof(void *));
|
||||||
|
fObjectList[toIndex] = tmp_mover;
|
||||||
|
};
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -339,24 +362,37 @@ BList::IsEmpty() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Iterating over the list. */
|
/* Iterating over the list. */
|
||||||
// DoForEach
|
//iterate a function over the whole list. If the function outputs a true
|
||||||
|
//value, then the process is terminated.
|
||||||
|
|
||||||
void
|
void
|
||||||
BList::DoForEach(bool (*func)(void *))
|
BList::DoForEach(bool (*func)(void *))
|
||||||
{
|
{
|
||||||
if (func) {
|
bool terminate = false; int32 index = 0; //set terminate condition variables to go.
|
||||||
for (int32 i = 0; i < fItemCount; i++)
|
if (func != NULL)
|
||||||
(*func)(fObjectList[i]);
|
{
|
||||||
|
while ((!terminate) && (index < fItemCount)) //check terminate condition.
|
||||||
|
{
|
||||||
|
terminate = func(fItemList[index]); //reset immediate terminator
|
||||||
|
index++; //advance along the list.
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DoForEach
|
|
||||||
void
|
void
|
||||||
BList::DoForEach(bool (*func)(void *, void *), void *arg2)
|
BList::DoForEach(bool (*func)(void *, void*), void * arg)
|
||||||
|
//same as above, except this function takes an argument.
|
||||||
{
|
{
|
||||||
if (func) {
|
bool terminate = false; int32 index = 0;
|
||||||
for (int32 i = 0; i < fItemCount; i++)
|
if (func != NULL)
|
||||||
(*func)(fObjectList[i], arg2);
|
{
|
||||||
|
while ((!terminate) && (index < fItemCount))
|
||||||
|
{
|
||||||
|
terminate = func(fItemList[index], arg);
|
||||||
|
index++;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user