storing two classes in a data stucture at once in EIFFEL - eiffel

I am making a clinic and I need to make a object that stores two medicines at once. The interaction object means MEDICATION_1 is interacting with MEDICATION_2
However the following code is not correct because it does not meet the generic parameter
ERROR: VICG: Actual generic parameter does not conform to constraint.
code:
interaction: HASH_TABLE[MEDICATION, MEDICATION]

HASH_TABLE allows associating objects of one type with objects of another (or the same) type. The first parameter to HASH_TABLE is a value and the second one is a key. Therefore one can keep at most one value for the same key. If this suits your needs, the type of keys has to be HASHABLE. This type defines a function hash_code that computes an integer value for an object. All basic types and STRING are HASHABLE, so you can rely on the existing implementations to compute hash_code for MEDICATION objects. For example, if a medication has a unique name, it makes sense to define is as follows:
class MEDICATION inherit HASHABLE ... feature ...
name: STRING
...
hash_code: INTEGER
do
Result := name.hash_code
end
end

Related

What types of data are supported in Memgraph?

What data types are supported in Memgraph? Is there a difference between node and relationship types? Do I need to define data type in similar way that you define variable types in programing languages?
Memgraph stores all of the data in nodes and relationships. There are some important differences when it comes to them:
Nodes can have labels that are used to label or group nodes. A label is of the type String, and each node can have none or multiple labels. Labels can be changed at any time.
Relationships have a type, also represented in the form of a String. Unlike nodes, relationships must have exactly one relationship type and once it is set upon creation, it can never be modified again.
Nodes and relationships can store various properties. Property names are represented as text, while values can be of different types.
Each property can store a single value, and it is not possible to have multiple properties with the same name on a single graph element. But, the same property names can be found across multiple graph elements.
There are no restrictions on the number of properties that can be stored in a single graph element. The only restriction is that the values must be of the supported types.
This are supported data types in Memgraph:
Null - Property has no value, which is the same as if the property doesn't exist.
String - Character string (text).
Boolean - Boolean value, either true or false.
Integer - Integer number.
Float - Floating-point number (real number).
List - List containing any number of property values of any supported type under a single property name.
Map - Mapping of string keys to values of any supported type.
Duration - Period of time.
Date - Date with year, month, and day.
LocalTime - Time without the time zone.
LocalDateTime - Date and time without the time zone.

How do I assign arrays with multiple data types in VBA and then use the data in functions requiring certain datatypes?

I am working on a program which performs calculations on several physical elements. There are about 5 types of elements, each requiring different inputs(with different datatypes) to calculations. There is no limit on the number of elements or order in which they can be calculated.
For example: the first set of data might have an element of type A, and element of type B and a second element of type A.
The second set of data might have an element of type C, an element of type B, and five elements of type A.
Element type A might have a type(string) input and a pressure(double) input.
Element type B might have a type(string) input, a length(double) input, number of fins(integer) input.
Element type C might have a length(double) input, diameter(double) input and quantity(integer) input
Since I don't know the number or type of elements ahead of time, I have been using a variant array which has a number of rows which can be assigned after elements are selected by the user, and a number of columns equal to the maximum number of data points needed by any element.
The problem is that when I then try to use that data in other functions (by calling Array(1,2)) I get a "byref argument type mismatch" error.
From what I can tell, this means that my program doesn't KNOW that the value contained in the slot of my array can/should be the correct datatype. it just sees "variant" as opposed to "double" or "string" and throws an error.
I have found a couple ways around this.
1)I can put the data in parenthesis when I call the function. Apparent this forces the data to conform to the expected datatype.
function(input1, (Array(1,2)), input3)
2) I can go into all my functions and change the expected data-types to variant.
The problem is I am performing an engineering calculation, and I care about maintaining a certain level of accuracy in my numbers. I also DON'T really want my functions to be able to run if the inputs don't make sense. It seems to me that option 1 carries a high risk of loosing important data during the forced conversion. I'm not sure that option 2 is any better, since vba doesn't seem to assign/track these datatypes like I was imagining it did.
Is there any better way for me to assign and transfer data of a variety of types?
It sounds like you should probably create some custom Types or Classes (where each class has members with specific datatypes) for your different element types and then you can add those to a Collection/array and pass around for processing.
For example using classes:
Couple of very basic classes (just public variables, no methods):
'class module clsA
Public MyType As String
Public Pressure As Double
'class module clsB
Public MyType As String
Public Length As Double
Public FinCount As Integer
In a regular code module:
Sub tester()
Dim colThings As New Collection, e
colThings.Add MakeTypeAThing("TypeA", 67.92)
colThings.Add MakeTypeBThing("TypeB", 19.56, 4)
colThings.Add MakeTypeAThing("TypeA", 0.38)
For Each e In colThings
Debug.Print TypeName(e)
Next e
End Sub
'Couple of "factory" functions to create instances of your classes
' and set the member fields
Function MakeTypeAThing(typ As String, Pressure As Double) As clsA
Dim rv As New clsA
rv.MyType = typ
rv.Pressure = Pressure
Set MakeTypeAThing = rv
End Function
Function MakeTypeBThing(typ As String, l As Double, Fins As Integer) As clsB
Dim rv As New clsB
rv.MyType = typ
rv.Length = l
rv.FinCount = Fins
Set MakeTypeBThing = rv
End Function

