Kotlin lists have the useful property indices that provides the range of valid indices.
But according to https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/indices.html it is actually a property not just of lists but of collections. Tried an experiment and sure enough, a set also has this property.
But sets cannot be indexed by integer the way lists can. So it's not meaningful to talk about the indices of a set.
Given that, why is it a property of collections in general rather than just lists (and arrays)?
But sets cannot be indexed by integer the way lists can. So it's not meaningful to talk about the indices of a set.
Having the ability the randomly access an element by index is one of the usages of indices. But you could be using them as integer keys. Basically they are defined as a integer range mapping to elements, so this works for any collection. In code, it is simply implemented as [0..size)
Related
I have a mapping in solidity. How can I check if a key exist in this mapping. Mapping returns the data type default value if I try to access a non existent key.
Is there any graceful way to achieve the same.
There is no way to do it, because if value didn't set, there will be default value (false, 0, etc.). The best way out is to check that the value is different from the default value
There is no other other way to do this than just check if the key in question has a value initialized.
If you need more than that (e.g. to be able to keep track of which keys have values set, count them, list them, etc.) then a fairly common pattern is to maintain an array alongside the mapping, containing the known keys.
That way, when all you want is the value (or to check its existence), you can query the mapping, which is very gas efficient. When you want to do something with the set of known keys, you can interact with the array.
In indexedDB, if the keys are arrays of integers such as [n,0] through [n,m], for operations that involve getting all the records in which the first element of the array key is n or opening a cursor on the same set of records, is there any advantage to using an index on an additonal property that stores n over using a key range?
Reasons to think an index may not be better include that the browser has to maintain the index for each change to the object store, an additional property has to be added to each record to store already stored data n, and little may be gained since the keys in the index will always point to consecutive records in the object store rather than dispersed throughout.
If the number of different values of n are likely no more than 1,000 and for m no more than 50, is using an index superior to a key range?
Thank you.
I guess the purpose of indexedDB is to have object store locally.
It is not sql that you need to update columns in every object.
since you change the object structure (saying by adding property)
it is true that all the objects in the store must be rewriten as you said...
emm well... another option for you is to update the db with another store
which contain somthing similar to forien key in sql or uniqe key which store the other stored objects extentions... and in it every obj item is also supposed to be same structured.
I think this is the point you start to use onupgradeneeded intansively.
ListIterator can be used to traverse in both ways over the List.
Why don't we have something similar to ListIterator for Set? Is it because it is not ordered? Please advice.
Short answer:
Yes, because it's not ordered.
Long answer:
LISTS
In a singly linked List every element has a reference to its following element.
In a doubly linked List every element has a reference to its successor and its predecessor.
So it is easy to implement the next method of a Iterator. To iterate a list we just run through the next references of the list elements. A traverse iteration in a doubly linked list will run the pred. references. In a singly linked list, the list order will be inverted and iterated.
So a order is defined.
(src)
SETS
A Set is managed by a HashFunction
(src)
The advantage is, that the lookup function in a set improves to O(1). But we loose the references between the set elements. So it gets harder to iterate in both ways over the Set in both ways. There are ways to iterate this Set. But to traverse over the Set we need to define an order. But it is not.
It’s not very clear how idProperty is used in the data store when building a data model. The documentation says “If the store has a single primary key, this indicates the property to use as the identity property. The values of this property should be unique. This defaults to "id".
Is this assuming the schema from which the model is based, has a mostly flat structure? For example an array of objects – each with an identity property?
What if the schema is not a simple array but has more complex structure starting from a single object that contains several sub levels of properties within properties. OR is just multiple arrays on the same level where each group of arrays identify property are independent of one another?
A store is an extension of a collection.
A collection is the interface for a collection of items (your obect with a potentially complex schema).
You can use Custom Querying on a collection to define special queries to find your data with any subset of properties.
In short, yes you can querying your data even if it has a custom schema but you need to define a Custom Querying.
More info can be found here at the end of the article: https://github.com/SitePen/dstore/blob/master/docs/Collection.md
In the context of OOP, what is the name (or class name) of a data structure composed of a Dictionary of Arrays?
(a Dictionary where each key is mapped to a collection of values)
In the case you cannot find a class representing this data structure, what would be a proper name for this object?
I came from http://en.wikipedia.org/wiki/List_of_data_structure but the most similar I've found is http://en.wikipedia.org/wiki/Multimap which it seems to me that is wrong because the article talks about cardinality and I don't care about that.
I believe that it is usually just called a Dictionary of Arrays. ;) Usually you don't need a special data structure, you just have a dictionary that has references to Arrays as values.