Sort ArrayBuffer[A] in scala? - arrays

I have an array in Scala with the class ArrayBuffer[Actor], where Actor is a class that implements the Ordered[Actor] trait. How do I sort this array without coding it manually?
I know there is an object called Sorting, but it doesnt seem to work since ArrayBuffer doesn't implement/extend the right classes.
How do I sort ArrayBuffer[A] type arrays?

If you are using Scala 2.8, you could use the sortWith method of the ArrayBuffer[T] class, which is inherited from the SeqLike trait.
The following code snippet sorts an ArrayBuffer[T] object in ascending order:
def ascendingSort[T <% Ordered[T]](xs: ArrayBuffer[T]) = xs.sortWith(_ < _)
Note that this does not mutate the actual ArrayBuffer, but creates a new one with the elements in the right order.
If you are using Scala 2.7, you could use the stableSort method of the Sorting object. This takes the elements of the ArrayBuffer and produces an array of elements sorted in the right order (given by a closure as an argument, ascending as a default).
For example:
val a = new scala.collection.mutable.ArrayBuffer[Int]()
a += 5
a += 2
a += 3
scala.util.Sorting.stableSort(a)
The important question is what do you want to do with the ArrayBuffer. Usually, a Buffer is used internally in different algorithms in order to increase the performance of intermediate results. If you are using it for that, have a look at the ways of sorting the collection you want to return at the end of your algorithm. The Sorting object already provides a way of transforming an ArrayBuffer into a sorted Array.
From the scaladoc of the Buffer class:
Buffers are used to create sequences of elements incrementally
As you are using it with Actors, it might be used for some kind of actor queue - in which case, you might want to have a look at the Queue collection.
Hope it helps,
-- Flaviu Cipcigan

Btw, the Actor class here is my own class used for "Actors" in a world created using my new game engine for scala ("Awesome Game Engine for Scala ~ AGES"), so it has nothing to do with the concurrency actor class. Also, implementations of lists in scala are a jungle, everything is either deprecated or implemented in a lot of different ways...ArrayBuffer works for my need (my need being a variable size array for containing actors).
Hope this clarifies :)

Related

Difference between Dynamic arrays and list c#

I have a query regarding a concept.
Are dynamic arrays in c# also called list? Or they are totally different?
C# does not have a dynamic array type. However, you can use a List as a dynamic array, given that it supports indexed access:
List<string> list = new List<string>();
list.Add("Hello");
list.Add("Goodbye");
Console.WriteLine(list[1]);
Take a look at Generic Lists.
Since it's the first link I found out when looking for this difference I would like to contribute a little.
The Nitin Bisht response is not entirely wrong, but by most of programming definitions a dynamic array is a type of array that can be set when the program is already running, simply put, it's size is set during run time not in compilation.
In C# this is written as:
string[] myDynamicArray;
myDynamicArray = SomeMethodThatReturnsAnCollection().ToArray();
Other way would be a method which returns an existing array like:
string[] myDynamicArray;
myDynamicArray = SomeMethodThatReturnsAnArray();
Those are Dynamic Arrays, you don't tell your program which size it has but inside the logic of that context this will be decided.
Those Arrays in C# has the Resize method, which enables the programmer to grow the array, which I believe it was the Nitin Bisht and so many others interpretation around that question, but this is a wrong assumption, resizable is different than dynamic.
Instead of using Resize in your Array, you could just use a List type, List in C# implements an Array in it's core, which can be seen here, but it also has the ability to grow in an easier approach than the Array implementation enables us with the Add(T item) List method.
List in C# would be a native Dynamic Resizable Array, some languages call similarly implementations of this structure as Vector, which are not a LinkedList with nodes and pointers for example.
Summarizing the difference in code:
var myList = new List<string> { "initial value" };
myList.Add("New value"); // After add, myList has this new value
var myArray = new string[] { "initial value" };
myArray.Append("New value") // after Append myArray doesnt have a new value
With Append() method what happens is another IEnumerable collection is created with the new value, so if you iterate through both collections you will see that the list prints out the new value but the array doesn't, in that case one way you can fix that is doing this:
string[] myArray;
myArray = myArray.Append("New value").ToArray(); // now it will work
The difference here is that myArray variable is being rewritten with the new array generated by .Append().ToArray() line, in the List example the Add() method will resize and then add the new value.
Important to note that this resize is done in a similar manner than what we did with the Append() "fixed" code, the List creates a new Array with increased size and copies it's original contents to this new Array.
You can use the resources given by the Array class to get to the same result as the Add() method implemented by List, but off course if you need that method you should just use an List instead of writing your own implementation, very rarely you would have to do it and to be honest your implementation would be probably less efficient than the already built ones.
In summary that is the difference between them, as you can see they have similarities but I would still treat them very differently.
Usually you wanna use an array when you do not expect to grow any further, if that is your case then an array is more useful since it's has less implementations and that brings a performance gain which can be good in a lot of scenarios.
If not, just use lists.

