feat: initial commit
This commit is contained in:
commit
c18a85f1cc
7 changed files with 153 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
build/
|
||||||
56
CMakeLists.txt
Normal file
56
CMakeLists.txt
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
project(merk LANGUAGES C CXX)
|
||||||
|
|
||||||
|
# Standards
|
||||||
|
set(CMAKE_C_STANDARD 11)
|
||||||
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
option(BUILD_SHARED_LIBS "Build shared libraries by default" OFF)
|
||||||
|
|
||||||
|
# Collect all source files under src/ (recursively).
|
||||||
|
# CONFIGURE_DEPENDS makes CMake re-scan when files are added/removed.
|
||||||
|
file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/*.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/*.cc"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/*.cpp"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/*.cxx")
|
||||||
|
|
||||||
|
# Decide: executable (if src/main.* exists) or library (otherwise).
|
||||||
|
set(MAIN_CANDIDATES
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/main.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/main.cc"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/main.cpp"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/main.cxx")
|
||||||
|
set(HAVE_MAIN FALSE)
|
||||||
|
foreach(f IN LISTS MAIN_CANDIDATES)
|
||||||
|
if(EXISTS "${f}")
|
||||||
|
set(HAVE_MAIN TRUE)
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
|
||||||
|
if(HAVE_MAIN)
|
||||||
|
add_executable(merk ${PROJECT_SOURCES})
|
||||||
|
set(TARGET_NAME merk)
|
||||||
|
else()
|
||||||
|
add_library(${PROJECT_NAME} ${PROJECT_SOURCES})
|
||||||
|
set(TARGET_NAME ${PROJECT_NAME})
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Public headers live in ./include
|
||||||
|
target_include_directories(${TARGET_NAME}
|
||||||
|
PUBLIC "${CMAKE_SOURCE_DIR}/include"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Sensible warnings
|
||||||
|
if(MSVC)
|
||||||
|
target_compile_options(${TARGET_NAME} PRIVATE /W4 /permissive-)
|
||||||
|
else()
|
||||||
|
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Put built artifacts under build/bin and build/lib
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||||
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||||
|
|
||||||
20
include/action_list.h
Normal file
20
include/action_list.h
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef ACTION_LIST_H
|
||||||
|
#define ACTION_LIST_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "myers.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Action* actions;
|
||||||
|
uint64_t capacity;
|
||||||
|
uint64_t len;
|
||||||
|
} ActionList;
|
||||||
|
|
||||||
|
ActionList* new_list();
|
||||||
|
void add_action(ActionList*, Action);
|
||||||
|
void append_list(ActionList*, ActionList*);
|
||||||
|
|
||||||
|
#endif // ACTION_LIST_H
|
||||||
19
include/myers.h
Normal file
19
include/myers.h
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
#ifndef MYERS_H
|
||||||
|
#define MYERS_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
INSERT,
|
||||||
|
DELETE
|
||||||
|
} ActionType;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
ActionType type;
|
||||||
|
uint64_t line_original;
|
||||||
|
uint64_t line_changed;
|
||||||
|
} Action;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif // MYERS_H
|
||||||
37
src/action_list.c
Normal file
37
src/action_list.c
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
#include "action_list.h"
|
||||||
|
|
||||||
|
ActionList* new_list() {
|
||||||
|
ActionList* list = calloc(1, sizeof(ActionList));
|
||||||
|
list->capacity = 10;
|
||||||
|
list->actions = calloc(list->capacity, sizeof(Action));
|
||||||
|
list->len = 0;
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_action(ActionList* list, Action action) {
|
||||||
|
if (list->len >= list->capacity) {
|
||||||
|
list->capacity *= 2;
|
||||||
|
list->actions = realloc(list->actions, list->capacity*sizeof(Action));
|
||||||
|
if (list->actions == NULL) {
|
||||||
|
perror("Actions list reallocation failed!");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
list->actions[list->len++] = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
void append_list(ActionList* list1, ActionList* list2) {
|
||||||
|
uint64_t available_space = list1->capacity - list1->len;
|
||||||
|
if (available_space < list2->len) {
|
||||||
|
list1->capacity += list2->capacity;
|
||||||
|
list1->actions = realloc(list1->actions, list1->capacity*sizeof(Action));
|
||||||
|
if (list1->actions == NULL) {
|
||||||
|
perror("Actions list reallaction failed!");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(list1->actions + list1->len, list2->actions, list2->len * sizeof *list2->actions);
|
||||||
|
list1->len += list2->len;
|
||||||
|
}
|
||||||
17
src/main.c
Normal file
17
src/main.c
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#include "myers.h"
|
||||||
|
#include "action_list.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Action act1 = (Action){.type=INSERT, .line_original=0, .line_changed=1};
|
||||||
|
Action act2 = (Action){.type=DELETE, .line_original=1, .line_changed=2};
|
||||||
|
|
||||||
|
ActionList* list1 = new_list();
|
||||||
|
add_action(list1, act1);
|
||||||
|
|
||||||
|
ActionList* list2 = new_list();
|
||||||
|
add_action(list2, act2);
|
||||||
|
|
||||||
|
append_list(list1, list2);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
3
src/myers.c
Normal file
3
src/myers.c
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
#include "myers.h"
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue