# AIAbstractList Class Reference

Class that creates a list which can keep item/value pairs, which you can walk. More...

`#include <ai_abstractlist.hpp>`

Inheritance diagram for AIAbstractList:

## Public Types

enum | SorterType { SORT_BY_VALUE, SORT_BY_ITEM }
| Type of sorter. More...
typedef std::set< int32 > | AIItemList
| The list of items inside the bucket.
typedef std::map< int32, AIItemList > | AIAbstractListBucket
| The bucket list per value.
typedef std::map< int32, int32 > | AIAbstractListMap
| List per item.

## Public Member Functions

void | Clear ()
| Clear the list, making Count() returning 0 and IsEmpty() returning true.
bool | HasItem (int32 item)
| Check if an item is in the list.
int32 | Begin ()
| Go to the beginning of the list.
int32 | Next ()
| Go to the next item in the list.
bool | IsEmpty ()
| Check if a list is empty.
bool | HasNext ()
| Check if there is a next element.
int32 | Count ()
| Returns the amount of items in the list.
int32 | GetValue (int32 item)
| Get the value that belongs to this item.
bool | SetValue (int32 item, int32 value)
| Set a value of an item directly.
void | Sort (SorterType sorter, bool ascending)
| Sort this list by the given sorter and direction.
void | AddList (AIAbstractList *list)
| Add one list to another one.
void | RemoveAboveValue (int32 value)
| Removes all items with a higher value than 'value'.
void | RemoveBelowValue (int32 value)
| Removes all items with a lower value than 'value'.
void | RemoveBetweenValue (int32 start, int32 end)
| Removes all items with a value above start and below end.
void | RemoveValue (int32 value)
| Remove all items with this value.
void | RemoveTop (int32 count)
| Remove the first count items.
void | RemoveBottom (int32 count)
| Remove the last count items.
void | RemoveList (AIAbstractList *list)
| Remove everything that is in the given list from this list (same item index that is).
void | KeepAboveValue (int32 value)
| Keep all items with a higher value than 'value'.
void | KeepBelowValue (int32 value)
| Keep all items with a lower value than 'value'.
void | KeepBetweenValue (int32 start, int32 end)
| Keep all items with a value above start and below end.
void | KeepValue (int32 value)
| Keep all items with this value.
void | KeepTop (int32 count)
| Keep the first count items, i.e.
void | KeepBottom (int32 count)
| Keep the last count items, i.e.
void | KeepList (AIAbstractList *list)
| Keeps everything that is in the given list from this list (same item index that is).
SQInteger | _get (HSQUIRRELVM vm)
| Used for 'foreach()' and [] get from Squirrel.
SQInteger | _nexti (HSQUIRRELVM vm)
| Used for 'foreach()' from Squirrel.
SQInteger | Valuate (HSQUIRRELVM vm)
| The Valuate() wrapper from Squirrel.

## Static Public Member Functions

static const char * | GetClassName ()

## Data Fields

AIAbstractListMap | items
| The items in the list.
AIAbstractListBucket | buckets
| The items in the list, sorted by value.

## Static Public Attributes

static const bool | SORT_ASCENDING = true
| Sort ascending.
static const bool | SORT_DESCENDING = false
| Sort descnding.

## Protected Member Functions

void | AddItem (int32 item)
| Add a single item to the list.
void | RemoveItem (int32 item)
| Remove a single item from the list.

## Private Attributes

AIAbstractListSorter * | sorter
| Sorting algorithm.
SorterType | sorter_type
| Sorting type.
bool | sort_ascending
| Whether to sort ascending or descending.
bool | initialized
| Whether an iteration has been started.
int | modifications
| Number of modification that has been done. To prevent changing data while valuating.

## Detailed Description

Class that creates a list which can keep item/value pairs, which you can walk.

Definition at line 25 of file ai_abstractlist.hpp.

## Member Enumeration Documentation

enum AIAbstractList::SorterType

Type of sorter.

Enumerator:

  SORT_BY_VALUE | Sort the list based on the value of the item.
  SORT_BY_ITEM | Sort the list based on the item itself.

Definition at line 30 of file ai_abstractlist.hpp.

## Member Function Documentation

void AIAbstractList::AddItem | ( | int32 | item | ) | `[protected]`

Add a single item to the list.

Parameters:

  | item | the item to add. Should be unique, otherwise it is ignored.

Note:
  the value is set to 0 by default.

Definition at line 407 of file ai_abstractlist.cpp.

References buckets, HasItem(), items, and modifications.

