Skip to content

Instantly share code, notes, and snippets.

@jackfirth
Last active May 26, 2022 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackfirth/fada5ddfe087d9dd6220715113f084cd to your computer and use it in GitHub Desktop.
Save jackfirth/fada5ddfe087d9dd6220715113f084cd to your computer and use it in GitHub Desktop.
Rhombus collection API ideas

Mandatory APIs:

interface List {
  size
  add(x)
  get(i)
  set(i, x)
  removeAt(i)
  insert(i, x)
}

interface Set {
  size
  contains(x)
  add(x)
  remove(x)
  list.get(i)
}

interface Multiset {
  size
  getCount(x)
  setCount(x, n)
  uniqueElements.size
  uniqueElements.list.get(i)
  list.get(i)
}

interface SortedSet {
  size
  comparator
  add(x)
  remove(x)
  find(x, searchType)
  list.get(i)
}

interface SortedMultiset extends Collection {
  size
  comparator
  getCount(x)
  setCount(x, n)
  find(x, searchType)
  uniqueElements.size
  uniqueElements.find(x, searchType)
  uniqueElements.list.get(i)
  list.get(i)
}

searchType = Exact | Before | After | ExactOrBefore | ExactOrAfter

Default implementations:

class SortedSubset(delegate, range) implements SortedSet {
  start =
      match range.lowerBound {
          Unbounded -> 0
          Closed(x) -> delegate.find(x, ExactOrAfter)
          Open(x) -> delegate.find(x, After)
      }
  end =
      match range.upperBound {
          Unbounded -> delegate.size
          Closed(x) -> delegate.find(x, ExactOrBefore) + 1
          Open(x) -> delegate.find(x, Before) + 1
      }
  size = end - start
  comparator = delegate.comparator
  add(x) = if range.contains(x) then SortedSubset(delegate.add(x), range) else this
  remove(x) = if range.contains(x) then SortedSubset(delegate.remove(x), range) else this
  find(x, searchType) = match delegate.find(x, searchType) {
      i < start || i >= end -> -1
      i >= end -> -1
      x -> x - start
  }
  list.get(i) = delegate.list.get(i + start)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment