473,659 Members | 3,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Suitable data structure for a "context sensitive" 3-tuple?

Hi,

I want to store data in a 3-tuple {a,b,c}.
Element a is an enumeration, and element c is an enumeration which is
specific (i.e. determined) by a.

An example will help clarify further. I give two examples, one of a
valid instance, and the other, an invalid instance.

typedef enum { bovine, gamebird, seafood } general_meat ;

Note: each of the elements above is a category of its own. I can
further partition the seafood category to yield the following grouping:

typedef enum { scallops, lobster, cod, prawn, salmon} seafood ;
class Meal {
general_meat m ;
bool cook_first ;
X data_type_that_ will_accept_spe cific_types_of_ general_meat_m ;
}

I hope the illustration above provides some illumination on the problem.
class Meal will (obviously?) have to be a template class - but I
don't know how to enforce X to be a subset (i.e. a permissable type)
determined by m.

I look forward to any ideas for a possible solution. Thanks

Jul 23 '05 #1
12 1869


Susan Baker wrote:
Hi,

I want to store data in a 3-tuple {a,b,c}.
Element a is an enumeration, and element c is an enumeration which is
specific (i.e. determined) by a.

An example will help clarify further. I give two examples, one of a
valid instance, and the other, an invalid instance.

typedef enum { bovine, gamebird, seafood } general_meat ;

Note: each of the elements above is a category of its own. I can further
partition the seafood category to yield the following grouping:

typedef enum { scallops, lobster, cod, prawn, salmon} seafood ;
class Meal {
general_meat m ;
bool cook_first ;
X data_type_that_ will_accept_spe cific_types_of_ general_meat_m ;
}

I hope the illustration above provides some illumination on the problem.
class Meal will (obviously?) have to be a template class - but I don't
know how to enforce X to be a subset (i.e. a permissable type)
determined by m.

I look forward to any ideas for a possible solution. Thanks


Ok. Here are the two examples (of objects) I promised earlier:

meal1 { seafood, no, scallops } // <- valid
meal2 { bovine, yes, scallops } //<- invalid
Jul 23 '05 #2


Susan Baker wrote:
Hi,

I want to store data in a 3-tuple {a,b,c}.
Element a is an enumeration, and element c is an enumeration which is
specific (i.e. determined) by a.

An example will help clarify further. I give two examples, one of a
valid instance, and the other, an invalid instance.

typedef enum { bovine, gamebird, seafood } general_meat ;

Note: each of the elements above is a category of its own. I can
further partition the seafood category to yield the following grouping:

typedef enum { scallops, lobster, cod, prawn, salmon} seafood ;
class Meal {
general_meat m ;
bool cook_first ;
X data_type_that_ will_accept_spe cific_types_of_ general_meat_m ;
}

I hope the illustration above provides some illumination on the problem.
class Meal will (obviously?) have to be a template class - but I
don't know how to enforce X to be a subset (i.e. a permissable type)
determined by m.

I look forward to any ideas for a possible solution. Thanks


What is wrong with inheritance? Make
data_type_that_ will_accept_spe cific_types_of_ general_meat_m a pointer
to a general_meal. Derive bovine, gamberid and seafood from
general_meal.

dan

Jul 23 '05 #3


Dan Cernat wrote:

Susan Baker wrote:
Hi,

I want to store data in a 3-tuple {a,b,c}.
Element a is an enumeration, and element c is an enumeration which is
specific (i.e. determined) by a.

An example will help clarify further. I give two examples, one of a
valid instance, and the other, an invalid instance.

typedef enum { bovine, gamebird, seafood } general_meat ;

Note: each of the elements above is a category of its own. I can
further partition the seafood category to yield the following grouping:

typedef enum { scallops, lobster, cod, prawn, salmon} seafood ;
class Meal {
general_meat m ;
bool cook_first ;
X data_type_that_ will_accept_spe cific_types_of_ general_meat_m ;
}

I hope the illustration above provides some illumination on the problem.
class Meal will (obviously?) have to be a template class - but I
don't know how to enforce X to be a subset (i.e. a permissable type)
determined by m.

I look forward to any ideas for a possible solution. Thanks

What is wrong with inheritance? Make
data_type_that_ will_accept_spe cific_types_of_ general_meat_m a pointer
to a general_meal. Derive bovine, gamberid and seafood from
general_meal.

dan


Is general_meal a new class, or did you mean general_meat ?. I can see
where you're going with the inheritence suggestion. I could have a
heirarchy like:

meat
|
-------------------------
| | |
bovine gamebird seafood
and then sub-class each one of them accordingly. This is one way of ding
it - it also means a lot of coding - with all its attendant problems. I
was wondering if there was a way of applying generic programming
philosophy so I could have one generic factory that produces each of
these groupings (bovine, gamebird etc).