Referenced by AddList(), AITileList::AddRectangle(), AITileList::AddTile(), AIBridgeList_Length::AIBridgeList_Length(), AICargoList_IndustryAccepting::AICargoList_IndustryAccepting(), AICargoList_IndustryProducing::AICargoList_IndustryProducing(), AIDepotList::AIDepotList(), AIEngineList::AIEngineList(), AIIndustryList_CargoAccepting::AIIndustryList_CargoAccepting(), AIIndustryList_CargoProducing::AIIndustryList_CargoProducing(), AIStationList::AIStationList(), AIStationList_Vehicle::AIStationList_Vehicle(), AIVehicleList_DefaultGroup::AIVehicleList_DefaultGroup(), AIVehicleList_Depot::AIVehicleList_Depot(), AIVehicleList_Group::AIVehicleList_Group(), AIVehicleList_SharedOrders::AIVehicleList_SharedOrders(), AIVehicleList_Station::AIVehicleList_Station(), AIWaypointList::AIWaypointList(), AIWaypointList_Vehicle::AIWaypointList_Vehicle(), and KeepList().

void AIAbstractList::AddList | ( | AIAbstractList * | list | ) |

Add one list to another one.

Parameters:

  | list | The list that will be added to the caller.

Postcondition:
  The list to be added ('list') stays unmodified.

Note:
  All added items keep their value as it was in 'list'.
  If the item already exists inside the caller, the value of the list that is added is set on the item.

Definition at line 522 of file ai_abstractlist.cpp.

References AddItem(), items, and SetValue().

int32 AIAbstractList::Begin | ( |  | ) |

Go to the beginning of the list.

Returns:
  the item value of the first item.

Definition at line 431 of file ai_abstractlist.cpp.

References AIAbstractListSorter::Begin(), initialized, and sorter.

Referenced by _nexti().

int32 AIAbstractList::Count | ( |  | ) |

Returns the amount of items in the list.

Returns:
  amount of items in the list.

Definition at line 460 of file ai_abstractlist.cpp.

References items.

Referenced by KeepBottom(), and KeepTop().

int32 AIAbstractList::GetValue | ( | int32 | item | ) |

Get the value that belongs to this item.

Parameters:

  | item | the item to get the value from

Returns:
  the value that belongs to this item.

Definition at line 465 of file ai_abstractlist.cpp.

References HasItem(), and items.

Referenced by _get(), RemoveItem(), and SetValue().

bool AIAbstractList::HasItem | ( | int32 | item | ) |

Check if an item is in the list.

Parameters:

  | item | the item to check for.

Returns:
  true if the item is in the list.

Definition at line 393 of file ai_abstractlist.cpp.

References items.

Referenced by _get(), AIList::_set(), AddItem(), GetValue(), RemoveItem(), and SetValue().

bool AIAbstractList::HasNext | ( |  | ) |

Check if there is a next element.

In other words, if this is true, Next() will return a valid item.

Returns:
  true if there is a next item.

Definition at line 451 of file ai_abstractlist.cpp.

References AIAbstractListSorter::HasNext(), initialized, and sorter.

Referenced by _nexti().

bool AIAbstractList::IsEmpty | ( |  | ) |

Check if a list is empty.

Returns:
  true if the list is empty.

Definition at line 446 of file ai_abstractlist.cpp.

References items.

Referenced by _nexti().

void AIAbstractList::KeepAboveValue | ( | int32 | value | ) |

Keep all items with a higher value than 'value'.

Parameters:

  | value | the value above which all items are kept.

Definition at line 674 of file ai_abstractlist.cpp.

References buckets, items, and modifications.

void AIAbstractList::KeepBelowValue | ( | int32 | value | ) |

Keep all items with a lower value than 'value'.

Parameters:

  | value | the value below which all items are kept.

Definition at line 689 of file ai_abstractlist.cpp.

References buckets, items, and modifications.

void AIAbstractList::KeepBetweenValue | ( | int32 | start,
|  | int32 | end |
| ) |  |  |

Keep all items with a value above start and below end.

Parameters:

  | start | the lower bound of the to be kept values (exclusive).
  | end | the upper bound of the to be kept values (exclusive).

Definition at line 704 of file ai_abstractlist.cpp.

References buckets, items, and modifications.

void AIAbstractList::KeepBottom | ( | int32 | count | ) |

Keep the last count items, i.e.

remove everything except the last count items.

Parameters:

  | count | the amount of items to keep.

Definition at line 741 of file ai_abstractlist.cpp.

References Count(), modifications, and RemoveTop().

void AIAbstractList::KeepList | ( | AIAbstractList * | list | ) |

Keeps everything that is in the given list from this list (same item index that is).

Parameters:

  | list | the list of items to keep.

Precondition:
  list != NULL

Definition at line 748 of file ai_abstractlist.cpp.

References AddItem(), items, modifications, RemoveList(), and SetValue().

void AIAbstractList::KeepTop | ( | int32 | count | ) |

Keep the first count items, i.e.

remove everything except the first count items.

Parameters:

  | count | the amount of items to keep.

Definition at line 734 of file ai_abstractlist.cpp.

References Count(), modifications, and RemoveBottom().

void AIAbstractList::KeepValue | ( | int32 | value | ) |

