Skip to content

CollectionsNDE: module to speed up Java collections creation #85

@EmilyGraceSeville7cf

Description

@EmilyGraceSeville7cf

Description

  • Don't call Java constructors manually
  • Create collections quicker (NewArrayList, NewArrayList(capacity), ...)
  • Create collections from ranges and random ones (ArrayListFromRange(from, to_), ArrayListFromRandom(from, to_, count), ...)
  • Read collections from keyboard (ReadIntegerArrayList(count), ReadRealArrayList(count), ...)

Code

unit CollectionsNDE;

/*
  Simplifies Java's collections creation by providing wrappers.
 */

interface
type
  ArrayListT = java_util_ArrayList;
  LinkedListT = java_util_LinkedList;
  ListT = ArrayListT; // default list type
  
  ListIteratorT = java_util_ListIterator;

function NewArrayList(): ArrayListT;
function NewArrayList(capacity: integer): ArrayListT;
function NewLinkedList(): LinkedListT;

function L(): ListT;
function L(capacity: integer): ListT;

function ArrayListFromRange(from, to_: integer): ArrayListT;
function ArrayListFromRandom(from, to_, count: integer): ArrayListT;
function LinkedListFromRange(from, to_: integer): LinkedListT;
function LinkedListFromRandom(from, to_, count: integer): LinkedListT;

function ReadIntegerArrayList(count: integer): ArrayListT;
function ReadRealArrayList(count: integer): ArrayListT;
function ReadStringArrayList(count: integer): ArrayListT;
function ReadIntegerLinkedList(count: integer): LinkedListT;
function ReadRealLinkedList(count: integer): LinkedListT;
function ReadStringLinkedList(count: integer): LinkedListT;


type
  HashSetT = java_util_HashSet;
  TreeSetT = java_util_TreeSet;
  LinkedHashSetT = java_util_LinkedHashSet;
  SetT = HashSetT; // default set type

function NewHashSet(): HashSetT;
function NewHashSet(capacity: integer): HashSetT;
function NewHashSet(capacity, loadFactor: integer): HashSetT;

function NewTreeSet(): TreeSetT;

function NewLinkedHashSet(): LinkedHashSetT;
function NewLinkedHashSet(capacity: integer): LinkedHashSetT;
function NewLinkedHashSet(capacity, loadFactor: integer): LinkedHashSetT;

function S(): SetT;
function S(capacity: integer): SetT;
function S(capacity, loadFactor: integer): SetT;

function HashSetFromRange(from, to_: integer): HashSetT;
function HashSetFromRandom(from, to_, count: integer): HashSetT;


type
  HashMapT = java_util_HashMap;
  TreeMapT = java_util_TreeMap;
  LinkedHashMapT = java_util_LinkedHashMap;
  MapT = HashMapT; // default map type

function NewHashMap(): HashMapT;
function NewHashMap(capacity: integer): HashMapT;
function NewHashMap(capacity, loadFactor: integer): HashMapT;

function NewTreeMap(): TreeMapT;

function NewLinkedHashMap(): LinkedHashMapT;
function NewLinkedHashMap(capacity: integer): LinkedHashMapT;
function NewLinkedHashMap(capacity, loadFactor: integer): LinkedHashMapT;
function NewLinkedHashMap(capacity, loadFactor: integer; accessOrder: boolean): LinkedHashMapT;

function M(): MapT;
function M(capacity: integer): MapT;
function M(capacity, loadFactor: integer): HashMapT;


implementation
const
  Prompt = '> ';
  
function NewArrayList(): ArrayListT;
var
  result: ArrayListT;
  
begin
  New(result);
  NewArrayList := result;
end;

function NewArrayList(capacity: integer): ArrayListT;
var
  result: ArrayListT;
  
begin
  New(result, capacity);
  NewArrayList := result;
end;

function NewLinkedList(): LinkedListT;
var
  result: LinkedListT;
  
