Templates for algorithms to work with some common data types. More...
Macros | |
| #define | BINARY_SEARCH(_ARRAY, _KEY, _VALUE, _SIZE) |
| Binary search algorithm. | |
| #define | ARRAY_INSERT(_ARRAY, _POS, _SIZE) |
| Insert into array algorithm. | |
| #define | ARRAY_DELETE(_ARRAY, _POS, _SIZE) |
| Delete from array algorithm. | |
| #define | INSERT_SORT(_ARRAY, _KEY, _VALUE, _SIZE, _MAX) |
| Allocate space for inserting item into sorted array. | |
| #define | HASH_EMPTY 0xFFFFFFFFU |
| Reserved value denoting that tash table key is empty. | |
| #define | HASH_SEARCH(_HASHTABLE, _KEY, _VALUE, _MAX) |
| Find entry in hash table. | |
| #define | BITMAP_SET(_BITMAP, _POS, _SIZE) _BITMAP[(_POS) >> 5] |= (1U << ((_POS) & 31)); |
| Template for setting bit in bitmap. | |
| #define | BITMAP_CLEAR(_BITMAP, _POS, _SIZE) _BITMAP[(_POS) >> 5] &= ~(1U << ((_POS) & 31)); |
| Template for clearing bit in bitmap. | |
| #define | BITMAP_TEST(_BITMAP, _POS, _SIZE) ((_BITMAP[(_POS) >> 5] & (1U << ((_POS) & 31))) != 0) |
| Template for testing if bit is set in bitmap. | |
| #define | BITMAP_FIRST(_BITMAP, _SIZE) |
| Return first set bit in the bitmap. | |
| #define | BITMAP_COPY(_TARGET, _SOURCE, _SIZE) for (unsigned q = 0; q < _SIZE; ++q) _TARGET[q] = _SOURCE[q]; |
Functions | |
| uint32_t | os_hash_key (uint32_t key) |
Templates for algorithms to work with some common data types.
Algorithms implemented here are "templates". They are provided as macros which adapt to underlying types. This means these algorithms can run faster as they are not generic, but it also means that each use will generate its own copy.
Currently algorithms to work with arrays (insert, delete, binary search), hash tables (searching) and bitmaps (set, clear, test, find first) are implemented. Main user of these algorithms is kernel itself yet as they are optimized for embedded they are accessible to userspace programs as well.
| #define ARRAY_DELETE | ( | _ARRAY, | |
| _POS, | |||
| _SIZE | |||
| ) |
Delete from array algorithm.
This template will delete item from array, move all entries after the place of insertion one entry lower and decrease array size.
| _ARRAY | array being inserted into |
| _POS | position at which deletion happens |
| _SIZE | lvalue holding current array size (e.g. count of actual stored items rather than allocation size) |
| #define ARRAY_INSERT | ( | _ARRAY, | |
| _POS, | |||
| _SIZE | |||
| ) |
Insert into array algorithm.
This template will create free space in array, move all entries after the place of insertion one entry higher and increase array size.
| _ARRAY | array being inserted into |
| _POS | position at which insertion happens |
| _SIZE | lvalue holding current array size (e.g. count of actual stored items rather than allocation size) |
| #define BINARY_SEARCH | ( | _ARRAY, | |
| _KEY, | |||
| _VALUE, | |||
| _SIZE | |||
| ) |
Binary search algorithm.
This is a template for binary searching over a sorted compacted array. It will search either for exact match or in case the sought key is not present in dataset, for lower bounds - entry present in array sorting just before the sought entry.
As with any binary search, this algorithm assumes that data is sorted. This template expects data to be stored in array of structures with structure containing member holding item's unique ID. This ID is used for searching.
Example:
| _ARRAY | identification of array which is used to search for entry |
| _KEY | name of structure member which holds unique ID of entry |
| _VALUE | unique ID sought for in the array |
| _SIZE | amount of valid entries currently stored in the array |
| #define BITMAP_CLEAR | ( | _BITMAP, | |
| _POS, | |||
| _SIZE | |||
| ) | _BITMAP[(_POS) >> 5] &= ~(1U << ((_POS) & 31)); |
Template for clearing bit in bitmap.
Clears bit in bitmap of arbitrary size as if it was one continuous array of bits.
| _BITMAP | bitmap identity |
| _POS | bit to be set |
| _SIZE | bitmap size |
| #define BITMAP_COPY | ( | _TARGET, | |
| _SOURCE, | |||
| _SIZE | |||
| ) | for (unsigned q = 0; q < _SIZE; ++q) _TARGET[q] = _SOURCE[q]; |
| #define BITMAP_FIRST | ( | _BITMAP, | |
| _SIZE | |||
| ) |
Return first set bit in the bitmap.
Returns first bit set in bitmap of arbitrary size as if it was one continuous array of bits.
| _BITMAP | bitmap identity |
| _SIZE | bitmap size |
| #define BITMAP_SET | ( | _BITMAP, | |
| _POS, | |||
| _SIZE | |||
| ) | _BITMAP[(_POS) >> 5] |= (1U << ((_POS) & 31)); |
Template for setting bit in bitmap.
Sets bit in bitmap of arbitrary size as if it was one continuous array of bits.
| _BITMAP | bitmap identity |
| _POS | bit to be set |
| _SIZE | bitmap size |
| #define BITMAP_TEST | ( | _BITMAP, | |
| _POS, | |||
| _SIZE | |||
| ) | ((_BITMAP[(_POS) >> 5] & (1U << ((_POS) & 31))) != 0) |
Template for testing if bit is set in bitmap.
Tests if bit in bitmap of arbitrary size is set.
| _BITMAP | bitmap identity |
| _POS | bit to be set |
| _SIZE | bitmap size |
| #define HASH_EMPTY 0xFFFFFFFFU |
Reserved value denoting that tash table key is empty.
| #define HASH_SEARCH | ( | _HASHTABLE, | |
| _KEY, | |||
| _VALUE, | |||
| _MAX | |||
| ) |
Find entry in hash table.
This is a template for searching hash tables. It either finds the entry if it is present in the table or returns position of an entry that is marked as empty.
Example:
| _HASHTABLE | identification of the hash table where search is performed |
| _KEY | name of structure member which holds unique ID of entry |
| _VALUE | unique ID sought for in the hash table |
| _MAX | allocation size of the hash table |
| #define INSERT_SORT | ( | _ARRAY, | |
| _KEY, | |||
| _VALUE, | |||
| _SIZE, | |||
| _MAX | |||
| ) |
Allocate space for inserting item into sorted array.
This template will allocate space for inserting new item into sorted array so that it stays sorted. It will do so that the array remains sorted after the insertion. Template code will move array contents around to make space for new entry but won't write it. That's caller responsibility.
This algorithm assumes that identifiers of items in array are unique. Thus, if ID passed for new entry already exists in the array, it will be overwritten by subsequent call.
Example:
Note that the algorithm will avoid corrupting memory if there is a request to insert new entry into already-full array but won't return any special return value. It's caller's responsibility to check if array is already full before making the call.
This algorithm works in a way that if ID of new entry already exists in the array, array content won't be moved around and subsequent write will overwrite the existing entry. If ID doesn't exist, space for it will be allocated by moving items around the array so the array remains sorted after insertion.
| _ARRAY | identification of array which is used to search for entry |
| _KEY | name of structure member which holds unique ID of entry |
| _VALUE | unique ID sought for in the array |
| _SIZE | amount of valid entries currently stored in the array. This argument must be a lvalue as it will be modified during the process. |
| _MAX | capacity of the array. |
|
inline |