#import "ArrayUtil.h"
#import "Constraints.h"

@implementation NSArray (Util)

-(NSData*) toUint8Data {
    NSUInteger n = [self count];
    uint8_t x[n];
    for (NSUInteger i = 0; i < n; i++) {
        x[i] = [(NSNumber*)[self objectAtIndex:i] unsignedCharValue];
    }
    return [NSData dataWithBytes:x length:n];
}
-(NSData*) concatDatas {
    NSUInteger t = 0;
    for (id d in self) {
        require([d isKindOfClass:[NSData class]]);
        t += [(NSData*)d length];
    }
    
    NSMutableData* result = [NSMutableData dataWithLength:t];
    uint8_t* dst = [result mutableBytes];
    for (NSData* d in self) {
        memcpy(dst, [d bytes], [d length]);
        dst += [d length];
    }
    return result;
}
-(NSArray*) concatArrays {
    NSMutableArray* r = [NSMutableArray array];
    for (id e in self) {
        require([e isKindOfClass:[NSArray class]]);
        [r addObjectsFromArray:e];
    }
    return r;
}

@end
