Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Foxcapades/pdk

Repository files navigation

Primitive Deque Types

pdk jdk 1 docs dokka darkgreen Maven Central

⚠️
This library has been deprecated in favor of KPD

Simple library that provides Deque implementations for all Kotlin’s primitive types that deal in unboxed values.

Usage

build.gradle.kts
dependencies {
  implementation("io.foxcapades.lib:pdk:1.2.0")
}

Examples

Reading Longs from a Stream
val deque = ByteDeque(8192)
val read  = deque.fillFrom(inputStream)

for (i in 0 until read step 8)
  println(deque.popLong())
ℹ️
This is just an example, the JDK’s builtin type ByteBuffer is able to read long values from a stream up to 50% faster than ByteDeque due to ByteBuffer utilizing the Unsafe class to raw cast bytes in memory as a long value.

Method & Property Aliases

Similar to the JDK’s Queue type and various implementations, the *Deque types have multiple aliases for most properties and methods that align with different use cases or implementations from different languages. Each alias is an inline method or property allowing library users to choose the specific syntax they prefer without any call-stack overhead. This is particularly useful when porting libraries or code from other languages such as C++ into Kotlin.

The head and tail of the deque are named just that, "head", and "tail", however they are also accessible via "front", "first", "back", and "last" for most properties and methods, for example, the property head, which is handle on the first element in the deque, has the alias properties first and front, similarly the method pushHead, which pushes an element onto the head of the deque, has the alias methods pushFirst and pushFront.

Footprint

pdk aims to be as lightweight as possible both in terms of memory and compute footprint. All of the PrimitiveDeque implementations wrap 3 properties, a primitive array of a specified capacity, an int value recording the current size of the deque, and one more int value tracking the internal 'head' position in the deque’s backing array.

Additionally, in all situations where it is possible, copy operations are performed by the native arrayCopy method.

Presently the only situation where arrayCopy is not used on copy is when copying the contents of a Collection instance into a deque.

Dependencies

pdk only depends on the Kotlin and JDK standard library.

Methods

Comparison / Mapping

Comparison of basic deque functions across libraries.

Action C++
std::deque
JDK
Deque
JS
denque
PDK
*Deque

Peek first element

  • front()

  • peek()

  • peekFirst()

  • element()

  • getFirst()

  • peekFront()

  • head

  • first

  • front

Peek last element

  • back()

  • peekLast()

  • getLast()

  • peekBack()

  • tail

  • last

  • back

Pop & return first element

  • poll()

  • pollFirst()

  • pop()

  • remove()

  • removeFirst()

  • shift()

  • pop()

  • popHead()

  • popFirst()

  • popFront()

Pop & return last element

  • pollLast()

  • removeLast()

  • pop()

  • popTail()

  • popLast()

  • popBack()

Push element to front

  • push_front(e)

  • addFirst(e)

  • offerFirst(e)

  • push(e)

  • unshift(e)

  • pushHead(e)

  • pushFirst(e)

  • pushFront(e)

Push element to back

  • push_back(e)

  • add(e)

  • addLast(e)

  • offer(e)

  • offerLast(e)

  • push(e)

  • pushTail(e)

  • pushLast(e)

  • pushBack(e)

  • += e

Delete first element

  • pop_front()

  • removeHead()

  • removeFirst()

  • removeFront()

Delete last element

  • pop_back()

  • removeTail()

  • removeLast()

  • removeBack()

Empty test

  • empty()

  • isEmpty()

  • isEmpty()

  • isEmpty

Clear deque

  • clear()

  • clear()

  • clear()

  • clear()

Indexed access

  • at(i)

  • [i]

  • peekAt(i)

  • get(i)

  • get(i)

  • [i]

Additional Functionality

All Deque Types

The following additional functions and operators are available on all deque implementations included in the pdk library.

Method / Operator Description

copy()

Clones the deque and it’s data.

contains(value)

Tests whether the deque contains an element equal to the given value.

copyInto(array, offset)

Copies data from the deque into the given array starting at the given offset.

iterator()

Returns an iterator over the contents of the deque.

forEach(fn)

Calls the given function on every element of the deque.

+= array

Appends the elements of the given value to the back of the deque.

+= deque

+= collection

+ deque

Creates a new deque containing the elements from both original deques.

slice(from, to)

Returns a new deque containing the contents of the target deque in the given index range.

slice(from..to)

sliceToArray(from, to)

Returns an array containing the contents of the target deque in the given index range.

sliceToArray(from..to)

Byte Deque

Method / Operator Description

fillFrom(InputStream)

Fills the remaining space in the ByteDeque from the contents of the given input stream.

popShort(littleEndian=false)

Pops the first 2 bytes from the deque and parses them as a Short value.

popInt(littleEndian=false)

Pops the first 4 bytes from the deque and parses them as an Int value.

popLong(littleEndian=false)

Pops the first 8 bytes from the deque and parses them as a Long value.

popFloat(littleEndian=false)

Pops the first 4 bytes from the deque and parses them as a Float value.

popDouble(littleEndian=false)

Pops the first 8 bytes from the deque and parses them as a Double value.

popUByte()

Pops the first byte from the deque as a UByte value.

popUShort(littleEndian=false)

Pops the first 2 bytes from the deque and parses them as a UShort value.

popUInt(littleEndian=false)

Pops the first 4 bytes from the deque and parses them as a UInt value.

popULong(littleEndian=false)

Pops the first 8 bytes from the deque and parses them as a ULong value.

Char Deque

Method / Operator Description

+= String

Appends the characters of the given value to the back of the deque.

+= CharSequence

stringValue()

Creates a new String value representing the current contents of the deque.

UByte Deque

Method / Operator Description

fillFrom(InputStream)

Fills the remaining space in the ByteDeque from the contents of the given input stream.

popByte()

Pops the first value from the deque as a Byte value.

popShort(littleEndian=false)

Pops the first 2 unsigned bytes from the deque and parses them as a Short value.

popInt(littleEndian=false)

Pops the first 4 unsigned bytes from the deque and parses them as an Int value.

popLong(littleEndian=false)

Pops the first 8 unsigned bytes from the deque and parses them as a Long value.

popFloat(littleEndian=false)

Pops the first 4 unsigned bytes from the deque and parses them as a Float value.

popDouble(littleEndian=false)

Pops the first 8 unsigned bytes from the deque and parses them as a Double value.

popUShort(littleEndian=false)

Pops the first 2 unsigned bytes from the deque and parses them as a UShort value.

popUInt(littleEndian=false)

Pops the first 4 unsigned bytes from the deque and parses them as a UInt value.

popULong(littleEndian=false)

Pops the first 8 unsigned bytes from the deque and parses them as a ULong value.