Git Product home page Git Product logo

lists.jl's Introduction

Lists

Build Status

List collections for Julia

This package provides a singly linked list and a doubly linked list implementation, as Julia collections. Singly linked lists are supported with cons, car, and cdr, but not as a standard collection. Doubly linked lists are included in the samples but, again, not as a collection. This doesn't do anything fancy like create an array of nodes. Maybe it should.

List

List is a doubly linked list. Deletions happen in constant time. If code contains an index to an item in the list, then removing other items in the list won't invalidate that index.

Usage:

a = List{Int}()    # Create a list of the given type.
isempty(l)         # Test whether there are any items.
empty!(l)          # Remove all items.
length(l)          # The number of entries. An O(n) operation.
2 in l             # Test whether the given item is an entry in the list. O(n).
eltype(l)          # Returns the item type, here Int64.
indexin(a, l)      # Highest index in list for each value of a that is member.
first(l)           # First item in the list.
last(l)            # Last item in the list, the item value.
push!(l, d)        # Add item d to end of list. Returns index of item.
pop!(l, d)         # Remove and return item at end of list.
unshift!(l, d)     # Add item to start of list. Return index of item.
shift!(l)          # Remove first item and return value.
append!(l, items)  # Add items to end of list.
prepend!(l, items) # Add items to start of list.

There can be an index into the list. It is a reference to a list node but can be treated as an opaque index.

getindex(l, index)     # Returns value of item at this index.
setindex!(l, index, d) # Sets item value at this index.
endof(l)               # Returns index of last node. An O(n) operation.
insert!(l, index, d)   # Insert item at index, pushing values back. Return index.
deleteat!(l, index)    # Delete item at index. Return list.
splice!(l, index)      # Remove value at index, returning data value.
splice!(l, index, d)   # Replace item at index with data value.
find(l, d)             # Find first occurrence of item in list. Return its index.
find(l, index, d)      # Find first occurrence of d after the given index.

There are two kinds of iterators for List. One access items. The other loops over indices.

l = List{Int}()
prepend!(l, [2, 4, 6])
for item::Int in l
    println(item)
end

for index in indexed(l)
    item=getindex(l, index)
    println(item)
end

SList

SList is a singly linked list over items of a given type. Appending to the end of this list takes an order of the number of the items in the list.

Usage:

a = SList{Int}()    # Create a list of the given type.
isempty(l)         # Test whether there are any items.
empty!(l)          # Remove all items.
eltype(l)          # Returns the item type, here Int64.
first(l)           # First item in the list.
unshift!(l, d)     # Add item to start of list. Return index of item.
shift!(l)          # Remove first item and return value.
prepend!(l, items) # Add items to start of list.

There can be an index into the list. It is a reference to a list node but can be treated as an opaque index.

getindex(l, index)     # Returns value of item at this index.
setindex!(l, index, d) # Sets item value at this index.
insert!(l, index, d)   # Inserts *after* the given index. Returns index.

The following methods are O(n) for singly linked lists. They are included for completeness, but needing these is an indication that using a doubly linked list, or Vector, might be a better choice.

length(l)          # The number of entries.
2 in l             # Test whether the given item is an entry in the list.
indexin(a, l)      # Highest index in list for each value of a that is member.
last(l)            # Last item in the list, the item value.
push!(l, d)        # Add item d to end of list. Returns index of item.
pop!(l, d)         # Remove and return item at end of list.
append!(l, items)  # Add items to end of list.
endof(l)               # Returns index of last node.
deleteat!(l, index)    # Delete item at index. Return list.
splice!(l, index)      # Remove value at index, returning data value.
splice!(l, index, d)   # Replace item at index with data value.
find(l, d)             # Find first occurrence of item in list. Return its index.
find(l, index, d)      # Find first occurrence of d after the given index.

As with List, there are two kinds of iterators for SList. One access items. The other loops over indices.

l = SList{Int}()
prepend!(l, [2, 4, 6])
for item::Int in l
    println(item)
end

for index in indexed(l)
    item=getindex(l, index)
    println(item)
end

Implementation Notes

The code comments each time a method for these classes differs from interfaces described for collections in the manual. All differences stem from an assumption that the index to a collection will be an integer.

If you have comments, or especially if I have the wrong idea about how to write good code in Julia, please send me an email.

lists.jl's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

samayel wikunia jkrt

lists.jl's Issues

Unrolled linked lists idea, and a question on motivation

An idea to implement:

https://en.wikipedia.org/wiki/Unrolled_linked_list

See e.g.:
http://www.sanfoundry.com/java-program-implement-unrolled-linked-list/

I'm not sure how often ULL are done, say in C, as they are harder to implement, and C is not a generic/type safe language.

I support Julia, being an array-based language first, not List-based as in Lisp, as they are faster as the general data structure [for some things, know O(1) for list that would be O(n) for arrays]

I do wander why lists where left out in the Julia standard library (I guess, because someone like you cn make their own), with the default collections often ok. What was your motivation to do lists? Or was it maybe just a Julia exercise?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.