Archived

This topic is now archived and is closed to further replies.

The Heretic

Shifting arrays...

Recommended Posts

I''m finally almost done with some semantic network code I''ve been working on. I know the code compiles fine but I''m not sure if it will shift everything properly... Would someone just look it over for me?
  
//Clean clean all nodes and links that are slated  for demolition

void semNet::clean()
{
     unsigned short i, j;                                                           //Declare our counters


     cleanAllLinks();                                                               //Clean all links

     for (i = 0; i < nU; i++)                                                       //Clean the nodes

     {
          //If one of them is slated for demolition, kill the link

          if ((net[i].flags & 1) == 1)
          {
               for (j = i + 1; j < nU; j++)                                 //Move the other links down

               {
                    net[j - 1].knowledge = net[j].knowledge;
                    net[j - 1].pos = net[j].pos;
                    net[j - 1].lU = net[j].lU;
                    net[j - 1].flags = net[j].flags;

                    //Copy the links

                    for (unsigned char i = 0; i < net[nU].lU; i++)
                    {
                         net[j - 1].links[i].type = net[j].links[i].type;
                         net[j - 1].links[i].truth = net[j].links[i].truth;
                         net[j - 1].links[i].ptr = net[j].links[i].ptr;
                         net[j - 1].links[i].flags = net[j].links[i].flags;
                    }
               }
               nU--;                                                                //Get rid of a node

          }
     }
}

//Clean all links for all nodes

void semNet::cleanAllLinks()
{
     unsigned short i, j, l;                                                        //Declare our counters


     for (l = 0; l < nTot; l++)
     {
          for (i = 0; i < net[l].lU; i++)
          {
               //If one of them is slated for demolition, kill the link

               if (((net[l].links[i].flags | net[net[l].links[i].ptr].flags) & 1) == 1)
               {
                    for (j = i + 1; j < net[l].lU; j++)                            //Move the other links down

                    {
                         net[l].links[j - 1].type = net[l].links[j].type;           //Copy the type

                         net[l].links[j - 1].truth = net[l].links[j].truth;         //Copy the truth value

                         net[l].links[j - 1].ptr = net[l].links[j].ptr;             //Copy the pointer

                         net[l].links[j - 1].flags = net[l].links[j].flags;         //Copy the flags

                    }
                    net[l].lU--;                                                   //Get rid of a link

               }
          }
     }
}

//Clean the links for a certain node

void semNet::cleanLinks(unsigned short nN)
{
     unsigned short i, j;                                                           //Declare our counters


     for (i = 0; i < net[nN].lU; i++)
     {
          //If one of them is slated for demolition, kill the link

          if (((net[nN].links[i].flags | net[net[nN].links[i].ptr].flags) & 1) == 1)
          {
               for (j = i + 1; j < net[nN].lU; j++)                                 //Move the other links down

               {
                    net[nN].links[j - 1].type = net[nN].links[j].type;              //Copy the type

                    net[nN].links[j - 1].truth = net[nN].links[j].truth;            //Copy the truth value

                    net[nN].links[j - 1].ptr = net[nN].links[j].ptr;                //Copy the pointer

                    net[nN].links[j - 1].flags = net[nN].links[j].flags;            //Copy the flags

               }
               net[nN].lU--;                                                        //Get rid of a link

          }
     }
}
  

Share this post


Link to post
Share on other sites
I think there''s an error: if an item at index I is deleted,
then item at index I+1 will move at index I (as well as the others, which will shift up). But the for loop will then increment, and process item I+1 (which was the previous I+2 item) leaving the item at I (previously at I+1) unprocessed.

Besides, the code looks ok to me. Why not test it? Perhaps with a log to file, maybe?

Share this post


Link to post
Share on other sites