Solved

Efficient way to load a linked list C like datastructure

Posted on 2009-07-06
9
352 Views
Last Modified: 2012-05-08
Hi guys,

I'm looking for an efficient and clean way to fill up a linked list datastructure that I use with JNI to interface with a C program. I have to use this same structure as a parameter when calling the JNI again.

The first class is the class I want to fill up and as you can see there is a pointer to the next structure p_next_rec

And below that is a snippet of how I'm currently proposing on implementing what I want to do but I was wondering if there is a better way.

(Note I haven't run this through a compiler only wrote it in notepad so not too worried about minor syntax errors if there is any).
//Class that is similar to what I'm using which is part of the JNI

public class API_DATASTRUCTURE_T implements Serializable, Cloneable 

{

  // Fields

  public static String[] _meths;

  private API_DATASTRUCTURE_T p_next_rec;

  private String name;
 

  // Constructors

  public API_DATASTRUCTURE_T() { }
 

  // Methods

  public API_DATASTRUCTURE_T getP_next_rec() { return null;}

  public String get_name() { return null;}

  private static native void initFIDs();

  public void setP_next_rec(API_DATASTRUCTURE_T aPI_DATASTRUCTURE_T) { }

  public void setname(String string) { }

}
 
 

//Snippet of code to fill up the above structure
 

		//this variable will be my final object that is full with data

        API_DATASTRUCTURE_T setupDataStructure = new API_DATASTRUCTURE_T();

        

        //Use this variable to keep a pointer reference when traversing the structure above

        API_DATASTRUCTURE_T pointerToNextStructure = new API_DATASTRUCTURE_T();
 

		//this is a collection with the names

		Iterator<String> dataIt = collectionWithData.iterator();

        

        //Fill first structure

        if(dataIt.hasNext())

        {

        	setupDataStructure.setname(dataIt.next());

		}

        

        //if we have more than one then keep a pointer reference so we can traverse the structure

        if(dataIt.hasNext())

        {

            pointerToNextStructure = setupDataStructure;

        }

        

        while(dataIt.hasNext())

        {

            //place holder for data for each iteration.

            API_DATASTRUCTURE_T placeHolder = new API_DATASTRUCTURE_T();

            
 

            //setting name

            placeHolder.setname(dataIt.next());

            

            pointerToNextStructure.setP_next_rec(placeHolder);
 

            //Traversing through the structure while keeping a reference to the pointer

            pointerToNextStructure = pointerToNextStructure.getP_next_rec(); 

        }        
 

		//WHEN I GET HERE I CAN USE setupDataStructure which is now full with data.

Open in new window

0
Comment
Question by:LiamOB
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24787593
Personally i would keep it more Java-like and you probably ought to implement some collection-oriented interfaces. You might want a ctor with the following prototype
public ApiDataStructure(Collection data)

Open in new window

0
 

Author Comment

by:LiamOB
ID: 24787623
Are you proposing I change API_DATASTRUCTURE_T class?

If you are then unfortunately I can't, this is a very large scale program and this class is auto generated as part of our JNI infrastructure. So I don't have an option of changing API_DATASTRUCTURE_T class.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24787678
If it's auto-generated, then don't worry about names.

Otherwise, just keep a reference to the head and tail of the list and fill it with elements from your collection
0
 
LVL 2

Expert Comment

by:KenClement
ID: 24787728
If efficiency is what you are really after and the use of JNI with C is already a committed decision then consider outsourcing the serialization/deserialization of the API_DATASTRUCTURE_T to the native side of things.  That way you could either implement the serialized list as a sequence of records in a file (formatted appropriately for C file semantics) or as a memory mapped file with the mapping to a fixed virtual address or with non-pointer mechanisms (e.g. relative offsets) used to connect things together.

While I don't have the JNI reference handy, I recall that JNI had everything necessary to expose structures on the native side as Java objects to the JVM.  You could even implement iterator semantics on a collection of these beasts.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:LiamOB
ID: 24787791
CEHJ: I don't understand what you mean when you say "don't worry about names"?? are you talking where I set the name? if so this is just a variable I have to set, in reality there is more variables other than name.

KenClement: This is a large scale project and I don't have time to investigate what you said, I'm just looking for a clean way to implement this in Java in the current format that is presented to me. Thanks for the comment though.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 24789529
>>CEHJ: I don't understand what you mean when you say "don't worry about names"??

Only about naming conventions. Don't worry about them if aut-generated
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 24790113

you create some instances unnecessarily, and it can be simplified a little.
try this:

//this variable will be my final object that is full with data

API_DATASTRUCTURE_T setupDataStructure = null;
       
//Use this variable to keep a pointer reference when traversing the structure above

API_DATASTRUCTURE_T pointerToNextStructure = null;
 
//this is a collection with the names
Iterator dataIt = collectionWithData.iterator();
       
while(dataIt.hasNext())
{
     //place holder for data for each iteration.
     API_DATASTRUCTURE_T placeHolder = new API_DATASTRUCTURE_T();
 
     //setting name
     placeHolder.setname(dataIt.next());
     
     // check if its first
     if (setupDataStructure==null)
     {
          setupDataStructure = placeHolder;
     }
     else
     {
          pointerToNextStructure.setP_next_rec(placeHolder);
     }
     //Traversing through the structure while keeping a reference to the pointer
     pointerToNextStructure = placeHolder;
}        
 
//WHEN I GET HERE I CAN USE setupDataStructure which is now full with data.
0
 
LVL 2

Assisted Solution

by:KenClement
KenClement earned 150 total points
ID: 24793666
>> This is a large scale project and I don't have time to investigate what you said, I'm just looking for a clean way to implement this in Java in the current format that is presented to me.

In that case as long as the linked list is not too large (< 100 entries or so) almost any correct method will do.  If that is the case you ought to stick to whatever you are comfortable with.
0
 

Author Closing Comment

by:LiamOB
ID: 31656992
Thanks guys for your comments, I gave the points between anyone that helped, the most points for objects as he used a bit of code. I ended up tweaking my code a different way but not much really. Thanks again anyway.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now