Programing question

in General Discussion edited January 2014
Have an interesting set up but need to know something.

i have a class, netowrk that has a group of connections, and a group of layers. the connections link items in each layer to other items in other layers. each layer has a list of items, and a list of connections that are asscoiated with that layer. the thing is i want the list of connections in the layer to be pointers to the objects back in the network class. As far as a layer is concerned no other layers exists, only the items and connections. but a any mchange made to the connection object needs to be apparent to the other layers.

so i guess my question is how does one make an array of pointers in objective-c?

for reference:

Network a has 3 layers.

first two layers has 2 items.

3rd ayer has 1.

total of 6 connections

2 from each item in layer 1 to the two in layer 2.

1 from each item to the item in layer 3.

layer 1 has a list of connections that are realted to it: 4 conns

layer 2 has a list of connections that are realted to it: 6 conns

layer 3 has a list of connections that are realted to it: 2 conns


  • Reply 1 of 5
    thuh freakthuh freak Posts: 2,664member
    quickly written pseudocode, cuz i think easier with code in front of me:


    @interface Network:NSObject {

    NSArray *nconnections, *layers;


    @interface Layer:NSObject {

    NSArray *lconnections, *items;


    So you want the objects contains in Layer's `lconnections' to point to the same objects in Network's `nconnections'. This is quite simple. All objects in objc are pointers. In fact, your not allowed to put an object on the stack (as you would if it weren't a pointer). So, when you add object to the nconnection (Network's connections), add that same object to lconnection (Layer's connections). Both the Network object's list and the Layer object's list will point to the same object. And, if you use NSArray (or NSMutableArray), the array class will increment the objects refcount ([obj retain]), and they dec the count when the obejcts are removed ([obj release]).

    But really, a better solution than I showed above would be to get rid of the nconnections in Network. Make an accessor, that added up all the connections from the layers (removing duplicates if necessary), and returned that.


    @implementation Network

    -(NSArray*)getListOfConnections {

    NSEnumerator *enu = [layers objectEnumerator];

    NSMutableArray *retVal = [[NSMutableArray alloc] autoretain];

    Layer *lyr;

    while ( (lyr = [enu nextObject]) ) {

       [retVal addObjectsFromArray: [ltr getConnections]];

    /* Layer would need a 'getConnections' message.

    this doesn't take care of duplicate connections.

    You can iterate through the lyr's conns, and test

    if they are already in 'retVal' before adding them. */


    return retVal;


  • Reply 2 of 5
    myahmacmyahmac Posts: 222member
    I like the first idea, the problem with the second i dea is it puts the connections back with the layers. The connections have to be handled created and handled by the network because as the prgram builds its own networks the layers would not be properly connected. A connection would exist with one layer in it, but that would not be the same object in the second layer. what you have is what is more towards what i was thinking of at first.

    if connections are made in the layers:

    layerer1 has a1, a2, b1, b2

    layer2 has c1, c2, d1, d2

    network has a1, a2, b1, b2 c1, c2, d1, d2

    there would be 8 connections because each layer would produce its own connection objects and then give that list to the netwrk.

    but c1 et all need to be the same objects that are in layer1 because that is how they are linked.
  • Reply 3 of 5
    thuh freakthuh freak Posts: 2,664member
    i c. so the network class builds the connections, then the layers have to grab certain connections from the network? are the connections built on request by the layer, or all at once then given out?

    are the networks and layers each building the connections independantly, even if they are building the same objects? if that is the case, then you need to appoint one of the classes as the supervisor over the connections (i'd recommend the network). Have it build the connections as it needs them (or per the layers' requests). But, build an 'isEqual' method in the connection class like this:


    @implementation Connection

    -(BOOL)isEqual:obj {

    /* first throw a NO if obj is the wrong type */

    Connection* connObj = (Connection*)obj;

    /* go through each of the Connection's important

    properties, comparing [self] to [connObj]. */

    /* return a YES if they all equal, NO if any is wrong. */



    Now, when the layer wants a connection, it should ask the Network for one. The Network will create one ('newConn'), test its list of connections if it already has that one ([nconnections indexOfObject:newConn] != NSNotFoundIndex). If it has it already, it should release the newly created version ('newConn'), and return the already existing one from its list ([nconnections objectAtIndex:/*index from earlier indexOfObject call*/]). If it doesn't already have it, it should return the new obj (probably autoreleasing it; or you'll have to work out ownership and memory issues).

    You can also move the creation of the temp connection ('newConn') to the layer, and have it as a parameter for the Network's connectionGiver message (or you can do both):


    @implementation Network


     (Connection*)conn {

    unsigned index = [nconnections indexOfObject:conn];

    if ( index==NSNotFoundIndex ) {

    [nconnections addObject:conn];

    return [conn retain];

    /* that retain makes sure that the caller can always release

    the connection it used as a parameter, even if its the

    same as the return value */


    return [nconnections objectAtIndex:index];



    with that code, the caller has to create a connection, and pass it to Network:getConectionMatching. It should keep the returned val as its connection, and release the connection it passed as a parameter.
  • Reply 4 of 5
    myahmacmyahmac Posts: 222member
    well to help simplify things, the network is a neural network. the layers are layers of a neural networtk, and the items in the lkayer are neurons. each neuron in a layer has to be connected to a neuron in another layer.

    the network then builkds the conne3ctions based on how the netowork is organized 1 or 2 layers might input into another layer etc. network also designate an input and ouput pointer to the input and output layers. That way if a network is interlaced withanother network, all the consturctor has to do is build another connection obje4ct with the input and ouytput pointers as params.

    if a netowrk has several hundred connections, going through the list of for every tconnection for each neuron would be hella slow for just about any CPU. thats why i want a smaller list in a layer of the connections because everyone of them will yield a connection for at least one neuron vs searching through a list of 200 or so.

    maybe the order would help as well

    a network build some layers with some amount of neurons

    it then is told to connect a pair of layers, a sender and a receiver. a connection is then made for every instance of a neuron connecting to antoher neuron. so neuron a1 has a conection to neuron b1 and neuron b2.

    once things are built an order is given to begin a synapse with the first layer, or input layer. the layer then goes to each neuron, tells it to compute, and give its output to the connection object associated with it. then it return the control to the next layer it is linked to. values from neurons and conection paths are held in the connection object. the next layer upon a synapse, takes any input from a changed connection, gives it to the right neurons, all neurons fire. if any are activated they give an output and the process continues until the output layer's neurons are activated. If they do not activate the input layer fires again, and the process keeps repeating until the output is given.

    BTW I really apreciate your help and ideas
  • Reply 5 of 5
    thuh freakthuh freak Posts: 2,664member
    i think i understand you, but my neurons may be out of place .

    you want the connections contained that the Network has, to point to the same connections that the Layer has? So, first, you decide who is going to create the connection. I can't quite figure out which class (Layer or Network) would be better suited (I'm leaning toward Network though). The other class has to ask for a connection from the first. If you need speed, sort your lists, and enumerate through them in some special order.
Sign In or Register to comment.