Keep all items with this value.

Parameters:

  | value | the value to keep.

Definition at line 719 of file ai_abstractlist.cpp.

References buckets, items, and modifications.

int32 AIAbstractList::Next | ( |  | ) |

Go to the next item in the list.

Returns:
  the item value of the next item.

Note:
  returns 0 if beyond end-of-list. Use HasNext() to check for end-of-list.

Definition at line 437 of file ai_abstractlist.cpp.

References initialized, AIAbstractListSorter::Next(), and sorter.

Referenced by _nexti().

void AIAbstractList::RemoveAboveValue | ( | int32 | value | ) |

Removes all items with a higher value than 'value'.

Parameters:

  | value | the value above which all items are removed.

Definition at line 531 of file ai_abstractlist.cpp.

References buckets, items, and modifications.

void AIAbstractList::RemoveBelowValue | ( | int32 | value | ) |

Removes all items with a lower value than 'value'.

Parameters:

  | value | the value below which all items are removed.

Definition at line 546 of file ai_abstractlist.cpp.

References buckets, items, and modifications.

void AIAbstractList::RemoveBetweenValue | ( | int32 | start,
|  | int32 | end |
| ) |  |  |

Removes all items with a value above start and below end.

Parameters:

  | start | the lower bound of the to be removed values (exclusive).
  | end | the upper bound of the to be removed valuens (exclusive).

Definition at line 561 of file ai_abstractlist.cpp.

References buckets, items, and modifications.

void AIAbstractList::RemoveBottom | ( | int32 | count | ) |

Remove the last count items.

Parameters:

  | count | the amount of items to remove.

Definition at line 628 of file ai_abstractlist.cpp.

References buckets, items, modifications, RemoveItem(), RemoveTop(), Sort(), sort_ascending, SORT_BY_ITEM, SORT_BY_VALUE, and sorter_type.

Referenced by KeepTop(), and RemoveTop().

void AIAbstractList::RemoveItem | ( | int32 | item | ) | `[protected]`

Remove a single item from the list.

Parameters:

  | item | the item to remove. If not existing, it is ignored.

Reimplemented in AIList.

Definition at line 417 of file ai_abstractlist.cpp.

References buckets, GetValue(), HasItem(), items, modifications, AIAbstractListSorter::Remove(), and sorter.

Referenced by RemoveBottom(), RemoveList(), AITileList::RemoveRectangle(), AITileList::RemoveTile(), and RemoveTop().

void AIAbstractList::RemoveList | ( | AIAbstractList * | list | ) |

Remove everything that is in the given list from this list (same item index that is).

Parameters:

  | list | the list of items to remove.

Precondition:
  list != NULL

Definition at line 664 of file ai_abstractlist.cpp.

References items, modifications, and RemoveItem().

Referenced by KeepList().

void AIAbstractList::RemoveTop | ( | int32 | count | ) |

Remove the first count items.

Parameters:

  | count | the amount of items to remove.

Definition at line 591 of file ai_abstractlist.cpp.

References buckets, items, modifications, RemoveBottom(), RemoveItem(), Sort(), sort_ascending, SORT_BY_ITEM, SORT_BY_VALUE, and sorter_type.

Referenced by KeepBottom(), and RemoveBottom().

void AIAbstractList::RemoveValue | ( | int32 | value | ) |

Remove all items with this value.

Parameters:

  | value | the value to remove.

Definition at line 576 of file ai_abstractlist.cpp.

References buckets, items, and modifications.

bool AIAbstractList::SetValue | ( | int32 | item,
|  | int32 | value |
| ) |  |  |

Set a value of an item directly.

Parameters:

  | item | the item to set the value for.
  | value | the value to give to the item

Returns:
  true if we could set the item to value, false otherwise.

Note:
  Changing values of items while looping through a list might cause entries to be skipped. Be very careful with such operations.

Definition at line 472 of file ai_abstractlist.cpp.

References buckets, GetValue(), HasItem(), items, modifications, AIAbstractListSorter::Remove(), and sorter.

Referenced by AIList::AddItem(), AddList(), AIList::ChangeItem(), and KeepList().

void AIAbstractList::Sort | ( | SorterType | sorter,
|  | bool | ascending |
| ) |  |  |

Sort this list by the given sorter and direction.

Parameters:

  | sorter | the type of sorter to use
  | ascending | if true, lowest value is on top, else at bottom.

Note:
  the current item stays at the same place.

See also:
  SORT_ASCENDING SORT_DESCENDING

Definition at line 489 of file ai_abstractlist.cpp.

References modifications, sort_ascending, SORT_BY_ITEM, SORT_BY_VALUE, and sorter_type.

Referenced by RemoveBottom(), and RemoveTop().

The documentation for this class was generated from the following files:

- src/ai/api/ai_abstractlist.hpp
- src/ai/api/ai_abstractlist.cpp