RPG Next Gen
Wiki RSS Feed Weblog
Business picture

Vector

A vector is a one-dimensional array. In our case it is also a dynamic array which means that the size is not set at compile time but at runtime and it can grow if required.

An array allocates memory for all its elements lumped together as one block of memory. In this implementation of a vector only the pointer to the data is stored in this one block of memory. In this way millions of entries can be stored and accessed.

Features

The following features are available in the Vector service program (in no particular order):

  • Creating a vector
  • Adding to the vector (beginning, end, by index)
  • Adding all entries of another vector
  • Replacing entries of the vector
  • Removing entries from the vector (beginning, end, by index, range)
  • Creating a new vector with a subset of the entries
  • Clearing the vector
  • Check size of the vector
  • Check fill status of the vector (empty or not empty)
  • Get entry of the vector (beginning, end, by index)
  • Check if the vector contains an item
  • Get index of an entry
  • Get last index of an entry
  • Copy all entries to a character array
  • Swap entries in the vector
  • Execute a procedure on every entry of the vector
  • Reverse vector
  • Create a vector from a character string
  • Create a copy of the vector
  • Count the frequency of an entry in the vector
  • Data type specific procedures for storing and getting values

Vector iteration has no special procedures in the service program as in the Linked List service program because the access to any entry of the vector is constant and can be made with passing the index of the desired entry.

The following features may find it into the next release:

  • sort — sort list (optional with a user-defined comparator)

Implementation

The Vector service program uses dynamic memory allocation via the ILE CEE procedures. It create a new heap for every vector. Because of that it is necessary to use the dispose procedure after using the vector for freeing up the allocated memory. If the memory is not freed with the dispose procedure it will be released with the ending of the activation group or job.

The create() procedure of the vector has two optional parameters:

  1. Init size: the initial size of the vector (default: 10)
  2. Increment size: the space for the number of entries which will be added if there is no space left for a new entry (default: current size * 2)

Both parameters are optional and need not to be passed.

The code is written in RPG IV free format. It uses some C-functions for working with memory and strings and intensely uses pointers (even more than the Linked List service program).

Code sample

// creating a vector vector = vector_create(); // check if the vector is empty (it should be) if (vector_isEmpty(vector)); dsply 'Vector is empty'; else; dsply 'Vector is not empty'; endif; // add a string to the vector vector_addString(vector : 'This a test string.'); // add a data structure to the vector vector_add(vector : %addr(my_data_structure) : %size(my_data_structure)); // add an integer to the vector vector_add(vector : %addr(my_int_var) : 4); // add an integer with the data type specific procedure vector_addInteger(vector : my_int_var); // create a new vector which is populated with // a subset of data from the original vector subset = vector_sublist(vector : 2); // iterate through the entries of the list for i = 0 to vector_getSize(vector) - 1; value = %str(vector_get(vector : i)); dsply value; endfor; // freeing the allocated memory and removing the created heap vector_dispose(vector);

Installation Instructions

A short description of how to extract the source and compile the code can be found here and in the source package.

Examples

The examples section contains some examples of how to use the vector.

Download

For source and binary packages take a look at the download area.

Documentation

API documentation of the Linked List Utilities service program can be downloaded in HTML format.