diff --git a/Spring-2023/CS-2124/Assignment-2/.clang-format b/Spring-2023/CS-2124/Assignment-2/.clang-format new file mode 100644 index 0000000..bf3583d --- /dev/null +++ b/Spring-2023/CS-2124/Assignment-2/.clang-format @@ -0,0 +1,225 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAfterAttributes: Never +BreakAfterJavaFieldAnnotations: false +BreakArrays: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: Attach +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: false +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +LineEnding: DeriveLF +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PackConstructorInitializers: BinPack +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +PPIndentWidth: -1 +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseTab: Never +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE +... + diff --git a/Spring-2023/CS-2124/Assignment-2/.gitignore b/Spring-2023/CS-2124/Assignment-2/.gitignore new file mode 100644 index 0000000..b19ba41 --- /dev/null +++ b/Spring-2023/CS-2124/Assignment-2/.gitignore @@ -0,0 +1,5 @@ +bin +CMakeFiles +CMakeCache.txt +cmake_install.cmake +README.pdf diff --git a/Spring-2023/CS-2124/Assignment-2/Assignment.pdf b/Spring-2023/CS-2124/Assignment-2/Assignment.pdf new file mode 100644 index 0000000..44a6e34 Binary files /dev/null and b/Spring-2023/CS-2124/Assignment-2/Assignment.pdf differ diff --git a/Spring-2023/CS-2124/Assignment-2/CMakeLists.txt b/Spring-2023/CS-2124/Assignment-2/CMakeLists.txt new file mode 100644 index 0000000..fe68030 --- /dev/null +++ b/Spring-2023/CS-2124/Assignment-2/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.25) + +set(SOURCES src/lib/lib.h src/lib/lib.c src/lib/vec.h) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin) + + +project(AssignmentTwo LANGUAGES C) + +add_executable(PartOne src/part_one.c ${SOURCES}) +add_executable(PartTwo src/part_two.c ${SOURCES}) +add_executable(PartThree src/part_three.c ${SOURCES}) diff --git a/Spring-2023/CS-2124/Assignment-2/Makefile b/Spring-2023/CS-2124/Assignment-2/Makefile new file mode 100644 index 0000000..090ce30 --- /dev/null +++ b/Spring-2023/CS-2124/Assignment-2/Makefile @@ -0,0 +1,296 @@ +# CMAKE generated file: DO NOT EDIT! +# Generated by "Unix Makefiles" Generator, CMake Version 3.28 + +# Default target executed when no arguments are given to make. +default_target: all +.PHONY : default_target + +# Allow only one "make -f Makefile2" at a time, but pass parallelism. +.NOTPARALLEL: + +#============================================================================= +# Special targets provided by cmake. + +# Disable implicit rules so canonical targets will work. +.SUFFIXES: + +# Disable VCS-based implicit rules. +% : %,v + +# Disable VCS-based implicit rules. +% : RCS/% + +# Disable VCS-based implicit rules. +% : RCS/%,v + +# Disable VCS-based implicit rules. +% : SCCS/s.% + +# Disable VCS-based implicit rules. +% : s.% + +.SUFFIXES: .hpux_make_needs_suffix_list + +# Command-line flag to silence nested $(MAKE). +$(VERBOSE)MAKESILENT = -s + +#Suppress display of executed commands. +$(VERBOSE).SILENT: + +# A target that is always out of date. +cmake_force: +.PHONY : cmake_force + +#============================================================================= +# Set environment variables for the build. + +# The shell in which to execute make rules. +SHELL = /bin/sh + +# The CMake executable. +CMAKE_COMMAND = /usr/bin/cmake + +# The command to remove a file. +RM = /usr/bin/cmake -E rm -f + +# Escaping for special characters. +EQUALS = = + +# The top-level source directory on which CMake was run. +CMAKE_SOURCE_DIR = /home/sam/Git/College/Spring-2023/CS-2124/Assignment-2 + +# The top-level build directory on which CMake was run. +CMAKE_BINARY_DIR = /home/sam/Git/College/Spring-2023/CS-2124/Assignment-2 + +#============================================================================= +# Targets provided globally by CMake. + +# Special rule for the target edit_cache +edit_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake cache editor..." + /usr/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : edit_cache + +# Special rule for the target edit_cache +edit_cache/fast: edit_cache +.PHONY : edit_cache/fast + +# Special rule for the target rebuild_cache +rebuild_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake to regenerate build system..." + /usr/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : rebuild_cache + +# Special rule for the target rebuild_cache +rebuild_cache/fast: rebuild_cache +.PHONY : rebuild_cache/fast + +# The main all target +all: cmake_check_build_system + $(CMAKE_COMMAND) -E cmake_progress_start /home/sam/Git/College/Spring-2023/CS-2124/Assignment-2/CMakeFiles /home/sam/Git/College/Spring-2023/CS-2124/Assignment-2//CMakeFiles/progress.marks + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all + $(CMAKE_COMMAND) -E cmake_progress_start /home/sam/Git/College/Spring-2023/CS-2124/Assignment-2/CMakeFiles 0 +.PHONY : all + +# The main clean target +clean: + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 clean +.PHONY : clean + +# The main clean target +clean/fast: clean +.PHONY : clean/fast + +# Prepare targets for installation. +preinstall: all + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall + +# Prepare targets for installation. +preinstall/fast: + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall/fast + +# clear depends +depend: + $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 +.PHONY : depend + +#============================================================================= +# Target rules for targets named PartOne + +# Build rule for target. +PartOne: cmake_check_build_system + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 PartOne +.PHONY : PartOne + +# fast build rule for target. +PartOne/fast: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartOne.dir/build.make CMakeFiles/PartOne.dir/build +.PHONY : PartOne/fast + +#============================================================================= +# Target rules for targets named PartTwo + +# Build rule for target. +PartTwo: cmake_check_build_system + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 PartTwo +.PHONY : PartTwo + +# fast build rule for target. +PartTwo/fast: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartTwo.dir/build.make CMakeFiles/PartTwo.dir/build +.PHONY : PartTwo/fast + +#============================================================================= +# Target rules for targets named PartThree + +# Build rule for target. +PartThree: cmake_check_build_system + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 PartThree +.PHONY : PartThree + +# fast build rule for target. +PartThree/fast: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartThree.dir/build.make CMakeFiles/PartThree.dir/build +.PHONY : PartThree/fast + +src/lib/lib.o: src/lib/lib.c.o +.PHONY : src/lib/lib.o + +# target to build an object file +src/lib/lib.c.o: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartOne.dir/build.make CMakeFiles/PartOne.dir/src/lib/lib.c.o + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartTwo.dir/build.make CMakeFiles/PartTwo.dir/src/lib/lib.c.o + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartThree.dir/build.make CMakeFiles/PartThree.dir/src/lib/lib.c.o +.PHONY : src/lib/lib.c.o + +src/lib/lib.i: src/lib/lib.c.i +.PHONY : src/lib/lib.i + +# target to preprocess a source file +src/lib/lib.c.i: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartOne.dir/build.make CMakeFiles/PartOne.dir/src/lib/lib.c.i + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartTwo.dir/build.make CMakeFiles/PartTwo.dir/src/lib/lib.c.i + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartThree.dir/build.make CMakeFiles/PartThree.dir/src/lib/lib.c.i +.PHONY : src/lib/lib.c.i + +src/lib/lib.s: src/lib/lib.c.s +.PHONY : src/lib/lib.s + +# target to generate assembly for a file +src/lib/lib.c.s: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartOne.dir/build.make CMakeFiles/PartOne.dir/src/lib/lib.c.s + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartTwo.dir/build.make CMakeFiles/PartTwo.dir/src/lib/lib.c.s + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartThree.dir/build.make CMakeFiles/PartThree.dir/src/lib/lib.c.s +.PHONY : src/lib/lib.c.s + +src/part_one.o: src/part_one.c.o +.PHONY : src/part_one.o + +# target to build an object file +src/part_one.c.o: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartOne.dir/build.make CMakeFiles/PartOne.dir/src/part_one.c.o +.PHONY : src/part_one.c.o + +src/part_one.i: src/part_one.c.i +.PHONY : src/part_one.i + +# target to preprocess a source file +src/part_one.c.i: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartOne.dir/build.make CMakeFiles/PartOne.dir/src/part_one.c.i +.PHONY : src/part_one.c.i + +src/part_one.s: src/part_one.c.s +.PHONY : src/part_one.s + +# target to generate assembly for a file +src/part_one.c.s: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartOne.dir/build.make CMakeFiles/PartOne.dir/src/part_one.c.s +.PHONY : src/part_one.c.s + +src/part_three.o: src/part_three.c.o +.PHONY : src/part_three.o + +# target to build an object file +src/part_three.c.o: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartThree.dir/build.make CMakeFiles/PartThree.dir/src/part_three.c.o +.PHONY : src/part_three.c.o + +src/part_three.i: src/part_three.c.i +.PHONY : src/part_three.i + +# target to preprocess a source file +src/part_three.c.i: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartThree.dir/build.make CMakeFiles/PartThree.dir/src/part_three.c.i +.PHONY : src/part_three.c.i + +src/part_three.s: src/part_three.c.s +.PHONY : src/part_three.s + +# target to generate assembly for a file +src/part_three.c.s: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartThree.dir/build.make CMakeFiles/PartThree.dir/src/part_three.c.s +.PHONY : src/part_three.c.s + +src/part_two.o: src/part_two.c.o +.PHONY : src/part_two.o + +# target to build an object file +src/part_two.c.o: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartTwo.dir/build.make CMakeFiles/PartTwo.dir/src/part_two.c.o +.PHONY : src/part_two.c.o + +src/part_two.i: src/part_two.c.i +.PHONY : src/part_two.i + +# target to preprocess a source file +src/part_two.c.i: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartTwo.dir/build.make CMakeFiles/PartTwo.dir/src/part_two.c.i +.PHONY : src/part_two.c.i + +src/part_two.s: src/part_two.c.s +.PHONY : src/part_two.s + +# target to generate assembly for a file +src/part_two.c.s: + $(MAKE) $(MAKESILENT) -f CMakeFiles/PartTwo.dir/build.make CMakeFiles/PartTwo.dir/src/part_two.c.s +.PHONY : src/part_two.c.s + +# Help Target +help: + @echo "The following are some of the valid targets for this Makefile:" + @echo "... all (the default if no target is provided)" + @echo "... clean" + @echo "... depend" + @echo "... edit_cache" + @echo "... rebuild_cache" + @echo "... PartOne" + @echo "... PartThree" + @echo "... PartTwo" + @echo "... src/lib/lib.o" + @echo "... src/lib/lib.i" + @echo "... src/lib/lib.s" + @echo "... src/part_one.o" + @echo "... src/part_one.i" + @echo "... src/part_one.s" + @echo "... src/part_three.o" + @echo "... src/part_three.i" + @echo "... src/part_three.s" + @echo "... src/part_two.o" + @echo "... src/part_two.i" + @echo "... src/part_two.s" +.PHONY : help + + + +#============================================================================= +# Special targets to cleanup operation of make. + +# Special rule to run CMake to check the build system integrity. +# No rule that depends on this can have commands that come from listfiles +# because they might be regenerated. +cmake_check_build_system: + $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 +.PHONY : cmake_check_build_system + diff --git a/Spring-2023/CS-2124/Assignment-2/src/lib/lib.c b/Spring-2023/CS-2124/Assignment-2/src/lib/lib.c new file mode 100644 index 0000000..af3b937 --- /dev/null +++ b/Spring-2023/CS-2124/Assignment-2/src/lib/lib.c @@ -0,0 +1,57 @@ +// Credit to +// https://stackoverflow.com/questions/40159892/using-asprintf-on-windows/49873938#49873938 +// with modifications by me + + +#include /* needed for va_* */ +#include /* needed for vsnprintf */ +#include /* needed for malloc, free */ + +#ifdef _MSC_VER +#include +#else +#include +#endif + +int vscprintf(const char *format, va_list ap) { + va_list ap_copy; + va_copy(ap_copy, ap); + int retval = vsnprintf(NULL, 0, format, ap_copy); + va_end(ap_copy); + return retval; +} + +/* + * asprintf, vasprintf: + * MSVC does not implement these, thus we implement them here + * GNU-C-compatible compilers implement these with the same names, thus we + * don't have to do anything + */ +int vasprintf(char **strp, const char *format, va_list ap) { + int len = vscprintf(format, ap); + if (len == -1) + return -1; + char *str = (char *)malloc((size_t)len + 1); + if (!str) + return -1; + int retval = vsnprintf(str, len + 1, format, ap); + if (retval == -1) { + free(str); + return -1; + } + *strp = str; + return retval; +} + +char* fmtstr(const char *format, ...) { + char *str = NULL; + va_list ap; + va_start(ap, format); + vasprintf(&str, format, ap); + va_end(ap); + return str; +} + +void print_header() { + printf("Price Hiller | zfp106 | \n"); +} diff --git a/Spring-2023/CS-2124/Assignment-2/src/lib/lib.h b/Spring-2023/CS-2124/Assignment-2/src/lib/lib.h new file mode 100644 index 0000000..a87b90a --- /dev/null +++ b/Spring-2023/CS-2124/Assignment-2/src/lib/lib.h @@ -0,0 +1,35 @@ +// Credit to +// https://stackoverflow.com/questions/40159892/using-asprintf-on-windows/49873938#49873938 +// with modifications by me + +#ifndef FMTSTR_H +#define FMTSTR_H + +#if defined(__GNUC__) && !defined(_GNU_SOURCE) +#define _GNU_SOURCE /* needed for (v)asprintf, affects '#include ' */ +#endif + +#include /* needed for va_* */ + +/* + * vscprintf: + * MSVC implements this as _vscprintf, thus we just 'symlink' it here + * GNU-C-compatible compilers do not implement this, thus we implement it here + */ +#ifdef _MSC_VER +#define vscprintf _vscprintf +#endif + + +#ifdef _MSC_VER +int vasprintf(char **strp, const char *format, va_list ap); +#endif + +#ifdef __GNUC__ +int vscprintf(const char *format, va_list ap); +#endif + +char* fmtstr(const char *format, ...); +void print_header(); + +#endif diff --git a/Spring-2023/CS-2124/Assignment-2/src/lib/vec.h b/Spring-2023/CS-2124/Assignment-2/src/lib/vec.h new file mode 100644 index 0000000..918901b --- /dev/null +++ b/Spring-2023/CS-2124/Assignment-2/src/lib/vec.h @@ -0,0 +1,71 @@ +#pragma once +#define VECTOR_LIB +#include +#include + +#define DECL_VEC(type_name, type) \ + typedef struct { \ + size_t elem_count; \ + size_t capacity; \ + type *data; \ + } type_name; \ + \ + type_name type_name##_new(size_t initial_capacity) { \ + initial_capacity = (initial_capacity < 1) ? 1 : initial_capacity; \ + type_name v = {.capacity = initial_capacity, \ + .elem_count = 0, \ + .data = malloc(sizeof(type *) * initial_capacity)}; \ + return v; \ + } \ + \ + bool type_name##_alloc(type_name *v, size_t extra_capacity) { \ + if (extra_capacity == 0) { \ + return false; \ + } \ + \ + int desired_size = v->capacity + extra_capacity; \ + type *newData = realloc(v->data, sizeof(type *) * desired_size); \ + if (newData == NULL) \ + return false; \ + v->data = newData; \ + v->capacity = desired_size; \ + return true; \ + } \ + \ + bool type_name##_ensure_capacity(type_name *v, size_t capacity) { \ + if (v->capacity < capacity) \ + return type_name##_alloc(v, capacity - v->capacity); \ + return true; \ + } \ + \ + void type_name##_push(type_name *v, type item) { \ + type_name##_ensure_capacity(v, v->elem_count + 1); \ + v->data[v->elem_count++] = item; \ + } \ + \ + type *type_name##_get(type_name *v, size_t index) { \ + if (index >= 0 && index < v->elem_count) \ + return &v->data[index]; \ + return NULL; \ + } \ + \ + void type_name##_del(type_name *v, size_t index) { \ + if (index >= 0 && index < v->elem_count) { \ + for (int i = index; (i < v->elem_count - 1); ++i) { \ + v->data[i] = v->data[i + 1]; \ + } \ + v->elem_count--; \ + } \ + } \ + \ + type type_name##_pop(type_name *v) { \ + type popped_value = v->data[v->elem_count - 1]; \ + type_name##_del(v, v->elem_count - 1); \ + return popped_value; \ + } \ + \ + type *type_name##_peek(type_name *v) { \ + return type_name##_get(v, v->elem_count - 1); \ + } \ + \ + void type_name##_free(type_name *v) { free(v->data); } diff --git a/Spring-2023/CS-2124/Assignment-2/src/part_one.c b/Spring-2023/CS-2124/Assignment-2/src/part_one.c new file mode 100644 index 0000000..741c2b1 --- /dev/null +++ b/Spring-2023/CS-2124/Assignment-2/src/part_one.c @@ -0,0 +1,39 @@ +#include "./lib/lib.h" +#include "lib/vec.h" +#include +#include + +DECL_VEC(IntVec, int); +DECL_VEC(CharVec, char); + +int main(int argc, char *argv[]) { + print_header(); + + IntVec v = IntVec_new(2); + IntVec_push(&v, 1); + IntVec_push(&v, 2); + IntVec_push(&v, 99); + IntVec_push(&v, 3); + printf("%d\n", *IntVec_peek(&v)); + IntVec_pop(&v); + printf("%d\n", *IntVec_peek(&v)); + IntVec_pop(&v); + printf("%d\n", *IntVec_peek(&v)); + printf("%d\n", *IntVec_get(&v, 0)); + + CharVec cv = CharVec_new(3); + CharVec_push(&cv, 'a'); + CharVec_push(&cv, 'b'); + CharVec_pop(&cv); + CharVec_push(&cv, 'c'); + CharVec_push(&cv, 'd'); + CharVec_pop(&cv); + CharVec_push(&cv, 'f'); + + printf("CharVec has: %zu\n", cv.elem_count); + for (int i = 0; i < cv.elem_count; i++) { + // printf("Getting out of %d\n", i); + printf("%c", *CharVec_get(&cv, i)); + } + return EXIT_SUCCESS; +} diff --git a/Spring-2023/CS-2124/Assignment-2/src/part_three.c b/Spring-2023/CS-2124/Assignment-2/src/part_three.c new file mode 100644 index 0000000..e69de29 diff --git a/Spring-2023/CS-2124/Assignment-2/src/part_two.c b/Spring-2023/CS-2124/Assignment-2/src/part_two.c new file mode 100644 index 0000000..e69de29