Access array elements from string argument in Modelica

I'm having a task in Modelica, where within a function, I want to read out values of a record (parameters) according to a given string type argument, similar to the dictionary type in Python.
For example I have a record containing coefficicents for different media, I want to read out the coefficients for methane, so my argument is the string "Methane".
Until now I solve this by presenting a second array in my coefficients-record storing the names of the media in strings. This array I parse in a for loop to match the requested media-name and then access the coefficients-array by using the found index.
This is obviously very complicated and leads to a lot of confusing code and nested for loops. Isn't there a more convenient way like the one Python presents with its dictionary type, where a string is directly linked to a value?
Thanks for the help!
There are several different alternatives you can use. I will add the pattern I like most:
model M
function index
input String[:] keys;
input String key;
output Integer i;
algorithm
i := Modelica.Math.BooleanVectors.firstTrueIndex({k == key for k in keys});
end index;
constant String[3] keys = {"A","B","C"};
Real[size(keys,1)] values = {1,2*time,3};
Real c = values[index(keys,"B")] "Coefficient";
annotation(uses(Modelica(version="3.2.1")));
end M;
The reason I like this code is because it can be made efficient by a Modelica compiler. You create a keys vector, and a corresponding data vector. The reason it is not a record is that you want the keys vector to be constant, and the values may vary over time (for a more generic dictionary than you wanted).
The compiler can then create a constant index for any constant names you want to lookup from this. This makes sorting and matching better in the compiler (since there are no unknown indexes). If there is a key you want to lookup at run-time, the code will work for this as well.

What is the difference between array and enum in C ?

