commit 915661d03a7fa698f169e543b65316356cf783a6 Author: huangsimin Date: Wed Jul 31 19:06:06 2019 +0800 test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1e671b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin +build +.vscode \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..eead66e --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +CC := g++ +C_FLAGS := -std=c++17 -Wall -Wextra -g + +BIN := bin +SRC := src +INCLUDE := include +LIB := lib + +LIBRARIES := + +ifeq ($(OS),Windows_NT) +EXECUTABLE := main.exe +else +EXECUTABLE := main +endif + +all: $(BIN)/$(EXECUTABLE) + +clean: + $(RM) $(BIN)/$(EXECUTABLE) + +run: all + ./$(BIN)/$(EXECUTABLE) + +$(BIN)/$(EXECUTABLE): $(SRC)/* + $(CC) $(C_FLAGS) -I$(INCLUDE) -L$(LIB) $^ -o $@ $(LIBRARIES) \ No newline at end of file diff --git a/src/heap.cpp b/src/heap.cpp new file mode 100644 index 0000000..5812a5a --- /dev/null +++ b/src/heap.cpp @@ -0,0 +1 @@ +#include "heap.h" \ No newline at end of file diff --git a/src/heap.h b/src/heap.h new file mode 100644 index 0000000..8126322 --- /dev/null +++ b/src/heap.h @@ -0,0 +1,51 @@ + +#include +using namespace std; + +template +class Heap +{ +public: + typedef int (*Compare)(T& , T& ); + +private: + std::vector data; + Compare compare; + +public: + Heap() + { + } + + Heap(Compare _compare) + { + this->compare = _compare; + } + + ~Heap() + { + } + + + + void push(T value) + { + unsigned long cur_idx = this->data.size(); + this->data.push_back(value); + + for (;cur_idx != 0;) + { + unsigned long push_idx = (cur_idx - 1) >> 1; + T &push_value = this->data[push_idx]; + if (this->compare(value, push_value) > 0) + { + this->data[cur_idx] = push_value; + cur_idx = push_idx; + } else { + break; + } + } + + this->data[cur_idx] = value; + } +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..4e4af3b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,12 @@ +#include +#include "heap.h" + +int main(int argc, char *argv[]) { + Heap h ; + h.push(3); + h.push(5); + h.push(2); + h.push(7); + h.push(1); + std::cout << "Hello Easy C++ project!" << std::endl; +} \ No newline at end of file