1#ifndef FREQUENCY_LIST_H
2#define FREQUENCY_LIST_H
4#ifdef LINKED_LIST_VISUALIZATION_H
5#include "../../visualization/list_visual/linked_list_visualization.h"
44 : head(nullptr), tail(nullptr) {
46 for (const auto &item: data) {
47 this->push_back(item);
61 for (
auto iter = list.head; iter !=
nullptr; iter = iter->next) {
62 this->push_back(iter->data);
78 void push_back(T data);
89 void push_front(T data);
109 int64_t get_frequency(T key);
126 void reset_frequency();
134 std::vector<std::pair<T, int64_t> > elements();
142 bool empty() {
return head ==
nullptr; }
182 auto iterator = flist.head;
183 while (iterator !=
nullptr) {
184 os << iterator->data <<
"(" << iterator->freq <<
") ";
185 iterator = iterator->next;
194 std::shared_ptr<node> next;
195 std::weak_ptr<node> prev;
197 node(T data, int64_t freq, std::weak_ptr<node> prev, std::shared_ptr<node> next)
198 : data(data), freq(freq), prev(prev), next(next) {}
200 void add_freq() { freq++; }
201 void reset_freq() { freq = 1; }
204 std::shared_ptr<node> head;
218 void update_list_sequence();
230 void swap_nodes(std::shared_ptr<node> a, std::shared_ptr<node> b);
237 std::string generate();
244 if(a ==
nullptr || b ==
nullptr || a->next != b)
257 a->prev.lock()->next = b;
270 auto current_node = head;
272 while (current_node !=
nullptr && current_node->next !=
nullptr) {
273 if (current_node->freq < current_node->next->freq) {
274 swap_nodes(current_node, current_node->next);
276 if(current_node->prev.lock())
277 current_node = current_node->prev.lock();
280 current_node = current_node->next;
286 auto iterator = head;
288 while (iterator !=
nullptr) {
289 iterator->reset_freq();
290 iterator = iterator->next;
304 update_list_sequence();
311 auto new_node = std::make_shared<node>(data, 1, std::weak_ptr<node>(), head);
313 if (head !=
nullptr) {
314 head->prev = new_node;
324 auto iterator = head;
327 if (head !=
nullptr && head->data == key) {
336 while (iterator !=
nullptr) {
337 if (iterator->data == key) {
339 if(
auto sharedPrev = iterator->prev.lock()){
340 sharedPrev->next = iterator->next;
342 if (iterator->next) {
343 iterator->next->prev = iterator->prev;
349 iterator = iterator->next;
355 auto iterator = head;
357 while(iterator !=
nullptr) {
358 if(iterator->data == key) {
359 return iterator->freq;
362 iterator = iterator->next;
370 auto iterator = head;
372 while (iterator !=
nullptr) {
373 if (iterator->data == key) {
374 iterator->add_freq();
375 update_list_sequence();
379 iterator = iterator->next;
388 head = std::make_shared<node>(data, 1, std::weak_ptr<node>(),
nullptr);
392 auto iterator = head;
394 if (iterator->data == data) {
395 iterator->add_freq();
396 update_list_sequence();
401 iterator = iterator->next;
404 auto newNode = std::shared_ptr<node>(
new node(data, 1, iterator,
nullptr));
405 if (iterator) iterator->next = newNode;
411 std::vector<std::pair<T, int64_t> > ans;
412 std::shared_ptr<node> root = head;
414 ans.push_back(std::make_pair(root -> data, root -> freq));
422 std::string gen =
"";
423 gen +=
"rankdir=LR;";
425 gen +=
"node [shape=record;]";
427 std::vector<std::pair<T, int64_t> > els = this->elements();
428 if (std::is_same_v<T, std::string> || std::is_same_v<T, char>) {
429 for (
auto &x : els) {
431 gen +=
" [label=<{ ";
439 std::shared_ptr<node> curr = head;
443 gen += curr->next->data;
444 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
447 gen += curr->next->data;
450 gen +=
":ref [arrowhead=vee, arrowtail=dot, dir=both];";
456 for (
auto &x : els) {
457 std::cout << std::to_string(x.first) <<
" " << std::to_string(x.second) <<
'\n';
458 gen += std::to_string(x.first);
459 gen +=
" [label=<{ ";
460 gen += std::to_string(x.first);
462 gen += std::to_string(x.second);
467 std::shared_ptr<node> curr = head;
469 gen += std::to_string(curr->data);
471 gen += std::to_string(curr->next->data);
472 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
475 gen += std::to_string(curr->next->data);
477 gen += std::to_string(curr->data);
478 gen +=
":ref [arrowhead=vee, arrowtail=dot, dir=both];";
487#ifdef LINKED_LIST_VISUALIZATION_H
489 std::string generated = this->generate();
490 linked_list_visualization::visualize(generated);
499 std::shared_ptr<node> current;
506 explicit Iterator(
const std::shared_ptr<node> &ptr) noexcept
534 current = current->next;
563 current = current -> prev;
Definition frequency_list.h:497
T operator*()
Dereference operator for the Iterator class.
Definition frequency_list.h:606
Iterator & operator=(const std::shared_ptr< node > &ptr)
Assignment operator for the Iterator class.
Definition frequency_list.h:519
Iterator & operator++()
Pre-increment operator for the Iterator class.
Definition frequency_list.h:532
Iterator(const std::shared_ptr< node > &ptr) noexcept
Iterator class for a linked list.
Definition frequency_list.h:506
Iterator operator++(int)
Post-increment operator for the Iterator class.
Definition frequency_list.h:547
bool operator==(const Iterator &it)
Equality operator for the Iterator class.
Definition frequency_list.h:589
bool operator!=(const Iterator &it)
Inequality operator for the Iterator class.
Definition frequency_list.h:599
Self-learning Frequency List that maintains a list of elements in descending order of their frequency...
Definition frequency_list.h:31
bool empty()
Checks if the frequency list is empty.
Definition frequency_list.h:142
void push_front(T data)
Adds an element to the front of the frequency list.
Definition frequency_list.h:296
void erase(T key)
Removes the first occurrence of a given key in the frequency list.
Definition frequency_list.h:323
int64_t get_frequency(T key)
Gets the frequency of a key in the frequency list.
Definition frequency_list.h:354
Iterator begin()
Get an iterator pointing to the beginning of the frequency list.
Definition frequency_list.h:151
friend std::ostream & operator<<(std::ostream &os, const frequency_list< T > &flist)
Overloaded output stream insertion operator for the frequency_list class.
Definition frequency_list.h:181
bool search(T key)
Searches for a given key in the frequency list.
Definition frequency_list.h:369
std::vector< std::pair< T, int64_t > > elements()
Returns all the elements of the list.
Definition frequency_list.h:410
void reset_frequency()
Resets the frequency of all nodes in the frequency list to 1.
Definition frequency_list.h:285
Iterator end()
Get an iterator pointing to the end of the frequency list.
Definition frequency_list.h:161
frequency_list(const frequency_list< T > &list)
Copy constructor for the frequency_list class.
Definition frequency_list.h:60
void push_back(T data)
Adds an element to the back of the frequency list.
Definition frequency_list.h:386
void visualize()
visualize function for frequency_list Class returns a .dot file that can be previewd with graphviz pl...
frequency_list(std::vector< T > data={}) noexcept
Constructs a frequency_list object with an optional initializer list of elements.
Definition frequency_list.h:43