Builtin for checking if vec contains specified element - vector

Let's say, I have a vec<int> containing a list of integers that may be non-continuous (due to element being removed from database).
Example:
$occupiedNumbers = vec[1, 2, 3, 5, 6, 8, 10, 11, 12, 13, 15, 16];
Now what I need is to check if this vec contains a given number, and if yes, increase it by 1 and repeat checking.
$newItemNumber = count($occupiedNumbers);
while (/* check if $occupiedNumbers contains $newItemNumber */) {
$a++;
}
In vanilla PHP there is in_array() for this, but would that work for a vec? Also, there is a builtin HH\Vector::linearSearch() but it's meant to be used with vec's predecessor, HH\Vector.
What is the right solution to check if a vec contains a given value?
Neither HHVM nor Hack docs say about that use case for a vec. Also, I'm missing some sort of REPL tool to check it manually off-project without building a whole (possibly faulty) project.

The recommendation from the HHVM/HackLang guys is to use C\contains() as you can read here but you need to install the HSL package (hhvm/hsl) using composer. This library contains loads of functions to deal with the new array-type structures: vec, dict, keyset. Those functions in the HSL Library are prefixed with C, Vec, Dict, Keyset and you'll find also Math, Str, etc., very useful for other needs.
After installing that package it becomes available but typically it is more handy to add use namespace to avoid the long prefix:
use namespace HH\Lib\C;
This is how you can do it:
$exists = C\contains($occupiedNumbers, $newItemNumber);

If you are really looking for the longest continuous interval starting from count($occupiedNumbers) then it may be much faster to find its initial index in the sorted list (if it exists), then use direct indexing from there:
// `use namespace HH\Lib\{C, Vec};` at top level
$occupiedNumbers = vec[1, 2, 3, 5, 6, 8, 10, 11, 12, 13, 15, 16];
$sorted = Vec\sort($occupiedNumbers); // if not sorted in general
$size = count($sorted);
$v = $size;
$i = C\find_key($sorted, $w ==> $w === $v);
if($i !== null) {
for(; $i < $size - 1 && $sorted[$i + 1] === $v + 1; $i++, $v++) {
}
}
// $i is null | <index of largest number in continuous interval>

Related

Metalkit: MTLBuffer and pointers in swift 3

I started with Metalkit and I have a very simple kernel as a test case.
kernel void compute(device float* outData [[ buffer(0) ]])
{
outData[0] = 234.5;
outData[3] = 345.6;
}
This "computed" data is stored in a MTLBuffer.
var buffer : MTLBuffer?
...
buffer = device.makeBuffer(length: MemoryLayout<Float>.size * 5, options: [])
...
commandBuffer.waitUntilCompleted()
At this point the kernel has written some test data to the MTLBuffer.
Question is how I should access that data from my main program?
I get a unsafeMutableRawPointer from buffer.contents(). How do I get a swift array of values that I can use everywhere else (displaying on screen, writing to file,...)?
These snippets work in this very simple app, but I am not sure if they are correct:
let raw = buffer.contents()
let b = raw.bindMemory(to: Float.self, capacity: 5)
print(b.advanced(by: 3).pointee)
let a = raw.assumingMemoryBound(to: Float.self)
print(a.advanced(by: 3).pointee)
let bufferPointer = UnsafeBufferPointer(start: b, count: 5)
let values = Array(bufferPointer)
print(values)
let value = raw.load(fromByteOffset: MemoryLayout<Float>.size * 3, as: Float.self)
print(value)
Both bindMemory and assumingMemoryBound work. Though assumingMemoryBound assumes the underlying bytes are already typed and bindMemory doesn't. I think that one of either should work, but not both. Which one should it be and why?
I use the code presented below to load to arrays, but I can't decide if mine or your version is best.
let count = 16
var array = [Float]()
array.reserveCapacity(count)
for i in 0..<count {
array.append(buffer.contents().load(fromByteOffset: MemoryLayout<Float>.size * i, as: Float.self))
}

Flex-Sorting on ArrayCollection

