#include <bits/stdc++.h>
#include <thread>
#include <mutex>
using namespace std;
mutex user_mutex;
mutex driver_mutex;
mutex ride_mutex;
class User {
public :
string username;
string contact;
string email;
pair< int , int > location;
User( string username, string contact) {
this- > username = username;
this- > contact = contact;
location = { 0 , 0 } ;
}
User( ) = default ;
} ;
class Driver {
public :
string username;
int status;
string vehicleNo;
int earnings;
pair< int , int > location;
Driver( string username, string vehicleNo, pair< int , int > location) {
this- > username = username;
this- > vehicleNo = vehicleNo;
this- > location = location;
status = 1 ;
earnings = 0 ;
}
Driver( ) = default ;
} ;
unordered_map< string, User> users;
unordered_map< string, Driver> drivers;
unordered_map< string, string> activeRides;
unordered_map< string, pair< pair< int , int > , pair< int , int >>> ridesRequested;
void add_user( string username, string contactNo) {
lock_guard< mutex> lock( user_mutex) ;
users[ username] = User( username, contactNo) ;
}
void update_user( string username, string updatedContact) {
lock_guard< mutex> lock( user_mutex) ;
if ( users.find ( username) ! = users.end ( ) ) {
users[ username] .contact = updatedContact;
} else {
cout << "User not found" << endl;
}
}
void update_userLocation( string username, pair< int , int > location) {
lock_guard< mutex> lock( user_mutex) ;
if ( users.find ( username) ! = users.end ( ) ) {
users[ username] .location = location;
} else {
cout << "User not found" << endl;
}
}
void add_driver( string name, string vehicleNo, pair< int , int > location) {
lock_guard< mutex> lock( driver_mutex) ;
drivers[ name] = Driver( name, vehicleNo, location) ;
}
void update_driverLocation( string driver_name, pair< int , int > location) {
lock_guard< mutex> lock( driver_mutex) ;
if ( drivers.find ( driver_name) ! = drivers.end ( ) ) {
drivers[ driver_name] .location = location;
} else {
cout << "Driver not found" << endl;
}
}
void change_driver_status( string driver_name, int status) {
lock_guard< mutex> lock( driver_mutex) ;
if ( drivers.find ( driver_name) ! = drivers.end ( ) ) {
drivers[ driver_name] .status = status;
} else {
cout << "Driver not found" << endl;
}
}
vector< string> find_ride( string username, pair< int , int > source, pair< int , int > destination) {
lock_guard< mutex> lock( ride_mutex) ;
if ( users.find ( username) == users.end ( ) ) {
cout << "User not found" << endl;
return { } ;
}
ridesRequested[ username] = { source, destination} ;
vector< string> available_drivers;
for ( auto & [ name, driver] : drivers) {
int dist = abs ( driver.location .first - source.first ) + abs ( driver.location .second - source.second ) ;
if ( dist <= 5 && driver.status == 1 ) {
available_drivers.push_back ( name) ;
}
}
if ( available_drivers.empty ( ) ) {
cout << "No ride found [Since all the drivers are more than 5 units away from user]" << endl;
}
return available_drivers;
}
void concurrent_find_ride( string username, pair< int , int > source, pair< int , int > destination) {
vector< string> drivers_found = find_ride( username, source, destination) ;
for ( const auto & driver : drivers_found) {
cout << "Driver found for " << username << ": " << driver << endl;
}
}
int main( ) {
add_user( "Abhay" , "1234" ) ;
add_user( "Vikram" , "2234" ) ;
add_user( "Kirti" , "2345" ) ;
update_userLocation( "Abhay" , { 0 , 0 } ) ;
update_userLocation( "Vikram" , { 10 , 0 } ) ;
update_userLocation( "Kirti" , { 15 , 6 } ) ;
add_driver( "Driver1" , "Swift, KA-01-12345" , { 10 , 1 } ) ;
add_driver( "Driver2" , "Swift, KA-01-12345" , { 11 , 10 } ) ;
add_driver( "Driver3" , "Swift, KA-01-12345" , { 5 , 3 } ) ;
// Create threads for each user's ride request to simulate concurrency
thread thread2( concurrent_find_ride, "Vikram" , make_pair( 10 , 0 ) , make_pair( 15 , 3 ) ) ;
thread thread1( concurrent_find_ride, "Abhay" , make_pair( 0 , 0 ) , make_pair( 20 , 1 ) ) ;
thread thread3( concurrent_find_ride, "Kirti" , make_pair( 15 , 6 ) , make_pair( 20 , 4 ) ) ;
// Join threads
thread1.join ( ) ;
thread2.join ( ) ;
thread3.join ( ) ;
return 0 ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDx0aHJlYWQ+CiNpbmNsdWRlIDxtdXRleD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgptdXRleCB1c2VyX211dGV4OwptdXRleCBkcml2ZXJfbXV0ZXg7Cm11dGV4IHJpZGVfbXV0ZXg7Cgpjb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