I find enum to be similar to an array,with the elements in it being numbered from 0. So what is the difference between an array and enum?
An Enum is basically a group of named constants.
It is an alternative to numbered flag parameters.
(It also doesn't have to be numbered from zero, you can specify the numbering.)
An Enum could be days of the week for example.
A Enum could departments in a company: eg SALES, BILLING, HR ...
A array is a sequence of memory locations. It is a collection. Each element in that collection is indexed by a number. So using that number you can retrieve the value stored at that location. Much like the page number in a book lets you look up the content of that page, the index on an array lets you look up the value stored at that location.
For example, if your company had numbered physical mail boxes for each department (starting from zero):
and you were creating some very simple software to let users log in to check how many letters they had uncollected,
you might choose to store them in an Array of Ints. Where the index is the mail box number, and the value is the number of letters in the box.
Then for ease of programming, you might choose define the departments as Enums (as described before) such that you could index of department name. But that would be getting more advanced.
You may be confusing Enum, with Enumerable which is a term used in some languages to describe any collection type which can be iterated though in sequence (Eg IEnumerable in C#). This term is not often used in C. It is not in anyway the same at all as Enum.
Array is a variable that can contain multiple elements with index starting from 0 whereas enum is an user defined datatype that contains a list of members for which an integer constant is assigned starting from 0. in case of enum the numbers starting from 0 are not indexes whereas in case of an array they are indexes. Also in case of enum you can assign your own constant values to the members that may or may not start from 0 and may or may not be in a sequence.
The main difference is that an array is a value and an enum is a type. And One main difference we can say that an array is a collection of other values (that is it contains other values, you can iterate through them or access individual ones by index), whereas an enum value is simply one atomic value. It doesn't contain any other values.
Both of them are totally different. Array is a value and enum is a type. array is a collection of different values whereas enum value is simply one value.
Array is used to iterate among various values using index whereas enum is assigned some atomic value and iterated so that we can easily iterate the type.
Array's value are assigned by using = operator 'eg. int a[4]={1,2,3,4}' and enum is the user defined data type and it is assigned like
enum days{sun,mon,tue,wed,thr,fri,sat}

Distinguishing Data Types and Data structures

Well, somehow even after reading in a lot of textbooks (really a lot) and on the Internet for a long while I still can’t completely comprehend what the difference between the two mentioned things is.
To simplify the question, according to let’s say Wikipedia a data type is:
a classification identifying one of various types of data, such as real, integer or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of that type can be stored.
with it being mostly the implementation of some Abstract data type like the real numbers or the whole numbers.
All good, then comes the data structure:
is a particular way of organizing data in a computer so that it can be used efficiently.[1][2] Data structures can implement one or more particular abstract data types., which are the means of specifying the contract of operations and their complexity. In comparison, a data structure is a concrete implementation of the contract provided by an ADT.
So a data structure is an implementation of an ADT like say a stack or queue.
But wouldn’t that make it a data type as well??
All that I can truly see is that a data type could range from really simple things without any structural organization to sophisticated structures of data what really counts is that they are an implementation of an ADT mirroring the important aspects of it and that they could be envisioned as a single entity like ( a list or tree), but data structures must contain at least some sort of logical or mathematical organization to classify as a data structure, but sadly this difference would make many entities both a data structure and a data type simultaneously.
So what is the solid difference between a simple plain (data type) and a (data structure)?
I would gladly accept an answer with specifying a specific book about this topic which goes deep enough to explain all this matters, also if someone can recommend me some good books about data structures in C.
In C, a data type is a language-level construct. There are a limited number of predefined types (int, char, double, etc.), and a practically unlimited number of derived types (array types, structure types, union types, function types, pointer types, atomic types (the latter are new in C11)).
Any type may be given a simple name via a typedef declaration. For any type other than a function type or an incomplete type, you can have objects of that type; each object occupies a contiguous region of memory.
The types that can exist in C are completely described in section 6.2.5 of the C standard; see, for example, the N1570 draft.
A data structure, on the other hand, is a construct defined by your own code. The language does not define the concept of a linked list, or a binary tree, or a hash table, but you can implement such a data structure, usually by building it on top of derived data types. Typically there is no such thing as an object that's a linked list. An instance of a linked list data structure consists of a collection of related objects, and only the logic of your code turns that collection into a coherent entity. But you'll typically have an object of some data type that your program uses to refer to the linked list data structure, perhaps a structure or a pointer to a structure.
You'll typically have a set of functions that operate on instances of a data structure. Whether those functions are part of the data structure is a difficult question that I won't try to answer here.
An array, for example, can be considered both a data type and a data structure; more precisely, you can think of it as a data structure implemented using the existing array type.
Referring >=C99:
The are two kinds of data types:
intrinsic: char, int, float, double, _Complex, _Bool, void (for some of them there a variation to long and unsigned around)
derived: arrays, structures, unions, pointers, functions
The latter are build from the former and/or the latter.
So to answer your question:
So what is the solid difference between a simple plain (data type) and a (data structure)?
A "data structure [type]" is derived from "simple plain data type"(s) and/or other "data structure [type]"(s).
A data type specifies the values and operations allowed for a single expression or object; a data structure is a region of storage and algorithms that organize objects in that storage.
An example of a data type is int; objects of this type may store whole number values in at least the range [-32767, 32767], the usual arithmetic operations may be performed these objects (although the result of integer division is also an integer, which trips people up the first time around). You can't use the subscript operator [] on an int, nor may you use the function call () operator on an int object.
For an example of a data structure, we can look at a simple stack. We'll use an array as our region of storage. We'll define an additional integer item to serve as a stack pointer - it will contain the index of the element most recently added to the array. We define two algorithms - push and pop - that will add and remove items to the stack in a specific order.
push: if sp is less than stack size then
add 1 to sp
write input to array[sp]
else
stack overflow
end if
pop: if sp is greater than 0 then
get value from array[sp]
subtract 1 from sp
return value
else
stack underflow
end if
Our stack data structure stores a number of objects of some data type such that the last item added is always the first item removed, a.k.a. a last-in first-out (LIFO) queue. If we push the values 1, 2, and 3 onto the stack, they will be popped off in the order 3, 2, and 1.
Note that it's the algorithms that distinguish data structures from each other, not the type of storage. If your algorithms add an item to one end of the array and pull them from the other end, you have a first-in first-out (FIFO) queue. If your algorithms add items to the array such that for each element i in the array a[i] >= a[2*i] and a[i] >= a[2*i+1] are both true, you have a heap.
Basically,
A Data type defines a certain domain of values and it defines the operations allowed on those values. All the basic data types defined by the compiler are called Primitive Data Types
A Data structure is rather an User defined data type and is the systematic way to organize data so that it can be used efficiently. The operations and values of these are not specified in the language itself , but it is specified by the user.
The Book to learn more about this is "The C Programming Language - Dennis Ritchie"
A data type represents the type of data that is going to be stored in a variable. It specifies that a variable will only assign values of a specific type.
A data structure is a collection that holds various from of data.

Resources