I have an ArrayCollection(neList) of Objects(neObj). Each neObj has several fields like ipAddress,TID,etc.. In most cases neObj will be have values of both TID and ipAddress, rarely it will not have TID but have ipAddress... After adding Objects(neObj), I need to sort the ArrayCollection whose behaviour must be similar to array.sort() which has got strings only..(i.e nos first followed by strings in alphabetical order)
Things I have tried:
1)Using neList.source.sort() and neList.refresh.. but it did not work as neList.source has objects not straight forward things like strings
2)I think i cannot use sortOn function of ArrayCollection as it can be done on only 1 field
My Requirement:
Use Case1:- Objects in ArrayCollection have both TID and IP
neObj1.TID="RAPC" neObj1.ipAddress="121.1.1.2"; neObj2.TID="RAPA" neObj2.ipAddress="121.1.1.1"
O/P after sorting should be
neObj2 neObj1
Use Case2:- 1 of the objects does not have TID
neObj1.ipAddress="121.1.1.2"; neObj2.TID="RAPA" neObj2.ipAddress="121.1.1.1"
O/P after sorting should be
neObj1 neObj2
As hinted in the comments, you'll need to use a sort compareFunction to decide how the items will be sorted.
I do like to point out that sorting a combination of letters and numbers is tricky in the sense that there is no natural order by default. e.g. when sorting, 1, 2 and 11, the order will be 1, 11, 2. You can however solve this using the naturalCompare method in the AS3Commons Lang project.
Here's a code sample for your case. The sort is implemented as a subclass of the Sort class so that you can easily reuse it in other collections:
package {
import mx.collections.Sort;
import org.as3commons.lang.StringUtils;
public class NaturalSort extends Sort {
public function NaturalSort() {
compareFunction = function (a:Object, b:Object, fields:Array = null):int {
var stringA:String = (("TID" in a) ? a.TID : "AAAA") + a.ipAddress;
var stringB:String = (("TID" in b) ? b.TID : "AAAA") + b.ipAddress;
return StringUtils.naturalCompare(stringA, stringB);
};
}
}
}
To apply this:
var collection:ArrayCollection;
collection.sort = new NaturalSort();
collection.refresh();

Xquery: Selecting n number of records at a time

I'm using XQUERY, I have 25000 records saved in Database, I want to select these records in chunks(1000), How do I get the records recursively?
Window queries
You could use the tumbling window for this purpose, sadly it isn't supported by many XQuery engines yet.
For the following suggestions, you have to wrap the results into XML as XQuery only knows flat sequences, no nested ones.
Building your own tumbling windows
You can build your own tumbling windows functions which could look like this one, it creates "window" elements containing $count "item" elements each:
declare function local:window($seq as item()*, $size as xs:integer) as item()* {
for $i in 1 to xs:integer(fn:ceiling(count($seq) div $size))
return
element {"window"} {
for $j in (($i - 1) * $size + 1) to $i*$size
return
element {"item"} {$seq[$j]}
}
};
local:window((2, 4, 6, 8, 10, 12, 14), 3)
Splitting sequences for recursive windows
If you want to solve this problem recursively, you will need some split function which sadly isn't available in standard xquery. You could use $n=1000, work with the "head" elements and call your "worker function" recursively with the "tail".
declare function local:split($seq as item()*, $n as xs:integer) as element()* {
(
element {"head"} {
for $i in subsequence($seq, 1, $n)
return element {"item"} {$i}
},
element {"tail"} {
for $i in subsequence($seq, $n+1)
return element {"item"} {$i}
}
)
};
local:split((2, 4, 6, 8, 10, 12, 14), 3)
To access the tail elements as a sequence, use
local:split((2, 4, 6, 8, 10, 12, 14), 3)[2]//item/data()

Reflection on a Scala case class