When using any of these meat derived classes (which should really be
abstract - since they are classifications ), the derived class can only
instantiate with meaningful values, so that if I ask for a seafood Meal,
I do not end up with raw pork (for instance0. since pork is not a valid
member (enumeration item if you like) for the class seafood. I know all
of this can be done using inheritance and judious inspection of types
being passed in the class constructor etc - but this is not easily
extendable, for example, if I decide to allow new items to be part of
the seafood category, I have an awful lot of refactoring to do - I was
just wondering if there was a way of writing this in a generic way (if
not all, then at least some of it).

Jul 23 '05 #4
Susan,

I took a look at your second post

meal1 { seafood, no, scallops } // <- valid
meal2 { bovine, yes, scallops } //<- invalid

why would you want to do this? Pass in the scallops and that is all. No
need for validation. This is using inheritance. So, before talking
more, what is the exact problem you are trying to solve? So far you are
asking help in implementing _a_ solution, but what is your problem?
There may be better ways to solve it.

dan

Jul 23 '05 #5


Dan Cernat wrote:
Susan,

I took a look at your second post

meal1 { seafood, no, scallops } // <- valid
meal2 { bovine, yes, scallops } //<- invalid

why would you want to do this? Pass in the scallops and that is all. No
need for validation. This is using inheritance. So, before talking
more, what is the exact problem you are trying to solve? So far you are
asking help in implementing _a_ solution, but what is your problem?
There may be better ways to solve it.

dan


Hi Dan,

Just FYI, the examples I gave was not pseudocode (i.e. I was not passing
anything to the meal types). basically, what I meanto say was that a
valid meal (meal1), could comprise of the following "properties :

{
seafood /*food category*/,
no /*no cooking reqd b4 consumption*/,
scallops /* the specific food type */
}

In the second example I gave, the object was invalid because scallops do
not belong to the bovine food category. The actual problem statement is
too domain specific (i.e. requires too much speciliazed domain
knowledge) to go into detail here - it will simply further obfuscucate
the problem, besides, my boss is not likely to be too pleased with me
discussing this here (we've signed an NDA etc ...). However, the
description I gave is accurate. The more I think about it, the more I am
convinced to keep things simple. I think the inheritance route is the
way forward - once I've got that working, I'll look to "generalize " the
code later.

Thanks for your help anyhows..

Jul 23 '05 #6


Susan Baker wrote:
Dan Cernat wrote:
Susan,

I took a look at your second post

meal1 { seafood, no, scallops } // <- valid
meal2 { bovine, yes, scallops } //<- invalid

why would you want to do this? Pass in the scallops and that is all. No
need for validation. This is using inheritance. So, before talking
more, what is the exact problem you are trying to solve? So far you are
asking help in implementing _a_ solution, but what is your problem?
There may be better ways to solve it.

dan


Hi Dan,

Just FYI, the examples I gave was not pseudocode (i.e. I was not passing
anything to the meal types). basically, what I meanto say was that a
valid meal (meal1), could comprise of the following "properties :

{
seafood /*food category*/,
no /*no cooking reqd b4 consumption*/,
scallops /* the specific food type */
}

In the second example I gave, the object was invalid because scallops do
not belong to the bovine food category.

[snip]

Hi Susan,

scallops could be only seafood. There is no need to have the food
category in the meal. This way, the meals are always valid:
class meal
{
private:
bool cooked;
Food* food; /* the specific food type */
public:
meal(bool Cooked, Food* SpecificFood)
{
cooked = Cooked;
food = SpecificFood;
}

FoodCateg GetFoodCategory ()
{
food->GetCategory( );
}
};
meal m1(false, new Scallops);
meal m2(true, new GroundMeat);

How you store the category inside the Food class is a different story.
It could be a member variable or an inheritance chain or a templated
class. your call.

dan

Jul 23 '05 #7


Dan Cernat wrote:
FoodCateg GetFoodCategory ()
{
food->GetCategory( );
}


should be:

return food->GetCategory( );

Jul 23 '05 #8
"Susan Baker" <sb****@no.spam .net> wrote in message
news:da******** **@nwrdmz03.dmz .ncs.ea.ibs-infra.bt.com
Hi,

I want to store data in a 3-tuple {a,b,c}.
Element a is an enumeration, and element c is an enumeration which is
specific (i.e. determined) by a.

An example will help clarify further. I give two examples, one of a
valid instance, and the other, an invalid instance.

typedef enum { bovine, gamebird, seafood } general_meat ;

Note: each of the elements above is a category of its own. I can
further partition the seafood category to yield the following
grouping:
typedef enum { scallops, lobster, cod, prawn, salmon} seafood ;
class Meal {
general_meat m ;
bool cook_first ;
X data_type_that_ will_accept_spe cific_types_of_ general_meat_m ;
}

I hope the illustration above provides some illumination on the
problem. class Meal will (obviously?) have to be a template class -
but I don't know how to enforce X to be a subset (i.e. a permissable type)
determined by m.

I look forward to any ideas for a possible solution. Thanks


There may be more elegant ways to do it, but this seems to work:

enum general_meat { bovine, gamebird, seafood };

enum bovine_subtype { rump, sirloin, topside};
enum gamebird_subtyp e { pheasant, duck };
enum seafood_subtype { scallops, lobster, cod, prawn, salmon};

// general template class
template <general_meat gm>
struct SubTypeEnum
{};

// specialisations for each general_meat case

template<>
struct SubTypeEnum<bov ine>
{
typedef bovine_subtype subtype;
};

template<>
struct SubTypeEnum<sea food>
{
typedef seafood_subtype subtype;
};

template<>
struct SubTypeEnum<gam ebird>
{
typedef gamebird_subtyp e subtype;
};

template<genera l_meat gm>
class Meal : private SubTypeEnum<gm>
{
using typename SubTypeEnum<gm> ::subtype;
bool cook_first ;
subtype s;
public:
Meal(bool cf, subtype st) : cook_first(cf), s(st)
{}
};

int main()
{
Meal<seafood> m1(true, scallops); // compiles

Meal<seafood> m2(true, rump); // won't compile
}

To add a new subtype, you just have to add it to the enum for that subtype.

To add a new type (say, pork), you need to:

1. Add pork to the general_meat enum.
2. Define a enum pork_subtype.
3. Add

template<>
struct SubTypeEnum<por k>
{
typedef pork_subtype subtype;
};
--
John Carson

Jul 23 '05 #9
"John Carson" <jc************ ****@netspace.n et.au> wrote in message
news:da******** ***@otis.netspa ce.net.au

template<genera l_meat gm>
class Meal : private SubTypeEnum<gm>
{
using typename SubTypeEnum<gm> ::subtype;
bool cook_first ;
subtype s;
public:
Meal(bool cf, subtype st) : cook_first(cf), s(st)
{}
};

I omitted the template parameter from the list of variables since I made no
use of it, but you probably want to add it back in:
template<genera l_meat gm>
class Meal : private SubTypeEnum<gm>
{
using typename SubTypeEnum<gm> ::subtype;
general_meat meat_type;
bool cook_first ;
subtype s;
public:
Meal(bool cf, subtype st) : meat_type(gm), cook_first(cf), s(st)
{}
};
--
John Carson

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5214
by: john bailo | last post by:
I wrote a c# program that does some file manipulation on a remote server. Testing it from my workstation, it ran fine. When I copied it to another server, and ran it from there, on load, it threw a balloon message: "This application is running in a partially trusted context. Some functionality in the application may be disabled due to
0
2448
by: Tom Dacon | last post by:
"Open .Net Command Window Here" context menu for Windows Explorer: The reg file described below adds a new menu item to Windows Explorer's context menu when you right-click over a folder (or the drive root). The menu item text is "Open .Net Command Window Here". When you click it, it opens a command window positioned at that directory and runs the vsvars32.bat file to set the .Net Framework environment variable settings. This makes it...
8
19209
by: H. S. | last post by:
I am getting this error if I try to compile the file demarcated below. What I am missing here? I am using g++ (GCC) 3.3.5 (Debian 1:3.3.5-8). {tp2}> g++ -ansi -g -Wall tp2.cc -o tp2 tp2.cc: In member function `void Other::DoSomething()': tp2.cc:11: error: `int Base::iX' is protected tp2.cc:29: error: within this context // %< %< ----------------------------------- %< %<
2
55578
by: Eirik M. | last post by:
Hi, I've got the following piece of code that's causing me a bit of a problem XmlDocument doc = new XmlDocument (); XmlNode rootNode = doc.CreateNode (XmlNodeType.Element, "usersettings", null); User user = new User (userid); XmlNode rolesNode = user.GetRoles (); rootNode.AppendChild (rolesNode);
5
72244
by: Horst Walter | last post by:
What is wrong here? IPAddress ipAddress = IPAddress.Parse("10.10.20.1"); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, this.port); this.tcpClient = new TcpClient(ipEndPoint); // PROBLEM HERE => Exception: "The requested address is not valid in its context" This works:
3
1634
by: trint | last post by:
Here is the "catch exception" message: "The requested lookup key was not found in any active activation context" Here is where it happens: public void DoRead(IAsyncResult ar) { int BytesRead; string strMessage;
5
3068
by: | last post by:
Hoping someone can help with a simple but puzzling problem. I have some code that I want to build as a class method. The code works fine when I embed it in Page_Load. But when I try to generalize the code into the method of a class I am trying to build, it gives me strange errors: "CS0103: The name 'Server' does not exist in the current context". (Server being a call to Server.MapPath.) I'm working in ASP.NET 2.0 and VS.NET 2005. ...
1
1479
by: Roy | last post by:
From the MS site: NAME: ClientScriptManager.GetCallbackEventReference (Control, String, String, String) DESCRIPTION: Obtains a reference to a client-side function that, when invoked, initiates a client call back to a server-side event. The client-side function for this overloaded method includes a specified control, argument, client-side script, and context. What is "context?"
2
9911
by: Jeff | last post by:
hey asp.net 2.0 (C'#) In the code behind file I have this method: public String AddBR(Object param) { String text = (String) param; return text; }
3
2579
dlite922
by: dlite922 | last post by:
Hey guys, My brains asleep and I don't know what's wrong with my session class. I'm over riding session with sesstion_set_save_handler() in a class; When in my member functions (open, close, read) I use "$this" I get the error "Using $this when not in object context". Here's my constructor:
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.