Object | +---DIntArray
The DIntArray class implements an one dimensional array of integers. It is of course possible to create an array of integers with the classes DArray and DInt, but this is not very efficient and a little bit clumsy. This class also implements some statistical methods.
#include <stdio.h> #include "ofc/DIntArray.h" int main(int argc, char *argv[]) { DIntArray *arr1 = [DIntArray alloc]; DIntArray *arr2 = [DIntArray new ]; DText *str; int int1[] = {9, 4, 2, 9, 3, 7}; int int2[] = {3, 2, 1}; [arr1 init :int1 :sizeof(int1)/sizeof(int)]; // Init with an array of integers printf("Length of array1:%ld.\n", [arr1 length]); str = [arr1 toText]; // Convert the array to a string printf("Array1 as string:%s.\n", [str cstring]); [str free]; [arr1 put :3 :8]; // Change value on index 3 from 9 to 8 [arr1 insert :2 :2 :int2 :sizeof(int2)/sizeof(int)]; // Replace value on index 2 with the int2 array [arr1 delete :4 :5]; // Delete two values from the array str = [arr1 toText]; printf("Array1 as string:%s.\n", [str cstring]); // Print the modified string [str free]; // Searching in the array printf("Value 3 is present %ld times in array1.\n", [arr1 count :3 :0 :-1]); // Count the occurences of an integer printf("First index of value 3:%ld.\n", [arr1 index :3 :0 :-1]); // Find first index // Statistical methods printf("Sum of array1:%ld\n", [arr1 sum :0 :-1]); printf("Max of array1:%d\n", [arr1 max :0 :-1]); printf("Min of array1:%d\n", [arr1 min :0 :-1]); printf("Avg of array1:%.1f\n", [arr1 average :0 :-1]); printf("Std of array1:%.1f\n", [arr1 standardDeviation :0 :-1]); [arr1 sort :0 :-1]; // Sort the array str = [arr1 toText]; printf("Array1 sorted as string:%s.\n", [str cstring]); [str free]; // Use array2 as lifo/fifo buffer [arr2 size :50]; // Insure the size of the buffer [arr2 push :1]; [arr2 push :2]; [arr2 push :3]; printf("Length of lifo:%ld.\n", [arr2 length]); printf("Top of lifo:%d.\n", [arr2 tos]); printf("Pop value (%d) and another (%d).\n", [arr2 pop], [arr2 pop]); [arr2 clear]; [arr2 enqueue :4]; [arr2 enqueue :5]; [arr2 enqueue :6]; [arr2 enqueue :7]; printf("Length of fifo:%ld.\n", [arr2 length]); printf("Dequeue value (%d) and another (%d).\n", [arr2 dequeue], [arr2 dequeue]); // Compare arrays if ([arr1 compare :arr2] == 0) printf("Array1 is equal to array2.\n"); else if ([arr1 compare :arr2] < 0) printf("Array1 is smaller than array2.\n"); else printf("Array1 is greater than array2.\n"); [arr1 free]; // Cleanup [arr2 free]; return 0; }