Enum
From CometPublic
Comet contains support for enumerated types. Enumerated Types are useful for indexing arrays and matrices with something more informative than just integers. Enumerated types or enums are created as follows:
enum animals = {dog, cat, bird};
You can find examples of enums being used in Constraint Based Local Search book in Statement 4.2 on Page 46, the Send More Money problem example. The Zebra Puzzle described in Statement 4.3 on Page 49 also uses enums.
To specify that an array is indexed by an enum:
int ages[animals];
You will now be able to access the array elements using the values from the enum you specified.
ages[dog] = 7; cout << ages[dog] << endl;
Slightly more complicated is when you want to use a matrix within a class. In this case you can't use Comet's construct for both declaring and instantiating a matrix, you need to separate the declaration from the instantiation
enum cities = {Boston, Providence, Newport};
class MyClass{
int [enum cities, enum cities] distances;
MyClass(){
distances = new int[cities, cities];
}
}
Enums can also be accessed with looping constructs such as forall. The following example shows how to iterate through an array indexed by enums. The syntax is exactly the same as though it were a range of integers.
enum furniture = {chair, table, desk, lamp};
int prices[furniture];
forall(f in furniture){
cout << f << endl;
cout << prices[f] << endl; // Will display 0 for each element
}

