From b6e62b4e3567fa50814cf37a854307263e23c0da Mon Sep 17 00:00:00 2001 From: orthographic-pedant Date: Wed, 30 Sep 2015 18:51:52 -0400 Subject: [PATCH 01/42] Fixed typographical error, changed arbitary to arbitrary in README. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9f2ddb6f..f347d414 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Prime test(trial division) Prime test(Miller-Rabin's method) 2D Array - Arbitary Integer + Arbitrary Integer Linear congruential generator Maximum subarray problem From a5c52404a3f716b9582faa5c4265b804f1a3b40b Mon Sep 17 00:00:00 2001 From: xtaci Date: Thu, 22 Oct 2015 15:52:26 +0800 Subject: [PATCH 02/42] fix a bug in dijkstra --- include/dijkstra.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dijkstra.h b/include/dijkstra.h index c7b9c9c3..f1831c18 100644 --- a/include/dijkstra.h +++ b/include/dijkstra.h @@ -76,7 +76,7 @@ namespace alg { list_for_each_entry(v, &u->v_head, v_node){ uint32_t alt = dist_u + v->weight; uint32_t dist_v = dist[v->id]; - if (alt < dist_v && !visited[v->id]) { + if (alt < dist_v) { /* uint32_t tmp = dist[v->id]; if (tmp != INT_MAX) { From 8caabf1bcb10cb89ad7a1df80af29f4abf2edd2a Mon Sep 17 00:00:00 2001 From: xtaci Date: Fri, 23 Oct 2015 14:55:03 +0800 Subject: [PATCH 03/42] a better implementation of heap inspired by golang/heap --- Makefile | 5 -- README.md | 1 - include/astar.h | 10 +-- include/dijkstra.h | 31 +++---- include/heap.h | 190 +++++++++++++++++++---------------------- include/heap_sort.h | 54 ------------ include/kruskal_mst.h | 2 +- include/prim_mst.h | 20 ++--- include/scc.h | 8 +- src/heap_demo.cpp | 21 +++-- src/heap_sort_demo.cpp | 34 -------- 11 files changed, 133 insertions(+), 243 deletions(-) delete mode 100644 include/heap_sort.h delete mode 100644 src/heap_sort_demo.cpp diff --git a/Makefile b/Makefile index 04870219..e9ba280c 100644 --- a/Makefile +++ b/Makefile @@ -53,8 +53,6 @@ PROGRAMS = m_based_demo \ random_demo \ k-means_demo \ kmp_demo \ - heap_sort_demo \ - kruskal_mst_demo \ LRU_cache_demo \ base64_demo \ max_subarray_demo \ @@ -209,9 +207,6 @@ k-means_demo: $(SRCDIR)/k-means_demo.cpp kmp_demo : $(SRCDIR)/kmp_demo.cpp $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) -heap_sort_demo: $(SRCDIR)/heap_sort_demo.cpp - $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) - kruskal_mst_demo: $(SRCDIR)/kruskal_mst_demo.cpp $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) diff --git a/README.md b/README.md index 9f2ddb6f..1a431a9f 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,6 @@ Radix sort Quick sort Merge sort - Heap sort Double linked list Skip list Self-organized linked-list ops (move-to-front, move-ahead-one) diff --git a/include/astar.h b/include/astar.h index 78fa276f..03474b34 100644 --- a/include/astar.h +++ b/include/astar.h @@ -93,7 +93,7 @@ namespace alg { // initialy containing the start node // encoding [x,y] to [x*ncol + y] // using binary heap ... - m_openset.insert(0, x1*ncol+y1); + m_openset.push(0, x1*ncol+y1); // record the starting point in openset_grid m_openset_grid(x1,y1) = true; @@ -109,7 +109,8 @@ namespace alg { // the main A*algorithm while(!m_openset.is_empty()) { - uint32_t value = m_openset.min_value(); + Heap::elem e = m_openset.pop(); + uint32_t value = e.data; int cx = value/ncol; int cy = value%ncol; @@ -136,8 +137,7 @@ namespace alg { return as; } - // delete current positon from openset and move it into closed set. - m_openset.delete_min(); + // move it into closed set. m_closedset(cx, cy) = true; m_openset_grid(cx, cy) = false; @@ -168,7 +168,7 @@ namespace alg { g_score(nx,ny) = tentative; // update path cost for current position f_score(nx,ny) = tentative + estimate(nx,ny,x2,y2); // record path cost to this neighbour if (!m_openset_grid(nx,ny)) { // only insert the neighbour if it hasn't been add to the openset. - m_openset.insert(f_score(nx,ny), nx*ncol+ny); + m_openset.push(f_score(nx,ny), nx*ncol+ny); m_openset_grid(nx,ny) = true; } } diff --git a/include/dijkstra.h b/include/dijkstra.h index f1831c18..d0bebe7d 100644 --- a/include/dijkstra.h +++ b/include/dijkstra.h @@ -37,6 +37,7 @@ namespace alg { class Dijkstra { public: static const int UNDEFINED = -1; + static const int LARGE_NUMBER = 999999; // run dijkstra algorithm, and return the previous table static HashTable * run(const Graph & g, uint32_t src_id) { // a binary heap @@ -51,43 +52,35 @@ namespace alg { // all vertices Graph::Adjacent * a; list_for_each_entry(a, &g.list(), a_node){ - dist[a->v.id] = INT_MAX; // set inital distance to each vertex as INT_MAX + dist[a->v.id] = LARGE_NUMBER; // set inital distance to each vertex to a large number (*previous)[a->v.id] = UNDEFINED; // clear path to UNDEFINED visited[a->v.id] = false; // all vertices are not visited + Q.push(LARGE_NUMBER, a->v.id); // push all vertices to heap } // source vertex, the first vertex in Heap-Q - Q.insert(0, src_id); dist[src_id] = 0; + // decrease-key the source vertex to 0 + Q.decrease_key(src_id,0); while(!Q.is_empty()) { // for every un-visited vertex, try relaxing the path - int32_t id = Q.min_value(); - Q.delete_min(); // remove u from Q - if (visited[id]) { // jump visited vertex, it means a closer vertex has found - // printf("visted:%d %d\n", id, dist[id]); + Heap::elem e = Q.pop(); + uint32_t id = e.data; + if (visited[id]) { // ignore visited vertex continue; } Graph::Adjacent * u = g[id]; // the vertex to process int dist_u = dist[id]; // current known shortest distance to u - visited[id] = true; // mark the vertex as visited. + visited[id] = true; // mark the vertex as visited. Graph::Vertex * v; list_for_each_entry(v, &u->v_head, v_node){ uint32_t alt = dist_u + v->weight; - uint32_t dist_v = dist[v->id]; - if (alt < dist_v) { - /* - uint32_t tmp = dist[v->id]; - if (tmp != INT_MAX) { - printf("old %d %d\n", v->id, tmp); - printf("new %d %d\n", v->id, dist[v->id]); - } - */ - + if (alt < dist[v->id]) { dist[v->id] = alt; - (*previous)[v->id] = u->v.id; - Q.insert(alt, v->id); + (*previous)[v->id] = id; + Q.decrease_key(v->id, alt); // decrease-key } } } diff --git a/include/heap.h b/include/heap.h index 3462660a..959d6a3e 100644 --- a/include/heap.h +++ b/include/heap.h @@ -8,8 +8,8 @@ * Heap Data structure * * Heaps can be used as an array. For any key at array position I, - I left child is at ( 2i ), right child is at ( 2i+1 ) and parent is - I at (int) (i / 2). Heap size is stored at index 0. + * left child is at ( 2i ), right child is at ( 2i+1 ) and parent is + * at (int) (i / 2). Heap size is stored at index 0. * * Basic operations of a heap are: * @@ -27,8 +27,7 @@ #include #include #include -#include "hash_code.h" -#include "hash_table.h" +#include "generic.h" namespace alg { /** @@ -36,33 +35,29 @@ namespace alg { */ template class Heap { - private: + public: /** * define key-value pair of heap struct. */ - struct KV { + struct elem { public: - int32_t key; - T value; + int key; + T data; }; - int32_t m_size; // current heap size. - int32_t m_max; // max heap size. - KV * m_kvs; // key value pairs. - - HashTable * m_idx; // key -> idx + private: + int m_size; // current heap size. + int m_max; // max heap size. + elem * m_heap; // key value pairs. public: Heap(int max) { m_size = 0; m_max = max+1; - m_kvs = new KV[m_max]; - m_kvs[0].key = INT_MIN; - m_idx = new HashTable(m_max); + m_heap = new elem[m_max]; }; ~Heap() { - delete [] m_kvs; - delete m_idx; + delete [] m_heap; }; private: @@ -70,37 +65,20 @@ namespace alg { Heap& operator=(const Heap&); public: - - inline int min_key() const { return m_kvs[1].key; }; - inline const T & min_value() const { return m_kvs[1].value; }; - // for loop through the kvs - inline uint32_t count() const { return m_size; }; - inline const T & operator[] (uint32_t idx) const { return m_kvs[idx+1].value; }; + inline int count() const { return m_size; }; /** * insert a 'key'->'value' pair into the heap. */ - void insert(int key, const T & value) { + void push(int key, const T & data) { // heap full, just return; if(m_size == m_max) return; - + // put in the back, and try move upward the heap + m_heap[m_size].key = key; + m_heap[m_size].data= data; + up(m_size); m_size++; - m_kvs[m_size].key = key; - m_kvs[m_size].value = value; - (*m_idx)[value] = m_size; - - // Adjust its position - int now = m_size; - while(m_kvs[now/2].key > key) { - m_kvs[now] = m_kvs[now/2]; - (*m_idx)[m_kvs[now/2].value] = now; - now /= 2; - } - - m_kvs[now].key = key; - m_kvs[now].value = value; - (*m_idx)[value] = now; } /** @@ -113,84 +91,94 @@ namespace alg { */ inline void clear() { m_size = 0; } + bool contains(const T & data) { + for(int i=1;i<=m_size;i++) { + if(m_heap[i].data== data) return true; + } + return false; + } + /** - * contains test + * pop the min element */ - bool contains(const T & value) { - for(int32_t i=1;i<=m_size;i++) { - if(m_kvs[i].value == value) return true; - } + elem pop() { + int n = m_size-1; + swap(m_heap[0],m_heap[n]); + down(0, n); + m_size--; + return m_heap[m_size]; + } + /** + * remove the given data + */ + bool remove(T data) { + for (int i=0;i heap top. + * decrease key + * simpliy implemented as remove then push */ - void delete_min() { - // heap[1] is the minimum key. So we remove heap[1]. - // Size of the heap is decreased. Now heap[1] has to be filled. - // We put the last key in its place and see if it fits. If it - // does not fit, take minimum key among both its children and - // replaces parent with it. Again See if the last key fits - //in that place. - int32_t lastKey; - T lastValue; - int32_t child,now; - - // empty heap, just return - if (m_size == 0) return; - - lastKey = m_kvs[m_size].key; - lastValue = m_kvs[m_size].value; - m_size--; + void decrease_key(T data, int newkey) { + if (remove(data)) { + push(newkey, data); + } + } - // now refers to the index at which we are now - for(now = 1; now*2 <= m_size ;now = child) { - // child is the index of the key which is minimum among - // both the children, Indexes of children are i*2 and i*2 + 1 - child = now*2; - // child!=heapSize beacuse heap[heapSize+1] does not exist, - // which means it has only one child - if(child != m_size && m_kvs[child+1].key < m_kvs[child].key) { - child++; // choose the minium one. + void up(int j) { + for (;;) { + int i = (j-1)/2; // parent + if (i==j || !less(j,i)) { // j not smaller than i + break; } - // To check if the last key fits or not it suffices to check - // if the last key is less than the minimum key among both the children - if(lastKey > m_kvs[child].key) { - m_kvs[now] = m_kvs[child]; - (*m_idx)[m_kvs[now].value] = now; // record index + swap(m_heap[i], m_heap[j]); + j=i; + } + } + + void down(int i, int n) { + for(;;) { + int j1 = 2*i+1; // left child + if (j1 >=n || j1 < 0) { // j1 < 0 after int overflow + break; + } + + int j = j1; + int j2 = j1+1; // left child + if (j2 < n && !less(j1,j2)) { + j = j2; // choose the minium one. } - else { // It fits there + + if (!less(j,i)) { break; } + swap(m_heap[i], m_heap[j]); + i=j; } - - m_kvs[now].key = lastKey; - m_kvs[now].value= lastValue; - (*m_idx)[lastValue] = now; // record index } - /** - * so called DECREASE KEY operation. - * step 1. find the value - * step 2. decrease the key to the newkey - */ - void decrease_key(T value, int32_t newkey) { - int32_t index = (*m_idx)[value]; - if (index > m_size || index == 0) return; // value not found - if (newkey >= m_kvs[index].key) return; // violate DECREASE meanning. - T oldvalue = m_kvs[index].value; - - int now = index; - while(m_kvs[now/2].key > newkey) { - m_kvs[now] = m_kvs[now/2]; - (*m_idx)[m_kvs[now].value] = now; // record index - now /= 2; + void print_heap() { + for (int i=0;i - * _| - * - * HEAPSORT - * - * Features: - * 1. Although somewhat slower in practice on most machines than a well-implemented quicksort, - it has the advantage of a more favorable worst-case O(n log n) runtime - * - * http://en.wikipedia.org/wiki/Heapsort - * - ******************************************************************************/ - -#ifndef __HEAPSORT_H__ -#define __HEAPSORT_H__ - -#include - -namespace alg { - /** - * heap sort an array - */ - template - static void heapsort(T *array,int number_of_elements) { - Heap heap(number_of_elements); - int i; - - // In order to build a heap structure from input array - for(i=0;iheap.insert(v->weight, v); // weight->vertex + pa->heap.push(v->weight, v); // weight->vertex } } diff --git a/include/prim_mst.h b/include/prim_mst.h index b3bf5069..8c4892fa 100644 --- a/include/prim_mst.h +++ b/include/prim_mst.h @@ -30,10 +30,12 @@ #include "undirected_graph.h" #include "double_linked_list.h" #include "heap.h" +#include "hash_table.h" namespace alg { class Prim { public: + static const int LARGE_NUMBER = 999999; /** * Prim's Algorithm. * @@ -62,24 +64,20 @@ namespace alg { // all vertices Graph::Adjacent * a; list_for_each_entry(a, &g.list(), a_node){ - if (a->v.id != src_id) { - Q.insert(INT_MAX, a->v.id); - keys[a->v.id] = INT_MAX; - } + Q.push(LARGE_NUMBER, a->v.id); + keys[a->v.id] = LARGE_NUMBER; } - - Q.insert(0, src_id); + + Q.decrease_key(src_id, 0); keys[src_id] = 0; while (!Q.is_empty()) { - int32_t id = Q.min_value(); - Q.delete_min(); // remove u from Q - - Graph::Adjacent * u = g[id]; // the vertex to process + Heap::elem e = Q.pop(); + Graph::Adjacent * u = g[e.data]; // the vertex to process Graph::Vertex * v; list_for_each_entry(v, &u->v_head, v_node) { if (Q.contains(v->id) && v->weight < keys[v->id]) { - pi[v->id] = id; + pi[v->id] = e.data; Q.decrease_key(v->id, v->weight); keys[v->id] = v->weight; } diff --git a/include/scc.h b/include/scc.h index 23e34e0e..1d641a1b 100644 --- a/include/scc.h +++ b/include/scc.h @@ -39,7 +39,7 @@ namespace alg { Heap Q(g.vertex_count()) ; Graph::Adjacent * a; list_for_each_entry(a, &g.list(), a_node) { - Q.insert(INT_MAX - a->f, a->v.id); // descending order of a->f + Q.push(INT_MAX - a->f, a->v.id); // descending order of a->f } // step 2. discover @@ -51,9 +51,9 @@ namespace alg { // step 3. call DFS(GT), but in the main loop of DFS, consider the vertices // in order of decreasing u.f (as computed in line 1) while(!Q.is_empty()) { - int32_t key = Q.min_key(); - int32_t id = Q.min_value(); - Q.delete_min(); + Heap::elem e = Q.pop(); + int32_t key = e.key; + int32_t id = e.data; if ((*GT)[id]->color == Graph::WHITE) { printf("component:%d %d\n",id, INT_MAX - key); _DFS_VISIT(*GT, (*GT)[id]); diff --git a/src/heap_demo.cpp b/src/heap_demo.cpp index bf761a66..ccae4aea 100644 --- a/src/heap_demo.cpp +++ b/src/heap_demo.cpp @@ -9,20 +9,25 @@ int main() int MAXELEMENTS=10; Heap heap(MAXELEMENTS); - int32_t i; + int i; srand(time(NULL)); for (i=0;i < MAXELEMENTS; i++) { - int32_t value = i; - heap.insert(i, value); - printf("inserting: %d->%d\n", i, value); + heap.push(100-i, i); + printf("push: key:%d->value:%d\n", 100-i, i); } + heap.print_heap(); + + for (i=0;i%d\n", heap.min_key(), heap.min_value()); - heap.delete_min(); + Heap::elem e = heap.pop(); + printf("pop: key:%d->value:%d\n", e.key, e.data); } + heap.print_heap(); return 0; } diff --git a/src/heap_sort_demo.cpp b/src/heap_sort_demo.cpp deleted file mode 100644 index 7a8423c4..00000000 --- a/src/heap_sort_demo.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include - -#include "generic.h" -#include "heap_sort.h" - -using namespace alg; - -int main() -{ - const int MAX_ELEMENTS = 10; - int list[MAX_ELEMENTS]; - - int i = 0; - - srand(time(NULL)); - // generate random numbers and fill them to the list - for(i = 0; i < MAX_ELEMENTS; i++ ){ - list[i] = rand()%100; - } - printf("The list before sorting is:\n"); - printlist(list,MAX_ELEMENTS); - - // sort the list using heap sort - heapsort(&list[0],MAX_ELEMENTS); - - // print the result - printf("The list after sorting using heapsort algorithm:\n"); - printlist(list,MAX_ELEMENTS); - return 0; -} - - From 2fb4c5ab2755c6814e180a23e8b6378f1bab12e8 Mon Sep 17 00:00:00 2001 From: xtaci Date: Fri, 23 Oct 2015 15:13:08 +0800 Subject: [PATCH 04/42] clean --- include/prim_mst.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/prim_mst.h b/include/prim_mst.h index 8c4892fa..3949e895 100644 --- a/include/prim_mst.h +++ b/include/prim_mst.h @@ -73,11 +73,12 @@ namespace alg { while (!Q.is_empty()) { Heap::elem e = Q.pop(); - Graph::Adjacent * u = g[e.data]; // the vertex to process + uint32_t id = e.data; + Graph::Adjacent * u = g[id]; // the vertex to process Graph::Vertex * v; list_for_each_entry(v, &u->v_head, v_node) { if (Q.contains(v->id) && v->weight < keys[v->id]) { - pi[v->id] = e.data; + pi[v->id] = id; Q.decrease_key(v->id, v->weight); keys[v->id] = v->weight; } From b5756134050f5aeab5b430674cc27ad1984ae88c Mon Sep 17 00:00:00 2001 From: Daniel Fu Date: Fri, 23 Oct 2015 17:05:22 +0800 Subject: [PATCH 05/42] Update heap.h --- include/heap.h | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/include/heap.h b/include/heap.h index 959d6a3e..c82f4d06 100644 --- a/include/heap.h +++ b/include/heap.h @@ -7,14 +7,23 @@ * * Heap Data structure * - * Heaps can be used as an array. For any key at array position I, - * left child is at ( 2i ), right child is at ( 2i+1 ) and parent is - * at (int) (i / 2). Heap size is stored at index 0. + * In computer science, a heap is a specialized tree-based data structure that + * satisfies the heap property: If A is a parent node of B then the key of node + * A is ordered with respect to the key of node B with the same ordering applying + * across the heap. Heaps can be classified further as either a "max heap" or + * a "min heap". In a max heap, the keys of parent nodes are always greater + * than or equal to those of the children and the highest key is in the root node. + * In a min heap, the keys of parent nodes are less than or equal to those of + * the children and the lowest key is in the root node. Heaps are crucial in + * several efficient graph algorithms such as Dijkstra's algorithm, and in + * the sorting algorithm heapsort. A common implementation of a heap is the + * binary heap, in which the tree is a complete binary tree (see figure). * * Basic operations of a heap are: * - * 1. Insert – Insert an key. - * 2. Delete minimum – Delete and return the smallest item in the heap. + * 1. Push – Insert an key. + * 2. Pop – Delete and return the smallest item in the heap. + * 3. Remove - Remove an element * * http://en.wikipedia.org/wiki/Binary_heap ******************************************************************************/ From e95583a3d8adccfddc6b5737dc6389014a796105 Mon Sep 17 00:00:00 2001 From: xtaci Date: Fri, 23 Oct 2015 17:06:53 +0800 Subject: [PATCH 06/42] fix ident --- include/heap.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/heap.h b/include/heap.h index c82f4d06..d6343ef7 100644 --- a/include/heap.h +++ b/include/heap.h @@ -57,7 +57,7 @@ namespace alg { private: int m_size; // current heap size. int m_max; // max heap size. - elem * m_heap; // key value pairs. + elem * m_heap; // key value pairs. public: Heap(int max) { m_size = 0; @@ -122,8 +122,8 @@ namespace alg { * remove the given data */ bool remove(T data) { - for (int i=0;i Date: Fri, 23 Oct 2015 17:09:48 +0800 Subject: [PATCH 07/42] add qualifier --- include/heap.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/heap.h b/include/heap.h index d6343ef7..8712109b 100644 --- a/include/heap.h +++ b/include/heap.h @@ -121,7 +121,7 @@ namespace alg { /** * remove the given data */ - bool remove(T data) { + bool remove(const T &data) { for (int i=0;i Date: Sun, 8 Nov 2015 23:19:15 -0200 Subject: [PATCH 08/42] Add Fenwick Tree algorithm - Gabriel123Duarte --- include/fenwick_tree.h | 44 +++++++++++++++++++++++++++++++++++++++ src/fenwick_tree_demo.cpp | 16 ++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 include/fenwick_tree.h create mode 100644 src/fenwick_tree_demo.cpp diff --git a/include/fenwick_tree.h b/include/fenwick_tree.h new file mode 100644 index 00000000..ba3f581c --- /dev/null +++ b/include/fenwick_tree.h @@ -0,0 +1,44 @@ +#ifndef __FENWICK_H__ +#define __FENWICK_H__ + +#include + +#define LSONE(x) (x & (-x)) + +class Fenwick +{ + private: + std::vector fen; + public: + Fenwick() {} + + // We don't use the index 0 + Fenwick(int n) + { + fen.assign(n + 1, 0); + } + + // RSQ 1..a + int rsq(int a) + { + int ans = 0; + for(; a; a -= LSONE(a)) + ans += fen[a]; + return ans; + } + + // RSQ a..b + inline int rsq(int a, int b) + { + return rsq(b) - (a == 1 ? 0 : rsq(a - 1)); + } + + // Update the value of the k-th element by x + void update(int k, int x) + { + for(; k < (int)fen.size(); k += LSONE(k)) + fen[k] += x; + } +}; + +#endif \ No newline at end of file diff --git a/src/fenwick_tree_demo.cpp b/src/fenwick_tree_demo.cpp new file mode 100644 index 00000000..d5e294de --- /dev/null +++ b/src/fenwick_tree_demo.cpp @@ -0,0 +1,16 @@ +#include +#include "fenwick_tree.h" + +int main() +{ + Fenwick ft(5); + + ft.update(2, 1); + ft.update(4, 10); + + printf("%d\n", ft.rsq(1)); + + ft.update(1, 5); + printf("%d\n", ft.rsq(1)); + return 0; +} \ No newline at end of file From 99defa0c2139648cc551509bf583457137598da5 Mon Sep 17 00:00:00 2001 From: Gabriel Duarte Date: Mon, 9 Nov 2015 08:53:33 -0200 Subject: [PATCH 09/42] Added some comments in the header file --- include/.goutputstream-UUQE8X | 61 +++++++++++++++++++++++++++++++++++ include/fenwick_tree.h | 23 +++++++++++-- 2 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 include/.goutputstream-UUQE8X diff --git a/include/.goutputstream-UUQE8X b/include/.goutputstream-UUQE8X new file mode 100644 index 00000000..e92d1ab5 --- /dev/null +++ b/include/.goutputstream-UUQE8X @@ -0,0 +1,61 @@ +/******************************************************************************* + * Fenwick Tree + * + * Data structure providing prefix sums and modify the table in O(log n) - n is the size o the table. + * + * In this algorithm we use two functions: + * - RSQ - This function calculates the range sum query in O(log n) + * - Update - This function adjusts the values in the given range in O(log n) + * + * https://en.wikipedia.org/wiki/Fenwick_tree + * + * @author Gabriel Duarte (gabriellagoa10@yahoo.com.br) + * @github Gabriel123Duarte + * + ******************************************************************************/ + +#ifndef __FENWICK_H__ +#define __FENWICK_H__ + +#include + +#define LSONE(x) (x & (-x)) + +class Fenwick +{ + private: + // Vector representing the table + std::vector fen; + public: + Fenwick() {} + + // We don't use the index 0, because it is the base case + Fenwick(int n) + { + fen.assign(n + 1, 0); + } + + // Calculate the + int rsq(int a) + { + int ans = 0; + for(; a; a -= LSONE(a)) + ans += fen[a]; + return ans; + } + + // RSQ a..b + inline int rsq(int a, int b) + { + return rsq(b) - (a == 1 ? 0 : rsq(a - 1)); + } + + // Update the value of the k-th element by x + void update(int k, int x) + { + for(; k < (int)fen.size(); k += LSONE(k)) + fen[k] += x; + } +}; + +#endif diff --git a/include/fenwick_tree.h b/include/fenwick_tree.h index ba3f581c..e92d1ab5 100644 --- a/include/fenwick_tree.h +++ b/include/fenwick_tree.h @@ -1,3 +1,19 @@ +/******************************************************************************* + * Fenwick Tree + * + * Data structure providing prefix sums and modify the table in O(log n) - n is the size o the table. + * + * In this algorithm we use two functions: + * - RSQ - This function calculates the range sum query in O(log n) + * - Update - This function adjusts the values in the given range in O(log n) + * + * https://en.wikipedia.org/wiki/Fenwick_tree + * + * @author Gabriel Duarte (gabriellagoa10@yahoo.com.br) + * @github Gabriel123Duarte + * + ******************************************************************************/ + #ifndef __FENWICK_H__ #define __FENWICK_H__ @@ -8,17 +24,18 @@ class Fenwick { private: + // Vector representing the table std::vector fen; public: Fenwick() {} - // We don't use the index 0 + // We don't use the index 0, because it is the base case Fenwick(int n) { fen.assign(n + 1, 0); } - // RSQ 1..a + // Calculate the int rsq(int a) { int ans = 0; @@ -41,4 +58,4 @@ class Fenwick } }; -#endif \ No newline at end of file +#endif From 943b52a8eb2d42b3fb1f92dd00a05f963fbd361f Mon Sep 17 00:00:00 2001 From: Yanzhe Chen Date: Wed, 30 Mar 2016 20:16:12 +0800 Subject: [PATCH 10/42] Add shell sort --- Makefile | 4 ++++ README.md | 1 + include/shell_sort.h | 44 +++++++++++++++++++++++++++++++++++++++++ src/shell_sort_demo.cpp | 32 ++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 include/shell_sort.h create mode 100644 src/shell_sort_demo.cpp diff --git a/Makefile b/Makefile index e9ba280c..c0c50190 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ LIBS = -lm PROGRAMS = m_based_demo \ integer_demo \ insertion_sort_demo \ + shell_sort_demo \ radix_sort_demo \ shuffle_demo \ quick_sort_demo \ @@ -81,6 +82,9 @@ integer_demo: $(SRCDIR)/integer_demo.cpp insertion_sort_demo: $(SRCDIR)/insertion_sort_demo.cpp $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) +shell_sort_demo: $(SRCDIR)/shell_sort_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + radix_sort_demo: $(SRCDIR)/radix_sort_demo.cpp $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) diff --git a/README.md b/README.md index 04958eaf..5bb32f29 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ Bubble sort Selection sort Insertion sort + Shell sort Radix sort Quick sort Merge sort diff --git a/include/shell_sort.h b/include/shell_sort.h new file mode 100644 index 00000000..d3d2db2e --- /dev/null +++ b/include/shell_sort.h @@ -0,0 +1,44 @@ +/******************************************************************************* + * DANIEL'S ALGORITHM IMPLEMENTAIONS + * + * /\ | _ _ ._ o _|_ |_ ._ _ _ + * /--\ | (_| (_) | | |_ | | | | | _> + * _| + * + * SHELL SORT + * + * 1. sort array in O(n^(3/2)) time. + * + * https://en.wikipedia.org/wiki/Shellsort + * + ******************************************************************************/ + +#ifndef __SHELL_SORT_H__ +#define __SHELL_SORT_H__ + +namespace alg { + /** + * shell sort an array + */ + template + static void shell_sort(T *array, int len) { + int h = 1; + while (h < len / 3) { + h = 3 * h + 1; // 1, 4, 13, 40, 121, ... + } + while (h >= 1) { + for (int i = h; i < len; i++) { + int cur = array[i]; + int j = i - h; + while (j >= 0 && array[j] > cur) { + array[j + h] = array[j]; + j = j - h; + } + array[j + h] = cur; + } + h = h / 3; + } + } +} + +#endif // diff --git a/src/shell_sort_demo.cpp b/src/shell_sort_demo.cpp new file mode 100644 index 00000000..e284bd8d --- /dev/null +++ b/src/shell_sort_demo.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +#include "generic.h" +#include "shell_sort.h" + +using namespace alg; + +int main() +{ + const int MAX_ELEMENTS = 10; + int list[MAX_ELEMENTS]; + + int i = 0; + + srand(time(NULL)); + // generate random numbers and fill them to the list + for(i = 0; i < MAX_ELEMENTS; i++ ){ + list[i] = rand()%100; + } + printf("The list before sorting is:\n"); + printlist(list,MAX_ELEMENTS); + + // sort the list using shell sort + shell_sort(&list[0],MAX_ELEMENTS); + + // print the result + printf("The list after sorting using shell sort algorithm:\n"); + printlist(list,MAX_ELEMENTS); + return 0; +} From cc7793cf77d550bd47d18dd55de8c97390f7af01 Mon Sep 17 00:00:00 2001 From: Yanzhe Chen Date: Wed, 30 Mar 2016 22:04:42 +0800 Subject: [PATCH 11/42] Fix bugs in heap --- include/heap.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/heap.h b/include/heap.h index 8712109b..351ba0db 100644 --- a/include/heap.h +++ b/include/heap.h @@ -61,7 +61,7 @@ namespace alg { public: Heap(int max) { m_size = 0; - m_max = max+1; + m_max = max; m_heap = new elem[m_max]; }; @@ -101,7 +101,7 @@ namespace alg { inline void clear() { m_size = 0; } bool contains(const T & data) { - for(int i=1;i<=m_size;i++) { + for(int i=0;i Date: Thu, 31 Mar 2016 09:47:50 +0800 Subject: [PATCH 12/42] indent --- include/shell_sort.h | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/include/shell_sort.h b/include/shell_sort.h index d3d2db2e..22b04de9 100644 --- a/include/shell_sort.h +++ b/include/shell_sort.h @@ -22,22 +22,22 @@ namespace alg { */ template static void shell_sort(T *array, int len) { - int h = 1; - while (h < len / 3) { - h = 3 * h + 1; // 1, 4, 13, 40, 121, ... - } - while (h >= 1) { - for (int i = h; i < len; i++) { - int cur = array[i]; - int j = i - h; - while (j >= 0 && array[j] > cur) { - array[j + h] = array[j]; - j = j - h; - } - array[j + h] = cur; - } - h = h / 3; - } + int h = 1; + while (h < len / 3) { + h = 3 * h + 1; // 1, 4, 13, 40, 121, ... + } + while (h >= 1) { + for (int i = h; i < len; i++) { + int cur = array[i]; + int j = i - h; + while (j >= 0 && array[j] > cur) { + array[j + h] = array[j]; + j = j - h; + } + array[j + h] = cur; + } + h = h / 3; + } } } From c3c99c40033d0e3a6030f1a699a1b233504f85e2 Mon Sep 17 00:00:00 2001 From: xtaci Date: Tue, 26 Apr 2016 17:40:14 +0800 Subject: [PATCH 13/42] update --- README.md | 4 ++++ donate_alg.png | Bin 0 -> 4349 bytes 2 files changed, 4 insertions(+) create mode 100644 donate_alg.png diff --git a/README.md b/README.md index 5bb32f29..b2203990 100644 --- a/README.md +++ b/README.md @@ -101,3 +101,7 @@ ZhangYou0122: Push-Relabel algorithm, Suffix Tree UsingtcNower: Suffix Array afernandez90: AVL trees + +####支持此项目 ( Donations ) : +![donate](donate_alg.png) +欢迎使用支付宝手扫描上面的二维码,对该项目进行捐赠。捐赠款项将用于持续优化补全及完善。 diff --git a/donate_alg.png b/donate_alg.png new file mode 100644 index 0000000000000000000000000000000000000000..04e73bb091a433492a5a564e25cec76445518861 GIT binary patch literal 4349 zcmZWtdpy(o|9`vW(p{7&m!Bdf9CRwT(Kiy!kr26*UlQe3G_2XIi*k2Ja>@OekWE6a z!_i5sOf80CxvVwAVrI6Peb+hlJDv0Us{fc^`dW zt0bXde2^)%bku>ekk(+wvZIU*=M0Yx!`rv56^-TRG`uLJ_I34b4y;~h{a7Y96rewC z1=cU+faXPMu+>Ns#P5^fk|`yYd?%K@Ce-cQLEnfeC2}oDLdc)YA@uMOWl0oAJ-JP;lx)$!J0O48}m1_y&n_bl9$wR6VjN`BfW@sm+ysDeLiLF1$`GAF44ghYJ~hb?wI zUOi!BQ0eh<_`VB=oepguYS15)xVG2L@SQ{Qu9CV|c(iwHR|87(kU5TQXFIK;C$dVf zEDTCda_l{=rK|zZyU9q!G4eu2E-C|o8})X)v(%@D7D4pvwNke>>-!rGV>9R#{Pg(N z+FAI9r0@r`?!raqHjnzyD)3t4*SOm51L=FZav*OpkRn0`wB6wne0|$*s}ko<9vmDv zyBTfaMl0Ww*%}lLjeU02#~PXetx9lJd#82WKb7bL3&>z>sQlm`A$z*=KV`Jxt{nW^ z5_L_puAFhBXHH|K^^aX9zp$$YUG{_C@e*C_u*pf;N)>eyV$szx>Gg7nAKR62plY4a zazhafKLL#;c=4>IGoV{?ljXCGzpicKpL*&QC+E{!iGIDFx&w-m@Je+DV`QTJu-iOn z*sk_#a7Oj^-gPyb^%oJ^i;r)2CF9Jke9&cao560Co}~!njdjUi0}}ks=((0xE9)Y# zIhin8YP%eK16oj7YL9U;@WL?EtpB&O#Tfaf1`4z{2JlC>MM-!;dvA=bTft2&g6Qc( z;FCr=+dE6P=zf;G^ZYXThAJ>`uh{qXi(O%>Q(bb)0ql*h&^oZZB9;3vnSrBMs8T;? z#}qc3sgMc}x)k)Nb(G0;O9sZj53h(nx*2FYVMjCp=be_*e#AkRjPyUS|F6_II0R1j zQXyA@-tk{gFhHhnsJ~8VG0xEt-QX0CmnA8BD+nWGb{x7vY zq!GVngOAraU*QI8>u&6Xh;0QYmfYNs^ zFjE8}dHW*U4pXCe4eWV5^93@pI79>)Df!Wi9l_u(-=JYTRxpa8M)}~GMt9C*kZb)S zX0ne5NCE5bob=R0;v=88 zis8O}kFs<$(Y(n|PH0=w;(|8dQi2jJ6pSNlq7*1@C5$hDFwpEUWUa7&$un{P@P zur0-MVnc?0Fa*YWMka6zV0i8@i^d;A!C3Y~1davD{gs^9Oz`HrgF9u=Oh`Seo{FXO zl2Q#Pn^BZyrR!}5zb~y~mw1SE;$Y1F(3b_D#|WaaOzs#xN_g-d%V-HxA59i)sB^0kgF;=RKiu=tTG6cVr7^a5>8>$n z|3Jc)-aajH@WesMEx3;roV(ND$!qel^7_c!`6aT(Pl@TcU3uHPs&0L%RYhbS1m3@vh079}Ds4~Du7 z7ZSJ#iC|b6k*YTY;@!ML1>};=(D!O`8^41!2V4_kA!PRQ4i4g+VLzg3zAEV1xEWdt z&%JbL;2|Uy*C!1?#5W{*)JS}6ar)Fu^mNy#ojK$^%&tt`eyFXxD1?Wh#cBP8i_h7$ zh5fL%l0{=*<0Ozx9 zf%2)SOb#gB><+QZ3zf7;&;cH5O61WV zF_W$O;T+RE^6I!ttcAYFqPcN`=f!tgje;!Q*&h&cT(A|WIbZ%D(!lMum=F*wsq*mCOo35nzOIv@GaXMoRyHZbCZa=^1p}9)yTxS~OC6cGc?n8?Dm{5@2 z*VQr}(HFVp_eyr3p0a4nWJCD4qe)yhE~>mAv5*w+&GKEM4xm6oEKZEx7yFd*<y@A2nxf)&hA+9o09g|(4BE_ zRX^w|ZQb!oBm2QSpS)zE7xQA<@<$`yD=YdhE4f#;YFda}-&?)u{fu=i5!={9&C9s2 ze0Y99SK*xZ)Wvb(bCo7;C+EVEhHcUsLv80ENEFXVs_udqU;^H3bD=D5q&sHZP(73C z(>k}Jy2fdiVj>e+s}psr^r2UTJMP|yFWFzORa(X|Q+6hPQt>SL0=tC|6)ZInH{Ufo zu{9X@_Eg(_q9qqX2|}EQYQIU7503x(%Q-8b-1V(rKA@U}+N}ylFG*X=t|B9yKism< zfzMUchWJixDWR@~oi7j(Wb;2Dx<5!xN#EK7%q|JIPBGzOr`iwfR=yUeDd$on;X4xW z9EqpG5Q%tJiPJgq9~bU1h1(Nf)|MAm`$_?{<)3nF>&~{QC-sI4MIwy?U#SjG!8Ht# z+j71t^t~N&WIuB8#r@v5V`SRnxiqSi@6Tj`UZZ-%{;Of`;-ofe34`w|Y#5dQ(xYJ@ zsp;z-U1KYjbz;(O9Ph$CiE;54Jb|?<7a8EGUA~^N&OD!iVE|4igFw@=!L_3Bw)ZJV z7%vmVTrKAF50+bk&BKBzbXfJgB{!Jjhls&pVnrbe!2s0QeN&4t9WaAJjdIza@Z?I$ zd<7jb*cqvYW-Y^P10(_3^qasGSJ5w)MAl6;Ym%Rm?VOP%HFHiqrOg{xb*+thT=(CV zlOl-03cg@yf_aj1eK#ljE>kEKOg?tkOs79bTuf5`E+&mA$Y@ky+Y?KSH!kIfP`IM* zw;(il<*~eRR{Wc~e~Vhe3<^)>w8)0go0urW+gErYBEj9nA9SzSeRys)*K*5jGcn8) z&!RJu8rKB8&_5mfBi&n%A&*k%-ATQAd=%xCbS^6F7>OR`?_>QJga4(b2Xnv^1hJ)r zkHl5K7*oW}b0wAP%jb=DkfvIiLyLYao!=AyKN)fVai5}KT$Y-}f?p@GlTwd_6b2Rd zv6UJBH09@oHXNP>Xn9iyOkfZ`0z+_(1MT10z=E2 z>1B)Be(2>9Ak}drr2RTCm0SE`EP{ALA0Hy{jt~alW?HTeDQ>l^1 z$c@{w7}L(|uS1chm}`Q%e+M}bq49KndBF0r?@jz1cf#&Rm;4=zzxN*xK=!uueY~r3 z;K`ZgH2#!RsjQ@C^5w~_q1Sl^ z7`lF^poesuHX?8g2Iq56@1~OK09)@-Dn| z#sGcMbNXl7?!7q{oxef54A6sT4oMb!cuO}PPOnURWPWB>NUA68qK{HKK4tT3m$C=R zYUhD_-D|sEwac%Lf($5^eq~|DoBMVH^b@Z*2uFhKwGv};Ui`@n#wM$5^5k5k&;F*# zHNwD}36@O4&%{GdM!8nsv^)NK(RJ+e^Hm3lUz1N=fHW`o7RA4AFwNOk#<$7ioC5LF z6*tr3TbtiJ>uFrFzkJ@V-W*idD^;)e&PwZ@46eKv>EFC0wcdJG=M2YR0ghlO9zeZ5 zJ1@C6=d8a#$IDiS?aYb0G_4Cs0%qb9PCYEN^0CL#W0rurmbmUc%LGlb|}xRTGIE+shh^s za+J2`d5`7Cw3d5udr41~1B>eoR*$dhlH*Wg{%)B1b-`1ddr1-+!$s1(&FHwO$O1|CL6?g~|r z3^baT8To^DG?#>036~q)t7Ql~XVA->>%y%yGveM@`!Yu}E`8l<#%LP!ED>}J-S4m? zXh(GJYB1xDozqip4Kb}bqBrTUpYkYurMQgNenVchj!(+na5UH+<2G3ljB~kek9nwD z)_ZW*iI&WBdZ0by`T{*>T)Mo-6%9JH+!+=w8wZb6(^7p{uRq8Y=~a518po zyzKZ^&&=dQqyrHS(;lRd_|XZ>uG3kA8Qi4N6>90?GfV2b2*($8zuB^6yw~DlqIp(D znR`i2!Ofd6Pj|4)2Xn<`!0P{<2K?Sxd-dpds{bl@*S36!(1I7Izs(mPYXE!OUu>}d H@xA?Dd2w2* literal 0 HcmV?d00001 From 50035c9941f8fca3d7e810ad8455536f3d5d7bc5 Mon Sep 17 00:00:00 2001 From: xtaci Date: Tue, 26 Apr 2016 18:28:38 +0800 Subject: [PATCH 14/42] update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b2203990..33744dd0 100644 --- a/README.md +++ b/README.md @@ -104,4 +104,4 @@ ####支持此项目 ( Donations ) : ![donate](donate_alg.png) -欢迎使用支付宝手扫描上面的二维码,对该项目进行捐赠。捐赠款项将用于持续优化补全及完善。 +欢迎使用支付宝扫描上面的二维码,对该项目进行捐赠。捐赠款项将用于持续优化补全及完善。 From b8c632b6352e06fdd990fa2c012ebe7eb188cf0c Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 10:39:55 +0900 Subject: [PATCH 15/42] definiton -> definition --- include/binary_search_tree.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/binary_search_tree.h b/include/binary_search_tree.h index 5066c771..2fc544c0 100644 --- a/include/binary_search_tree.h +++ b/include/binary_search_tree.h @@ -31,7 +31,7 @@ namespace alg { class BST { private: /** - * binary search tree definiton. + * binary search tree definition. */ struct treeNode { KeyT key; // key From 841a1bdbba7cd5a52f33fa3fc3e017937e4d2f28 Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 10:44:45 +0900 Subject: [PATCH 16/42] h:55 inital -> initial --- include/dijkstra.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dijkstra.h b/include/dijkstra.h index d0bebe7d..5013f14f 100644 --- a/include/dijkstra.h +++ b/include/dijkstra.h @@ -52,7 +52,7 @@ namespace alg { // all vertices Graph::Adjacent * a; list_for_each_entry(a, &g.list(), a_node){ - dist[a->v.id] = LARGE_NUMBER; // set inital distance to each vertex to a large number + dist[a->v.id] = LARGE_NUMBER; // set initial distance to each vertex to a large number (*previous)[a->v.id] = UNDEFINED; // clear path to UNDEFINED visited[a->v.id] = false; // all vertices are not visited Q.push(LARGE_NUMBER, a->v.id); // push all vertices to heap From 70a90bdde9638c1f5c21b172272409608591e27c Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 10:47:51 +0900 Subject: [PATCH 17/42] h:39 definiton -> definition --- include/hash_table.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/hash_table.h b/include/hash_table.h index 0fcdb713..1c5abd6e 100644 --- a/include/hash_table.h +++ b/include/hash_table.h @@ -36,7 +36,7 @@ namespace alg { typedef _HashCode hash_code_fn; private: /** - * definiton of Key-Value pair. + * definition of Key-Value pair. */ struct HashKV { key_type key; // 32-bit key From a2554338602c9e3c49f0a1e147eb20cd7c5185cc Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 10:51:02 +0900 Subject: [PATCH 18/42] h:16 seperately -> separately h:17 fucntion -> function --- include/merge_sort.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/merge_sort.h b/include/merge_sort.h index 8a45aece..5eab291d 100644 --- a/include/merge_sort.h +++ b/include/merge_sort.h @@ -13,8 +13,8 @@ * and right part. * Example: Say the input is -10 32 45 -78 91 1 0 -16 then the left part will be * -10 32 45 -78 and the right part will be 91 1 0 6. - * (2) Sort Each of them seperately. Note that here sort does not mean to sort it using some other - * method. We already wrote fucntion to sort it. Use the same. + * (2) Sort Each of them separately. Note that here sort does not mean to sort it using some other + * method. We already wrote function to sort it. Use the same. * (3) Then merge the two sorted parts. * * ------------ From 0556913a27d355357b45172a35a26b5c88dd0848 Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 10:54:31 +0900 Subject: [PATCH 19/42] h:78 apropriate -> appropriate --- include/priority_queue.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/priority_queue.h b/include/priority_queue.h index 4a8dbe28..e81edf35 100644 --- a/include/priority_queue.h +++ b/include/priority_queue.h @@ -75,7 +75,7 @@ namespace alg { list_add(&n->node, &m_head); m_count++; } else { - // sequentially find the apropriate position + // sequentially find the appropriate position PQNode * pos; bool found = false; list_for_each_entry(pos, &m_head, node) { From a0ce506be525bb55aad2cf1779adcfd07cd594ed Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 10:57:31 +0900 Subject: [PATCH 20/42] h:100 higer -> higher --- include/skiplist.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/skiplist.h b/include/skiplist.h index 1c330ba7..f45d4f33 100644 --- a/include/skiplist.h +++ b/include/skiplist.h @@ -97,7 +97,7 @@ namespace alg { if(x == NULL || x->key != key) { int lvl = random_level(); // random promotion - // for nodes higer than current max level + // for nodes higher than current max level // make 'header node' as it's prev if(lvl > m_level) { for(int i = m_level + 1; i <= lvl; i++) { From 4cf920ed7d76cbdb2e02a3389fea8609c97499cf Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 11:01:26 +0900 Subject: [PATCH 21/42] h:27: postion -> position h:260: beginnig -> beginning h:269: seperate -> separate --- include/suffix_tree.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/suffix_tree.h b/include/suffix_tree.h index 23bd04d6..d738d9c3 100644 --- a/include/suffix_tree.h +++ b/include/suffix_tree.h @@ -24,7 +24,7 @@ class SuffixTree SuffixTree(string str):test_str(str), root(test_str), active_point(&root, 0, 0), remainder(0), pos(0), active_e(0), ls() {} int construct(void); - // return -1 if no such sub exist, return the beginning postion of this substring in thr original string if it exist + // return -1 if no such sub exist, return the beginning position of this substring in thr original string if it exist int search(string sub); // return the length of the longest prefix of sub which can be matched in suffix tree @@ -257,7 +257,7 @@ class SuffixTree int remainder; // how many characters inserted? unsigned int pos; - unsigned int active_e; // the beginnig position of suffixes need to be inserted + unsigned int active_e; // the beginning position of suffixes need to be inserted char get_ele(int i) { return test_str[i]; } // insert a char from pos to suffix tree int insert(); @@ -266,7 +266,7 @@ class SuffixTree int print_node(Node* node, int level); - Node* seperate_edge(Node * node, Edge* edge); + Node* separate_edge(Node * node, Edge* edge); // check if we can change active node bool check_active_node(void) From 56e957685304afbff7d110821288ffd893e367f9 Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 11:03:08 +0900 Subject: [PATCH 22/42] h:66: arbitary -> arbitrary --- include/universal_hash.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/universal_hash.h b/include/universal_hash.h index 29afcf7d..1b162857 100644 --- a/include/universal_hash.h +++ b/include/universal_hash.h @@ -63,7 +63,7 @@ namespace alg { } /** - * hash an arbitary length integer. + * hash an arbitrary length integer. * len, number of 32-bit integer, max len is 32 */ static uint32_t uhash_bigint(const struct UHash * params, uint32_t * key, uint32_t len) { From 20ecf38f3b78fe96e65b538abf26d0e2e7014096 Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 11:06:34 +0900 Subject: [PATCH 23/42] 85: seperate -> separate --- src/suffix_tree_demo.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/suffix_tree_demo.cpp b/src/suffix_tree_demo.cpp index 8862e9b2..d4425c68 100644 --- a/src/suffix_tree_demo.cpp +++ b/src/suffix_tree_demo.cpp @@ -82,7 +82,7 @@ int SuffixTree::construct(void) ls.ins_link(node); break; } - Node *newnode = seperate_edge(node, a_edge); + Node *newnode = separate_edge(node, a_edge); Edge* newedge = new Edge(pos, numeric_limits::max(), test_str); newnode->add_edge(newedge); ls.ins_link(newnode); @@ -106,7 +106,7 @@ int SuffixTree::construct(void) SuffixTree::Node* SuffixTree::seperate_edge(Node * node, Edge* a_edge) { - cout << "seperate the old edge here: " << (*a_edge) << endl; + cout << "separate the old edge here: " << (*a_edge) << endl; int new_begin = a_edge->begin + get_active_length(); int new_end = a_edge->end; @@ -175,7 +175,7 @@ using namespace std; int main() { - cout << "Begining" << endl; + cout << "Beginning" << endl; SuffixTree st("mississippi"); cout << "Constructing..." << endl; From e9a800739c10c2bbc4c2617cd07b74a62a72f9da Mon Sep 17 00:00:00 2001 From: "Hesham.Safi" Date: Sat, 21 May 2016 15:06:47 +0200 Subject: [PATCH 24/42] issue #46 : include/suffix_tree.h : - fixed build errors ( access modifiers issue) - removed typedef struct ... as it is unnecessary in C++ src/suffic_tree_demo.cpp : - fixed typo in function declaration name (seperate -> separate) --- include/suffix_tree.h | 16 +++------------- src/suffix_tree_demo.cpp | 2 +- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/include/suffix_tree.h b/include/suffix_tree.h index d738d9c3..7fd684e6 100644 --- a/include/suffix_tree.h +++ b/include/suffix_tree.h @@ -31,8 +31,6 @@ class SuffixTree template Iterator inc_search(Iterator sub) { - typedef typename Iterator::value_type T; // extract real type - Iterator result = sub; Node* node = &root; Edge* edge = NULL; @@ -73,13 +71,7 @@ class SuffixTree return result; } - int print_tree(void); -private: - string test_str; - struct Node; - typedef struct Node Node; - struct Edge{ // the begin and end pos of this edge, note that INT_MAX stands for #(the changing end pos of this entire string) unsigned int begin, end; @@ -146,7 +138,6 @@ class SuffixTree bool is_none(void) { return begin == 0 && end == 0; } }; - typedef struct Edge Edge; struct Node{ string& test_node_str; @@ -224,10 +215,9 @@ class SuffixTree return os; } }; - //typedef struct Node Node; - - friend struct Node; - + int print_tree(void); +private: + string test_str; class ActivePoint{ public: Node* active_node; diff --git a/src/suffix_tree_demo.cpp b/src/suffix_tree_demo.cpp index d4425c68..4a5170b5 100644 --- a/src/suffix_tree_demo.cpp +++ b/src/suffix_tree_demo.cpp @@ -104,7 +104,7 @@ int SuffixTree::construct(void) return 0; } -SuffixTree::Node* SuffixTree::seperate_edge(Node * node, Edge* a_edge) +SuffixTree::Node* SuffixTree::separate_edge(Node * node, Edge* a_edge) { cout << "separate the old edge here: " << (*a_edge) << endl; int new_begin = a_edge->begin + get_active_length(); From 7f8d1ab173f3f1d1e9016c39745fb53d7b67134f Mon Sep 17 00:00:00 2001 From: ahmetince Date: Sat, 6 Aug 2016 12:55:28 +0300 Subject: [PATCH 25/42] rearranged I tried to rearrange the code with cpp codes. --- src/random_demo.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/random_demo.cpp b/src/random_demo.cpp index 3c5788a5..d2df1d8c 100644 --- a/src/random_demo.cpp +++ b/src/random_demo.cpp @@ -1,9 +1,9 @@ -#include +#include #include "random.h" int main(void) { - printf("generate random numbers\n"); + std::cout <<"generate random numbers\n"; for (int i=0;i<100;i++) { - printf("%u\n",alg::LCG()); + std::cout< Date: Sun, 7 Aug 2016 21:47:05 +0800 Subject: [PATCH 26/42] add .travis.yml --- .travis.yml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..e4d9ce7f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: cpp + +compiler: + - gcc + - clang + +script: + - make From 788277858072c74ed13c9c6ee14abdb7d8b98bfe Mon Sep 17 00:00:00 2001 From: xtaci Date: Sun, 7 Aug 2016 21:47:59 +0800 Subject: [PATCH 27/42] upd readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 33744dd0..d9b7eb9f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ ###Algorithms & Data Structures in C++ +[![Build Status][1]][2] +[1]: https://travis-ci.org/xtaci/algorithms.svg?branch=master +[2]: https://travis-ci.org/xtaci/algorithms ####目标 ( goal ) : From e30dd73ee55ca26558288b37cf79198a97c3b938 Mon Sep 17 00:00:00 2001 From: Vic Luo Date: Thu, 1 Sep 2016 14:05:37 +0800 Subject: [PATCH 28/42] Correct import guard macro name --- include/2darray.h | 4 ++-- include/8queen.h | 6 +++--- include/astar.h | 4 ++-- include/avl.h | 4 ++-- include/bellman_ford.h | 4 ++-- include/binary_search_tree.h | 4 ++-- include/bitset.h | 4 ++-- include/bloom_filter.h | 4 ++-- include/btree.h | 4 ++-- include/dijkstra.h | 4 ++-- include/directed_graph.h | 4 ++-- include/disjoint-set.h | 4 ++-- include/dos_tree.h | 4 ++-- include/double_linked_list.h | 4 ++-- include/edmonds_karp.h | 4 ++-- include/fenwick_tree.h | 4 ++-- include/fib-heap.h | 4 ++-- include/generic.h | 4 ++-- include/graph_defs.h | 4 ++-- include/graph_search.h | 4 ++-- include/hash_code.h | 4 ++-- include/hash_multi.h | 4 ++-- include/hash_string.h | 4 ++-- include/hash_table.h | 4 ++-- include/heap.h | 4 ++-- include/huffman.h | 4 ++-- include/imath.h | 4 ++-- include/insertion_sort.h | 4 ++-- include/integer.h | 4 ++-- include/interval_tree.h | 4 ++-- include/k-means.h | 4 ++-- include/kmp.h | 4 ++-- include/kruskal_mst.h | 4 ++-- include/lcs.h | 4 ++-- include/md5.h | 4 ++-- include/merge_sort.h | 4 ++-- include/perfect_hash.h | 4 ++-- include/prim_mst.h | 4 ++-- include/prime.h | 4 ++-- include/priority_queue.h | 4 ++-- include/queue.h | 4 ++-- include/quick_sort.h | 4 ++-- include/radix_sort.h | 4 ++-- include/random.h | 4 ++-- include/random_select.h | 4 ++-- include/rbtree.h | 4 ++-- include/rbtree_defs.h | 4 ++-- include/relabel_to_front.h | 4 ++-- include/scc.h | 4 ++-- include/selection_sort.h | 6 +++--- include/sha1.h | 4 ++-- include/shell_sort.h | 4 ++-- include/shuffle.h | 4 ++-- include/simhash.h | 4 ++-- include/skiplist.h | 4 ++-- include/sol.h | 4 ++-- include/stack.h | 4 ++-- include/suffix_array.h | 6 +++--- include/trie.h | 4 ++-- include/undirected_graph.h | 4 ++-- include/universal_hash.h | 4 ++-- include/word_seg.h | 4 ++-- msvc/alg_vs.h | 6 +++--- utils/byteorder.h | 4 ++-- utils/gb18030.h | 4 ++-- 65 files changed, 134 insertions(+), 134 deletions(-) diff --git a/include/2darray.h b/include/2darray.h index 1383e242..929a286a 100644 --- a/include/2darray.h +++ b/include/2darray.h @@ -10,8 +10,8 @@ * Simulated by 1-dimension array. ******************************************************************************/ -#ifndef __2D_ARRAY_H__ -#define __2D_ARRAY_H__ +#ifndef 2D_ARRAY_H__ +#define 2D_ARRAY_H__ #include #include diff --git a/include/8queen.h b/include/8queen.h index 27062e3a..17eaefef 100644 --- a/include/8queen.h +++ b/include/8queen.h @@ -9,8 +9,8 @@ * http://en.wikipedia.org/wiki/Eight_queens_puzzle ******************************************************************************/ -#ifndef __8QUEEN_H__ -#define __8QUEEN_H__ +#ifndef 8QUEEN_H__ +#define 8QUEEN_H__ #include #include @@ -84,4 +84,4 @@ namespace alg { }; } -#endif //__8QUEEN_H__ +#endif //8QUEEN_H__ diff --git a/include/astar.h b/include/astar.h index 03474b34..6dc68f02 100644 --- a/include/astar.h +++ b/include/astar.h @@ -19,8 +19,8 @@ * ******************************************************************************/ -#ifndef __ASTAR_H__ -#define __ASTAR_H__ +#ifndef ASTAR_H__ +#define ASTAR_H__ #include #include diff --git a/include/avl.h b/include/avl.h index 4b8f00cd..aa1ef6ca 100644 --- a/include/avl.h +++ b/include/avl.h @@ -19,8 +19,8 @@ * ******************************************************************************/ -#ifndef __AVL_H__ -#define __AVL_H__ +#ifndef AVL_H__ +#define AVL_H__ #include #include diff --git a/include/bellman_ford.h b/include/bellman_ford.h index 2d6df417..d859d72a 100644 --- a/include/bellman_ford.h +++ b/include/bellman_ford.h @@ -44,8 +44,8 @@ * ******************************************************************************/ -#ifndef __BELLMAN_FORD_H__ -#define __BELLMAN_FORD_H__ +#ifndef BELLMAN_FORD_H__ +#define BELLMAN_FORD_H__ #include #include diff --git a/include/binary_search_tree.h b/include/binary_search_tree.h index 2fc544c0..254cb0f2 100644 --- a/include/binary_search_tree.h +++ b/include/binary_search_tree.h @@ -18,8 +18,8 @@ * ******************************************************************************/ -#ifndef __BINARY_SEARCH_TREE_H__ -#define __BINARY_SEARCH_TREE_H__ +#ifndef BINARY_SEARCH_TREE_H__ +#define BINARY_SEARCH_TREE_H__ #include #include diff --git a/include/bitset.h b/include/bitset.h index 83171c12..fd6ae57b 100644 --- a/include/bitset.h +++ b/include/bitset.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef __BIT_SET_H__ -#define __BIT_SET_H__ +#ifndef BIT_SET_H__ +#define BIT_SET_H__ #include #include diff --git a/include/bloom_filter.h b/include/bloom_filter.h index 810badd5..952d44ab 100644 --- a/include/bloom_filter.h +++ b/include/bloom_filter.h @@ -18,8 +18,8 @@ * ******************************************************************************/ -#ifndef __BLOOM_FILTER_H__ -#define __BLOOM_FILTER_H__ +#ifndef BLOOM_FILTER_H__ +#define BLOOM_FILTER_H__ #include #include diff --git a/include/btree.h b/include/btree.h index bec96048..5f50b6e2 100644 --- a/include/btree.h +++ b/include/btree.h @@ -24,8 +24,8 @@ * http://en.wikipedia.org/wiki/B-tree ******************************************************************************/ -#ifndef __BTREE_H__ -#define __BTREE_H__ +#ifndef BTREE_H__ +#define BTREE_H__ #include #include diff --git a/include/dijkstra.h b/include/dijkstra.h index 5013f14f..73b6c2ee 100644 --- a/include/dijkstra.h +++ b/include/dijkstra.h @@ -19,8 +19,8 @@ * ******************************************************************************/ -#ifndef __DIJKSTRA_H__ -#define __DIJKSTRA_H__ +#ifndef DIJKSTRA_H__ +#define DIJKSTRA_H__ #include #include diff --git a/include/directed_graph.h b/include/directed_graph.h index 0eb8e56e..7694e5a3 100644 --- a/include/directed_graph.h +++ b/include/directed_graph.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef __DIRECTED_GRAPH_H__ -#define __DIRECTED_GRAPH_H__ +#ifndef DIRECTED_GRAPH_H__ +#define DIRECTED_GRAPH_H__ #include #include diff --git a/include/disjoint-set.h b/include/disjoint-set.h index c13a002e..1d1568c4 100644 --- a/include/disjoint-set.h +++ b/include/disjoint-set.h @@ -18,8 +18,8 @@ * http://en.wikipedia.org/wiki/Disjoint-set_data_structure ******************************************************************************/ -#ifndef __DISJOINTSET_H__ -#define __DISJOINTSET_H__ +#ifndef DISJOINTSET_H__ +#define DISJOINTSET_H__ namespace alg { template diff --git a/include/dos_tree.h b/include/dos_tree.h index 7440990f..f754f180 100644 --- a/include/dos_tree.h +++ b/include/dos_tree.h @@ -16,8 +16,8 @@ * ******************************************************************************/ -#ifndef __DOS_TREE_H__ -#define __DOS_TREE_H__ +#ifndef DOS_TREE_H__ +#define DOS_TREE_H__ #include #include diff --git a/include/double_linked_list.h b/include/double_linked_list.h index 7b8a8637..be595e7e 100644 --- a/include/double_linked_list.h +++ b/include/double_linked_list.h @@ -16,8 +16,8 @@ * http://en.wikipedia.org/wiki/Double_linked_list ******************************************************************************/ -#ifndef __DOUBLE_LINKED_LIST_H__ -#define __DOUBLE_LINKED_LIST_H__ +#ifndef DOUBLE_LINKED_LIST_H__ +#define DOUBLE_LINKED_LIST_H__ struct list_head { struct list_head *next, *prev; diff --git a/include/edmonds_karp.h b/include/edmonds_karp.h index 396c9fbc..f9ca4b56 100644 --- a/include/edmonds_karp.h +++ b/include/edmonds_karp.h @@ -21,8 +21,8 @@ * ******************************************************************************/ -#ifndef __EDMONDS_KARP_H__ -#define __EDMONDS_KARP_H__ +#ifndef EDMONDS_KARP_H__ +#define EDMONDS_KARP_H__ #include #include diff --git a/include/fenwick_tree.h b/include/fenwick_tree.h index e92d1ab5..cc12d602 100644 --- a/include/fenwick_tree.h +++ b/include/fenwick_tree.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef __FENWICK_H__ -#define __FENWICK_H__ +#ifndef FENWICK_H__ +#define FENWICK_H__ #include diff --git a/include/fib-heap.h b/include/fib-heap.h index 68100b8b..b5303bf6 100644 --- a/include/fib-heap.h +++ b/include/fib-heap.h @@ -17,8 +17,8 @@ * http://en.wikipedia.org/wiki/Fibonacci_heap ******************************************************************************/ -#ifndef __FIB_HEAP_H__ -#define __FIB_HEAP_H__ +#ifndef FIB_HEAP_H__ +#define FIB_HEAP_H__ #include #include #include diff --git a/include/generic.h b/include/generic.h index 8671f740..16d50e84 100644 --- a/include/generic.h +++ b/include/generic.h @@ -9,8 +9,8 @@ * ******************************************************************************/ -#ifndef __ALG_INC_H__ -#define __ALG_INC_H__ +#ifndef ALG_INC_H__ +#define ALG_INC_H__ #include #include #include diff --git a/include/graph_defs.h b/include/graph_defs.h index 8860c9b1..f2b61ada 100644 --- a/include/graph_defs.h +++ b/include/graph_defs.h @@ -1,5 +1,5 @@ -#ifndef __GRAPH_DEFS_H__ -#define __GRAPH_DEFS_H__ +#ifndef GRAPH_DEFS_H__ +#define GRAPH_DEFS_H__ #include "double_linked_list.h" diff --git a/include/graph_search.h b/include/graph_search.h index 438ea660..7605aed6 100644 --- a/include/graph_search.h +++ b/include/graph_search.h @@ -20,8 +20,8 @@ * ******************************************************************************/ -#ifndef __BREADTH_FIRST_SEARCH_H__ -#define __BREADTH_FIRST_SEARCH_H__ +#ifndef BREADTH_FIRST_SEARCH_H__ +#define BREADTH_FIRST_SEARCH_H__ #include #include diff --git a/include/hash_code.h b/include/hash_code.h index 89f22b2b..7773a108 100644 --- a/include/hash_code.h +++ b/include/hash_code.h @@ -1,5 +1,5 @@ -#ifndef __HASH_CODE_H__ -#define __HASH_CODE_H__ +#ifndef HASH_CODE_H__ +#define HASH_CODE_H__ #include #include "hash_string.h" namespace alg { diff --git a/include/hash_multi.h b/include/hash_multi.h index 4639e229..73040cef 100644 --- a/include/hash_multi.h +++ b/include/hash_multi.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef __HASH_MULTIPLICATION_H__ -#define __HASH_MULTIPLICATION_H__ +#ifndef HASH_MULTIPLICATION_H__ +#define HASH_MULTIPLICATION_H__ #include #include diff --git a/include/hash_string.h b/include/hash_string.h index 52d1b7cf..9a95d4fa 100644 --- a/include/hash_string.h +++ b/include/hash_string.h @@ -12,8 +12,8 @@ * ******************************************************************************/ -#ifndef __STRING_HASH_H__ -#define __STRING_HASH_H__ +#ifndef STRING_HASH_H__ +#define STRING_HASH_H__ #include diff --git a/include/hash_table.h b/include/hash_table.h index 1c5abd6e..9ed41eb3 100644 --- a/include/hash_table.h +++ b/include/hash_table.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef __HASH_TABLE_H__ -#define __HASH_TABLE_H__ +#ifndef HASH_TABLE_H__ +#define HASH_TABLE_H__ #include #include diff --git a/include/heap.h b/include/heap.h index 351ba0db..dec4f9bc 100644 --- a/include/heap.h +++ b/include/heap.h @@ -28,8 +28,8 @@ * http://en.wikipedia.org/wiki/Binary_heap ******************************************************************************/ -#ifndef __HEAP_H__ -#define __HEAP_H__ +#ifndef HEAP_H__ +#define HEAP_H__ #include #include diff --git a/include/huffman.h b/include/huffman.h index b47a4d12..c95fc9b5 100644 --- a/include/huffman.h +++ b/include/huffman.h @@ -21,8 +21,8 @@ * ******************************************************************************/ -#ifndef __HUFFMAN_CODING_H__ -#define __HUFFMAN_CODING_H__ +#ifndef HUFFMAN_CODING_H__ +#define HUFFMAN_CODING_H__ #include #include diff --git a/include/imath.h b/include/imath.h index 6a23ab52..00e1e472 100644 --- a/include/imath.h +++ b/include/imath.h @@ -9,8 +9,8 @@ * ******************************************************************************/ -#ifndef __IMATH_H__ -#define __IMATH_H__ +#ifndef IMATH_H__ +#define IMATH_H__ #include #include diff --git a/include/insertion_sort.h b/include/insertion_sort.h index 42427e4b..8fcf6f42 100644 --- a/include/insertion_sort.h +++ b/include/insertion_sort.h @@ -13,8 +13,8 @@ * ******************************************************************************/ -#ifndef __INSERTION_SORT_H__ -#define __INSERTION_SORT_H__ +#ifndef INSERTION_SORT_H__ +#define INSERTION_SORT_H__ namespace alg { /** diff --git a/include/integer.h b/include/integer.h index f177d5ad..3c8e7d1b 100644 --- a/include/integer.h +++ b/include/integer.h @@ -12,8 +12,8 @@ * ******************************************************************************/ -#ifndef __INTEGER_H__ -#define __INTEGER_H__ +#ifndef INTEGER_H__ +#define INTEGER_H__ #include #include diff --git a/include/interval_tree.h b/include/interval_tree.h index c4edca19..5b6848e2 100644 --- a/include/interval_tree.h +++ b/include/interval_tree.h @@ -16,8 +16,8 @@ * ******************************************************************************/ -#ifndef __INTERVAL_TREE_H__ -#define __INTERVAL_TREE_H__ +#ifndef INTERVAL_TREE_H__ +#define INTERVAL_TREE_H__ #include #include diff --git a/include/k-means.h b/include/k-means.h index 953430de..966c085e 100644 --- a/include/k-means.h +++ b/include/k-means.h @@ -12,8 +12,8 @@ * https://github.com/wycg1984 ******************************************************************************/ -#ifndef __KMEANS_H__ -#define __KMEANS_H__ +#ifndef KMEANS_H__ +#define KMEANS_H__ #include #include #include diff --git a/include/kmp.h b/include/kmp.h index ada982a1..acb38297 100644 --- a/include/kmp.h +++ b/include/kmp.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef __KMP_H__ -#define __KMP_H__ +#ifndef KMP_H__ +#define KMP_H__ #include namespace alg { diff --git a/include/kruskal_mst.h b/include/kruskal_mst.h index d6ed961e..b4f8078e 100644 --- a/include/kruskal_mst.h +++ b/include/kruskal_mst.h @@ -25,8 +25,8 @@ * By Contibutor:xmuliang ******************************************************************************/ -#ifndef __KRUSKAL_MST_H__ -#define __KRUSKAL_MST_H__ +#ifndef KRUSKAL_MST_H__ +#define KRUSKAL_MST_H__ #include #include diff --git a/include/lcs.h b/include/lcs.h index a3c3e9d9..93d6fa5f 100644 --- a/include/lcs.h +++ b/include/lcs.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef __LCS_H__ -#define __LCS_H__ +#ifndef LCS_H__ +#define LCS_H__ #include "generic.h" #include "2darray.h" diff --git a/include/md5.h b/include/md5.h index 5ff52525..3d8c1410 100644 --- a/include/md5.h +++ b/include/md5.h @@ -23,8 +23,8 @@ */ -#ifndef __MD5_H__ -#define __MD5_H__ +#ifndef MD5_H__ +#define MD5_H__ #include /* Data structure for MD5 (Message Digest) computation */ diff --git a/include/merge_sort.h b/include/merge_sort.h index 5eab291d..519c19a2 100644 --- a/include/merge_sort.h +++ b/include/merge_sort.h @@ -32,8 +32,8 @@ * ******************************************************************************/ -#ifndef __MERGE_SORT_H__ -#define __MERGE_SORT_H__ +#ifndef MERGE_SORT_H__ +#define MERGE_SORT_H__ namespace alg { /** diff --git a/include/perfect_hash.h b/include/perfect_hash.h index 52dd9801..28b23191 100644 --- a/include/perfect_hash.h +++ b/include/perfect_hash.h @@ -10,8 +10,8 @@ * http://en.wikipedia.org/wiki/Perfect_hash * ******************************************************************************/ -#ifndef __PERFECT_HASH_H__ -#define __PERFECT_HASH_H__ +#ifndef PERFECT_HASH_H__ +#define PERFECT_HASH_H__ #include #include #include diff --git a/include/prim_mst.h b/include/prim_mst.h index 3949e895..97d8e0a7 100644 --- a/include/prim_mst.h +++ b/include/prim_mst.h @@ -22,8 +22,8 @@ * ******************************************************************************/ -#ifndef __PRIM_MST_H__ -#define __PRIM_MST_H__ +#ifndef PRIM_MST_H__ +#define PRIM_MST_H__ #include #include diff --git a/include/prime.h b/include/prime.h index 81f772e4..593fb81a 100644 --- a/include/prime.h +++ b/include/prime.h @@ -11,8 +11,8 @@ * http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test * ******************************************************************************/ -#ifndef __PRIME_H__ -#define __PRIME_H__ +#ifndef PRIME_H__ +#define PRIME_H__ #include #include diff --git a/include/priority_queue.h b/include/priority_queue.h index e81edf35..10214a83 100644 --- a/include/priority_queue.h +++ b/include/priority_queue.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef __PRIORITY_QUEUE_H__ -#define __PRIORITY_QUEUE_H__ +#ifndef PRIORITY_QUEUE_H__ +#define PRIORITY_QUEUE_H__ #include #include diff --git a/include/queue.h b/include/queue.h index 1de7b292..7e322aec 100644 --- a/include/queue.h +++ b/include/queue.h @@ -13,8 +13,8 @@ * ******************************************************************************/ -#ifndef __QUEUE_H__ -#define __QUEUE_H__ +#ifndef QUEUE_H__ +#define QUEUE_H__ #include #include diff --git a/include/quick_sort.h b/include/quick_sort.h index b8962216..936d0860 100644 --- a/include/quick_sort.h +++ b/include/quick_sort.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef __QUICKSORT_H__ -#define __QUICKSORT_H__ +#ifndef QUICKSORT_H__ +#define QUICKSORT_H__ #include diff --git a/include/radix_sort.h b/include/radix_sort.h index 4272ce48..16298355 100644 --- a/include/radix_sort.h +++ b/include/radix_sort.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef __RADIX_SORT_H__ -#define __RADIX_SORT_H__ +#ifndef RADIX_SORT_H__ +#define RADIX_SORT_H__ #include #include diff --git a/include/random.h b/include/random.h index 456ab698..2ae56aa3 100644 --- a/include/random.h +++ b/include/random.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef __RANDOM_H__ -#define __RANDOM_H__ +#ifndef RANDOM_H__ +#define RANDOM_H__ #include #include diff --git a/include/random_select.h b/include/random_select.h index 3577bdd9..500d0c9a 100644 --- a/include/random_select.h +++ b/include/random_select.h @@ -17,8 +17,8 @@ * ******************************************************************************/ -#ifndef __RANDOM_SELECT_H__ -#define __RANDOM_SELECT_H__ +#ifndef RANDOM_SELECT_H__ +#define RANDOM_SELECT_H__ #include diff --git a/include/rbtree.h b/include/rbtree.h index c9b8a318..bace1061 100644 --- a/include/rbtree.h +++ b/include/rbtree.h @@ -15,8 +15,8 @@ * http://en.literateprograms.org/Red-black_tree_(C) ******************************************************************************/ -#ifndef __RBTREE_H__ -#define __RBTREE_H__ +#ifndef RBTREE_H__ +#define RBTREE_H__ #include #include #include diff --git a/include/rbtree_defs.h b/include/rbtree_defs.h index ab5ef4ea..00395bb4 100644 --- a/include/rbtree_defs.h +++ b/include/rbtree_defs.h @@ -15,8 +15,8 @@ * http://en.literateprograms.org/Red-black_tree_(C) ******************************************************************************/ -#ifndef __RBTREE_DEFS_H__ -#define __RBTREE_DEFS_H__ +#ifndef RBTREE_DEFS_H__ +#define RBTREE_DEFS_H__ #include #include diff --git a/include/relabel_to_front.h b/include/relabel_to_front.h index c8a4d950..8e1ecf37 100644 --- a/include/relabel_to_front.h +++ b/include/relabel_to_front.h @@ -9,8 +9,8 @@ * * */ -#ifndef __RELABEL_TO_FRONT_H__ -#define __RELABEL_TO_FRONT_H__ +#ifndef RELABEL_TO_FRONT_H__ +#define RELABEL_TO_FRONT_H__ #include #include diff --git a/include/scc.h b/include/scc.h index 1d641a1b..5e4b9f44 100644 --- a/include/scc.h +++ b/include/scc.h @@ -17,8 +17,8 @@ * http://en.wikipedia.org/wiki/Strongly_connected_component ******************************************************************************/ -#ifndef __SCC_H__ -#define __SCC_H__ +#ifndef SCC_H__ +#define SCC_H__ #include #include #include diff --git a/include/selection_sort.h b/include/selection_sort.h index a956ad9a..b08f9d96 100644 --- a/include/selection_sort.h +++ b/include/selection_sort.h @@ -17,8 +17,8 @@ * http://en.wikipedia.org/wiki/Selection_sort ******************************************************************************/ -#ifndef __SELECTION_SORT_H__ -#define __SELECTION_SORT_H__ +#ifndef SELECTION_SORT_H__ +#define SELECTION_SORT_H__ #include #include @@ -49,4 +49,4 @@ namespace alg { } } -#endif //__SELECTION_SORT_H__ +#endif //SELECTION_SORT_H__ diff --git a/include/sha1.h b/include/sha1.h index 3079e20c..d68c0a4a 100644 --- a/include/sha1.h +++ b/include/sha1.h @@ -37,8 +37,8 @@ * http://en.wikipedia.org/wiki/SHA-1 */ -#ifndef __SHA1_H__ -#define __SHA1_H__ +#ifndef SHA1_H__ +#define SHA1_H__ #include diff --git a/include/shell_sort.h b/include/shell_sort.h index 22b04de9..c6a8c717 100644 --- a/include/shell_sort.h +++ b/include/shell_sort.h @@ -13,8 +13,8 @@ * ******************************************************************************/ -#ifndef __SHELL_SORT_H__ -#define __SHELL_SORT_H__ +#ifndef SHELL_SORT_H__ +#define SHELL_SORT_H__ namespace alg { /** diff --git a/include/shuffle.h b/include/shuffle.h index fe8b97ab..16f5deab 100644 --- a/include/shuffle.h +++ b/include/shuffle.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef __SHUFFLE_H__ -#define __SHUFFLE_H__ +#ifndef SHUFFLE_H__ +#define SHUFFLE_H__ #include #include diff --git a/include/simhash.h b/include/simhash.h index f68e9e58..d98b87c0 100644 --- a/include/simhash.h +++ b/include/simhash.h @@ -12,8 +12,8 @@ * ******************************************************************************/ -#ifndef __SIMHASH_H__ -#define __SIMHASH_H__ +#ifndef SIMHASH_H__ +#define SIMHASH_H__ #include #include diff --git a/include/skiplist.h b/include/skiplist.h index f45d4f33..e34d8f04 100644 --- a/include/skiplist.h +++ b/include/skiplist.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef __SKIP_LIST_H__ -#define __SKIP_LIST_H__ +#ifndef SKIP_LIST_H__ +#define SKIP_LIST_H__ #include #include #include diff --git a/include/sol.h b/include/sol.h index 070ee4c5..a2e148b5 100644 --- a/include/sol.h +++ b/include/sol.h @@ -16,8 +16,8 @@ * ******************************************************************************/ -#ifndef __SOL_H__ -#define __SOL_H__ +#ifndef SOL_H__ +#define SOL_H__ #include "double_linked_list.h" namespace alg { diff --git a/include/stack.h b/include/stack.h index acc75414..8b664f5e 100644 --- a/include/stack.h +++ b/include/stack.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef __STACK_H__ -#define __STACK_H__ +#ifndef STACK_H__ +#define STACK_H__ #include #include diff --git a/include/suffix_array.h b/include/suffix_array.h index c078bd0a..a47fe2fc 100644 --- a/include/suffix_array.h +++ b/include/suffix_array.h @@ -20,8 +20,8 @@ * AUTHOR: nowerzt@gmail.com ******************************************************************************/ -#ifndef __SUFFIX_ARRAY_H__ -#define __SUFFIX_ARRAY_H__ +#ifndef SUFFIX_ARRAY_H__ +#define SUFFIX_ARRAY_H__ #include #include @@ -100,4 +100,4 @@ namespace alg { } } -#endif // __SUFFIX_ARRAY_H__ +#endif // SUFFIX_ARRAY_H__ diff --git a/include/trie.h b/include/trie.h index c9990f28..ac253704 100644 --- a/include/trie.h +++ b/include/trie.h @@ -10,8 +10,8 @@ * http://en.wikipedia.org/wiki/Trie ******************************************************************************/ -#ifndef __TRIE_H__ -#define __TRIE_H__ +#ifndef TRIE_H__ +#define TRIE_H__ #include #include #include diff --git a/include/undirected_graph.h b/include/undirected_graph.h index 5570267c..c1e4cd08 100644 --- a/include/undirected_graph.h +++ b/include/undirected_graph.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef __UNDIRECTED_GRAPH_H__ -#define __UNDIRECTED_GRAPH_H__ +#ifndef UNDIRECTED_GRAPH_H__ +#define UNDIRECTED_GRAPH_H__ #include #include diff --git a/include/universal_hash.h b/include/universal_hash.h index 1b162857..1a8abe20 100644 --- a/include/universal_hash.h +++ b/include/universal_hash.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef __UNIVERSAL_HASH_H__ -#define __UNIVERSAL_HASH_H__ +#ifndef UNIVERSAL_HASH_H__ +#define UNIVERSAL_HASH_H__ #include #include diff --git a/include/word_seg.h b/include/word_seg.h index eb0f65c7..00528827 100644 --- a/include/word_seg.h +++ b/include/word_seg.h @@ -17,8 +17,8 @@ * ******************************************************************************/ -#ifndef __WORD_SEG_H__ -#define __WORD_SEG_H__ +#ifndef WORD_SEG_H__ +#define WORD_SEG_H__ #include #include diff --git a/msvc/alg_vs.h b/msvc/alg_vs.h index 13b5aa67..78e56b88 100644 --- a/msvc/alg_vs.h +++ b/msvc/alg_vs.h @@ -1,5 +1,5 @@ -#ifndef __ALGVS_H__ -#define __ALGVS_H__ +#ifndef ALGVS_H__ +#define ALGVS_H__ #ifdef _MSC_VER #define _CRT_SECURE_NO_WARNINGS @@ -15,4 +15,4 @@ #define typeof decltype #endif//_MSC_VER -#endif//__ALGVS_H__ \ No newline at end of file +#endif//ALGVS_H__ \ No newline at end of file diff --git a/utils/byteorder.h b/utils/byteorder.h index ca2fada9..1cfcaca9 100644 --- a/utils/byteorder.h +++ b/utils/byteorder.h @@ -1,5 +1,5 @@ -#ifndef __BYTEORDER_H__ -#define __BYTEORDER_H__ +#ifndef BYTEORDER_H__ +#define BYTEORDER_H__ #include #include diff --git a/utils/gb18030.h b/utils/gb18030.h index 88d28e85..fdc2803d 100644 --- a/utils/gb18030.h +++ b/utils/gb18030.h @@ -1,5 +1,5 @@ -#ifndef __GB18030_H__ -#define __GB18030_H__ +#ifndef GB18030_H__ +#define GB18030_H__ /** * Read from the string encoded in GB18030 into WORD From b8b2ed5b72c9e8ab81d99c0e57999d7900f2a92e Mon Sep 17 00:00:00 2001 From: Vic Luo Date: Thu, 1 Sep 2016 14:14:36 +0800 Subject: [PATCH 29/42] Rename all identifiers with 2 underscores at the beginning --- include/2darray.h | 4 ++-- include/8queen.h | 6 +++--- include/astar.h | 4 ++-- include/avl.h | 4 ++-- include/bellman_ford.h | 4 ++-- include/binary_search_tree.h | 12 ++++++------ include/bitset.h | 4 ++-- include/bloom_filter.h | 4 ++-- include/btree.h | 4 ++-- include/dijkstra.h | 4 ++-- include/directed_graph.h | 4 ++-- include/disjoint-set.h | 4 ++-- include/dos_tree.h | 4 ++-- include/double_linked_list.h | 26 +++++++++++++------------- include/edmonds_karp.h | 4 ++-- include/fenwick_tree.h | 4 ++-- include/fib-heap.h | 4 ++-- include/generic.h | 4 ++-- include/graph_defs.h | 4 ++-- include/graph_search.h | 4 ++-- include/hash_code.h | 4 ++-- include/hash_multi.h | 4 ++-- include/hash_string.h | 4 ++-- include/hash_table.h | 4 ++-- include/heap.h | 4 ++-- include/huffman.h | 4 ++-- include/imath.h | 4 ++-- include/insertion_sort.h | 4 ++-- include/integer.h | 4 ++-- include/interval_tree.h | 4 ++-- include/k-means.h | 4 ++-- include/kmp.h | 4 ++-- include/kruskal_mst.h | 4 ++-- include/lcs.h | 4 ++-- include/max_subarray.h | 4 ++-- include/md5.h | 4 ++-- include/merge_sort.h | 8 ++++---- include/perfect_hash.h | 4 ++-- include/prim_mst.h | 4 ++-- include/prime.h | 4 ++-- include/priority_queue.h | 6 +++--- include/queue.h | 4 ++-- include/quick_sort.h | 8 ++++---- include/radix_sort.h | 14 +++++++------- include/random.h | 4 ++-- include/random_select.h | 8 ++++---- include/rbtree.h | 4 ++-- include/rbtree_defs.h | 4 ++-- include/relabel_to_front.h | 4 ++-- include/scc.h | 4 ++-- include/selection_sort.h | 6 +++--- include/sha1.h | 4 ++-- include/shell_sort.h | 4 ++-- include/shuffle.h | 4 ++-- include/simhash.h | 4 ++-- include/skiplist.h | 4 ++-- include/sol.h | 12 ++++++------ include/stack.h | 4 ++-- include/suffix_array.h | 6 +++--- include/trie.h | 4 ++-- include/undirected_graph.h | 4 ++-- include/universal_hash.h | 4 ++-- include/word_seg.h | 4 ++-- msvc/alg_vs.h | 6 +++--- src/lcs_demo.cpp | 6 +++--- utils/byteorder.h | 4 ++-- utils/gb18030.h | 4 ++-- 67 files changed, 170 insertions(+), 170 deletions(-) diff --git a/include/2darray.h b/include/2darray.h index 929a286a..2a565810 100644 --- a/include/2darray.h +++ b/include/2darray.h @@ -10,8 +10,8 @@ * Simulated by 1-dimension array. ******************************************************************************/ -#ifndef 2D_ARRAY_H__ -#define 2D_ARRAY_H__ +#ifndef ALGO_2D_ARRAY_H__ +#define ALGO_2D_ARRAY_H__ #include #include diff --git a/include/8queen.h b/include/8queen.h index 17eaefef..511ebe21 100644 --- a/include/8queen.h +++ b/include/8queen.h @@ -9,8 +9,8 @@ * http://en.wikipedia.org/wiki/Eight_queens_puzzle ******************************************************************************/ -#ifndef 8QUEEN_H__ -#define 8QUEEN_H__ +#ifndef ALGO_8QUEEN_H__ +#define ALGO_8QUEEN_H__ #include #include @@ -84,4 +84,4 @@ namespace alg { }; } -#endif //8QUEEN_H__ +#endif //ALGO_8QUEEN_H__ diff --git a/include/astar.h b/include/astar.h index 6dc68f02..a821488d 100644 --- a/include/astar.h +++ b/include/astar.h @@ -19,8 +19,8 @@ * ******************************************************************************/ -#ifndef ASTAR_H__ -#define ASTAR_H__ +#ifndef ALGO_ASTAR_H__ +#define ALGO_ASTAR_H__ #include #include diff --git a/include/avl.h b/include/avl.h index aa1ef6ca..697317ab 100644 --- a/include/avl.h +++ b/include/avl.h @@ -19,8 +19,8 @@ * ******************************************************************************/ -#ifndef AVL_H__ -#define AVL_H__ +#ifndef ALGO_AVL_H__ +#define ALGO_AVL_H__ #include #include diff --git a/include/bellman_ford.h b/include/bellman_ford.h index d859d72a..40771ae4 100644 --- a/include/bellman_ford.h +++ b/include/bellman_ford.h @@ -44,8 +44,8 @@ * ******************************************************************************/ -#ifndef BELLMAN_FORD_H__ -#define BELLMAN_FORD_H__ +#ifndef ALGO_BELLMAN_FORD_H__ +#define ALGO_BELLMAN_FORD_H__ #include #include diff --git a/include/binary_search_tree.h b/include/binary_search_tree.h index 254cb0f2..574914af 100644 --- a/include/binary_search_tree.h +++ b/include/binary_search_tree.h @@ -18,8 +18,8 @@ * ******************************************************************************/ -#ifndef BINARY_SEARCH_TREE_H__ -#define BINARY_SEARCH_TREE_H__ +#ifndef ALGO_BINARY_SEARCH_TREE_H__ +#define ALGO_BINARY_SEARCH_TREE_H__ #include #include @@ -57,7 +57,7 @@ namespace alg { BST():m_root(NULL){}; ~BST() { - __destruct(m_root); + destruct_(m_root); } /** @@ -159,10 +159,10 @@ namespace alg { } private: - void __destruct(treeNode *n) { + void destruct_(treeNode *n) { if (n==NULL) return; - __destruct(n->left); - __destruct(n->right); + destruct_(n->left); + destruct_(n->right); delete n; } diff --git a/include/bitset.h b/include/bitset.h index fd6ae57b..9e7b9bbc 100644 --- a/include/bitset.h +++ b/include/bitset.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef BIT_SET_H__ -#define BIT_SET_H__ +#ifndef ALGO_BIT_SET_H__ +#define ALGO_BIT_SET_H__ #include #include diff --git a/include/bloom_filter.h b/include/bloom_filter.h index 952d44ab..08253593 100644 --- a/include/bloom_filter.h +++ b/include/bloom_filter.h @@ -18,8 +18,8 @@ * ******************************************************************************/ -#ifndef BLOOM_FILTER_H__ -#define BLOOM_FILTER_H__ +#ifndef ALGO_BLOOM_FILTER_H__ +#define ALGO_BLOOM_FILTER_H__ #include #include diff --git a/include/btree.h b/include/btree.h index 5f50b6e2..6e17050f 100644 --- a/include/btree.h +++ b/include/btree.h @@ -24,8 +24,8 @@ * http://en.wikipedia.org/wiki/B-tree ******************************************************************************/ -#ifndef BTREE_H__ -#define BTREE_H__ +#ifndef ALGO_BTREE_H__ +#define ALGO_BTREE_H__ #include #include diff --git a/include/dijkstra.h b/include/dijkstra.h index 73b6c2ee..853376cc 100644 --- a/include/dijkstra.h +++ b/include/dijkstra.h @@ -19,8 +19,8 @@ * ******************************************************************************/ -#ifndef DIJKSTRA_H__ -#define DIJKSTRA_H__ +#ifndef ALGO_DIJKSTRA_H__ +#define ALGO_DIJKSTRA_H__ #include #include diff --git a/include/directed_graph.h b/include/directed_graph.h index 7694e5a3..9e46cfe0 100644 --- a/include/directed_graph.h +++ b/include/directed_graph.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef DIRECTED_GRAPH_H__ -#define DIRECTED_GRAPH_H__ +#ifndef ALGO_DIRECTED_GRAPH_H__ +#define ALGO_DIRECTED_GRAPH_H__ #include #include diff --git a/include/disjoint-set.h b/include/disjoint-set.h index 1d1568c4..be6bff7d 100644 --- a/include/disjoint-set.h +++ b/include/disjoint-set.h @@ -18,8 +18,8 @@ * http://en.wikipedia.org/wiki/Disjoint-set_data_structure ******************************************************************************/ -#ifndef DISJOINTSET_H__ -#define DISJOINTSET_H__ +#ifndef ALGO_DISJOINTSET_H__ +#define ALGO_DISJOINTSET_H__ namespace alg { template diff --git a/include/dos_tree.h b/include/dos_tree.h index f754f180..42689457 100644 --- a/include/dos_tree.h +++ b/include/dos_tree.h @@ -16,8 +16,8 @@ * ******************************************************************************/ -#ifndef DOS_TREE_H__ -#define DOS_TREE_H__ +#ifndef ALGO_DOS_TREE_H__ +#define ALGO_DOS_TREE_H__ #include #include diff --git a/include/double_linked_list.h b/include/double_linked_list.h index be595e7e..678c66b8 100644 --- a/include/double_linked_list.h +++ b/include/double_linked_list.h @@ -16,8 +16,8 @@ * http://en.wikipedia.org/wiki/Double_linked_list ******************************************************************************/ -#ifndef DOUBLE_LINKED_LIST_H__ -#define DOUBLE_LINKED_LIST_H__ +#ifndef ALGO_DOUBLE_LINKED_LIST_H__ +#define ALGO_DOUBLE_LINKED_LIST_H__ struct list_head { struct list_head *next, *prev; @@ -39,7 +39,7 @@ struct list_head { * the prev/next entries already! */ static inline void -__list_add(struct list_head *n, +list_add_(struct list_head *n, struct list_head *prev, struct list_head *next) { @@ -57,14 +57,14 @@ __list_add(struct list_head *n, * the prev/next entries already! */ static inline void -__list_del(struct list_head *prev, struct list_head *next) +list_del_(struct list_head *prev, struct list_head *next) { next->prev = prev; prev->next = next; } static inline void -__list_splice(struct list_head *list, struct list_head *head) +list_splice_(struct list_head *list, struct list_head *head) { struct list_head *first = list->next; struct list_head *last = list->prev; @@ -88,7 +88,7 @@ __list_splice(struct list_head *list, struct list_head *head) static inline void list_add(struct list_head *n, struct list_head *head) { - __list_add(n, head, head->next); + list_add_(n, head, head->next); } /** @@ -102,7 +102,7 @@ list_add(struct list_head *n, struct list_head *head) static inline void list_add_tail(struct list_head *n, struct list_head *head) { - __list_add(n, head->prev, head); + list_add_(n, head->prev, head); } /** @@ -113,7 +113,7 @@ list_add_tail(struct list_head *n, struct list_head *head) static inline void list_del(struct list_head *entry) { - __list_del(entry->prev, entry->next); + list_del_(entry->prev, entry->next); entry->next = NULL; entry->prev = NULL; } @@ -125,7 +125,7 @@ list_del(struct list_head *entry) static inline void list_del_init(struct list_head *entry) { - __list_del(entry->prev, entry->next); + list_del_(entry->prev, entry->next); INIT_LIST_HEAD(entry); } @@ -137,7 +137,7 @@ list_del_init(struct list_head *entry) static inline void list_move(struct list_head *list, struct list_head *head) { - __list_del(list->prev, list->next); + list_del_(list->prev, list->next); list_add(list, head); } @@ -149,7 +149,7 @@ list_move(struct list_head *list, struct list_head *head) static inline void list_move_tail(struct list_head *list, struct list_head *head) { - __list_del(list->prev, list->next); + list_del_(list->prev, list->next); list_add_tail(list, head); } @@ -172,7 +172,7 @@ static inline void list_splice(struct list_head *list, struct list_head *head) { if (!list_empty(list)) - __list_splice(list, head); + list_splice_(list, head); } /** @@ -186,7 +186,7 @@ static inline void list_splice_init(struct list_head *list, struct list_head *head) { if (!list_empty(list)) { - __list_splice(list, head); + list_splice_(list, head); INIT_LIST_HEAD(list); } } diff --git a/include/edmonds_karp.h b/include/edmonds_karp.h index f9ca4b56..295de725 100644 --- a/include/edmonds_karp.h +++ b/include/edmonds_karp.h @@ -21,8 +21,8 @@ * ******************************************************************************/ -#ifndef EDMONDS_KARP_H__ -#define EDMONDS_KARP_H__ +#ifndef ALGO_EDMONDS_KARP_H__ +#define ALGO_EDMONDS_KARP_H__ #include #include diff --git a/include/fenwick_tree.h b/include/fenwick_tree.h index cc12d602..4806536e 100644 --- a/include/fenwick_tree.h +++ b/include/fenwick_tree.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef FENWICK_H__ -#define FENWICK_H__ +#ifndef ALGO_FENWICK_H__ +#define ALGO_FENWICK_H__ #include diff --git a/include/fib-heap.h b/include/fib-heap.h index b5303bf6..44af130a 100644 --- a/include/fib-heap.h +++ b/include/fib-heap.h @@ -17,8 +17,8 @@ * http://en.wikipedia.org/wiki/Fibonacci_heap ******************************************************************************/ -#ifndef FIB_HEAP_H__ -#define FIB_HEAP_H__ +#ifndef ALGO_FIB_HEAP_H__ +#define ALGO_FIB_HEAP_H__ #include #include #include diff --git a/include/generic.h b/include/generic.h index 16d50e84..785e5078 100644 --- a/include/generic.h +++ b/include/generic.h @@ -9,8 +9,8 @@ * ******************************************************************************/ -#ifndef ALG_INC_H__ -#define ALG_INC_H__ +#ifndef ALGO_ALG_INC_H__ +#define ALGO_ALG_INC_H__ #include #include #include diff --git a/include/graph_defs.h b/include/graph_defs.h index f2b61ada..06e77be2 100644 --- a/include/graph_defs.h +++ b/include/graph_defs.h @@ -1,5 +1,5 @@ -#ifndef GRAPH_DEFS_H__ -#define GRAPH_DEFS_H__ +#ifndef ALGO_GRAPH_DEFS_H__ +#define ALGO_GRAPH_DEFS_H__ #include "double_linked_list.h" diff --git a/include/graph_search.h b/include/graph_search.h index 7605aed6..e0d18f4d 100644 --- a/include/graph_search.h +++ b/include/graph_search.h @@ -20,8 +20,8 @@ * ******************************************************************************/ -#ifndef BREADTH_FIRST_SEARCH_H__ -#define BREADTH_FIRST_SEARCH_H__ +#ifndef ALGO_BREADTH_FIRST_SEARCH_H__ +#define ALGO_BREADTH_FIRST_SEARCH_H__ #include #include diff --git a/include/hash_code.h b/include/hash_code.h index 7773a108..a310580c 100644 --- a/include/hash_code.h +++ b/include/hash_code.h @@ -1,5 +1,5 @@ -#ifndef HASH_CODE_H__ -#define HASH_CODE_H__ +#ifndef ALGO_HASH_CODE_H__ +#define ALGO_HASH_CODE_H__ #include #include "hash_string.h" namespace alg { diff --git a/include/hash_multi.h b/include/hash_multi.h index 73040cef..ec22f65f 100644 --- a/include/hash_multi.h +++ b/include/hash_multi.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef HASH_MULTIPLICATION_H__ -#define HASH_MULTIPLICATION_H__ +#ifndef ALGO_HASH_MULTIPLICATION_H__ +#define ALGO_HASH_MULTIPLICATION_H__ #include #include diff --git a/include/hash_string.h b/include/hash_string.h index 9a95d4fa..6eee926c 100644 --- a/include/hash_string.h +++ b/include/hash_string.h @@ -12,8 +12,8 @@ * ******************************************************************************/ -#ifndef STRING_HASH_H__ -#define STRING_HASH_H__ +#ifndef ALGO_STRING_HASH_H__ +#define ALGO_STRING_HASH_H__ #include diff --git a/include/hash_table.h b/include/hash_table.h index 9ed41eb3..72018610 100644 --- a/include/hash_table.h +++ b/include/hash_table.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef HASH_TABLE_H__ -#define HASH_TABLE_H__ +#ifndef ALGO_HASH_TABLE_H__ +#define ALGO_HASH_TABLE_H__ #include #include diff --git a/include/heap.h b/include/heap.h index dec4f9bc..d793cdab 100644 --- a/include/heap.h +++ b/include/heap.h @@ -28,8 +28,8 @@ * http://en.wikipedia.org/wiki/Binary_heap ******************************************************************************/ -#ifndef HEAP_H__ -#define HEAP_H__ +#ifndef ALGO_HEAP_H__ +#define ALGO_HEAP_H__ #include #include diff --git a/include/huffman.h b/include/huffman.h index c95fc9b5..50088b53 100644 --- a/include/huffman.h +++ b/include/huffman.h @@ -21,8 +21,8 @@ * ******************************************************************************/ -#ifndef HUFFMAN_CODING_H__ -#define HUFFMAN_CODING_H__ +#ifndef ALGO_HUFFMAN_CODING_H__ +#define ALGO_HUFFMAN_CODING_H__ #include #include diff --git a/include/imath.h b/include/imath.h index 00e1e472..b3fa2430 100644 --- a/include/imath.h +++ b/include/imath.h @@ -9,8 +9,8 @@ * ******************************************************************************/ -#ifndef IMATH_H__ -#define IMATH_H__ +#ifndef ALGO_IMATH_H__ +#define ALGO_IMATH_H__ #include #include diff --git a/include/insertion_sort.h b/include/insertion_sort.h index 8fcf6f42..eb9f58ef 100644 --- a/include/insertion_sort.h +++ b/include/insertion_sort.h @@ -13,8 +13,8 @@ * ******************************************************************************/ -#ifndef INSERTION_SORT_H__ -#define INSERTION_SORT_H__ +#ifndef ALGO_INSERTION_SORT_H__ +#define ALGO_INSERTION_SORT_H__ namespace alg { /** diff --git a/include/integer.h b/include/integer.h index 3c8e7d1b..1d5ff609 100644 --- a/include/integer.h +++ b/include/integer.h @@ -12,8 +12,8 @@ * ******************************************************************************/ -#ifndef INTEGER_H__ -#define INTEGER_H__ +#ifndef ALGO_INTEGER_H__ +#define ALGO_INTEGER_H__ #include #include diff --git a/include/interval_tree.h b/include/interval_tree.h index 5b6848e2..ec107364 100644 --- a/include/interval_tree.h +++ b/include/interval_tree.h @@ -16,8 +16,8 @@ * ******************************************************************************/ -#ifndef INTERVAL_TREE_H__ -#define INTERVAL_TREE_H__ +#ifndef ALGO_INTERVAL_TREE_H__ +#define ALGO_INTERVAL_TREE_H__ #include #include diff --git a/include/k-means.h b/include/k-means.h index 966c085e..86fdef3c 100644 --- a/include/k-means.h +++ b/include/k-means.h @@ -12,8 +12,8 @@ * https://github.com/wycg1984 ******************************************************************************/ -#ifndef KMEANS_H__ -#define KMEANS_H__ +#ifndef ALGO_KMEANS_H__ +#define ALGO_KMEANS_H__ #include #include #include diff --git a/include/kmp.h b/include/kmp.h index acb38297..d75d207b 100644 --- a/include/kmp.h +++ b/include/kmp.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef KMP_H__ -#define KMP_H__ +#ifndef ALGO_KMP_H__ +#define ALGO_KMP_H__ #include namespace alg { diff --git a/include/kruskal_mst.h b/include/kruskal_mst.h index b4f8078e..6db3458f 100644 --- a/include/kruskal_mst.h +++ b/include/kruskal_mst.h @@ -25,8 +25,8 @@ * By Contibutor:xmuliang ******************************************************************************/ -#ifndef KRUSKAL_MST_H__ -#define KRUSKAL_MST_H__ +#ifndef ALGO_KRUSKAL_MST_H__ +#define ALGO_KRUSKAL_MST_H__ #include #include diff --git a/include/lcs.h b/include/lcs.h index 93d6fa5f..ce218022 100644 --- a/include/lcs.h +++ b/include/lcs.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef LCS_H__ -#define LCS_H__ +#ifndef ALGO_LCS_H__ +#define ALGO_LCS_H__ #include "generic.h" #include "2darray.h" diff --git a/include/max_subarray.h b/include/max_subarray.h index c791df3b..96b0d943 100644 --- a/include/max_subarray.h +++ b/include/max_subarray.h @@ -21,8 +21,8 @@ * http://en.wikipedia.org/wiki/Maximum_subarray_problem ******************************************************************************/ -#ifndef __MAX_SUBARRAY__ -#define __MAX_SUBARRAY__ +#ifndef MAX_SUBARRAY__ +#define MAX_SUBARRAY__ namespace alg { /** diff --git a/include/md5.h b/include/md5.h index 3d8c1410..284d8c70 100644 --- a/include/md5.h +++ b/include/md5.h @@ -23,8 +23,8 @@ */ -#ifndef MD5_H__ -#define MD5_H__ +#ifndef ALGO_MD5_H__ +#define ALGO_MD5_H__ #include /* Data structure for MD5 (Message Digest) computation */ diff --git a/include/merge_sort.h b/include/merge_sort.h index 519c19a2..b50d1976 100644 --- a/include/merge_sort.h +++ b/include/merge_sort.h @@ -32,15 +32,15 @@ * ******************************************************************************/ -#ifndef MERGE_SORT_H__ -#define MERGE_SORT_H__ +#ifndef ALGO_MERGE_SORT_H__ +#define ALGO_MERGE_SORT_H__ namespace alg { /** * Merge functions merges the two sorted parts. Sorted parts will be from [left, mid] and [mid+1, right]. */ template - static void __merge(T *array, int left, int mid, int right) { + static void merge_(T *array, int left, int mid, int right) { /*We need a Temporary array to store the new sorted part*/ T tempArray[right-left+1]; int pos=0,lpos = left,rpos = mid + 1; @@ -75,7 +75,7 @@ namespace alg { /* Sort the right part */ merge_sort(array,mid+1,right); /* Merge the two sorted parts */ - __merge(array,left,mid,right); + merge_(array,left,mid,right); } } diff --git a/include/perfect_hash.h b/include/perfect_hash.h index 28b23191..55af66c1 100644 --- a/include/perfect_hash.h +++ b/include/perfect_hash.h @@ -10,8 +10,8 @@ * http://en.wikipedia.org/wiki/Perfect_hash * ******************************************************************************/ -#ifndef PERFECT_HASH_H__ -#define PERFECT_HASH_H__ +#ifndef ALGO_PERFECT_HASH_H__ +#define ALGO_PERFECT_HASH_H__ #include #include #include diff --git a/include/prim_mst.h b/include/prim_mst.h index 97d8e0a7..be48ab58 100644 --- a/include/prim_mst.h +++ b/include/prim_mst.h @@ -22,8 +22,8 @@ * ******************************************************************************/ -#ifndef PRIM_MST_H__ -#define PRIM_MST_H__ +#ifndef ALGO_PRIM_MST_H__ +#define ALGO_PRIM_MST_H__ #include #include diff --git a/include/prime.h b/include/prime.h index 593fb81a..357f8b96 100644 --- a/include/prime.h +++ b/include/prime.h @@ -11,8 +11,8 @@ * http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test * ******************************************************************************/ -#ifndef PRIME_H__ -#define PRIME_H__ +#ifndef ALGO_PRIME_H__ +#define ALGO_PRIME_H__ #include #include diff --git a/include/priority_queue.h b/include/priority_queue.h index 10214a83..8795bc08 100644 --- a/include/priority_queue.h +++ b/include/priority_queue.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef PRIORITY_QUEUE_H__ -#define PRIORITY_QUEUE_H__ +#ifndef ALGO_PRIORITY_QUEUE_H__ +#define ALGO_PRIORITY_QUEUE_H__ #include #include @@ -80,7 +80,7 @@ namespace alg { bool found = false; list_for_each_entry(pos, &m_head, node) { if (n->priority <= pos->priority) { - __list_add(&n->node, pos->node.prev, &pos->node); + list_add_(&n->node, pos->node.prev, &pos->node); m_count++; found = true; break; diff --git a/include/queue.h b/include/queue.h index 7e322aec..55051228 100644 --- a/include/queue.h +++ b/include/queue.h @@ -13,8 +13,8 @@ * ******************************************************************************/ -#ifndef QUEUE_H__ -#define QUEUE_H__ +#ifndef ALGO_QUEUE_H__ +#define ALGO_QUEUE_H__ #include #include diff --git a/include/quick_sort.h b/include/quick_sort.h index 936d0860..b3a74489 100644 --- a/include/quick_sort.h +++ b/include/quick_sort.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef QUICKSORT_H__ -#define QUICKSORT_H__ +#ifndef ALGO_QUICKSORT_H__ +#define ALGO_QUICKSORT_H__ #include @@ -25,7 +25,7 @@ namespace alg { * the quick-sort partition routine */ template - static int __partition(T list[],int begin, int end) { + static int partition_(T list[],int begin, int end) { int pivot_idx = RANDOM(begin,end); T pivot = list[pivot_idx]; swap(list[begin], list[pivot_idx]); @@ -52,7 +52,7 @@ namespace alg { template static void quicksort(T list[],int begin,int end) { if( begin < end) { - int pivot_idx = __partition(list, begin, end); + int pivot_idx = partition_(list, begin, end); quicksort(list, begin, pivot_idx-1); quicksort(list, pivot_idx+1, end); } diff --git a/include/radix_sort.h b/include/radix_sort.h index 16298355..ec5e0733 100644 --- a/include/radix_sort.h +++ b/include/radix_sort.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef RADIX_SORT_H__ -#define RADIX_SORT_H__ +#ifndef ALGO_RADIX_SORT_H__ +#define ALGO_RADIX_SORT_H__ #include #include @@ -29,7 +29,7 @@ namespace alg { /** * couting sort */ - static void __radix(int byte, const unsigned N, const uint32_t *source, uint32_t *dest) { + static void radix_(int byte, const unsigned N, const uint32_t *source, uint32_t *dest) { unsigned count[256]; unsigned index[256]; memset(count, 0, sizeof (count)); @@ -51,10 +51,10 @@ namespace alg { */ static void radix_sort(uint32_t *source, const unsigned N) { uint32_t * temp = new uint32_t[N]; - __radix(0, N, source, temp); - __radix(1, N, temp, source); - __radix(2, N, source, temp); - __radix(3, N, temp, source); + radix_(0, N, source, temp); + radix_(1, N, temp, source); + radix_(2, N, source, temp); + radix_(3, N, temp, source); delete [] temp; } diff --git a/include/random.h b/include/random.h index 2ae56aa3..1b2fd82e 100644 --- a/include/random.h +++ b/include/random.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef RANDOM_H__ -#define RANDOM_H__ +#ifndef ALGO_RANDOM_H__ +#define ALGO_RANDOM_H__ #include #include diff --git a/include/random_select.h b/include/random_select.h index 500d0c9a..8893008d 100644 --- a/include/random_select.h +++ b/include/random_select.h @@ -17,8 +17,8 @@ * ******************************************************************************/ -#ifndef RANDOM_SELECT_H__ -#define RANDOM_SELECT_H__ +#ifndef ALGO_RANDOM_SELECT_H__ +#define ALGO_RANDOM_SELECT_H__ #include @@ -27,7 +27,7 @@ namespace alg { * the random_select partition routine */ template - static int __partition(T list[],int begin, int end) { + static int partition_(T list[],int begin, int end) { int pivot_idx = RANDOM(begin,end); T pivot = list[pivot_idx]; swap(list[begin],list[pivot_idx]); @@ -56,7 +56,7 @@ namespace alg { if(begin == end) return begin; - int pivot_idx = __partition(list, begin, end); + int pivot_idx = partition_(list, begin, end); int human_idx = pivot_idx - begin + 1; if(k < human_idx) diff --git a/include/rbtree.h b/include/rbtree.h index bace1061..61c129bd 100644 --- a/include/rbtree.h +++ b/include/rbtree.h @@ -15,8 +15,8 @@ * http://en.literateprograms.org/Red-black_tree_(C) ******************************************************************************/ -#ifndef RBTREE_H__ -#define RBTREE_H__ +#ifndef ALGO_RBTREE_H__ +#define ALGO_RBTREE_H__ #include #include #include diff --git a/include/rbtree_defs.h b/include/rbtree_defs.h index 00395bb4..a99a26b0 100644 --- a/include/rbtree_defs.h +++ b/include/rbtree_defs.h @@ -15,8 +15,8 @@ * http://en.literateprograms.org/Red-black_tree_(C) ******************************************************************************/ -#ifndef RBTREE_DEFS_H__ -#define RBTREE_DEFS_H__ +#ifndef ALGO_RBTREE_DEFS_H__ +#define ALGO_RBTREE_DEFS_H__ #include #include diff --git a/include/relabel_to_front.h b/include/relabel_to_front.h index 8e1ecf37..ef933e02 100644 --- a/include/relabel_to_front.h +++ b/include/relabel_to_front.h @@ -9,8 +9,8 @@ * * */ -#ifndef RELABEL_TO_FRONT_H__ -#define RELABEL_TO_FRONT_H__ +#ifndef ALGO_RELABEL_TO_FRONT_H__ +#define ALGO_RELABEL_TO_FRONT_H__ #include #include diff --git a/include/scc.h b/include/scc.h index 5e4b9f44..ef41af12 100644 --- a/include/scc.h +++ b/include/scc.h @@ -17,8 +17,8 @@ * http://en.wikipedia.org/wiki/Strongly_connected_component ******************************************************************************/ -#ifndef SCC_H__ -#define SCC_H__ +#ifndef ALGO_SCC_H__ +#define ALGO_SCC_H__ #include #include #include diff --git a/include/selection_sort.h b/include/selection_sort.h index b08f9d96..7b0f53e3 100644 --- a/include/selection_sort.h +++ b/include/selection_sort.h @@ -17,8 +17,8 @@ * http://en.wikipedia.org/wiki/Selection_sort ******************************************************************************/ -#ifndef SELECTION_SORT_H__ -#define SELECTION_SORT_H__ +#ifndef ALGO_SELECTION_SORT_H__ +#define ALGO_SELECTION_SORT_H__ #include #include @@ -49,4 +49,4 @@ namespace alg { } } -#endif //SELECTION_SORT_H__ +#endif //ALGO_SELECTION_SORT_H__ diff --git a/include/sha1.h b/include/sha1.h index d68c0a4a..da529b94 100644 --- a/include/sha1.h +++ b/include/sha1.h @@ -37,8 +37,8 @@ * http://en.wikipedia.org/wiki/SHA-1 */ -#ifndef SHA1_H__ -#define SHA1_H__ +#ifndef ALGO_SHA1_H__ +#define ALGO_SHA1_H__ #include diff --git a/include/shell_sort.h b/include/shell_sort.h index c6a8c717..7d9285c2 100644 --- a/include/shell_sort.h +++ b/include/shell_sort.h @@ -13,8 +13,8 @@ * ******************************************************************************/ -#ifndef SHELL_SORT_H__ -#define SHELL_SORT_H__ +#ifndef ALGO_SHELL_SORT_H__ +#define ALGO_SHELL_SORT_H__ namespace alg { /** diff --git a/include/shuffle.h b/include/shuffle.h index 16f5deab..0bdd69cd 100644 --- a/include/shuffle.h +++ b/include/shuffle.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef SHUFFLE_H__ -#define SHUFFLE_H__ +#ifndef ALGO_SHUFFLE_H__ +#define ALGO_SHUFFLE_H__ #include #include diff --git a/include/simhash.h b/include/simhash.h index d98b87c0..650af6c2 100644 --- a/include/simhash.h +++ b/include/simhash.h @@ -12,8 +12,8 @@ * ******************************************************************************/ -#ifndef SIMHASH_H__ -#define SIMHASH_H__ +#ifndef ALGO_SIMHASH_H__ +#define ALGO_SIMHASH_H__ #include #include diff --git a/include/skiplist.h b/include/skiplist.h index e34d8f04..7469511d 100644 --- a/include/skiplist.h +++ b/include/skiplist.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef SKIP_LIST_H__ -#define SKIP_LIST_H__ +#ifndef ALGO_SKIP_LIST_H__ +#define ALGO_SKIP_LIST_H__ #include #include #include diff --git a/include/sol.h b/include/sol.h index a2e148b5..c8064fc8 100644 --- a/include/sol.h +++ b/include/sol.h @@ -16,8 +16,8 @@ * ******************************************************************************/ -#ifndef SOL_H__ -#define SOL_H__ +#ifndef ALGO_SOL_H__ +#define ALGO_SOL_H__ #include "double_linked_list.h" namespace alg { @@ -26,8 +26,8 @@ namespace alg { */ static inline void list_mtf(struct list_head *entry, struct list_head *head) { if (entry->prev == head) return; - __list_del(entry->prev, entry->next); - __list_add(entry, head, head->next); + list_del_(entry->prev, entry->next); + list_add_(entry, head, head->next); } @@ -38,8 +38,8 @@ namespace alg { // if the entry in the 1st position if (entry->prev == head) return; struct list_head * prev = entry->prev; - __list_del(entry->prev, entry->next); - __list_add(entry, prev->prev, prev); + list_del_(entry->prev, entry->next); + list_add_(entry, prev->prev, prev); } } #endif // diff --git a/include/stack.h b/include/stack.h index 8b664f5e..21ab1159 100644 --- a/include/stack.h +++ b/include/stack.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef STACK_H__ -#define STACK_H__ +#ifndef ALGO_STACK_H__ +#define ALGO_STACK_H__ #include #include diff --git a/include/suffix_array.h b/include/suffix_array.h index a47fe2fc..8c7e11fb 100644 --- a/include/suffix_array.h +++ b/include/suffix_array.h @@ -20,8 +20,8 @@ * AUTHOR: nowerzt@gmail.com ******************************************************************************/ -#ifndef SUFFIX_ARRAY_H__ -#define SUFFIX_ARRAY_H__ +#ifndef ALGO_SUFFIX_ARRAY_H__ +#define ALGO_SUFFIX_ARRAY_H__ #include #include @@ -100,4 +100,4 @@ namespace alg { } } -#endif // SUFFIX_ARRAY_H__ +#endif // ALGO_SUFFIX_ARRAY_H__ diff --git a/include/trie.h b/include/trie.h index ac253704..0cdba5ef 100644 --- a/include/trie.h +++ b/include/trie.h @@ -10,8 +10,8 @@ * http://en.wikipedia.org/wiki/Trie ******************************************************************************/ -#ifndef TRIE_H__ -#define TRIE_H__ +#ifndef ALGO_TRIE_H__ +#define ALGO_TRIE_H__ #include #include #include diff --git a/include/undirected_graph.h b/include/undirected_graph.h index c1e4cd08..51804de4 100644 --- a/include/undirected_graph.h +++ b/include/undirected_graph.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef UNDIRECTED_GRAPH_H__ -#define UNDIRECTED_GRAPH_H__ +#ifndef ALGO_UNDIRECTED_GRAPH_H__ +#define ALGO_UNDIRECTED_GRAPH_H__ #include #include diff --git a/include/universal_hash.h b/include/universal_hash.h index 1a8abe20..40c454ce 100644 --- a/include/universal_hash.h +++ b/include/universal_hash.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef UNIVERSAL_HASH_H__ -#define UNIVERSAL_HASH_H__ +#ifndef ALGO_UNIVERSAL_HASH_H__ +#define ALGO_UNIVERSAL_HASH_H__ #include #include diff --git a/include/word_seg.h b/include/word_seg.h index 00528827..ed99b763 100644 --- a/include/word_seg.h +++ b/include/word_seg.h @@ -17,8 +17,8 @@ * ******************************************************************************/ -#ifndef WORD_SEG_H__ -#define WORD_SEG_H__ +#ifndef ALGO_WORD_SEG_H__ +#define ALGO_WORD_SEG_H__ #include #include diff --git a/msvc/alg_vs.h b/msvc/alg_vs.h index 78e56b88..ca1d0163 100644 --- a/msvc/alg_vs.h +++ b/msvc/alg_vs.h @@ -1,5 +1,5 @@ -#ifndef ALGVS_H__ -#define ALGVS_H__ +#ifndef ALGO_ALGVS_H__ +#define ALGO_ALGVS_H__ #ifdef _MSC_VER #define _CRT_SECURE_NO_WARNINGS @@ -15,4 +15,4 @@ #define typeof decltype #endif//_MSC_VER -#endif//ALGVS_H__ \ No newline at end of file +#endif//ALGO_ALGVS_H__ \ No newline at end of file diff --git a/src/lcs_demo.cpp b/src/lcs_demo.cpp index 838c5d4b..71c2db20 100644 --- a/src/lcs_demo.cpp +++ b/src/lcs_demo.cpp @@ -6,9 +6,9 @@ #define printlistC(list,n) \ do { \ - int __list_counter; \ - for(__list_counter=0;__list_counter #include diff --git a/utils/gb18030.h b/utils/gb18030.h index fdc2803d..5da8c9f3 100644 --- a/utils/gb18030.h +++ b/utils/gb18030.h @@ -1,5 +1,5 @@ -#ifndef GB18030_H__ -#define GB18030_H__ +#ifndef ALGO_GB18030_H__ +#define ALGO_GB18030_H__ /** * Read from the string encoded in GB18030 into WORD From 68ad0f22303aec9db74eb6b8dfccc4de2bcc697e Mon Sep 17 00:00:00 2001 From: xtaci Date: Sat, 22 Oct 2016 17:23:02 +0800 Subject: [PATCH 30/42] Update README.md --- README.md | 131 ++++++++++++++++++++++++++---------------------------- 1 file changed, 62 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index d9b7eb9f..dac3795d 100644 --- a/README.md +++ b/README.md @@ -26,75 +26,68 @@ ####已实现 ( Implemented ): - Array shuffle - Prime test(trial division) - Prime test(Miller-Rabin's method) - 2D Array - Arbitrary Integer - Linear congruential generator - Maximum subarray problem - - Bit-Set - Queue - Stack - Binary Heap - Fibonacci Heap - Priority Queue (list based) - - Bubble sort - Selection sort - Insertion sort - Shell sort - Radix sort - Quick sort - Merge sort - Double linked list - Skip list - Self-organized linked-list ops (move-to-front, move-ahead-one) - Largest common sequence - - Binary search tree - AVL tree - Dynamic order statistics - Red-black tree - Interval tree - Prefix Tree(Trie) - Suffix Tree - B-Tree - Suffix Array - - Hash by multiplication - Hash table - Universal hash function - Perfect hash - Java's string hash - FNV-1a string hash - SimHash - Bloom Filter - SHA-1 Message Digest Algorithm - MD5 - Base64 - - Graph data structure - Strongly Connected Components(SCC) - Prim's minimum spanning tree - Kruskal MST - Directed/Undirected graph ops - Breadth First Search - Depth First Search - Dijkstra's algorithm - Bellman-Ford algorithm - Edmonds-Karp Maximal Flow - Push–Relabel algorithm - - Huffman Coding - Word segementation(CHN/GB18030) using HMM and viterbi algorithm. - A* algorithm - K-Means - Knuth–Morris–Pratt algorithm - Disjoint-Set - 8-Queue Problem - Palindrome +| Name | File | +|------|------| +|Array shuffle|https://github.com/xtaci/algorithms/blob/master/include/shuffle.h | +|Prime test(trial division)|https://github.com/xtaci/algorithms/blob/master/include/prime.h| +|Prime test(Miller-Rabin's method)|https://github.com/xtaci/algorithms/blob/master/include/prime.h| +|2D Array|https://github.com/xtaci/algorithms/blob/master/include/2darray.h| +|Arbitrary Integer|https://github.com/xtaci/algorithms/blob/master/include/integer.h| +|Linear congruential generator|https://github.com/xtaci/algorithms/blob/master/include/random.h| +|Maximum subarray problem|https://github.com/xtaci/algorithms/blob/master/include/max_subarray.h| +|Bit-Set|https://github.com/xtaci/algorithms/blob/master/include/bitset.h| +|Queue|https://github.com/xtaci/algorithms/blob/master/include/queue.h| +|Stack|https://github.com/xtaci/algorithms/blob/master/include/stack.h| +|Binary Heap|https://github.com/xtaci/algorithms/blob/master/include/heap.h| +|Fibonacci Heap|https://github.com/xtaci/algorithms/blob/master/include/fib-heap.h| +|Priority Queue (list based)|https://github.com/xtaci/algorithms/blob/master/include/priority_queue.h| +|Bubble sort|https://github.com/xtaci/algorithms/blob/master/include/bubble_sort.h| +|Selection sort|https://github.com/xtaci/algorithms/blob/master/include/selection_sort.h| +|Insertion sort|https://github.com/xtaci/algorithms/blob/master/include/insertion_sort.h| +|Shell sort|https://github.com/xtaci/algorithms/blob/master/include/shell_sort.h| +|Radix sort|https://github.com/xtaci/algorithms/blob/master/include/radix_sort.h| +|Quicksort|https://github.com/xtaci/algorithms/blob/master/include/quick_sort.h| +|Merge sort|https://github.com/xtaci/algorithms/blob/master/include/merge_sort.h| +|Double linked list|https://github.com/xtaci/algorithms/blob/master/include/double_linked_list.h| +|Skip list|https://github.com/xtaci/algorithms/blob/master/include/skiplist.h| +|Largest common sequence|https://github.com/xtaci/algorithms/blob/master/include/lcs.h| +|Binary search tree|https://github.com/xtaci/algorithms/blob/master/include/binary_search_tree.h| +|AVL tree|https://github.com/xtaci/algorithms/blob/master/include/avl.h| +|Dynamic order statistics|https://github.com/xtaci/algorithms/blob/master/include/dos_tree.h| +|Red-black tree|https://github.com/xtaci/algorithms/blob/master/include/rbtree.h| +|Interval tree|https://github.com/xtaci/algorithms/blob/master/include/interval_tree.h| +|Prefix Tree(Trie)|https://github.com/xtaci/algorithms/blob/master/include/trie.h| +|Suffix Tree|https://github.com/xtaci/algorithms/blob/master/include/suffix_tree.h| +|B-Tree|https://github.com/xtaci/algorithms/blob/master/include/btree.h| +|Suffix Array|https://github.com/xtaci/algorithms/blob/master/include/suffix_array.h| +|Hash by multiplication|https://github.com/xtaci/algorithms/blob/master/include/hash_multi.h| +|Hash table|https://github.com/xtaci/algorithms/blob/master/include/hash_table.h| +|Universal hash function|https://github.com/xtaci/algorithms/blob/master/include/universal_hash.h| +|Perfect hash|https://github.com/xtaci/algorithms/blob/master/include/perfect_hash.h| +|Java's string hash|https://github.com/xtaci/algorithms/blob/master/include/hash_string.h| +|FNV-1a string hash|https://github.com/xtaci/algorithms/blob/master/include/hash_string.h| +|SimHash|https://github.com/xtaci/algorithms/blob/master/include/simhash.h| +|Bloom Filter|https://github.com/xtaci/algorithms/blob/master/include/bloom_filter.h| +|SHA-1 Message Digest Algorithm|https://github.com/xtaci/algorithms/blob/master/include/sha1.h| +|MD5|https://github.com/xtaci/algorithms/blob/master/include/md5.h| +|Base64|https://github.com/xtaci/algorithms/blob/master/include/base64.h| +|Strongly Connected Components(SCC)|https://github.com/xtaci/algorithms/blob/master/include/scc.h| +|Prim's minimum spanning tree|https://github.com/xtaci/algorithms/blob/master/include/prim_mst.h| +|Kruskal MST|https://github.com/xtaci/algorithms/blob/master/include/kruskal_mst.h| +|Breadth First Search|https://github.com/xtaci/algorithms/blob/master/include/graph_search.h| +|Depth First Search|https://github.com/xtaci/algorithms/blob/master/include/graph_search.h| +|Dijkstra's algorithm|https://github.com/xtaci/algorithms/blob/master/include/dijkstra.h| +|Bellman-Ford algorithm|https://github.com/xtaci/algorithms/blob/master/include/bellman_ford.h| +|Edmonds-Karp Maximal Flow|https://github.com/xtaci/algorithms/blob/master/include/edmonds_karp.h| +|Push–Relabel algorithm|https://github.com/xtaci/algorithms/blob/master/include/relabel_to_front.h| +|Huffman Coding|https://github.com/xtaci/algorithms/blob/master/include/huffman.h| +|Word segementation|https://github.com/xtaci/algorithms/blob/master/include/word_seg.h| +|A\* algorithm|https://github.com/xtaci/algorithms/blob/master/include/astar.h| +|K-Means|https://github.com/xtaci/algorithms/blob/master/include/k-means.h| +|Knuth–Morris–Pratt algorithm|https://github.com/xtaci/algorithms/blob/master/include/kmp.h| +|Disjoint-Set|https://github.com/xtaci/algorithms/blob/master/include/disjoint-set.h| +|8-Queue Problem|https://github.com/xtaci/algorithms/blob/master/include/8queen.h| +|Palindrome|https://github.com/xtaci/algorithms/blob/master/include/palindrome.h| ####贡献者 ( Contributors ) : Samana: for heavy work of MSVC compatability From c9fae93ad4eadff6b8b2006db5cbf8c560893e60 Mon Sep 17 00:00:00 2001 From: Mohamed Ayman Date: Mon, 26 Dec 2016 00:51:56 +0200 Subject: [PATCH 31/42] Add Lowest commen ancestor algorithm --- src/lca_demo.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/lca_demo.cpp diff --git a/src/lca_demo.cpp b/src/lca_demo.cpp new file mode 100644 index 00000000..7bbf548d --- /dev/null +++ b/src/lca_demo.cpp @@ -0,0 +1,99 @@ +#include + +int numberOfNodes, MAXLOG; +std::vector< std::vector > adjList; +int parent[50005], nodeHeight[50005]; +bool visited[50005]; +int binaryLiftDp[50005][27]; + +void dfs(int currentNode, int currentParent) +{ + visited[currentNode] = true; + parent[currentNode] = currentParent; + nodeHeight[currentNode] = nodeHeight[currentParent] + 1; + int adjacencySize = adjList[currentNode].size(); + for(int idx = 0; idx < adjacencySize; idx++){ + int nextNode = adjList[currentNode][idx]; + if(!visited[nextNode]) + { + dfs(nextNode, currentNode); + } + } +} + +int getMaxLog(){ + int curValue = 1; + int curLog = 1; + while(curValue < numberOfNodes) curValue *= 2, curLog++; + return curLog; +} + +void initializeDP() +{ + nodeHeight[-1] = -1; + MAXLOG = getMaxLog(); + dfs(0, -1); + for(int i = 0; i < numberOfNodes; i++) binaryLiftDp[i][0] = parent[i]; + for(int i = 1; i <= MAXLOG; i++) + { + for(int j = 0; j < numberOfNodes; j++) + { + if(binaryLiftDp[j][i - 1] + 1) + binaryLiftDp[j][i] = binaryLiftDp[binaryLiftDp[j][i - 1]][i - 1]; + else binaryLiftDp[j][i] = -1; + } + } +} + +int LCA(int a, int b) +{ + if(nodeHeight[a] < nodeHeight[b]) std::swap(a,b); + for(int i = MAXLOG; i >= 0; i--) + { + if(binaryLiftDp[a][i] + 1 && nodeHeight[binaryLiftDp[a][i]] >= nodeHeight[b]) + a = binaryLiftDp[a][i]; + } + if(!(a - b)) return a; + for(int i = MAXLOG; i >= 0; i--) + { + if(binaryLiftDp[a][i] + 1 && binaryLiftDp[a][i] - binaryLiftDp[b][i]) + a = binaryLiftDp[a][i], b = binaryLiftDp[b][i]; + } + return parent[a]; +} + +void buildTree() +{ + printf("Enter number of nodes of the tree: "); + scanf("%d", &numberOfNodes); + adjList.resize(numberOfNodes, std::vector ()); + for(int i = 0; i < numberOfNodes - 1; i++) + { + int firstNode, secondNode; + printf("Enter the two nodes to be connected: "); + scanf("%d %d", &firstNode, &secondNode); + adjList[firstNode].push_back(secondNode); + adjList[secondNode].push_back(firstNode); + } +} + +void answerQueries() +{ + int queryCount; + printf("Enter the number of queries: "); + scanf("%d", &queryCount); + for(int i = 0; i < queryCount; i++) + { + int firstNode, secondNode; + printf("Enter the two nodes : "); + scanf("%d %d", &firstNode, &secondNode); + printf("%d\n", LCA(firstNode, secondNode)); + } +} + +int main() +{ + buildTree(); + initializeDP(); + answerQueries(); +} From ffe13e93ee0e0408cd54bdcc1982f247e075d241 Mon Sep 17 00:00:00 2001 From: Mohamed Ayman Date: Mon, 26 Dec 2016 00:56:30 +0200 Subject: [PATCH 32/42] Add const size for array --- src/lca_demo.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/lca_demo.cpp b/src/lca_demo.cpp index 7bbf548d..19002d3e 100644 --- a/src/lca_demo.cpp +++ b/src/lca_demo.cpp @@ -1,10 +1,14 @@ -#include +#include +#include -int numberOfNodes, MAXLOG; +const int MAX_NODE = 5000; +const int MAX_LOG = 20; + +int numberOfNodes, maxLog; std::vector< std::vector > adjList; -int parent[50005], nodeHeight[50005]; -bool visited[50005]; -int binaryLiftDp[50005][27]; +int parent[MAX_NODE], nodeHeight[MAX_NODE]; +bool visited[MAX_NODE]; +int binaryLiftDp[MAX_NODE][MAX_LOG]; void dfs(int currentNode, int currentParent) { @@ -31,10 +35,10 @@ int getMaxLog(){ void initializeDP() { nodeHeight[-1] = -1; - MAXLOG = getMaxLog(); + maxLog = getMaxLog(); dfs(0, -1); for(int i = 0; i < numberOfNodes; i++) binaryLiftDp[i][0] = parent[i]; - for(int i = 1; i <= MAXLOG; i++) + for(int i = 1; i <= maxLog; i++) { for(int j = 0; j < numberOfNodes; j++) { @@ -48,13 +52,13 @@ void initializeDP() int LCA(int a, int b) { if(nodeHeight[a] < nodeHeight[b]) std::swap(a,b); - for(int i = MAXLOG; i >= 0; i--) + for(int i = maxLog; i >= 0; i--) { if(binaryLiftDp[a][i] + 1 && nodeHeight[binaryLiftDp[a][i]] >= nodeHeight[b]) a = binaryLiftDp[a][i]; } if(!(a - b)) return a; - for(int i = MAXLOG; i >= 0; i--) + for(int i = maxLog; i >= 0; i--) { if(binaryLiftDp[a][i] + 1 && binaryLiftDp[a][i] - binaryLiftDp[b][i]) a = binaryLiftDp[a][i], b = binaryLiftDp[b][i]; From e65421342902b29efdd39fd4e7d32086f4c7c261 Mon Sep 17 00:00:00 2001 From: xtaci Date: Fri, 20 Jan 2017 11:08:17 +0800 Subject: [PATCH 33/42] update makefile --- .travis.yml | 3 --- Makefile | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index e4d9ce7f..8226360b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,5 @@ language: cpp - compiler: - - gcc - clang - script: - make diff --git a/Makefile b/Makefile index c0c50190..ee02999c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: all clean -CC=gcc -CPP=g++ +CC=clang +CPP=clang++ AR=ar RANLIB=ranlib CFLAGS= -g -Wall -Wno-unused-function From 5c50c332114874e8e175a83fef12d0b24547d45b Mon Sep 17 00:00:00 2001 From: Mohamed Ayman Date: Tue, 21 Feb 2017 00:19:27 +0200 Subject: [PATCH 34/42] apply OOP principles to LCA --- include/LCA.h | 38 ++++++++++++++ src/lca_demo.cpp | 126 ++++++++++++++++++++++++++--------------------- 2 files changed, 108 insertions(+), 56 deletions(-) create mode 100644 include/LCA.h diff --git a/include/LCA.h b/include/LCA.h new file mode 100644 index 00000000..ef0eb098 --- /dev/null +++ b/include/LCA.h @@ -0,0 +1,38 @@ +/******************************************************************************* + * + * + * /\ | _ _ ._ o _|_ |_ ._ _ _ + * /--\ | (_| (_) | | |_ | | | | | _> + * _| + * + * LCA Finding using Binary Lifting and Dynamic Programming + * + * Features: + * 1. Answers Query about LCA of two nodes in O(log N) + * where N is the total number of nodes in a tree. + * + * https://en.wikipedia.org/wiki/Lowest_common_ancestor + * http://www.csegeek.com/csegeek/view/tutorials/algorithms/trees/tree_part12.php + ******************************************************************************/ + +#ifndef LCA_H +#define LCA_H +#include + +class LCA +{ + public: + LCA(std::vector< std::pair > edges); + int lcaQuery(int a, int b); + + private: + int getMaxLog(); + void initDP(); + void dfs(int currentNode, int currentParent); + std::vector< std::vector > adjList, binaryLiftDp; + std::vector parent, nodeHeight; + std::vector visited; + int _numberOfNodes, _maxLog; +}; + +#endif // LCA_H diff --git a/src/lca_demo.cpp b/src/lca_demo.cpp index 19002d3e..899cc1ba 100644 --- a/src/lca_demo.cpp +++ b/src/lca_demo.cpp @@ -1,16 +1,36 @@ +#include "LCA.h" #include #include +#include +/** +*Constructor is initialized with a Adjacency List that +*describe a tree and If It doesn't describe a tree it asserts failure. +*/ -const int MAX_NODE = 5000; -const int MAX_LOG = 20; - -int numberOfNodes, maxLog; -std::vector< std::vector > adjList; -int parent[MAX_NODE], nodeHeight[MAX_NODE]; -bool visited[MAX_NODE]; -int binaryLiftDp[MAX_NODE][MAX_LOG]; +LCA::LCA(std::vector< std::pair > edges): _numberOfNodes(edges.size() + 1), _maxLog(getMaxLog()) +{ + //First we initialize the needed vectors + parent.resize(_numberOfNodes); + nodeHeight.resize(_numberOfNodes); + visited.resize(_numberOfNodes); + adjList.resize(_numberOfNodes); + binaryLiftDp = std::vector< std::vector >(_numberOfNodes, std::vector(_maxLog)); + /**Construction of the Adjacency List to increase + *The efficiency of the tree traversal to O(V + E). + */ + for(auto edge : edges){ + adjList[edge.first].push_back(edge.second); + adjList[edge.second].push_back(edge.first); + } + //Initialize the Dynamic programming Vector. + initDP(); +} -void dfs(int currentNode, int currentParent) +/** +*DFS is used to find the parent and the height of each node +*allowing the use of Binary Lifting. +*/ +void LCA::dfs(int currentNode, int currentParent) { visited[currentNode] = true; parent[currentNode] = currentParent; @@ -25,40 +45,58 @@ void dfs(int currentNode, int currentParent) } } -int getMaxLog(){ +/** +*Used to Calculate the Log to the base of two +*for the number of the nodes to create the sparse table +*used in binary Lifting. +*/ +int LCA::getMaxLog(){ int curValue = 1; int curLog = 1; - while(curValue < numberOfNodes) curValue *= 2, curLog++; + while(curValue < _numberOfNodes) curValue *= 2, curLog++; return curLog; } -void initializeDP() +void LCA::initDP() { - nodeHeight[-1] = -1; - maxLog = getMaxLog(); dfs(0, -1); - for(int i = 0; i < numberOfNodes; i++) binaryLiftDp[i][0] = parent[i]; - for(int i = 1; i <= maxLog; i++) + for(int i = 0; i < _numberOfNodes; i++) binaryLiftDp[i][0] = parent[i]; + for(int i = 1; i <= _maxLog; i++) { - for(int j = 0; j < numberOfNodes; j++) + for(int j = 0; j < _numberOfNodes; j++) { - if(binaryLiftDp[j][i - 1] + 1) + /** + * Since the ith parent of the current node is equal to + * the ith / 2 parent to the ith /2 parent of the current node + * That's why the Recurrence relation is described as follow + */ + if(binaryLiftDp[j][i - 1] != -1) binaryLiftDp[j][i] = binaryLiftDp[binaryLiftDp[j][i - 1]][i - 1]; else binaryLiftDp[j][i] = -1; } } } -int LCA(int a, int b) +int LCA::lcaQuery(int a, int b) { + /** + * First Both nodes must have same height + * So we will rise the node with the deeper height up in + * the tree to where they're equal. + */ if(nodeHeight[a] < nodeHeight[b]) std::swap(a,b); - for(int i = maxLog; i >= 0; i--) + for(int i = _maxLog; i >= 0; i--) { if(binaryLiftDp[a][i] + 1 && nodeHeight[binaryLiftDp[a][i]] >= nodeHeight[b]) a = binaryLiftDp[a][i]; } - if(!(a - b)) return a; - for(int i = maxLog; i >= 0; i--) + /** + * If the node Lower is the LCA then return it. + * Else keep moving both nodes up as much as they aren't the same + * until it's only 1 node left which is the direct parent of both of them + */ + if(a == b) return a; + for(int i = _maxLog; i >= 0; i--) { if(binaryLiftDp[a][i] + 1 && binaryLiftDp[a][i] - binaryLiftDp[b][i]) a = binaryLiftDp[a][i], b = binaryLiftDp[b][i]; @@ -66,38 +104,14 @@ int LCA(int a, int b) return parent[a]; } -void buildTree() -{ - printf("Enter number of nodes of the tree: "); - scanf("%d", &numberOfNodes); - adjList.resize(numberOfNodes, std::vector ()); - for(int i = 0; i < numberOfNodes - 1; i++) - { - int firstNode, secondNode; - printf("Enter the two nodes to be connected: "); - scanf("%d %d", &firstNode, &secondNode); - adjList[firstNode].push_back(secondNode); - adjList[secondNode].push_back(firstNode); - } -} - -void answerQueries() -{ - int queryCount; - printf("Enter the number of queries: "); - scanf("%d", &queryCount); - for(int i = 0; i < queryCount; i++) - { - int firstNode, secondNode; - printf("Enter the two nodes : "); - scanf("%d %d", &firstNode, &secondNode); - printf("%d\n", LCA(firstNode, secondNode)); - } -} - -int main() -{ - buildTree(); - initializeDP(); - answerQueries(); +int main(){ + std::vector< std::pair > edges; + edges.push_back({0,1}); + edges.push_back({1,2}); + edges.push_back({2,3}); + edges.push_back({1,4}); + LCA* l = new LCA(v); + std::cout << l->lcaQuery(0,1) << endl; + std::cout << l->lcaQuery(3,4) << endl; + std::cout << l->lcaQuery(3,2) << endl; } From 82dd1bb637986c3ed6394e8da09b7ddd5592600a Mon Sep 17 00:00:00 2001 From: xtaci Date: Tue, 21 Feb 2017 10:00:11 +0800 Subject: [PATCH 35/42] Delete .goutputstream-UUQE8X --- include/.goutputstream-UUQE8X | 61 ----------------------------------- 1 file changed, 61 deletions(-) delete mode 100644 include/.goutputstream-UUQE8X diff --git a/include/.goutputstream-UUQE8X b/include/.goutputstream-UUQE8X deleted file mode 100644 index e92d1ab5..00000000 --- a/include/.goutputstream-UUQE8X +++ /dev/null @@ -1,61 +0,0 @@ -/******************************************************************************* - * Fenwick Tree - * - * Data structure providing prefix sums and modify the table in O(log n) - n is the size o the table. - * - * In this algorithm we use two functions: - * - RSQ - This function calculates the range sum query in O(log n) - * - Update - This function adjusts the values in the given range in O(log n) - * - * https://en.wikipedia.org/wiki/Fenwick_tree - * - * @author Gabriel Duarte (gabriellagoa10@yahoo.com.br) - * @github Gabriel123Duarte - * - ******************************************************************************/ - -#ifndef __FENWICK_H__ -#define __FENWICK_H__ - -#include - -#define LSONE(x) (x & (-x)) - -class Fenwick -{ - private: - // Vector representing the table - std::vector fen; - public: - Fenwick() {} - - // We don't use the index 0, because it is the base case - Fenwick(int n) - { - fen.assign(n + 1, 0); - } - - // Calculate the - int rsq(int a) - { - int ans = 0; - for(; a; a -= LSONE(a)) - ans += fen[a]; - return ans; - } - - // RSQ a..b - inline int rsq(int a, int b) - { - return rsq(b) - (a == 1 ? 0 : rsq(a - 1)); - } - - // Update the value of the k-th element by x - void update(int k, int x) - { - for(; k < (int)fen.size(); k += LSONE(k)) - fen[k] += x; - } -}; - -#endif From 1a94def5c1dc01b051df1aa72315cd85757d3ca7 Mon Sep 17 00:00:00 2001 From: Mohamed Ayman Date: Wed, 22 Feb 2017 00:20:42 +0200 Subject: [PATCH 36/42] add LCA entry to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dac3795d..4bd9675a 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ |Disjoint-Set|https://github.com/xtaci/algorithms/blob/master/include/disjoint-set.h| |8-Queue Problem|https://github.com/xtaci/algorithms/blob/master/include/8queen.h| |Palindrome|https://github.com/xtaci/algorithms/blob/master/include/palindrome.h| +|LCA using Binary Lifting|https://github.com/xtaci/algorithms/blob/master/include/LCA.h| ####贡献者 ( Contributors ) : Samana: for heavy work of MSVC compatability From fb003ae73232d5ff619e80fed430a3384fcffca2 Mon Sep 17 00:00:00 2001 From: yrong Date: Sun, 16 Apr 2017 11:33:07 +0800 Subject: [PATCH 37/42] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4bd9675a..58cd686e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [1]: https://travis-ci.org/xtaci/algorithms.svg?branch=master [2]: https://travis-ci.org/xtaci/algorithms -####目标 ( goal ) : +#### 目标 ( goal ) : 1. 经典的算法实现 (classical algorithms implementations) @@ -12,7 +12,7 @@ 3. 正确,易于使用和改造, 一个头文件一个算法,并附带一个demo. (correct! and ease of use, one .header file per algorithm) -####约定 ( Convention ): +#### 约定 ( Convention ): 1. 一个算法用一个.h文件表示放到include下. ( one .header file per algorithm. ) 2. 算法演示的demo程序放到src下. ( one demo per algorithm. ) @@ -24,7 +24,7 @@ eg: ![demograph](demo_graph.png) -####已实现 ( Implemented ): +#### 已实现 ( Implemented ): | Name | File | |------|------| @@ -90,7 +90,7 @@ |Palindrome|https://github.com/xtaci/algorithms/blob/master/include/palindrome.h| |LCA using Binary Lifting|https://github.com/xtaci/algorithms/blob/master/include/LCA.h| -####贡献者 ( Contributors ) : +#### 贡献者 ( Contributors ) : Samana: for heavy work of MSVC compatability wycg1984: for K-Means xmuliang: for HeapSort, Kruskal MST @@ -99,6 +99,6 @@ UsingtcNower: Suffix Array afernandez90: AVL trees -####支持此项目 ( Donations ) : +#### 支持此项目 ( Donations ) : ![donate](donate_alg.png) 欢迎使用支付宝扫描上面的二维码,对该项目进行捐赠。捐赠款项将用于持续优化补全及完善。 From a4333de509ae5f04ddf83b7abc5dfc285021dc24 Mon Sep 17 00:00:00 2001 From: xtaci Date: Sat, 3 Jun 2017 21:00:38 +0800 Subject: [PATCH 38/42] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 58cd686e..b7fcffcb 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ -###Algorithms & Data Structures in C++ +### Algorithms & Data Structures in C++ + [![Build Status][1]][2] + [1]: https://travis-ci.org/xtaci/algorithms.svg?branch=master [2]: https://travis-ci.org/xtaci/algorithms From 21aed1508df1460182d5be231e771a9f99d5428c Mon Sep 17 00:00:00 2001 From: godbod Date: Sun, 18 Feb 2018 22:41:11 +0100 Subject: [PATCH 39/42] This pull request solve the cleaning of *.o file issue --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ee02999c..82f536a5 100644 --- a/Makefile +++ b/Makefile @@ -263,5 +263,5 @@ suffix_array_demo: $(SRCDIR)/suffix_array_demo.cpp $(CPP) $(C11FLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) clean: - rm -rf $(PROGRAMS) *.dSYM + rm -rf $(PROGRAMS) *.dSYM *.o From ed2726e1a8174807f40b26ee717a6795ae050737 Mon Sep 17 00:00:00 2001 From: Saurabh Deepak Shukla <47148573+SaurabhShukla2024@users.noreply.github.com> Date: Fri, 20 Nov 2020 22:25:10 +0530 Subject: [PATCH 40/42] I have fixed the typo in README.md 8-Queen Problem was *-Queue Problem. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b7fcffcb..cf2aa0bf 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ |K-Means|https://github.com/xtaci/algorithms/blob/master/include/k-means.h| |Knuth–Morris–Pratt algorithm|https://github.com/xtaci/algorithms/blob/master/include/kmp.h| |Disjoint-Set|https://github.com/xtaci/algorithms/blob/master/include/disjoint-set.h| -|8-Queue Problem|https://github.com/xtaci/algorithms/blob/master/include/8queen.h| +|8-Queen Problem|https://github.com/xtaci/algorithms/blob/master/include/8queen.h| |Palindrome|https://github.com/xtaci/algorithms/blob/master/include/palindrome.h| |LCA using Binary Lifting|https://github.com/xtaci/algorithms/blob/master/include/LCA.h| From cd540cfef27a25ed0f39acfaa45a2d92e5222450 Mon Sep 17 00:00:00 2001 From: xtaci Date: Wed, 12 May 2021 18:10:26 +0800 Subject: [PATCH 41/42] Update README.md --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index cf2aa0bf..907e484d 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,3 @@ ZhangYou0122: Push-Relabel algorithm, Suffix Tree UsingtcNower: Suffix Array afernandez90: AVL trees - -#### 支持此项目 ( Donations ) : -![donate](donate_alg.png) -欢迎使用支付宝扫描上面的二维码,对该项目进行捐赠。捐赠款项将用于持续优化补全及完善。 From 9449b5d50b940508780f980e6686692ab97e8792 Mon Sep 17 00:00:00 2001 From: xtaci Date: Thu, 1 Aug 2024 17:49:54 +0800 Subject: [PATCH 42/42] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 907e484d..91d25f57 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ [![Build Status][1]][2] -[1]: https://travis-ci.org/xtaci/algorithms.svg?branch=master -[2]: https://travis-ci.org/xtaci/algorithms +[1]: https://img.shields.io/github/created-at/xtaci/algorithms +[2]: https://img.shields.io/github/created-at/xtaci/algorithms #### 目标 ( goal ) :