Delimiter a List<byte[]> after every array inside the list - ios

Currently I am trying to store images in an SQLite DB and I have it working with one but I want to be able to store multiple Byte arrays inside the SQLite DB in one cell of the DB.
So to achieve this I have set up a list which contains multiple Byte Arrays. What I want to be able to do is add some sort of delimiter after every array inside the list so that when I retrieve the data back from the DB I can use the delimiters to split the List into the different byte arrays which I can then use for my table to show the different images.
I a bit stuck on how to do this so hopefully somebody can help.
This is the only thing I have come up with but really does not work at all (Still pretty new to this) Im using this to split the Byte array list into separate arrays and after each one appending a forward slash and then storing it in a new array to be added to the DB:
private byte[] SplitListAndReturnByteArray(List<byte[]> list)
{
int count = 0;
foreach (var item in list)
{
FormResults.finalByteArray.SetValue(item, count);
count++;
FormResults.finalByteArray.SetValue("/", count);
count++;
}
return FormResults.finalByteArray;
}

Related

How to add same value to a list multiple times in one go in Dart/Flutter

Let's say I have an empty list.
If I want to add a certain number of letters "g" to this list. for example 30 40 etc. the number I send may change.
Is there a way to do this in one go without using a loop?
What is it, if any?
https://api.flutter.dev/flutter/dart-core/List/fillRange.html
I need a method like fillRange.
FillRange does not work on an empty list.
If the list is empty, don't bother using it. Just generate a new list with List.filled:
final list = List.filled(30, 'g');
EDIT: For completeness, here is how to use the above with an existing list:
final list = <String>[...];
// Mutate original list
list.addAll(List.filled(30, 'g'));
// Create new list with spread syntax
final newList = [
...list,
...List.filled(30, 'g'),
];

UITableViewController Sections

I'm trying to recreate the buggy CNContactPickerViewController made by Apple so I have an array of data [CNContact] which i need to display neatly in a UITableView. It all works great until i try to add sections based on the first letter of the contacts' last names to that table. The only solution i found is to iterate through the [CNContact] array and manually group every contact into a dictionary, based on their initials resulting in a [String:[CNContact]]. Is there a better way to do this?
The end result can be viewed in the screenshot below.
This will sort your contacts by last name. Might be overkill, as you want them grouped only by the first letter of the last name, whereas this will sort using the whole name.
var contacts :[CNContact] = [CNContact]();
// fill your contacts here
contacts.sortInPlace { (contact1, contact2) -> Bool in
contact1.familyName.compare(contact2.familyName) == .OrderedAscending
}
Not sure if you need the original ordering. If you want the original array, do create copy of the old one, and do sort in place for the copy.

programatically making a 2d array in swift

I need to use a for loop to create a 2d array. So far "+=" and .append have not yielded any results. Here is my code. Please excuse the rushed variable naming.
let firstThing = contentsOfFile!.componentsSeparatedByString("\n")
var secondThing: [AnyObject] = []
for i in firstThing {
let temp = i.componentsSeparatedByString("\"")
secondThing.append(temp)
}
The idea is that it takes the contents of a csv file, then separates the individual lines. It then tries to separate each of the lines by quotation marks. This is where the problem arises. I am successfully making the quotation separated array (stored in temp), however, I cannot make a collection of these in one array (i.e. a 2d array) using the for loop. The above code generates an error. Does anybody have the answer of how to construct this 2d array?
You can do this using higher order functions...
let twoDimensionalArray = contentsOfFile!.componentsSeparatedByString("\n").map{
$0.componentsSeparatedByString("\"")
}
The map function takes an array of items and maps each one to another type. In this I'm mapping the strings from the first array into an array pf strings and so creating a 2d array.
This will infer the type of array that is created so no need to put [[String]].
Here you go...

Linked List without using pointers and struct

How to implement linked list in C/C++ with out using
struct node{
int data;
struct node *next;
}
just using two arrays, one for data, other for next node
Sure you can. There is some discussion about the topic here: Is a Linked-List implementation without using pointers possible or not?
One implementation would be to create an array, and store the nodes in the array. Wikipedia has a good pseudo-code example: http://en.wikipedia.org/wiki/Linked_list (Linked lists using arrays of nodes section)
You define two arrays
1.Data Array
2.Index Array
every data array will contain data value and corresponding index in Index array will contain next node to be pointed by the value element
you need to keep track of the last value added because it gives the index of Index array where you can add an index of the next value pointed by that value node.
Adding a data value is also easy just add a new value in the data array and
fill the previous element value in Index array by the index of the data value in data element.
Data Array 2,5,3,1,2.
Index Array 1,2,3,4,0 (0 means it is not pointing to any data element)
add element: 2,5,3,1,2,77.
Index Array 1,2,3,4,5,0.
delete element: 2,5,0,1,2,77
Index Array 1,3,0,4,5,0.

Lua table C api

I know of:
http://lua-users.org/wiki/SimpleLuaApiExample
It shows me how to build up a table (key, value) pair entry by entry.
Suppose instead, I want to build a gigantic table (say something a 1000 entry table, where both key & value are strings), is there a fast way to do this in lua (rather than 4 func calls per entry:
push
key
value
rawset
What you have written is the fast way to solve this problem. Lua tables are brilliantly engineered, and fast enough that there is no need for some kind of bogus "hint" to say "I expect this table to grow to contain 1000 elements."
For string keys, you can use lua_setfield.
Unfortunately, for associative tables (string keys, non-consecutive-integer keys), no, there is not.
For array-type tables (where the regular 1...N integer indexing is being used), there are some performance-optimized functions, lua_rawgeti and lua_rawseti: http://www.lua.org/pil/27.1.html
You can use createtable to create a table that already has the required number of slots. However, after that, there is no way to do it faster other than
for(int i = 0; i < 1000; i++) {
lua_push... // key
lua_push... // value
lua_rawset(L, tableindex);
}

Resources