how to bind methods to (nested) numpy dtype?

I am wishing to associate object methods with dtypes in a numpy structured array. That is the underlying object instance state of my collection of object is already packed into a numpy (nested dtype) record_arrays..
I already have a set of Python objects, that are constructed on top of views of these numpy structured array dtypes that then operate on this underlying continuously packed object instance data.
That is: I am wishing to use object oriented programming methods, while working with the underlying instance state in it's native packed, structured format.
-> Currently working with these numpy structured arrays using Python object methods, requires that explicitly instantiate a Python object on top of it's associated array view each time I re-reference it within the array..
Might it be possible to more directly associate object methods with a numpy structured array (nested) dtype, so that one could avoid having to reconstruct the object instance each time ??
Your description is confusing. Some basic code might help make it clearer. But I'll throw out some possibly relevant observations.
A numpy array has a (large) continuous data buffer (flat list of bytes), which it divides into 'records'. 'shape' and 'strides' are used to step through the records. 'dtype' determines how it 'views' each record.
While these arrays are a Python object type, and dtype is also a class, in general numpy programmers don't put a lot of effort into constructing added array object classes. The arrays may be attributes of larger objects (dictionaries, lists, tuples, or custom classes).
matrix and masked_arrays are examples of subclassing ndarray. I have not seen many user defined subclasses; in part because the amount of work to make them fully functional isn't worth it. recarray is a subclass that allows you to access fields of a structured array with attribute syntax. It may be worth looking at its 'getattr` method. Getting general array operations to return the correct array class or subclass is a bit tricky.
np.lib.index_tricks defines a few classes that let you use indexing syntax inplace of function syntax (ie. they define custom __getitem__ methods).
I am not aware of ways to subclass dtype. The most general base dtype is 'object'. That just stores a pointer to a Python object that is stored elsewhere. That pointer can point to anything - a number, None, a list, another array, etc. In structured arrays, dtype is a compound type, consisting of 'list' of sub-dtypes (which may be nested). But ultimately the structured dtype consists of multiple basic dtypes (ints, floats, strings, object).

is it possible to write a method to sort an array of objects with respect to a specific field?

i mean is it possible to have a method that gets as its parameters an array of objects and another parameter which indicates which field of the objects we are using to sort the array ?
for example if the objects are contacts if we call sort(contacts , name) it would sort them with respect to name. if we call sort(contacts , number) it sorts them according to their numbers.
maybe by sending an String of the field we want !! something like :
class sorting {
public static bubble_sort(Object[] array , String field){
for(int i =0; i<array.length ; i++){
if(array[i].field > array[i+1].field)
swap(array ,i ,i+1);
}
}
(preferably in java) (and please include examples of the solutions you give !)
Assuming this is Java: yes, it is possible. You can use reflection to get the field type and value and then compare them. It would not be a good idea. Much better to use Comparator with the existing sort method.
A method that would work in pretty much any language is to pass in some kind of function object.
class sorting {
public static bubble_sort(Object[] array, FunctionObject ordering) {
for(int i =0; i<array.length ; i++){
if(ordering(array[i+1], array[i]))
swap(array ,i ,i+1);
}
};
different languages are going to have different syntaxes for such a function object -- what its type is, etc -- but pretty much every language is going to have some way to do it.
Generally the best signature for it is one that takes two different objects, and returns true if the left one is less than the right one.
Similarly, different languages are going to have different ways of invoking a function object. Some may require ordering.Invoke( array[i+1], array[i] ).
In that function object, compare the field in question. If the language/objects have reflection, you can sometimes do this via field name directly.
As this pattern is very useful, languages tend to make it easier as they mature. So the most recent version of your language may have a syntax to create such objects with far less syntax, and invoke them with less syntax as well.

How do I resize an array in Scala

I am trying to create a DB management tool in Scala, and I want to be able to draw from this database into Arrays, whose size can shift based on the data being passed to them. I know how to do this in C, PHP, VB, etc. but can't seem to figure out the syntax for Scala.
I'm sure this should be a simple problem, so any help would be appreciated
Collections by default in Scala tend to be immutable. Operations will create new immutable collections from existing collections (by adding/removing elements etc.). The benefit of this is that collections don't change under iteration and writing multi-threaded applications tends to be easier (lots of caveats/assumptions with how you write standard Java apply here!).
Having said all that, if you need a mutable array, have you looked at an ArrayBuffer (a mutable collection with an underlying array implementation) ?
e.g.
val a = new scala.collection.mutable.ArrayBuffer[String]()
a += "A"
a += "B"
a(1) // gives you 'B'
You could use System.copy for this task, if you really want to use an array, or you could directly use a container that will resize itself automatically, such as ListBuffer or ArrayList.

Why no immutable arrays in scala standard library?

Scala has all sorts sorts of immutable sequences like List, Vector,etc. I have been surprised to find no implementation of immutable indexed sequence backed by a simple array (Vector seems way too complicated for my needs).
Is there a design reason for this? I could not find a good explanation on the mailing list.
Do you have a recommendation for an immutable indexed sequence that has close to the same performances as an array? I am considering scalaz's ImmutableArray, but it has some issues with scala trunk for example.
Thank you
You could cast your array into a sequence.
val s: Seq[Int] = Array(1,2,3,4)
The array will be implicitly converted to a WrappedArray. And as the type is Seq, update operations will no longer be available.
So, let's first make a distinction between interface and class. The interface is an API design, while the class is the implementation of such API.
The interfaces in Scala have the same name and different package to distinguish with regards to immutability: Seq, immutable.Seq, mutable.Seq.
The classes, on the other hand, usually don't share a name. A List is an immutable sequence, while a ListBuffer is a mutable sequence. There are exceptions, like HashSet, but that's just a coincidence with regards to implementation.
Now, and Array is not part of Scala's collection, being a Java class, but its wrapper WrappedArray shows clearly where it would show up: as a mutable class.
The interface implemented by WrappedArray is IndexedSeq, which exists are both mutable and immutable traits.
The immutable.IndexedSeq has a few implementing classes, including the WrappedString. The general use class implementing it, however, is the Vector. That class occupies the same position an Array class would occupy in the mutable side.
Now, there's no more complexity in using a Vector than using an Array, so I don't know why you call it complicated.
Perhaps you think it does too much internally, in which case you'd be wrong. All well designed immutable classes are persistent, because using an immutable collection means creating new copies of it, so they have to be optimized for that, which is exactly what Vector does.
Mostly because there are no arrays whatsoever in Scala. What you're seeing is java's arrays pimped with a few methods that help them fit into the collection API.
Anything else wouldn't be an array, with it's unique property of not suffering type erasure, or the broken variance. It would just be another type with indexes and values. Scala does have that, it's called IndexedSeq, and if you need to pass it as an array to some 3rd party API then you can just use .toArray
Scala 2.13 has added ArraySeq, which is an immutable sequence backed by an array.
Scala 3 now has IArray, an Immutable Array.
It is implemented as an Opaque Type Alias, with no runtime overhead.
The point of the scala Array class is to provide a mechanism to access the abilities of Java arrays (but without Java's awful design decision of allowing arrays to be covariant within its type system). Java arrays are mutable, hence so are those in the scala standard library.
Suppose there were also another class immutable.Array in the library but that the compiler were also to use a Java array as the underlying structure (for efficiency/speed). The following code would then compile and run:
val i = immutable.Array("Hello")
i.asInstanceOf[Array[String]](0) = "Goodbye"
println( i(0) ) //I thought i was immutable :-(
That is, the array would really be mutable.
The problem with Arrays is that they have a fixed size. There is no operation to add an element to an array, or remove one from it.
You can keep an array that you guess will be long enough as a backing store, "wasting" the memory you're not using, keep track of the last used index, and copy to a larger array if you need the extra space. That copying is O(N) obviously.
Changing a single element is also O(N) as you will need to copy over the entire array. There is no structural sharing, which is the lynchpin of performant functional datastructures.
You could also allocate an extra array for the "overflowing" elements, and somehow keep track of your arrays. At that point you're on your way of re-inventing Vector.
In short, due to their unsuitablility for structural sharing, immutable facades for arrays have terrible runtime performance characteristics for most common operations like adding an element, removing an element, and changing an element.
That only leaves the use-case of a fixed size fixed content data-carrier, and that use-case is relatively rare. Most uses better served with List, Stream or Vector
You can simply use Array[T].toIndexSeq to convert Array[T] to ArraySeq[T], which is of type immutable.IndexedSeq[T].
(after Scala 2.13.0)
scala> val array = Array(0, 1, 2)
array: Array[Int] = Array(0, 1, 2)
scala> array.toIndexedSeq
res0: IndexedSeq[Int] = ArraySeq(0, 1, 2)

Resources