collections

How can I take an item from a Vec in Rust?

这一生的挚爱 提交于 2019-12-22 03:46:18
问题 I'm looking for a method that consumes a Vec and returns one element, without the overhead of restoring Vec 's invariants the way remove and swap_remove do: fn take<T>(vec: Vec<T>, index: usize) -> Option<T> However, I can't find such a method. Am I missing something? Is this actually unsafe or impossible? This is a different question from Built in *safe* way to move out of Vec<T>? There the goal was a remove method that didn't panic on out of bounds access and returned a Result . I'm looking

what's the usage of the code in the implementation of AbstractCollection's toArray Method

人盡茶涼 提交于 2019-12-22 03:40:49
问题 public Object[] toArray() { // Estimate size of array; be prepared to see more or fewer elements Object[] r = new Object[size()]; Iterator<E> it = iterator(); for (int i = 0; i < r.length; i++) { if (! it.hasNext()) // fewer elements than expected return Arrays.copyOf(r, i); r[i] = it.next(); } return it.hasNext() ? finishToArray(r, it) : r; } here's the code of implementation of AbstractCollection.toArray method. if (! it.hasNext()) // fewer elements than expected return Arrays.copyOf(r, i);

Manually add item to existing object [Laravel 5]

杀马特。学长 韩版系。学妹 提交于 2019-12-22 03:37:28
问题 Here is what I try to do: $q = Question::where('id',$id -> id)->get(); $q[] = $q->push([ 'test' => true]); dd($q); This will output: Collection {#220 ▼ #items: array:3 [▼ 0 => Question {#225 ▶} 1 => array:1 [▼ "test" => true ] 2 => null ] } So 'test' => true will append as a new key, but I want to insert it in Question so latter I can access to it like this with foreach $q -> test So here is how I want access to item: @foreach($q as $qq) {{ $qq->test }} @endforeach 回答1: It can be done by

Same iteration order on Map.keySet and Map.values?

谁说胖子不能爱 提交于 2019-12-22 03:35:10
问题 For a map like: Map<Integer, Integer> map = ...; map.put(1, 1); map.put(2, 2); map.put(3, 3); map.put(4, 4); Is this code... for (Integer i : map.keySet()) System.out.println(i); for (Integer i : map.values()) System.out.println(i); ...guaranteed print the same same sequence twice? If not, are there any guarantees in for example java.util.HashMap ? 回答1: No, there is no guarantee, although in practice it will happen (there's no good reason for the map to use a different iterator for the keys

Does Rust have Collection traits?

▼魔方 西西 提交于 2019-12-22 03:24:28
问题 I'd like to write a library that's a thin wrapper around some of the functionality in BTreeMap. I'd prefer not to tightly couple it to that particular data structure though. Strictly speaking, I only need a subset of its functionality, something along the lines of the NavigableMap interface in Java. I was hoping to find an analogous trait I could use. I seem to recall that at some point there were traits like Map and MutableMap in the standard library, but they seem to be absent now. Is there

linq how to select a parent with a child collection that contains one or many of an array (or list) of values

喜夏-厌秋 提交于 2019-12-22 01:58:46
问题 This seems like it would be easy enough var orx = gg.Where(x=>x.ProductAttributes.Any (pa =>pa.AttributeId == "home")); returns gg when product attributes has a value of "home" I need it to return where and gg has product attribute values from an array i.e. var orx = gg.Where(x=>x.ProductAttributes.Any (pa =>pa.AttributeId in "home,work")); 回答1: what about... string[] values = new string[] { "home", "work" }; var orx = gg.Where(x => x.ProductAttributes.Any(pa => values.Contains(pa.AttributeId

Scala Map vs HashMap

佐手、 提交于 2019-12-22 01:44:57
问题 Is there a difference between a Scala Map and a HashMap ? I am using the scala.collection.immutable.HashMap . 回答1: scala.collection.immutable.Map is the interface for immutable maps while scala.collection.immutable.HashMap is a concrete implementation. Creating with Map() or Map.empty gives a special empty singleton map, with Map(a -> b) with up to 4 pairs yields specialized classes for such small maps, and 5 and upwards gives you scala.collection.immutable.HashMap 来源: https://stackoverflow

Is it possible to inject a list of resolved objects into a constructor using Autofac?

余生颓废 提交于 2019-12-22 01:39:58
问题 I'm new to Autofac (3) and am using it to find a number of classes in several assemblies that implement IRecognizer. So I have: builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()).As<IRecognizer>(); which is fine. But I'd like to inject references to the found components into a constructor - sort of: public Detector(List<IRecognizer> recognizers) { this.Recognizers = recognizers; } Is there any way to do this? 回答1: Autofac supports the IEnumerable<T> as a relationship type:

Serializing a vector

给你一囗甜甜゛ 提交于 2019-12-22 01:18:10
问题 I'm trying to implement loading and saving for a game I'm working on. What I want to save is: A char[][] (bidimensional array/matrix) An ArrayList<Entity> Entity is a super class for Dragon , Hero and Item . All three of these types can be contained at once in the ArrayList . So far I have this: package logic; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io

setting the default string value of Python's collections.defaultdict

吃可爱长大的小学妹 提交于 2019-12-22 01:12:29
问题 I am using Python 3.2.3 and want to change the default returned string value: from collections import defaultdict d=defaultdict(str) d["NonExistent"] The value returned is '' . How can I change this so that when a key is not found, "unknown" is returned instead of the empty string? 回答1: The argument to defaultdict is a function (or rather, a callable object) that returns the default value. So you can pass in a lambda that returns your desired default. >>> from collections import defaultdict >