|
forward_list
build forward list stl from sratch
|
#include <forward_list.hpp>
Classes | |
| struct | Node |
| class | Iterator |
Public Member Functions | |
| forward_lists () | |
| forward_lists (const Allocator &a) | |
| template<typename It> requires my_input_iterator<It> | |
| forward_lists (It begin, It end) | |
| constructor range,adalah constructor yang menginialisasi nilai dari sebuah container dengan element dari suatu rentang yang di tentukan oleh 2 iterator awal(first) dan akhir(last) | |
| forward_lists (std::initializer_list< T > arr) | |
| initializer list constructor | |
| forward_lists (const forward_lists &others) | |
| forward_lists & | operator= (const forward_lists &others) |
| forward_lists (forward_lists &&others) noexcept | |
| move constructor dipakai ketika ingin memindahkan node pada list a ke list b | |
| forward_lists & | operator= (forward_lists &&others) noexcept |
| move assignment constructor | |
| ~forward_lists () | |
| destructor | |
| Iterator | before_begin () |
| iterator yang menunjuk dummy node yaitu head | |
| Iterator | begin () |
| iterator yang menunjuk node pada element pertama pada list | |
| Iterator | end () |
| iterator yang menunjuk pointer setelah node terakhir(tail) | |
| Iterator | cbegin () const |
| Iterator | cend () const |
| Iterator | cbefore_begin () const |
| T | front () |
| method getter yang mengembalikan nilai node pada pos front | |
| int | get_size () |
| method getter yang mengembalilkan jumlah element saat ini | |
| bool | is_empty () |
| method getter yang mengembalilkan true jika list empty,sebalik false jika tidak kosong | |
| std::size_t | max_size () const noexcept |
| mengembalikan jumlah maksimum yang dapat di tampung list | |
| T | back () const noexcept |
| Allocator | get_allocator () const noexcept |
| bool | check_cycle () const noexcept |
| T | get_middle () const noexcept |
| void | push_front (T &&data) |
| method untuk insertion val pada pos front | |
| void | push_front (const T &data) |
| void | pop_front () |
| method untuk deletion val pada pos front | |
| void | push_back (const T &value) |
| adalah method untuk melakukan insertion dari posisi belakang(tail) | |
| void | push_back (T &&value) |
| void | pop_back () |
| pop_back adalah method untuk deletion node di posisi tail | |
| void | insert_after (Iterator iter_position, T &&val) |
| Menyisipkan elemen setelah posisi iterator tertentu. | |
| void | insert_after (Iterator iter_position, const T &val) |
| void | insert_after (Iterator Iterator_position, int n, T &&val) |
| void | insert_after (Iterator Iterator_position, int n, const T &val) |
| template<std::input_iterator It> requires (!std::same_as<It,Iterator>) | |
| void | insert_after (Iterator iter_position, It itr1, It itr2) |
| void | insert_after (const Iterator iter_position, Iterator listBegin, const Iterator listEnd) |
| void | erase_after (const Iterator iter_position) |
| void | erase_after (const Iterator pos_begin, const Iterator pos_end) |
| void | reverse () |
| void | assign (std::size_t n, const T &value) |
| Method Assign Assign dipakai untuk mereplace list saat ini dengan list baru overload method assign assign(n,value) ->insert value sebanyak n kali assign(initializer_list<T>)->replace value pada initializer list ke list saat ini assign(iterator begin,iterator end) ->replace mulai dari iterator begin sampai sebelum iterator end. | |
| void | assign (std::size_t n, T &&value) |
| void | assign (std::initializer_list< T > arr) |
| template<typename It> requires my_input_iterator<It> | |
| void | assign (It itr1, It itr2) |
| void | splice_after (const Iterator pos, forward_lists &others, const Iterator It) |
| slice after method untuk memindahkan node dari satu list ke list lain tanpa menyalin data,tetapi move node pointer | |
| void | splice_after (const Iterator pos, forward_lists &&others, const Iterator It) |
| void | splice_after (const Iterator pos, forward_lists &others) |
| void | splice_after (const Iterator pos, forward_lists &&others) |
| void | splice_after (const Iterator pos, forward_lists &others, const Iterator first, const Iterator last) |
| void | splice_after (const Iterator pos, forward_lists &&others, const Iterator first, const Iterator last) |
| void | swap (forward_lists &others) noexcept |
| void | swap (forward_lists &&others) noexcept |
| void | sort () |
| template<typename Compare = std::less<T>> | |
| void | sort (Compare comp=Compare{}) |
| void | merge (forward_lists &others) |
| void | merge (forward_lists &&others) |
| template<typename compare = std::less<T>> | |
| void | merge (forward_lists &others, compare comp=compare{}) |
| template<typename compare = std::less<T>> | |
| void | merge (forward_lists &&others, compare comp=compare{}) |
| void | merge_sort (forward_lists &others) |
| void | merge_sort (forward_lists &&others) |
| void | resize (std::size_t count) |
| resize adalah method untuk mengubah ukuran sebuah list | |
| void | resize (std::size_t count, const T &value) |
| void | uniqe_all () |
| void | uniqe () |
| template<std::ranges::input_range R> | |
| void | assign_range (R &&rg) |
| void | assign_range (std::initializer_list< T > arr) |
| void | remove (const T &value) |
| overload remove untuk menghapus node dengan nilai sama dengan parameter value | |
| void | remove (const std::size_t pos) |
| method overload remove,untuk menghapus node pada posisi pos | |
| std::size_t | remove_count (const T &value) |
| template<class UnaryPred> requires (std::predicate<UnaryPred&, const T&>) | |
| void | remove_if (UnaryPred p) |
| template<class UnaryPred> requires (std::predicate<UnaryPred&,const T&>) | |
| std::size_t | remove_if_count (UnaryPred p) |
| void | print_all (Iterator begin, Iterator end) |
| method untuk print semua node list | |
| template<std::ranges::input_range R> | |
| void | insert_range_after (const Iterator &pos, R &&r) |
| method untuk menghapus semua node pada head berguna pada destructor | |
| template<std::ranges::input_range R> | |
| void | prepend_range (R &&r) |
| template<class ... Args> | |
| void | emplace_front (Args &&... args) |
| template<class ... Args> | |
| void | emplace_after (Iterator pos, Args &&... args) |
| template<class... Args> | |
| void | emplace_back (Args &&... args) |
| void | clear () |
Private Types | |
| using | node_allocator_type |
| using | node_traits = std::allocator_traits<node_allocator_type> |
Private Member Functions | |
| Node * | _create_node (const T &value) |
| Allocator explanation Allocator adalah abstraksi cara mengelola memory pada container alih2 melakukan new dan delete,STL memakai allocator yaitu semacam policy class yang akan menentukan: 1.bagaimana alokasi memory dilakukan(allocate) 2.bagaimana membangun object di memori(construct) 3.bagaimana menghancurkan object(destroy) 4.bagaimana membebaskan memory(deallocate). | |
| void | _destroy_node (Node *n) noexcept |
| Node * | merge (Node *left, Node *right) |
| template<typename compare = std::less<T>> | |
| Node * | merge (Node *left, Node *right, compare comp) |
| Node * | getMiddle (Node *head) |
| Node * | helper (Node *head) |
| template<typename compare> | |
| Node * | helper (Node *head, compare comp) |
Private Attributes | |
| node_allocator_type | alloc |
| Node * | head = nullptr |
| Node * | tail |
| int | size |
|
private |
|
private |
|
inlineexplicit |
|
inline |
|
inline |
constructor range,adalah constructor yang menginialisasi nilai dari sebuah container dengan element dari suatu rentang yang di tentukan oleh 2 iterator awal(first) dan akhir(last)
time complexity o(n), dan space complexity O(n)
|
inline |
initializer list constructor
time complexity O(n),Space Complexity O(n)
|
inline |
|
inlinenoexcept |
move constructor dipakai ketika ingin memindahkan node pada list a ke list b
others.head dipakai untuk menghindari double delete ketika others keluar dari scope function maka destructor dipanggil karena head = others.head maka head == nullptr,akan mengakibatkan undefined behavior
|
inline |
destructor
|
inlineprivate |
Allocator explanation Allocator adalah abstraksi cara mengelola memory pada container alih2 melakukan new dan delete,STL memakai allocator yaitu semacam policy class yang akan menentukan: 1.bagaimana alokasi memory dilakukan(allocate) 2.bagaimana membangun object di memori(construct) 3.bagaimana menghancurkan object(destroy) 4.bagaimana membebaskan memory(deallocate).
policy class Policy class adalah konsep desain di C++ di mana sebuah class tidak menentukan perilaku sendiri, tapi “mendelegasikan” perilaku tertentu ke class lain
|
inlineprivatenoexcept |
|
inline |
|
inline |
|
inline |
Method Assign Assign dipakai untuk mereplace list saat ini dengan list baru overload method assign assign(n,value) ->insert value sebanyak n kali assign(initializer_list<T>)->replace value pada initializer list ke list saat ini assign(iterator begin,iterator end) ->replace mulai dari iterator begin sampai sebelum iterator end.
Time complexity O(n),Space Complexity O(n)
|
inline |
|
inline |
|
inline |
|
inlinenoexcept |
|
inline |
iterator yang menunjuk dummy node yaitu head
|
inline |
iterator yang menunjuk node pada element pertama pada list
|
inline |
|
inline |
|
inline |
|
inlinenoexcept |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
iterator yang menunjuk pointer setelah node terakhir(tail)
|
inline |
|
inline |
|
inline |
method getter yang mengembalikan nilai node pada pos front
time complexity O(1)
|
inlinenoexcept |
|
inlinenoexcept |
|
inline |
method getter yang mengembalilkan jumlah element saat ini
time complexity O(1)
|
inlineprivate |
|
inlineprivate |
|
inlineprivate |
|
inline |
|
inline |
|
inline |
|
inline |
Menyisipkan elemen setelah posisi iterator tertentu.
Method ini memiliki beberapa bentuk (overload) untuk menyesuaikan kebutuhan:
Overload insert_after(iterator pos, const T& value)
Method ini biasanya membutuhkan 2 overload function agar dapat bekerja optimal:
Catatan penting mengenai forwarding reference:
Dengan dua overload ini:
|
inline |
|
inline |
|
inline |
method untuk menghapus semua node pada head berguna pada destructor
time complexity O(n),Space Complexity O(n)
|
inline |
method getter yang mengembalilkan true jika list empty,sebalik false jika tidak kosong
time complexity O(1)
|
inlinenoexcept |
mengembalikan jumlah maksimum yang dapat di tampung list
konsep adalah maksimum byte element yang dapat ditampung type data dibagi oleh jumlah memory untuk 1 node satu node terdiri dari:
|
inline |
|
inline |
|
inline |
|
inline |
|
inlineprivate |
|
inlineprivate |
|
inline |
|
inline |
|
inline |
|
inlinenoexcept |
move assignment constructor
|
inline |
pop_back adalah method untuk deletion node di posisi tail
time complexity O(n),Space complexity O(1)
|
inline |
method untuk deletion val pada pos front
Time complexity O(1),Space Complexity O(1)
|
inline |
|
inline |
method untuk print semua node list
Time complexity O(n),Space Complexity O(n)
|
inline |
adalah method untuk melakukan insertion dari posisi belakang(tail)
time complexity O(1)
|
inline |
|
inline |
|
inline |
method untuk insertion val pada pos front
Time complexity O(1),Space Complexity O(1)
|
inline |
method overload remove,untuk menghapus node pada posisi pos
| pos | posisi node yang ingin dihapus |
Time complexity best case: O(1) jika pos di head->next(node pertama) average case: O(n) jika pos ditengah list worst case: O(n) jika pos diakhir list(tail)
|
inline |
overload remove untuk menghapus node dengan nilai sama dengan parameter value
| value | nilai node yang ingin dihapus pada list |
Time complexity Best case: O(1),jika hanya node dan berada di pos head->next average case: O(n) worst case: O(n),tetapi menghapus semua node pada list
|
inline |
|
inline |
|
inline |
|
inline |
resize adalah method untuk mengubah ukuran sebuah list
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
slice after method untuk memindahkan node dari satu list ke list lain tanpa menyalin data,tetapi move node pointer
Time complexity overloads
|
inlinenoexcept |
|
inlinenoexcept |
|
inline |
|
inline |
|
private |
|
private |
|
private |
|
private |