From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!newsfeed.straub-nv.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Runtime type selection Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <2c15c8a6-b555-4d08-8fe6-f77cb207e7a6@k33g2000yqa.googlegroups.com> Date: Sun, 11 Oct 2009 22:06:22 +0200 Message-ID: <1fv6ippku9z2n$.rbm60r20961$.dlg@40tude.net> NNTP-Posting-Date: 11 Oct 2009 22:06:22 CEST NNTP-Posting-Host: f4a69125.newsspool3.arcor-online.net X-Trace: DXC=XZ4fjgCClCPYQ5E:lKK4PYCS[[6LHn;2LCV^[[3E^ X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:8675 Date: 2009-10-11T22:06:22+02:00 List-Id: On Sun, 11 Oct 2009 09:41:54 -0700 (PDT), xorque wrote: > I'd like to develop a program that processes a graph. The contents of > the graph are a random selection of objects of different types (all > elements may be of the same type or may all be different - assuming > there are enough types defined). By 'type' in this case, I am > referring to Ada types. > > The basic problem is that I want to create an object of a type picked > at random, at runtime. I'm not entirely sure what my options are here. The problem is that graph nodes have nothing in common. Thus there is nothing except for the graph traversal operations you could implement on the graph as a whole. > I could create a base tagged type and derive a set of types from that > base, selecting at random. I could use variant records. It seems that > in either case, I'd need to create an enumeration type with a 1:1 mapping > between enumeration values and derived types. > > Both of these solutions seem a little unwieldly. They also seem like > they'd end up making the program difficult to extend (I'd ideally just > like to derive a new type and have it automatically available to be > selected at random for the graph). > > Any ideas would be appreciated. I appreciate the problem is a little > abstract... In Ada there is a way to create linked structures (in particular graphs) of absolutely any elements of any type. The idea goes as follows. You create a custom storage pool which holds the structure of links transparently to the objects allocated in it. For example, when you allocate something in the pool that is considered the contents of a node. The pointers to its neighbours in the graph are allocated in the same pool in front of the object itself. The operation new returns the pointer to the contents. So it is really anything that can become a node, even a task. (There are some difficulties when nodes are unconstrained arrays, but that works as well.) Simple components uses this approach to implement doubly-linked lists: http://www.dmitry-kazakov.de/ada/components.htm#Generic_Doubly_Linked_Web -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de