-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject3.cpp
More file actions
542 lines (480 loc) · 17.9 KB
/
project3.cpp
File metadata and controls
542 lines (480 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
* Author: Arboy Magomba
* Date: October 25, 2024
*
* Description:
* This program simulates a CPU job queue management system. It includes classes
* for managing CPU jobs (CPUJob), a queue structure (Queue), and an enhanced
* queue (NovelQueue) that allows operations such as enqueue, dequeue, modify,
* promote, reorder, and display. The main function processes user commands to
* manage jobs dynamically within the queue. This implementation supports job
* prioritization, job modification, and reordering based on specific attributes.
*
* Full code documentation and AI assistance details are provided at the end of the code.
*/
#include <iostream>
#include <string>
using namespace std;
class CPUJob {
public:
int jobID; // Unique identifier for the job
int priority; // Priority level for the job (1-10)
int job_type; // Job type (1-10)
int cpu_time_consumed; // Total CPU time consumed by the job
int memory_consumed; // Total memory consumed thus far
CPUJob(int id, int pri, int type, int cpuTime, int memory) //Constructor
: jobID(id), priority(pri), job_type(type), cpu_time_consumed(cpuTime), memory_consumed(memory) {}
void display() {
cout << "Job ID: " << jobID << ", Priority: " << priority << ", Job Type: " << job_type
<< ", CPU Time Consumed: " << cpu_time_consumed << ", Memory Consumed: " << memory_consumed;
}
};
template <class DT>
class Queue {
public:
DT JobPointer; // Pointer to a job (e.g., CPUJob)
Queue<DT>* next; // Pointer to the next node in the queue
int size; // Number of elements in the queue
Queue() : JobPointer(nullptr), next(nullptr), size(0) {}
~Queue() { // Destructor
while (!isEmpty()) {
dequeue();
}
}
void enqueue(DT* newJob) {
Queue<DT>* newNode = new Queue<DT>();
newNode->JobPointer = newJob;
newNode->next = nullptr;
if (isEmpty()) { //If no jobs have been added, the front becomes the enqueued object
this->JobPointer = newNode->JobPointer;
this->next = newNode->next;
} else {
Queue<DT>* temp = this;
while (temp->next != nullptr) { //Loop until you find an empty slot in the queue
temp = temp->next;
}
temp->next = newNode;
}
size++; //Useful for looping later
}
DT dequeue() {
if (isEmpty()) { //Can't dequeue empty space
return nullptr;
}
Queue<DT>* temp = this->next; //Create new queue without front element
DT ReturnJob = this->JobPointer; //Return job
this->JobPointer = temp->JobPointer;
this->next = temp->next;
delete temp;
size--;
return ReturnJob;
}
bool isEmpty() { //Useful for edge cases
return (size == 0);
}
void display() { //Displays all elements of the queue in order
Queue<DT>* temp = this;
while (temp != nullptr) {
if (temp->JobPointer) {
temp->JobPointer->display();
cout << endl;
}
temp = temp->next;
}
}
};
template <class DT>
class NovelQueue {
public:
Queue<DT>* front; // Pointer to the front of the queue
Queue<DT>** NodePointers; // Array of pointers to Queue nodes
int size; // Number of elements in the queue
int capacity; // Capacity of the queue
NovelQueue() : front(nullptr), NodePointers(nullptr), size(0), capacity(10) {
NodePointers = new Queue<DT>*[capacity];
}
~NovelQueue() {
delete[] NodePointers;
while (front != nullptr) {
dequeue();
}
}
void enqueue(DT newJob) {
// Check for duplicate job ID
for (int i = 0; i < size; ++i) {
if (NodePointers[i]->JobPointer->jobID == newJob->jobID) {
// If a job with the same jobID is found, print a message and return early
cout << "Job ID " << newJob->jobID << " already exists!" << endl;
return; // Exit the method without enqueueing or printing
}
}
// Resize if necessary
if (size == capacity) {
resize(capacity * 2);
}
// Create new node
Queue<DT>* newNode = new Queue<DT>();
newNode->JobPointer = newJob;
newNode->next = nullptr;
// Add to the queue
if (front == nullptr) {
front = newNode;
} else {
Queue<DT>* temp = front;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
NodePointers[size] = newNode;
size++;
// Sort by job ID if necessary
for (int i = size - 1; i > 0 && NodePointers[i]->JobPointer->jobID < NodePointers[i - 1]->JobPointer->jobID; --i) {
std::swap(NodePointers[i], NodePointers[i - 1]);
}
}
DT dequeue() {
if (front == nullptr) {
return DT(); //returns an instance of a null DT
}
Queue<DT>* temp = front;
front = front->next;
DT job = temp->JobPointer;
delete temp;
for (int i = 0; i < size - 1; i++) {
NodePointers[i] = NodePointers[i + 1];
}
size--;
if (size > 0 && size <= capacity / 2) { //resizes queue if enough spots are open
resize(capacity / 2);
}
return job;
}
void modify(int jobID, int new_priority, int new_job_type, int new_cpu_time_consumed, int new_memory_consumed) {
for (int i = 0; i < size; i++) {
if (NodePointers[i]->JobPointer->jobID == jobID) {
NodePointers[i]->JobPointer->priority = new_priority;
NodePointers[i]->JobPointer->job_type = new_job_type;
NodePointers[i]->JobPointer->cpu_time_consumed = new_cpu_time_consumed;
NodePointers[i]->JobPointer->memory_consumed = new_memory_consumed;
}
}
}
void promote(int jobID, int positions) {
int jobIndex = -1;
for (int i = 0; i < size; ++i) { //Loop to find the right job
if (NodePointers[i]->JobPointer->jobID == jobID) {
jobIndex = i;
break;
}
}
int newIndex = std::max(0, jobIndex - positions);
Queue<DT>* nodeToPromote = NodePointers[jobIndex];
for (int j = jobIndex; j > newIndex; --j) {
NodePointers[j] = NodePointers[j - 1];
}
NodePointers[newIndex] = nodeToPromote;
if (newIndex == 0) {
front = NodePointers[0];
}
for (int i = 0; i < size - 1; ++i) {
NodePointers[i]->next = NodePointers[i + 1];
}
NodePointers[size - 1]->next = nullptr;
}
void change(int jobID, int field_index, int new_value) {
bool changed = false; //Useful for edge cases
for (int i = 0; i < size; ++i) { //Loop to find right job
if (NodePointers[i]->JobPointer->jobID == jobID) {
cout << "Changed Job ID " << jobID << " field " << field_index << " to " << new_value << ":" << endl;
switch (field_index) {
case 1:
NodePointers[i]->JobPointer->priority = new_value;
changed = true;
break;
case 2:
NodePointers[i]->JobPointer->job_type = new_value;
changed = true;
break;
case 3:
NodePointers[i]->JobPointer->cpu_time_consumed = new_value;
changed = true;
break;
case 4:
NodePointers[i]->JobPointer->memory_consumed = new_value;
changed = true;
break;
default:
cout << "Invalid field_index!" << endl;
return;
}
if (changed) { //If the job was found
NodePointers[i]->JobPointer->display();
cout << endl;
cout << "Jobs after changing field: " << endl;
display();
return; // Stop searching after the job has been changed
}
}
}
//If the job was not found
if (!changed) {
cout << "Job with ID " << jobID << " not found in the queue." << endl;
}
}
NovelQueue<DT>* reorder(int attribute_index) {
NovelQueue<DT>* reorderedQueue = new NovelQueue<DT>();
// Sorting the NodePointers array based on the chosen attribute.
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
bool needSwap = false;
// Sort by Job ID when attribute_index is 1
if (attribute_index == 1) {
if (NodePointers[j]->JobPointer->jobID > NodePointers[j + 1]->JobPointer->jobID) {
needSwap = true;
}
} else if (attribute_index == 2) {
if (NodePointers[j]->JobPointer->priority > NodePointers[j + 1]->JobPointer->priority) {
needSwap = true;
}
} else if (attribute_index == 3) {
if (NodePointers[j]->JobPointer->cpu_time_consumed > NodePointers[j + 1]->JobPointer->cpu_time_consumed) {
needSwap = true;
}
}
if (needSwap) {
std::swap(NodePointers[j], NodePointers[j + 1]);
}
}
}
for (int i = 0; i < size; ++i) {
if (NodePointers[i]) {
reorderedQueue->enqueue(NodePointers[i]->JobPointer);
}
}
while (size > 0) {
this->dequeue(); // Ensures the old queue is cleared properly
}
return reorderedQueue;
}
void display() {
Queue<DT>* temp = front;
while (temp != nullptr) {
if (temp->JobPointer) {
temp->JobPointer->display();
cout << endl;
}
temp = temp->next;
}
}
void listJobs() {
for (int i = 0; i < size; i++) {
NodePointers[i]->JobPointer->display(); // Display the job information
cout << endl;
}
}
void resize(int newCapacity) { //If space runs out, or we have too much
Queue<DT>** newNodePointers = new Queue<DT>*[newCapacity];
for (int i = 0; i < size; i++) {
newNodePointers[i] = NodePointers[i];
}
delete[] NodePointers;
NodePointers = newNodePointers;
capacity = newCapacity;
}
int count() {
return size;
}
};
int main() {
int n;
cin >> n;
NovelQueue<CPUJob*>* myNovelQ = new NovelQueue<CPUJob*>();
char command;
int jobID, priority, job_type, cpu_time_consumed, memory_consumed;
int new_priority, new_job_type, new_cpu_time_consumed, new_memory_consumed;
int field_index, new_value;
int positions;
int attribute_index;
for (int i = 0; i < n; ++i) {
cin >> command;
switch (command) {
case 'A': {
// Read the job details
cin >> jobID >> priority >> job_type;
cin >> cpu_time_consumed >> memory_consumed;
// Create a new job
CPUJob* newJob = new CPUJob(jobID, priority, job_type, cpu_time_consumed, memory_consumed);
// Track the size before enqueue
int oldSize = myNovelQ->count();
// Enqueue the new job
myNovelQ->enqueue(newJob);
// Track the size after enqueue
int newSize = myNovelQ->count();
// Only display the job and queue if the size increased (job was successfully added)
if (newSize > oldSize) {
cout << "Enqueued Job:" << endl;
newJob->display();
cout << endl << "Jobs after enqueue:" << endl;
myNovelQ->display();
}
break;
}
case 'R': {
CPUJob* removedJob = myNovelQ->dequeue();
if (removedJob) {
cout << "Dequeued Job:" << endl;
removedJob->display();
cout << endl << "Jobs after dequeue:" << endl;
myNovelQ->display();
delete removedJob;
}
break;
}
case 'M': {
cin >> jobID >> new_priority >> new_job_type;
cin >> new_cpu_time_consumed >> new_memory_consumed;
myNovelQ->modify(jobID, new_priority, new_job_type, new_cpu_time_consumed, new_memory_consumed);
cout << "Modified Job ID " << jobID << ":" << endl;
for (int i = 0; i < myNovelQ->size; ++i) {
if (myNovelQ->NodePointers[i]->JobPointer->jobID == jobID) {
myNovelQ->NodePointers[i]->JobPointer->display();
cout << endl;
break;
}
}
cout << "Jobs after modification: " << endl;
myNovelQ->display();
break;
}
case 'C': {
cin >> jobID >> field_index >> new_value;
myNovelQ->change(jobID, field_index, new_value);
break;
}
case 'P': {
cin >> jobID >> positions;
myNovelQ->promote(jobID, positions);
cout << "Promoted Job ID " << jobID << " by " << positions << " Position(s):" << endl;
for (int i = 0; i < myNovelQ->size; ++i) {
if (myNovelQ->NodePointers[i]->JobPointer->jobID == jobID) {
myNovelQ->NodePointers[i]->JobPointer->display();
cout << endl;
break;
}
}
cout << "Jobs after promotion:" << endl;
myNovelQ->display();
break;
}
case 'O': {
cin >> attribute_index;
NovelQueue<CPUJob*>* reorderedQueue = myNovelQ->reorder(attribute_index);
delete myNovelQ; // Delete the old queue
myNovelQ = reorderedQueue; // Point myNovelQ to the reordered queue
cout << "Reordered Queue by attribute " << attribute_index << ":" << endl;
myNovelQ->display();
break;
}
case 'D': {
cout << "Displaying all jobs in the queue:" << endl;
myNovelQ->display();
break;
}
case 'N': {
cout << "Number of elements in the queue: " << myNovelQ->count() << endl;
break;
}
case 'L': {
cout << "List of jobs sorted by job IDs:" << endl;
myNovelQ->listJobs();
break;
}
default:
cout << "Invalid command!" << endl;
}
}
delete myNovelQ;
return 0;
}
/*
* CODE DOCUMENTATION
* Project: CPU Job Queue Management System
*
* Overview:
* The program implements a queue management system for CPU jobs using linked list and array structures.
* It defines two primary classes:
*
* 1. **CPUJob** - encapsulates individual job attributes such as job ID, priority, job type, CPU time consumed,
* and memory consumed.
*
* 2. **NovelQueue** - a dynamic queue that allows multiple operations on CPUJob objects, including enqueue,
* dequeue, job modification, promotion within the queue, reordering, displaying, and counting jobs.
*
* Main Functionalities:
* - **Enqueue**: Adds a new job to the end of the queue, ensuring no duplicate job IDs.
* - **Dequeue**: Removes the front job in the queue and returns it.
* - **Modify**: Updates multiple attributes of a job specified by its ID.
* - **Change**: Alters a specific attribute of a job based on an attribute index.
* - **Promote**: Moves a job up within the queue based on specified positions.
* - **Reorder**: Sorts the queue based on a specified job attribute.
* - **Display**: Outputs all jobs in the queue in their current order.
* - **Count**: Returns the current number of jobs in the queue.
*
* Documentation:
* - This program processes user commands to dynamically manipulate the queue, allowing real-time updates on job management.
* - Each command input modifies the queue and immediately reflects any changes in the queue’s structure, which is displayed to the user.
*
* LLM and GitHub Copilot Usage Documentation:
*
* This section details the questions, prompts, and incremental development strategies used when utilizing LLM tools or GitHub Copilot.
*
* 1. Prompts and Suggestions:
* - Prompts used: "How to implement enqueue for NovelQueue?", "Debugging dequeue functionality in a linked queue",
* "Can you suggest improvements for the modify function in NovelQueue?"
*
* 2. Rationale for Prompt Choices:
* - The suggestions from the AI were essential for guiding method structuring and ensuring consistency in method integration.
* - For instance, the prompt about enqueueing helped validate the conditions for adding new jobs to avoid duplicates,
* which saved time by catching potential errors in real-time.
*
* 3. Incremental Development:
* - Development was approached step-by-step, using AI prompts to build out a basic structure for each class and incrementally
* refining individual methods for consistency.
* - Examples include using AI to generate method headers, which were expanded to manage the linked structure and validate
* input data in a more robust way.
*
* Debugging and Testing Plan:
*
* This plan outlines the specific tests conducted, issues encountered, and verification processes used to ensure robust functionality.
*
* 1. Specific Tests:
* - Enqueue/Dequeue Operations: Ensured that the enqueue function correctly adds new jobs and maintains order.
* Dequeue was tested to confirm that front jobs are removed, and memory is managed.
* - Modify Function: Checked that modify successfully changes job attributes for jobs with valid IDs and does not alter
* unrelated jobs.
* - Edge Case Testing: Verified that the program handles an empty queue gracefully during dequeue attempts and manages
* duplicate IDs correctly.
*
* 2. Issues and Resolutions:
* - Issue: The program occasionally skipped the enqueue function for specific jobs with input2.txt and sometimes it also skipped jobs in input1.txt.
* - Resolution: Added debugging output statements to check conditions, revealing an issue in the duplicate-check logic and the reordering method that
* caused the enqueue to fail. Corrected the check, allowing all jobs to enqueue as expected.
*
* 3. Verification:
* - Command-line Testing: Validated each command by running a series of inputs directly in the terminal.
* - Automated Testing Suite: Created test cases for all operations (enqueue, dequeue, modify, change, promote, reorder)
* and observed output consistency.
* - Continuous Monitoring: Printed the queue state after each operation to confirm that the expected changes occurred.
*
* Reflection on AI Assistance:
* - Questioning AI tools throughout the coding process deepened understanding of queue management, memory handling,
* and error resolution. Examples include insights into structuring linked nodes and using pointers effectively.
* - The AI suggestions informed coding best practices such as pointer safety, memory allocation, and encapsulation,
* providing strategies to optimize queue operations without sacrificing readability.
*
* Overall, using targeted questions and AI-assisted debugging allowed for a cleaner, more efficient codebase while
* ensuring robust error handling and proper memory management.
*
* Note: The program accurately processes commands from input files, and all the issues faced during development have been resolved.
*/