begin
  New(result);
  NewLinkedList := result;
end;

function L(): ListT;
begin
  L := NewArrayList();
end;

function L(capacity: integer): ListT;
begin
  L := NewArrayList(capacity);
end;

function ArrayListFromRange(from, to_: integer): ArrayListT;
var
  result: ArrayListT;
  current: integer;

begin
  result := NewArrayList();
  
  current := from;
  while current <= to_ do
  begin
    result.add(current);
    Inc(current);
  end;
  
  ArrayListFromRange := result;
end;

function ArrayListFromRandom(from, to_, count: integer): ArrayListT;
var
  result: ArrayListT;
  itemCount: integer;

begin
  result := NewArrayList();
  
  itemCount := 0;
  while itemCount < count do
  begin
    result.add(Round(from + Random() * (to_ - from)));
    Inc(itemCount);
  end;
  
  ArrayListFromRandom := result;
end;

function LinkedListFromRange(from, to_: integer): LinkedListT;
var
  result: LinkedListT;
  current: integer;

begin
  result := NewLinkedList();
  
  current := from;
  while current <= to_ do
  begin
    result.add(current);
    Inc(current);
  end;
  
  LinkedListFromRange := result;
end;

function LinkedListFromRandom(from, to_, count: integer): LinkedListT;
var
  result: LinkedListT;
  itemCount: integer;

begin
  result := NewLinkedList();
  
  itemCount := 0;
  while itemCount < count do
  begin
    result.add(Round(from + Random() * (to_ - from)));
    Inc(itemCount);
  end;
  
  LinkedListFromRandom := result;
end;

function ReadIntegerArrayList(count: integer): ArrayListT;
var
  result: ArrayListT;
  index: integer;
  current: integer;

begin
  result := NewArrayList();
  
  index := 0;
  while index < count do
  begin
    Write(index + 1, Prompt);
    Readln(current);
    result.add(current);
    Inc(index);
  end;
  
  ReadIntegerArrayList := result;
end;

function ReadRealArrayList(count: integer): ArrayListT;
var
  result: ArrayListT;
  index: integer;
  current: real;

begin
  result := NewArrayList();
  
  index := 0;
  while index < count do
  begin
    Write(index + 1, Prompt);
    Readln(current);
    result.add(current);
    Inc(index);
  end;
  
  ReadRealArrayList := result;
end;

function ReadStringArrayList(count: integer): ArrayListT;
var
  result: ArrayListT;
  index: integer;
  current: string;

begin
  result := NewArrayList();
  
  index := 0;
  while index < count do
  begin
    Write(index + 1, Prompt);
    Readln(current);
    result.add(current);
    Inc(index);
  end;
  
  ReadStringArrayList := result;
end;

function ReadIntegerLinkedList(count: integer): LinkedListT;
var
  result: LinkedListT;
  index: integer;
  current: integer;

begin
  result := NewLinkedList();
  
  index := 0;
  while index < count do
  begin
    Write(index + 1, Prompt);
    Readln(current);
    result.add(current);
    Inc(index);
  end;
  
  ReadIntegerLinkedList := result;
end;

function ReadRealLinkedList(count: integer): LinkedListT;
var
  result: LinkedListT;
  index: integer;
  current: real;

begin
  result := NewLinkedList();
  
  index := 0;
  while index < count do
  begin
    Write(index + 1, Prompt);
    Readln(current);
    result.add(current);
    Inc(index);
  end;
  
  ReadRealLinkedList := result;
end;

function ReadStringLinkedList(count: integer): LinkedListT;
var
  result: LinkedListT;
  index: integer;
  current: string;

begin
  result := NewLinkedList();
  
  index := 0;
  while index < count do
  begin
    Write(index + 1, Prompt);
    Readln(current);
    result.add(current);
    Inc(index);
  end;
  
  ReadStringLinkedList := result;
end;


function NewHashSet(): HashSetT;
var
  result: HashSetT;
  
