AlgoPlus v0.1.0
Loading...
Searching...
No Matches
frequency_list< T > Class Template Reference

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.
 

Detailed Description

template<typename T>
class frequency_list< T >

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.

Constructor & Destructor Documentation

◆ frequency_list() [1/2]

template<typename T >
frequency_list< T >::frequency_list ( std::vector< T > data = {})
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.

Template Parameters
TThe type of elements stored in the frequency_list.
Parameters
dataAn optional initializer list of elements.

◆ frequency_list() [2/2]

template<typename T >
frequency_list< T >::frequency_list ( const frequency_list< T > & list)
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.

Parameters
listThe frequency_list object to copy from.

Member Function Documentation

◆ begin()

template<typename T >
Iterator frequency_list< T >::begin ( )
inline

Get an iterator pointing to the beginning of the frequency list.

The Iterator provides a way to traverse the frequency list.

Returns
An Iterator object pointing to the beginning of the frequency list.

◆ elements()

template<typename T >
std::vector< std::pair< T, int64_t > > frequency_list< T >::elements ( )

Returns all the elements of the list.

Returns
std::vector<T> the elements of the list

◆ empty()

template<typename T >
bool frequency_list< T >::empty ( )
inline

Checks if the frequency list is empty.

Returns
Returns true if the frequency list is empty, false otherwise.

◆ end()

template<typename T >
Iterator frequency_list< T >::end ( )
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.

Returns
An Iterator object pointing to the end of the frequency list.

◆ erase()

template<typename T >
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.

Parameters
keyThe key to be removed from the frequency list.

◆ get_frequency()

template<typename T >
int64_t frequency_list< T >::get_frequency ( T key)

Gets the frequency of a key in the frequency list.

Parameters
keyThe key to get the frequency of.
Returns
The frequency of the key, or -1 if the key is not found.

◆ push_back()

template<typename T >
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.

Parameters
dataThe data to be added to the frequency list.

◆ push_front()

template<typename T >
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.

Parameters
dataThe data to be added to the frequency list.

◆ search()

template<typename T >
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.

Parameters
keyThe key to search for in the frequency list.
Returns
Returns true if the key is found and the frequency is incremented, false otherwise.

Friends And Related Symbol Documentation

◆ operator<<

template<typename T >
std::ostream & operator<< ( std::ostream & os,
const frequency_list< T > & flist )
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)

Parameters
osThe output stream to write to.
flistThe frequency_list object to be printed.
Returns
The output stream.

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