1#ifndef FREQUENCY_LIST_H
2#define FREQUENCY_LIST_H
4#ifdef ENABLE_LINKED_LIST_VISUALIZATION
5#include "../../visualization/list_visual/linked_list_visualization.h"
46 : head(nullptr), tail(nullptr) {
48 for (const auto& item : data) {
49 this->push_back(item);
64 for (
auto iter = list.head; iter !=
nullptr; iter = iter->next) {
65 this->push_back(iter->data);
146 bool empty() {
return head ==
nullptr; }
188 auto iterator = flist.head;
189 while (iterator !=
nullptr) {
190 os << iterator->data <<
"(" << iterator->freq <<
") ";
191 iterator = iterator->next;
200 std::shared_ptr<node> next;
201 std::weak_ptr<node> prev;
203 node(T data, int64_t freq, std::weak_ptr<node> prev, std::shared_ptr<node> next)
204 : data(data), freq(freq), prev(prev), next(next) {}
206 void add_freq() { freq++; }
207 void reset_freq() { freq = 1; }
210 std::shared_ptr<node> head;
225 void update_list_sequence();
239 void swap_nodes(std::shared_ptr<node> a, std::shared_ptr<node> b);
247 std::string generate();
251inline void frequency_list<T>::swap_nodes(std::shared_ptr<node> a, std::shared_ptr<node> b) {
252 if (a ==
nullptr || b ==
nullptr || a->next != b)
265 a->prev.lock()->next = b;
276template <
typename T>
inline void frequency_list<T>::update_list_sequence() {
277 auto current_node = head;
279 while (current_node !=
nullptr && current_node->next !=
nullptr) {
280 if (current_node->freq < current_node->next->freq) {
281 swap_nodes(current_node, current_node->next);
283 if (current_node->prev.lock())
284 current_node = current_node->prev.lock();
287 current_node = current_node->next;
292 auto iterator = head;
294 while (iterator !=
nullptr) {
295 iterator->reset_freq();
296 iterator = iterator->next;
308 update_list_sequence();
315 auto new_node = std::make_shared<node>(data, 1, std::weak_ptr<node>(), head);
317 if (head !=
nullptr) {
318 head->prev = new_node;
327 auto iterator = head;
330 if (head !=
nullptr && head->data == key) {
339 while (iterator !=
nullptr) {
340 if (iterator->data == key) {
342 if (
auto sharedPrev = iterator->prev.lock()) {
343 sharedPrev->next = iterator->next;
345 if (iterator->next) {
346 iterator->next->prev = iterator->prev;
352 iterator = iterator->next;
357 auto iterator = head;
359 while (iterator !=
nullptr) {
360 if (iterator->data == key) {
361 return iterator->freq;
364 iterator = iterator->next;
371 auto iterator = head;
373 while (iterator !=
nullptr) {
374 if (iterator->data == key) {
375 iterator->add_freq();
376 update_list_sequence();
380 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));
406 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));
420template <
typename T>
inline std::string frequency_list<T>::generate() {
421 std::string gen =
"";
422 gen +=
"rankdir=LR;";
424 gen +=
"node [shape=record;]";
426 std::vector<std::pair<T, int64_t>> els = this->elements();
427 if (std::is_same_v<T, std::string> || std::is_same_v<T, char>) {
428 for (
auto& x : els) {
430 gen +=
" [label=<{ ";
438 std::shared_ptr<node> curr = head;
442 gen += curr->next->data;
443 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
446 gen += curr->next->data;
449 gen +=
":ref [arrowhead=vee, arrowtail=dot, dir=both];";
455 for (
auto& x : els) {
456 std::cout << std::to_string(x.first) <<
" " << std::to_string(x.second) <<
'\n';
457 gen += std::to_string(x.first);
458 gen +=
" [label=<{ ";
459 gen += std::to_string(x.first);
461 gen += std::to_string(x.second);
466 std::shared_ptr<node> curr = head;
468 gen += std::to_string(curr->data);
470 gen += std::to_string(curr->next->data);
471 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
474 gen += std::to_string(curr->next->data);
476 gen += std::to_string(curr->data);
477 gen +=
":ref [arrowhead=vee, arrowtail=dot, dir=both];";
486#ifdef ENABLE_LINKED_LIST_VISUALIZATION
488 std::string generated = this->generate();
489 linked_list_visualization::visualize(generated);
495 std::shared_ptr<node> current;
503 explicit Iterator(
const std::shared_ptr<node>& ptr) noexcept : current(ptr) {}
530 current = current->next;
561 current = current->prev;
Definition frequency_list.h:493
T operator*()
Dereference operator for the Iterator class.
Definition frequency_list.h:607
Iterator & operator=(const std::shared_ptr< node > &ptr)
Assignment operator for the Iterator class.
Definition frequency_list.h:514
Iterator & operator++()
Pre-increment operator for the Iterator class.
Definition frequency_list.h:528
Iterator(const std::shared_ptr< node > &ptr) noexcept
Iterator class for a linked list.
Definition frequency_list.h:503
Iterator operator++(int)
Post-increment operator for the Iterator class.
Definition frequency_list.h:544
bool operator==(const Iterator &it)
Equality operator for the Iterator class.
Definition frequency_list.h:589
Iterator operator--(int)
Post-increment –operator for the Iterator class. This operator overloads the post-increment operator ...
Definition frequency_list.h:574
Iterator & operator--()
Pre-increment – operator for the Iterator class. This operator overloads the pre-increment operator (...
Definition frequency_list.h:559
bool operator!=(const Iterator &it)
Inequality operator for the Iterator class.
Definition frequency_list.h:600
bool empty()
Checks if the frequency list is empty.
Definition frequency_list.h:146
void push_front(T data)
Adds an element to the front of the frequency list.
Definition frequency_list.h:300
void erase(T key)
Removes the first occurrence of a given key in the frequency list.
Definition frequency_list.h:326
int64_t get_frequency(T key)
Gets the frequency of a key in the frequency list.
Definition frequency_list.h:356
Iterator begin()
Get an iterator pointing to the beginning of the frequency list.
Definition frequency_list.h:155
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:187
bool search(T key)
Searches for a given key in the frequency list.
Definition frequency_list.h:370
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:291
Iterator end()
Get an iterator pointing to the end of the frequency list.
Definition frequency_list.h:166
frequency_list(const frequency_list< T > &list)
Copy constructor for the frequency_list class.
Definition frequency_list.h:63
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:45