begin
  New(result);
  NewHashSet := result;
end;

function NewHashSet(capacity: integer): HashSetT;
var
  result: HashSetT;
  
begin
  New(result, capacity);
  NewHashSet := result;
end;

function NewHashSet(capacity, loadFactor: integer): HashSetT;
var
  result: HashSetT;
  
begin
  New(result, capacity, loadFactor);
  NewHashSet := result;
end;

function NewTreeSet(): TreeSetT;
var
  result: TreeSetT;
  
begin
  New(result);
  NewTreeSet := result;
end;

function NewLinkedHashSet(): LinkedHashSetT;
var
  result: LinkedHashSetT;
  
begin
  New(result);
  NewLinkedHashSet := result;
end;

function NewLinkedHashSet(capacity: integer): LinkedHashSetT;
var
  result: LinkedHashSetT;
  
begin
  New(result, capacity);
  NewLinkedHashSet := result;
end;

function NewLinkedHashSet(capacity, loadFactor: integer): LinkedHashSetT;
var
  result: LinkedHashSetT;
  
begin
  New(result, capacity, loadFactor);
  NewLinkedHashSet := result;
end;

function S(): SetT;
begin
  S := NewHashSet();
end;

function S(capacity: integer): SetT;
begin
  S := NewHashSet(capacity);
end;

function S(capacity, loadFactor: integer): SetT;
begin
  S := NewHashSet(capacity, loadFactor);
end;

function HashSetFromRange(from, to_: integer): HashSetT;
var
  result: HashSetT;
  current: integer;

begin
  result := NewHashSet();
  
  current := from;
  while current <= to_ do
  begin
    result.add(current);
    Inc(current);
  end;
  
  HashSetFromRange := result;
end;

function HashSetFromRandom(from, to_, count: integer): HashSetT;
var
  result: HashSetT;
  itemCount: integer;

begin
  result := NewHashSet();
  
  itemCount := 0;
  while itemCount < count do
  begin
    result.add(Round(from + Random() * (to_ - from)));
    Inc(itemCount);
  end;
  
  HashSetFromRandom := result;
end;


function NewHashMap(): HashMapT;
var
  result: HashMapT;
  
begin
  New(result);
  NewHashMap := result;
end;

function NewHashMap(capacity: integer): HashMapT;
var
  result: HashMapT;
  
begin
  New(result, capacity);
  NewHashMap := result;
end;

function NewHashMap(capacity, loadFactor: integer): HashMapT;
var
  result: HashMapT;
  
begin
  New(result, capacity, loadFactor);
  NewHashMap := result;
end;

function NewTreeMap(): TreeMapT;
var
  result: TreeMapT;
  
begin
  New(result);
  NewTreeMap := result;
end;

function NewLinkedHashMap(): LinkedHashMapT;
var
  result: LinkedHashMapT;
  
begin
  New(result);
  NewLinkedHashMap := result;
end;

function NewLinkedHashMap(capacity: integer): LinkedHashMapT;
var
  result: LinkedHashMapT;
  
begin
  New(result, capacity);
  NewLinkedHashMap := result;
end;

function NewLinkedHashMap(capacity, loadFactor: integer): LinkedHashMapT;
var
  result: LinkedHashMapT;
  
begin
  New(result, capacity, loadFactor);
  NewLinkedHashMap := result;
end;

function NewLinkedHashMap(capacity, loadFactor: integer; accessOrder: boolean): LinkedHashMapT;
var
  result: LinkedHashMapT;
  
begin
  New(result, capacity, loadFactor, accessOrder);
  NewLinkedHashMap := result;
end;

function M(): MapT;
begin
  M := NewHashMap();
end;

function M(capacity: integer): MapT;
begin
  M := NewHashMap(capacity);
end;

function M(capacity, loadFactor: integer): HashMapT;
begin
  M := NewHashMap(capacity, loadFactor);
end;
end.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions