AlgoPlus v0.1.0
|
Self-learning Frequency List that maintains a list of elements in descending order of their frequency. More...
#include <frequency_list.h>
Classes | |
class | Iterator |
Public Member Functions | |
frequency_list (std::vector< T > data={}) noexcept | |
Constructs a frequency_list object with an optional initializer list of elements. | |
frequency_list (const frequency_list< T > &list) | |
Copy constructor for the frequency_list class. | |
void | push_back (T data) |
Adds an element to the back of the frequency list. | |
void | push_front (T data) |
Adds an element to the front of the frequency list. | |
bool | search (T key) |
Searches for a given key in the frequency list. | |
int64_t | get_frequency (T key) |
Gets the frequency of a key in the frequency list. | |
void | erase (T key) |
Removes the first occurrence of a given key in the frequency list. | |
void | reset_frequency () |
Resets the frequency of all nodes in the frequency list to 1. | |
std::vector< std::pair< T, int64_t > > | elements () |
Returns all the elements of the list. | |
bool | empty () |
Checks if the frequency list is empty. | |
Iterator | begin () |
Get an iterator pointing to the beginning of the frequency list. | |
Iterator | end () |
Get an iterator pointing to the end of the frequency list. | |
void | visualize () |
visualize function for frequency_list Class returns a .dot file that can be previewd with graphviz plugin in vscode | |
Friends | |
std::ostream & | operator<< (std::ostream &os, const frequency_list< T > &flist) |
Overloaded output stream insertion operator for the frequency_list class. | |
Self-learning Frequency List that maintains a list of elements in descending order of their frequency.
@template typename T
The frequency_list is a container that maintains a list of elements and their frequencies. Each element is stored in a node along with the count of the number of times it occurs in the list. The frequency_list is useful when it is important to keep track of the number of times an item appears in the list.
This class should be used for, for example, tracking user choices, making them more accessible for the user in the future, improving their experience.
|
inlineexplicitnoexcept |
Constructs a frequency_list object with an optional initializer list of elements.
The frequency_list object is constructed with an optional initializer list of elements. If the initializer list is not empty, the elements are added to the frequency_list in the same order as they appear in the vector.
T | The type of elements stored in the frequency_list. |
data | An optional initializer list of elements. |
|
inline |
Copy constructor for the frequency_list class.
This constructor creates a new frequency_list object by copying the elements from another frequency_list object. The elements are copied in the same order as they appear in the original frequency_list object.
list | The frequency_list object to copy from. |
|
inline |
std::vector< std::pair< T, int64_t > > frequency_list< T >::elements | ( | ) |
Returns all the elements of the list.
|
inline |
Checks if the frequency list is empty.
|
inline |
Get an iterator pointing to the end of the frequency list.
The end iterator is used to indicate the position after the last element of the frequency list, and can be used to determine when the end of the frequency list has been reached while iterating.
void frequency_list< T >::erase | ( | T | key | ) |
Removes the first occurrence of a given key in the frequency list.
If the key is found, it removes the corresponding node and updates the pointers. If the head node is the one to be removed, the head pointer is updated accordingly. If the node has a previous and/or next node, their pointers are updated to connect them together.
key | The key to be removed from the frequency list. |
int64_t frequency_list< T >::get_frequency | ( | T | key | ) |
Gets the frequency of a key in the frequency list.
key | The key to get the frequency of. |
void frequency_list< T >::push_back | ( | T | data | ) |
Adds an element to the back of the frequency list.
This function adds an element to the back of the frequency list. If the frequency list is empty, it creates a new node with the given data and frequency of 1 and sets it as the head. If the element already exists in the frequency list, it increments its frequency. Otherwise, it creates a new node with the given data and frequency of 1, and appends it to the end of the frequency list.
data | The data to be added to the frequency list. |
void frequency_list< T >::push_front | ( | T | data | ) |
Adds an element to the front of the frequency list.
If the frequency list is not empty, it updates the previous pointer of the current head node to the new node. Then it sets the new node as the new head of the frequency list.
data | The data to be added to the frequency list. |
bool frequency_list< T >::search | ( | T | key | ) |
Searches for a given key in the frequency list.
This function searches for a given key in the frequency list. If the key is found, it increments the frequency of the corresponding node and returns true. If the key is not found, it returns false.
key | The key to search for in the frequency list. |
|
friend |
Overloaded output stream insertion operator for the frequency_list class.
This operator allows the frequency_list object to be printed to the output stream. It iterates through the frequency_list and outputs each element along with its frequency in the format: element(frequency)
os | The output stream to write to. |
flist | The frequency_list object to be printed. |