-
Notifications
You must be signed in to change notification settings - Fork 4
Krish/cpp algo #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Krish/cpp algo #25
Changes from all commits
7163d58
202d159
a6b74ac
4191f53
fe8fec4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # Building the C++ Scheduling Algorithms | ||
|
|
||
| This project uses Node.js native addons to run scheduling algorithms in C++ for better performance. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Node.js (v16 or higher) | ||
| - Python 3.10 or higher (required for node-gyp) | ||
| - For Windows: Visual Studio Build Tools with C++ support | ||
| - For macOS: Xcode Command Line Tools | ||
| - For Linux: Build tools (gcc, g++, make) | ||
|
|
||
| ## Building | ||
|
|
||
| ### Native Dev/Build Mode | ||
| The C++ addon is built when you run native scripts: | ||
| ```bash | ||
| npm run dev:native | ||
| npm run build:native | ||
| ``` | ||
|
|
||
| ### Standard Mode (No Native Build Required) | ||
| For quick development without native compilation: | ||
| ```bash | ||
| npm run dev | ||
| npm run build | ||
| ``` | ||
|
|
||
| ### Manual Build | ||
| To manually build the native addon: | ||
| ```bash | ||
| npm run build-native | ||
| ``` | ||
|
|
||
| ### Clean Build | ||
| To clean build artifacts and rebuild: | ||
| ```bash | ||
| npm run clean-native | ||
| npm run build-native | ||
| ``` | ||
|
|
||
| ## Windows Specific Setup | ||
|
|
||
| If you encounter issues on Windows: | ||
|
|
||
| 1. Install Visual Studio Build Tools: | ||
| - Download from: https://visualstudio.microsoft.com/visual-cpp-build-tools/ | ||
| - Install with "Desktop development with C++" workload | ||
|
|
||
| 2. Ensure Python 3.10+ is installed and in your PATH | ||
|
|
||
| 3. Reopen your terminal after installation so toolchain environment changes are picked up. | ||
|
|
||
| ## Development | ||
|
|
||
| The project uses both C++ and TypeScript implementations: | ||
| - **C++ Implementation**: Located in `cpp/` directory | ||
| - **TypeScript Fallback**: In `lib/scheduling-native.ts` | ||
|
|
||
| If the C++ addon fails to load, the application automatically falls back to the TypeScript implementation, so development is not blocked. | ||
|
|
||
| ## Project Structure | ||
|
|
||
| ``` | ||
| cpp/ | ||
| ├── include/ | ||
| │ ├── Process.h # Process data structure | ||
| │ └── Algorithms.h # Algorithm declarations | ||
| ├── src/ | ||
| │ ├── binding.cc # Node.js bindings | ||
| │ ├── FirstComeFirstServe.cpp | ||
| │ ├── RoundRobin.cpp | ||
| │ ├── ShortestJobFirst.cpp | ||
| │ └── ShortestRemainingTimeFirst.cpp | ||
| binding.gyp # Build configuration file | ||
| ``` | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### "Native C++ addon not found" warning | ||
| This is normal during development. The application will use the TypeScript implementation as a fallback. | ||
|
|
||
| ### Build fails on Windows | ||
| 1. Check that Visual Studio Build Tools are installed | ||
| 2. Verify Python is in your PATH | ||
| 3. Try rebuilding: `npm run clean-native && npm run build-native` | ||
|
|
||
| ### Build fails on macOS/Linux | ||
| 1. Ensure Xcode/gcc is installed | ||
| 2. Try: `xcode-select --install` (macOS) | ||
| 3. Install build essentials: `sudo apt-get install build-essential` (Linux) | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,134 @@ | ||||||||||||||||
| # C++ Scheduling Algorithms Integration - Complete | ||||||||||||||||
|
|
||||||||||||||||
| I've successfully converted all scheduling algorithms to C++ and integrated them into your Next.js project. Here's what was implemented: | ||||||||||||||||
|
|
||||||||||||||||
| ## What Was Done | ||||||||||||||||
|
|
||||||||||||||||
| ### 1. **C++ Algorithm Implementations** ✅ | ||||||||||||||||
| All four scheduling algorithms have been converted to C++: | ||||||||||||||||
| - `cpp/src/FirstComeFirstServe.cpp` (FCFS) | ||||||||||||||||
| - `cpp/src/RoundRobin.cpp` (Round Robin with quantum) | ||||||||||||||||
| - `cpp/src/ShortestJobFirst.cpp` (SJF) | ||||||||||||||||
| - `cpp/src/ShortestRemainingTimeFirst.cpp` (SRTF) | ||||||||||||||||
|
|
||||||||||||||||
| ### 2. **Native Module Setup** ✅ | ||||||||||||||||
| - **Data Structure**: `cpp/include/Process.h` - Mirrors your TypeScript Process type | ||||||||||||||||
| - **Algorithm Interface**: `cpp/include/Algorithms.h` - Function declarations | ||||||||||||||||
| - **V8 Bindings**: `cpp/src/binding.cc` - Node.js integration with automatic type conversion | ||||||||||||||||
| - **Build Configuration**: `binding.gyp` - node-gyp setup for cross-platform compilation | ||||||||||||||||
|
|
||||||||||||||||
| ### 3. **Smart Wrapper Layer** ✅ | ||||||||||||||||
| Created `lib/scheduling-native.ts` that: | ||||||||||||||||
| - Attempts to load the compiled C++ addon | ||||||||||||||||
| - Falls back to TypeScript implementations if addon unavailable | ||||||||||||||||
| - Exports the same interface as before | ||||||||||||||||
| - **Result**: Zero changes needed to your UI components! | ||||||||||||||||
|
|
||||||||||||||||
| ### 4. **Updated Imports** ✅ | ||||||||||||||||
| All original TypeScript files now re-export from the wrapper: | ||||||||||||||||
| - `lib/FirstComeFirstServe.ts` | ||||||||||||||||
| - `lib/RoundRobin.ts` | ||||||||||||||||
| - `lib/ShortestJobFirst.ts` | ||||||||||||||||
| - `lib/ShortestRemainingTimeFirst.ts` | ||||||||||||||||
|
|
||||||||||||||||
| ### 5. **Build Integration** ✅ | ||||||||||||||||
| Updated `package.json` with: | ||||||||||||||||
| ```json | ||||||||||||||||
| "scripts": { | ||||||||||||||||
| "build-native": "node-gyp configure && node-gyp build", | ||||||||||||||||
| "prebuild": "npm run build-native || true", | ||||||||||||||||
| "dev": "npm run build-native && next dev", | ||||||||||||||||
| "build": "npm run build-native && next build", | ||||||||||||||||
|
Comment on lines
+39
to
+41
|
||||||||||||||||
| "prebuild": "npm run build-native || true", | |
| "dev": "npm run build-native && next dev", | |
| "build": "npm run build-native && next build", | |
| "dev": "next dev", | |
| "dev:native": "npm run build-native && next dev", | |
| "build": "next build", | |
| "build:native": "npm run build-native && next build", |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,121 @@ | ||||||||
| # Setting Up C++ Development in VS Code | ||||||||
|
|
||||||||
| ## Step 1: Install Visual Studio Build Tools | ||||||||
|
|
||||||||
| Since you want to use C++, you need the Visual Studio Build Tools compiler. Follow these steps: | ||||||||
|
|
||||||||
| ### On Windows: | ||||||||
|
|
||||||||
| 1. **Download Visual Studio Build Tools** | ||||||||
| - Go to: https://visualstudio.microsoft.com/visual-cpp-build-tools/ | ||||||||
| - Click "Download Build Tools" | ||||||||
|
|
||||||||
| 2. **Run the Installer** | ||||||||
| - Open the downloaded `.exe` file | ||||||||
| - When prompted for workloads, select: **"Desktop development with C++"** | ||||||||
| - Include these components: | ||||||||
| - MSVC v143 or later | ||||||||
| - Windows 10/11 SDK | ||||||||
| - CMake tools | ||||||||
|
|
||||||||
| 3. **Verify Installation** | ||||||||
| ```bash | ||||||||
| where cl.exe | ||||||||
| ``` | ||||||||
| You should see a path to the compiler. | ||||||||
|
|
||||||||
| 4. **Restart Your Terminal** | ||||||||
| - Close and reopen your terminal/VS Code | ||||||||
|
|
||||||||
| ### On macOS: | ||||||||
| ```bash | ||||||||
| xcode-select --install | ||||||||
| ``` | ||||||||
|
|
||||||||
| ### On Linux (Ubuntu/Debian): | ||||||||
| ```bash | ||||||||
| sudo apt-get install build-essential python3 | ||||||||
| ``` | ||||||||
|
|
||||||||
| ## Step 2: Install Dependencies | ||||||||
|
|
||||||||
| After installing the C++ tools, run: | ||||||||
| ```bash | ||||||||
| npm install | ||||||||
| ``` | ||||||||
|
|
||||||||
| This will: | ||||||||
| - Install all Node.js dependencies | ||||||||
| - Build the C++ addon automatically (via `postinstall`) | ||||||||
|
|
||||||||
|
Comment on lines
+49
to
+50
|
||||||||
| - Build the C++ addon automatically (via `postinstall`) | |
| It does not automatically build the C++ addon; to build it, run `npm run build-native` (see Step 4). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doc says Node.js v16+ is supported, but the repo is on Next.js 14 (and
@types/node20), which requires a newer Node version. Please align this prerequisite with the actual minimum supported Node version (and keep it consistent withREADME.md).