Best Search algorithm

Hi everyone,
I have a unique case wherein i have list of devices(8 in mycase) and each device can have 4 possible states for each device and has 2 channels (4 possible states for each channel).

I have to search a device based on the below algorithm.
1. Not yet opened device (DEVICE_CONNECTED)
2. If step 1 is not available, Opened device with both channels not used (DEVICE_OPENED and check ChannelState of both Channels, and both channelstate should be CHANNEL_CLOSED)
3. If step 1 and 2 are not available, opened device with one channel not used (DEVICE_OPENED and check ChannelState of both Channels, and atleast one channelstate should be CHANNEL_CLOSED)
3a. If step 2 and 3 are not available, Opened device with both channels disabled (DEVICE_OPENED and check ChannelState of both Channels, and both channelstate should be CHANNEL_DISABLED)
3b. If step 2,3 and 3a are not available, opened device with one channel disabled (DEVICE_OPENED and check ChannelState of both Channels, and atleast one channelstate should be CHANNEL_DISABLED)
4. If step 1, 2 and 3 are not possible, DEVICE_IN_USE error message
What you have there is a list of priorities. All you need to do is keep a priority queue if you can detect priority changes, or a std::vector or other sortable collection where you code in those priorities inside operator< or a functor/lambda.

For example:

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
enum ChannelState
{
    Closed = 0,
    Opened = 1
};

enum DeviceState
{
    Disconnected = 0,
    Connected = 1
};

class DeviceChannel
{
    ChannelState m_state;

public:
    ChannelState State() const { return m_state; }
    ChannelState &State() { return m_state; }
};

class Device
{
    DeviceChannel m_channels[2];
    DeviceState m_state;

public:
    DeviceState State() const { return m_state; }
    DeviceState& State() { return m_state; }
    const DeviceChannel& operator [](int index) const
    {
        if (index > 1 || index < 0) throw YourFavoriteOutOfRangeException;
        return m_channels[index];
    }
    DeviceChannel& operator [](int index)
    {
        if (index > 1 || index < 0) throw YourFavoriteOutOfRangeException;
        return m_channels[index];
    }
    //Operator<:  Used by std::sort() to sort a collection.
    //The lesser ones have higher priority.
    bool operator<(const Device &op2) const
    {
        if (State() == Disconnected) return true;
        if (m_channels[0].State() == Closed && m_channels[1].State() == Closed) return true;
        //ETC.  Program the rest of the conditions.
    }
};

//Now you can use std::sort in <algorithm> to sort a std::vector<Device>.
//The first item in the collection after sorting is the best choice. 
Topic archived. No new replies allowed.