I'm trying to write a trait (in Scala 2.8) that can be mixed in to a case class, allowing its fields to be inspected at runtime, for a particular debugging purpose. I want to get them back in the order that they were declared in the source file, and I'd like to omit any other fields inside the case class. For example:
trait CaseClassReflector extends Product {
def getFields: List[(String, Any)] = {
var fieldValueToName: Map[Any, String] = Map()
for (field <- getClass.getDeclaredFields) {
field.setAccessible(true)
fieldValueToName += (field.get(this) -> field.getName)
}
productIterator.toList map { value => fieldValueToName(value) -> value }
}
}
case class Colour(red: Int, green: Int, blue: Int) extends CaseClassReflector {
val other: Int = 42
}
scala> val c = Colour(234, 123, 23)
c: Colour = Colour(234,123,23)
scala> val fields = c.getFields
fields: List[(String, Any)] = List((red,234), (green,123), (blue,23))
The above implementation is clearly flawed because it guesses the relationship between a field's position in the Product and its name by equality of the value on those field, so that the following, say, will not work:
Colour(0, 0, 0).getFields
Is there any way this can be implemented?
Look in trunk and you'll find this. Listen to the comment, this is not supported: but since I also needed those names...
/** private[scala] so nobody gets the idea this is a supported interface.
*/
private[scala] def caseParamNames(path: String): Option[List[String]] = {
val (outer, inner) = (path indexOf '$') match {
case -1 => (path, "")
case x => (path take x, path drop (x + 1))
}
for {
clazz <- getSystemLoader.tryToLoadClass[AnyRef](outer)
ssig <- ScalaSigParser.parse(clazz)
}
yield {
val f: PartialFunction[Symbol, List[String]] =
if (inner.isEmpty) {
case x: MethodSymbol if x.isCaseAccessor && (x.name endsWith " ") => List(x.name dropRight 1)
}
else {
case x: ClassSymbol if x.name == inner =>
val xs = x.children filter (child => child.isCaseAccessor && (child.name endsWith " "))
xs.toList map (_.name dropRight 1)
}
(ssig.symbols partialMap f).flatten toList
}
}
Here's a short and working version, based on the example above
trait CaseClassReflector extends Product {
def getFields = getClass.getDeclaredFields.map(field => {
field setAccessible true
field.getName -> field.get(this)
})
}
In every example I've seen the fields are in reverse order: the last item in the getFields array is the first one listed in the case class. If you use case classes "nicely", then you should just be able to map productElement(n) onto getDeclaredFields()( getDeclaredFields.length-n-1).
But this is rather dangerous, as I don't know of anything in the spec that insists that it must be that way, and if you override a val in the case class, it won't even appear in getDeclaredFields (it'll appear in the fields of that superclass).
You might change your code to assume things are this way, but check that the getter method with that name and the productIterator return the same value and throw an exception if they don't (which means that you don't actually know what corresponds to what).
You can also use the ProductCompletion from the interpreter package to get to attribute names and values of case classes:
import tools.nsc.interpreter.ProductCompletion
// get attribute names
new ProductCompletion(Colour(1, 2, 3)).caseNames
// returns: List(red, green, blue)
// get attribute values
new ProductCompletion(Colour(1, 2, 3)).caseFields
Edit: hints by roland and virtualeyes
It is necessary to include the scalap library which is part of the scala-lang collection.
Thanks for your hints, roland and virtualeyes.

Changes to one variable propagates to another

For example I have two ArrayCollection's - firstAC and secondAC. If I do secondAC = firstAC, and than I make changes to secondAC (prehaps put a filterfunction on it) it somehow propagates to firstAC, would anyone tell me why that happens in Flex or Actionscript 3?
What can I do if I only want secondAC to get all data from firstAC but then when I make changes to secondAC it does not show in firstAC?
Thanxs a bunch for answers!
Ladislav
When you write secondAC = firstAC, you simply state that secondAC and firstAC are references to the same array collection.
What you want is to clone the first collection (as in, copy all elements one by one).
You should be able to do it with something like :
secondAC = new ArrayCollection();
secondAC.addAll(firstAC);
I have no idea of Flex or Actionscript, but looks like firstAC and secondAC point to the same array, therefore that's expected.
What you should do is just create another array, copy members, and they will be two real different entities.
Instead of secondAC = firstAC, you can try secondAC.addAll(firstAC).
In ECMAScript languages (AS1-3, JavaScript, et al.), when you use
var foo = //some value which is not a String or a Number
what you are really saying is "foo now points to the same object as that other variable." This means that in this situation, both arrays will be the same value:
var foo:Array = [ 1, 2, 3 ];
foo = bar;
bar.push( 4 );
trace( foo ); // 1, 2, 3, 4
This also works for functions:
var foo:Array = [ 1, 2, 3 ];
adder( foo );
function adder( bar:Array ):void {
bar.push( 4 );
}
trace( foo ); // 1, 2, 3, 4
and it even works with XML:
var xml:XML = <root><foo/></root>;
var bar:XML = xml;
bar.children()[ 0 ].#bar = 1;
trace( xml.toXMLString() ); // <root><foo bar="1"/></root>
This is called "passing by reference" instead of "passing by value" or "passing by copy". It means that every time that an item is referenced, each variable will point to the same object.
There are many ways to get around this, and most of them depend on your context. For arrays, my favorite is Array.concat(), which returns a literal clone of the array. This means that anything I do to the returned value will not effect the original in any way. If I'm dealing with XML, however, I will do something like: var xml2:XML = XML( xml.toXMLString() );.
In your case, I would actually recommend that you use:
var secondAC:ArrayCollection = new ArrayCollection( firstAC.source.concat() );
This has the major benefits of not only being faster (it relies on compiled code instead of Flex SDK code and it also does not first instantiate a new array and then re-populate it), but it also has the distinct benefit of being available in older versions of Flex 3's SDK -- it is entirely backwards compatible.

Resources