forked from etcd-io/raft
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request etcd-io#2 from kite707/feat/majority-String_Slice_…
…insertionSort Feat/majority string slice insertion sort
- Loading branch information
Showing
8 changed files
with
139 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.idea | ||
*.a | ||
*.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
|
||
int compare(const void *a, const void *b){ | ||
return(*(long long unsigned int*)a - *(long long unsigned int*)b); | ||
} | ||
|
||
// sort slice by ascending & slice -> string | ||
const char* cMajorityConfig(void* p, int size) { | ||
long long unsigned int* sl = (long long unsigned int*) p; | ||
|
||
qsort(sl, size, sizeof(long long unsigned int), compare); | ||
|
||
char* buf = (char*)malloc((2 * size + 3) * sizeof(char)); | ||
|
||
int index = 0; | ||
buf[index++] = '('; | ||
for (int i = 0; i < size; i++) { | ||
if (i > 0) { | ||
buf[index++] = ' '; | ||
} | ||
index += sprintf(buf + index, "%llu", sl[i]); | ||
} | ||
buf[index++] = ')'; | ||
buf[index] = '\0'; | ||
|
||
return buf; | ||
} | ||
|
||
// sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] }) 의 역활 | ||
void cSlice(void* p, int size) { | ||
// 형변환 | ||
long long unsigned int* arr = (long long unsigned int*)p; | ||
qsort(arr, size, sizeof(long long unsigned int), compare); | ||
} | ||
|
||
void cinsertionSort(void* p, int size) { | ||
long long unsigned int* sl = (long long unsigned int*) p; | ||
int a = 0, b = size; | ||
for (int i = a + 1; i < b; i++) { | ||
for (int j = i; j > a && sl[j] < sl[j-1]; j--) { | ||
long long unsigned int tmp = sl[j]; | ||
sl[j] = sl[j-1]; | ||
sl[j-1] = tmp; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//quorum.h | ||
#ifndef _MAJORITY_H | ||
#define _MAJORITY_H | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
|
||
int compare(const void *a, const void *b); | ||
|
||
// sort slice by ascending & slice -> string | ||
const char* cMajorityConfig(void* p, int size); | ||
|
||
// sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] }) 의 역활 | ||
void cSlice(void* p, int size); | ||
|
||
void cinsertionSort(void* p, int size); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef _QUORUM_H | ||
#define _QUORUM_H | ||
|
||
#include <inttypes.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
|
||
typedef uint64_t Index; | ||
|
||
const char *index_to_string(uint64_t i); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include "quorum.h" | ||
|
||
const char *index_to_string(uint64_t i) { | ||
static char buffer[21]; // buffer size for uint64_t | ||
if (i == UINT64_MAX) { | ||
return "∞"; | ||
} | ||
snprintf(buffer, sizeof(buffer), "%" PRIu64, (uint64_t)i); | ||
return buffer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef _QUORUM_H | ||
#define _QUORUM_H | ||
|
||
#include <inttypes.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
|
||
typedef uint64_t Index; | ||
|
||
const char *index_to_string(uint64_t i); | ||
|
||
#endif |