text stringlengths 54 60.6k |
|---|
<commit_before>a40ef0fe-2e4d-11e5-9284-b827eb9e62be<commit_msg>a413eb7c-2e4d-11e5-9284-b827eb9e62be<commit_after>a413eb7c-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>f72c3b74-2e4e-11e5-9284-b827eb9e62be<commit_msg>f7313228-2e4e-11e5-9284-b827eb9e62be<commit_after>f7313228-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>1f486d3c-2e4d-11e5-9284-b827eb9e62be<commit_msg>1f4d606c-2e4d-11e5-9284-b827eb9e62be<commit_after>1f4d606c-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>3560bb00-2e4e-11e5-9284-b827eb9e62be<commit_msg>3565d590-2e4e-11e5-9284-b827eb9e62be<commit_after>3565d590-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>dba2087a-2e4e-11e5-9284-b827eb9e62be<commit_msg>dba6fd80-2e4e-11e5-9284-b827eb9e62be<commit_after>dba6fd80-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>cfd5de7e-2e4c-11e5-9284-b827eb9e62be<commit_msg>cfdad0c8-2e4c-11e5-9284-b827eb9e62be<commit_after>cfdad0c8-2e4c-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>e5346baa-2e4c-11e5-9284-b827eb9e62be<commit_msg>e5397b22-2e4c-11e5-9284-b827eb9e62be<commit_after>e5397b22-2e4c-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>ce408d8c-2e4e-11e5-9284-b827eb9e62be<commit_msg>ce4588aa-2e4e-11e5-9284-b827eb9e62be<commit_after>ce4588aa-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>cc881f60-2e4d-11e5-9284-b827eb9e62be<commit_msg>cc8d20b4-2e4d-11e5-9284-b827eb9e62be<commit_after>cc8d20b4-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>11a61862-2e4f-11e5-9284-b827eb9e62be<commit_msg>11ab0d7c-2e4f-11e5-9284-b827eb9e62be<commit_after>11ab0d7c-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>bcc82742-2e4c-11e5-9284-b827eb9e62be<commit_msg>bccd22ce-2e4c-11e5-9284-b827eb9e62be<commit_after>bccd22ce-2e4c-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>207929d8-2e4f-11e5-9284-b827eb9e62be<commit_msg>207e1984-2e4f-11e5-9284-b827eb9e62be<commit_after>207e1984-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>3bed2c42-2e4e-11e5-9284-b827eb9e62be<commit_msg>3bf2479a-2e4e-11e5-9284-b827eb9e62be<commit_after>3bf2479a-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>9b0d095a-2e4d-11e5-9284-b827eb9e62be<commit_msg>9b1202ac-2e4d-11e5-9284-b827eb9e62be<commit_after>9b1202ac-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>388c032e-2e4f-11e5-9284-b827eb9e62be<commit_msg>3890f974-2e4f-11e5-9284-b827eb9e62be<commit_after>3890f974-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>dee5c742-2e4e-11e5-9284-b827eb9e62be<commit_msg>deeb31c8-2e4e-11e5-9284-b827eb9e62be<commit_after>deeb31c8-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>15c0174a-2e4f-11e5-9284-b827eb9e62be<commit_msg>15c558b8-2e4f-11e5-9284-b827eb9e62be<commit_after>15c558b8-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>0b11f210-2e4e-11e5-9284-b827eb9e62be<commit_msg>0b16e66c-2e4e-11e5-9284-b827eb9e62be<commit_after>0b16e66c-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>b14f8e36-2e4d-11e5-9284-b827eb9e62be<commit_msg>b16b00da-2e4d-11e5-9284-b827eb9e62be<commit_after>b16b00da-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>d4594d64-2e4c-11e5-9284-b827eb9e62be<commit_msg>d45e753c-2e4c-11e5-9284-b827eb9e62be<commit_after>d45e753c-2e4c-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>0bb2cf7e-2e4d-11e5-9284-b827eb9e62be<commit_msg>0bb7df32-2e4d-11e5-9284-b827eb9e62be<commit_after>0bb7df32-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>b7aa7660-2e4d-11e5-9284-b827eb9e62be<commit_msg>b7af6b5c-2e4d-11e5-9284-b827eb9e62be<commit_after>b7af6b5c-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>3419332a-2e4f-11e5-9284-b827eb9e62be<commit_msg>341e268c-2e4f-11e5-9284-b827eb9e62be<commit_after>341e268c-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>f16ff5a6-2e4c-11e5-9284-b827eb9e62be<commit_msg>f175042e-2e4c-11e5-9284-b827eb9e62be<commit_after>f175042e-2e4c-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>9a7323da-2e4d-11e5-9284-b827eb9e62be<commit_msg>9a781444-2e4d-11e5-9284-b827eb9e62be<commit_after>9a781444-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>ce0ab5a4-2e4e-11e5-9284-b827eb9e62be<commit_msg>ce0faadc-2e4e-11e5-9284-b827eb9e62be<commit_after>ce0faadc-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>2563f712-2e4e-11e5-9284-b827eb9e62be<commit_msg>256905ae-2e4e-11e5-9284-b827eb9e62be<commit_after>256905ae-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>fb9a7d84-2e4d-11e5-9284-b827eb9e62be<commit_msg>fb9f7e2e-2e4d-11e5-9284-b827eb9e62be<commit_after>fb9f7e2e-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>2fd1951a-2e4e-11e5-9284-b827eb9e62be<commit_msg>2fd687dc-2e4e-11e5-9284-b827eb9e62be<commit_after>2fd687dc-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>e9482a74-2e4c-11e5-9284-b827eb9e62be<commit_msg>e94d3320-2e4c-11e5-9284-b827eb9e62be<commit_after>e94d3320-2e4c-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>875160ea-2e4e-11e5-9284-b827eb9e62be<commit_msg>87567116-2e4e-11e5-9284-b827eb9e62be<commit_after>87567116-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>79e02536-2e4e-11e5-9284-b827eb9e62be<commit_msg>79e5994e-2e4e-11e5-9284-b827eb9e62be<commit_after>79e5994e-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>51c3fc54-2e4d-11e5-9284-b827eb9e62be<commit_msg>51c90758-2e4d-11e5-9284-b827eb9e62be<commit_after>51c90758-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>f73f3068-2e4d-11e5-9284-b827eb9e62be<commit_msg>f7442424-2e4d-11e5-9284-b827eb9e62be<commit_after>f7442424-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>9e06d172-2e4d-11e5-9284-b827eb9e62be<commit_msg>9e0bca92-2e4d-11e5-9284-b827eb9e62be<commit_after>9e0bca92-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>4d2a55f2-2e4e-11e5-9284-b827eb9e62be<commit_msg>4d4687cc-2e4e-11e5-9284-b827eb9e62be<commit_after>4d4687cc-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>49878d80-2e4d-11e5-9284-b827eb9e62be<commit_msg>498c9794-2e4d-11e5-9284-b827eb9e62be<commit_after>498c9794-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>d026dab6-2e4e-11e5-9284-b827eb9e62be<commit_msg>d02bd46c-2e4e-11e5-9284-b827eb9e62be<commit_after>d02bd46c-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>af6471e4-2e4e-11e5-9284-b827eb9e62be<commit_msg>af696afa-2e4e-11e5-9284-b827eb9e62be<commit_after>af696afa-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>1aee714e-2e4f-11e5-9284-b827eb9e62be<commit_msg>1af3842c-2e4f-11e5-9284-b827eb9e62be<commit_after>1af3842c-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>295c28c0-2e4f-11e5-9284-b827eb9e62be<commit_msg>29611c54-2e4f-11e5-9284-b827eb9e62be<commit_after>29611c54-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>dd644c92-2e4c-11e5-9284-b827eb9e62be<commit_msg>dd695426-2e4c-11e5-9284-b827eb9e62be<commit_after>dd695426-2e4c-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>e9f72808-2e4c-11e5-9284-b827eb9e62be<commit_msg>e9fc25e2-2e4c-11e5-9284-b827eb9e62be<commit_after>e9fc25e2-2e4c-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>3aef539c-2e4e-11e5-9284-b827eb9e62be<commit_msg>3af468fa-2e4e-11e5-9284-b827eb9e62be<commit_after>3af468fa-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>1bf119e0-2e4d-11e5-9284-b827eb9e62be<commit_msg>1bf64348-2e4d-11e5-9284-b827eb9e62be<commit_after>1bf64348-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>b267ebd2-2e4e-11e5-9284-b827eb9e62be<commit_msg>b26ce3b2-2e4e-11e5-9284-b827eb9e62be<commit_after>b26ce3b2-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>2d62af70-2e4f-11e5-9284-b827eb9e62be<commit_msg>2d67a1ba-2e4f-11e5-9284-b827eb9e62be<commit_after>2d67a1ba-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>d25e9c1a-2e4e-11e5-9284-b827eb9e62be<commit_msg>d26395f8-2e4e-11e5-9284-b827eb9e62be<commit_after>d26395f8-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>de9aff82-2e4e-11e5-9284-b827eb9e62be<commit_msg>de9ffc58-2e4e-11e5-9284-b827eb9e62be<commit_after>de9ffc58-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>ecb5f9a0-2e4e-11e5-9284-b827eb9e62be<commit_msg>ecbb2614-2e4e-11e5-9284-b827eb9e62be<commit_after>ecbb2614-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>696b799a-2e4d-11e5-9284-b827eb9e62be<commit_msg>697083ae-2e4d-11e5-9284-b827eb9e62be<commit_after>697083ae-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>176031fe-2e4d-11e5-9284-b827eb9e62be<commit_msg>176583f2-2e4d-11e5-9284-b827eb9e62be<commit_after>176583f2-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>02899d4a-2e4f-11e5-9284-b827eb9e62be<commit_msg>028e982c-2e4f-11e5-9284-b827eb9e62be<commit_after>028e982c-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>3f6322d8-2e4d-11e5-9284-b827eb9e62be<commit_msg>3f68285a-2e4d-11e5-9284-b827eb9e62be<commit_after>3f68285a-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>3ac27baa-2e4f-11e5-9284-b827eb9e62be<commit_msg>3ac77a2e-2e4f-11e5-9284-b827eb9e62be<commit_after>3ac77a2e-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>2405cd72-2e4f-11e5-9284-b827eb9e62be<commit_msg>240b1bc4-2e4f-11e5-9284-b827eb9e62be<commit_after>240b1bc4-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>5a462e74-2e4d-11e5-9284-b827eb9e62be<commit_msg>5a4b352c-2e4d-11e5-9284-b827eb9e62be<commit_after>5a4b352c-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>f62865ea-2e4e-11e5-9284-b827eb9e62be<commit_msg>f62d5f5a-2e4e-11e5-9284-b827eb9e62be<commit_after>f62d5f5a-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>26b31aa4-2e4d-11e5-9284-b827eb9e62be<commit_msg>26b80ffa-2e4d-11e5-9284-b827eb9e62be<commit_after>26b80ffa-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>21111e06-2e4e-11e5-9284-b827eb9e62be<commit_msg>21162bda-2e4e-11e5-9284-b827eb9e62be<commit_after>21162bda-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>/*
* Copyright (C) 2013 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
/** @file nway_merger.hh
* Implementation of a osv::nway_merger class.
* nway_merger::merge() function merges N sorted containers with the complexity
* of O(M*log(N)), where M is a total number of elements in all merged
* containers.
*/
#ifndef _NWAY_MERGE_HH
#define _NWAY_MERGE_HH
#include <queue>
#include <vector>
#include <list>
#include <functional>
/** @class Ptr
* Compares two containers by the elemnt at the head
*/
template <class Ptr>
class std_ptr_front_comparator {
public:
/**
* We want out heap to sort the elements in an increasing order.
* @param a
* @param b
*
* @return
*/
bool operator()(const Ptr a, const Ptr b)
{
return a->front() > b->front();
}
};
/** @class nway_merger "nway_merger.hh"
* Merge N containers S sorted in an increasing order into an iterator as a
* sorted sequence in an increasing order. The containers collection is passed
* to the method merge() in a container C.
*
* @note
* In order to invert the ordering of the elements in the output stream one may
* invert the semantics in the operator>(const S::value_type) to return "<="
* result and sort the input streams in a descreasing order. The resulting
* stream will have a decreasing order then.
*
* #### Algorithm:
* merge() method implements the "ideal merge" algorithm as described at
* http://xlinux.nist.gov/dads//HTML/idealmerge.html
*
* @note
* stl::priority_queue(heap) as we defined it will hold the "smallest" element
* at the "top".
*
* 1. Input containers should be sorted in an increasing order.
* 2. Push all the containers into the heap sorting by their front() elements.
* 3. Remove the container from the top of the heap. It'll have the smallest
* element among all containers.
* 4. Remove the front() element from this container and push it into the output
* iterator.
* 5. If this container still has elements, push it back to the heap.
* 6. Repeat steps (3)-(5) until there are containers in the heap.
*
* #### Complexity:
* O(M*log(N)), where M is a total number of elements in all merged containers
* (provided the complexity of a comparison between two S values is constant).
*
* @note S::value_type must implement operator>().
*/
template <class C,
class Comp = std_ptr_front_comparator<typename C::value_type> >
class nway_merger {
public:
/**
* Merges the containers and outputs the resulting stream into the output
* iterator res (see class description for more details).
*
* The input (sorted) container should implement:
* - front() - return the element from the HEAD element.
* - empty() - returns true if there are no more elements left
* - begin() - return the iterator pointing to the HEAD element:
* - erase(it) - deleting the element pointed by the iterator it.
*
* Output iterator should implement:
* - Required operators to implement the *it = xx in order to consume the
* xx value.
* - operator++() - to move to the next output position.
*
* @param sorted_lists collection of the pointers to the sorted collections
* to merge
* @param res Output stream for the results of an nway_merge
*/
template <class OutputIt>
void merge(const C& sorted_lists, OutputIt res)
{
create_heap(sorted_lists);
while (!_heads_heap.empty()) {
SPtr t = _heads_heap.top();
_heads_heap.pop();
auto t_it = t->begin();
/* Get the element from the "HEAD" of the container */
*res = *t_it;
++res;
/* Erase the "HEAD" */
t->erase(t_it);
if (!t->empty()) {
_heads_heap.push(t);
}
}
}
/**
* Pops the "smallest" element from the merged stream and pushes it into the
* output stream.
*
* The input ordering requirements is the same as described in
* merge() above.
* This functions performs a single step of the nway_merge algorithm:
* 1) Sorts the front elements.
* 2) Pushes the least among them into the output iterator.
*
* This function is convenient when you want to merge the input streams that
* are sometimes empty in a step-by-step manner.
*
* @param sorted_lists
* @param res
*
* @return true if the element has been popped and false if there was nothing
* to pop (all input sequences were empty).
*/
template <class OutputIt>
bool pop(OutputIt res)
{
refill_heap();
if (!_heads_heap.empty()) {
SPtr t = _heads_heap.top();
_heads_heap.pop();
auto t_it = t->begin();
/* Get the element from the "HEAD" of the container */
*res = *t_it;
++res;
/* Erase the "HEAD" */
t->erase(t_it);
if (!t->empty()) {
_heads_heap.push(t);
} else {
_empty_lists.emplace_back(t);
}
return true;
} else {
return false;
}
}
void clear() { _heads_heap = heap_type(); }
/**
* Create a new heap from the sorted sequences.
* @param sorted_lists
*/
void create_heap(const C& sorted_lists) {
clear();
/* Create a heap */
for (SPtr c : sorted_lists) {
if (!c->empty()) {
_heads_heap.emplace(c);
} else {
_empty_lists.emplace_back(c);
}
}
}
/**
* Push back all sequences from the _empty_list back to the heap.
*
* TODO:
* Come up with something better that walking on the whole list and check
* each list. One option is to use bitfield array and then use
* count_leading_zeros() based function to efficiently get the next set bit
* which may represent the non-empty list.
*
* This inefficiency may count in case of VMs with a large number of vCPUs
* when most of the queues would be empty.
*/
void refill_heap() {
/* TODO: Improve this by iterating only on those that are not empty */
auto it = _empty_lists.begin();
while (it != _empty_lists.end()) {
if (!(*it)->empty()) {
_heads_heap.emplace(*it);
auto tmp_it = it;
++it;
_empty_lists.erase(tmp_it);
} else {
++it;
}
}
}
template <class EmptyChecker>
bool empty(EmptyChecker checker) const {
return checker();
}
// A stupid implementation of an empty_checker()
bool silly_empty_checker() const {
if (!_heads_heap.empty()) {
return false;
}
for (SPtr c : _empty_lists) {
if (!c->empty()) {
return false;
}
}
return true;
}
private:
typedef typename C::value_type SPtr;
typedef std::priority_queue<SPtr, std::vector<SPtr>, Comp> heap_type;
heap_type _heads_heap;
C* _sorted_lists;
std::list<SPtr> _empty_lists;
};
#endif /* _NWAY_MERGE_HH */
<commit_msg>nway_merger: use pop_front() instead of erase()<commit_after>/*
* Copyright (C) 2013 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
/** @file nway_merger.hh
* Implementation of a osv::nway_merger class.
* nway_merger::merge() function merges N sorted containers with the complexity
* of O(M*log(N)), where M is a total number of elements in all merged
* containers.
*/
#ifndef _NWAY_MERGE_HH
#define _NWAY_MERGE_HH
#include <queue>
#include <vector>
#include <list>
#include <functional>
/** @class Ptr
* Compares two containers by the elemnt at the head
*/
template <class Ptr>
class std_ptr_front_comparator {
public:
/**
* We want out heap to sort the elements in an increasing order.
* @param a
* @param b
*
* @return
*/
bool operator()(const Ptr a, const Ptr b)
{
return a->front() > b->front();
}
};
/** @class nway_merger "nway_merger.hh"
* Merge N containers S sorted in an increasing order into an iterator as a
* sorted sequence in an increasing order. The containers collection is passed
* to the method merge() in a container C.
*
* @note
* In order to invert the ordering of the elements in the output stream one may
* invert the semantics in the operator>(const S::value_type) to return "<="
* result and sort the input streams in a descreasing order. The resulting
* stream will have a decreasing order then.
*
* #### Algorithm:
* merge() method implements the "ideal merge" algorithm as described at
* http://xlinux.nist.gov/dads//HTML/idealmerge.html
*
* @note
* stl::priority_queue(heap) as we defined it will hold the "smallest" element
* at the "top".
*
* 1. Input containers should be sorted in an increasing order.
* 2. Push all the containers into the heap sorting by their front() elements.
* 3. Remove the container from the top of the heap. It'll have the smallest
* element among all containers.
* 4. Remove the front() element from this container and push it into the output
* iterator.
* 5. If this container still has elements, push it back to the heap.
* 6. Repeat steps (3)-(5) until there are containers in the heap.
*
* #### Complexity:
* O(M*log(N)), where M is a total number of elements in all merged containers
* (provided the complexity of a comparison between two S values is constant).
*
* @note S::value_type must implement operator>().
*/
template <class C,
class Comp = std_ptr_front_comparator<typename C::value_type> >
class nway_merger {
public:
/**
* Merges the containers and outputs the resulting stream into the output
* iterator res (see class description for more details).
*
* The input (sorted) container should implement:
* - front() - return the element from the HEAD element.
* - empty() - returns true if there are no more elements left
* - begin() - return the iterator pointing to the HEAD element:
* - pop_front() - deleting the element returned by front().
*
* Output iterator should implement:
* - Required operators to implement the *it = xx in order to consume the
* xx value.
* - operator++() - to move to the next output position.
*
* @param sorted_lists collection of the pointers to the sorted collections
* to merge
* @param res Output stream for the results of an nway_merge
*/
template <class OutputIt>
void merge(const C& sorted_lists, OutputIt res)
{
create_heap(sorted_lists);
while (!_heads_heap.empty()) {
SPtr t = _heads_heap.top();
_heads_heap.pop();
auto t_it = t->begin();
/* Get the element from the "HEAD" of the container */
*res = *t_it;
++res;
/* Erase the "HEAD" */
t->pop_front();
if (!t->empty()) {
_heads_heap.push(t);
}
}
}
/**
* Pops the "smallest" element from the merged stream and pushes it into the
* output stream.
*
* The input ordering requirements is the same as described in
* merge() above.
* This functions performs a single step of the nway_merge algorithm:
* 1) Sorts the front elements.
* 2) Pushes the least among them into the output iterator.
*
* This function is convenient when you want to merge the input streams that
* are sometimes empty in a step-by-step manner.
*
* @param sorted_lists
* @param res
*
* @return true if the element has been popped and false if there was nothing
* to pop (all input sequences were empty).
*/
template <class OutputIt>
bool pop(OutputIt res)
{
refill_heap();
if (!_heads_heap.empty()) {
SPtr t = _heads_heap.top();
_heads_heap.pop();
auto t_it = t->begin();
/* Get the element from the "HEAD" of the container */
*res = *t_it;
++res;
/* Erase the "HEAD" */
t->pop_front();
if (!t->empty()) {
_heads_heap.push(t);
} else {
_empty_lists.emplace_back(t);
}
return true;
} else {
return false;
}
}
void clear() { _heads_heap = heap_type(); }
/**
* Create a new heap from the sorted sequences.
* @param sorted_lists
*/
void create_heap(const C& sorted_lists) {
clear();
/* Create a heap */
for (SPtr c : sorted_lists) {
if (!c->empty()) {
_heads_heap.emplace(c);
} else {
_empty_lists.emplace_back(c);
}
}
}
/**
* Push back all sequences from the _empty_list back to the heap.
*
* TODO:
* Come up with something better that walking on the whole list and check
* each list. One option is to use bitfield array and then use
* count_leading_zeros() based function to efficiently get the next set bit
* which may represent the non-empty list.
*
* This inefficiency may count in case of VMs with a large number of vCPUs
* when most of the queues would be empty.
*/
void refill_heap() {
/* TODO: Improve this by iterating only on those that are not empty */
auto it = _empty_lists.begin();
while (it != _empty_lists.end()) {
if (!(*it)->empty()) {
_heads_heap.emplace(*it);
auto tmp_it = it;
++it;
_empty_lists.erase(tmp_it);
} else {
++it;
}
}
}
template <class EmptyChecker>
bool empty(EmptyChecker checker) const {
return checker();
}
// A stupid implementation of an empty_checker()
bool silly_empty_checker() const {
if (!_heads_heap.empty()) {
return false;
}
for (SPtr c : _empty_lists) {
if (!c->empty()) {
return false;
}
}
return true;
}
private:
typedef typename C::value_type SPtr;
typedef std::priority_queue<SPtr, std::vector<SPtr>, Comp> heap_type;
heap_type _heads_heap;
C* _sorted_lists;
std::list<SPtr> _empty_lists;
};
#endif /* _NWAY_MERGE_HH */
<|endoftext|> |
<commit_before>3f6d28a0-2e4d-11e5-9284-b827eb9e62be<commit_msg>3f72297c-2e4d-11e5-9284-b827eb9e62be<commit_after>3f72297c-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>38dc0a90-2e4f-11e5-9284-b827eb9e62be<commit_msg>38e10b8a-2e4f-11e5-9284-b827eb9e62be<commit_after>38e10b8a-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>29e7d6fe-2e4f-11e5-9284-b827eb9e62be<commit_msg>29eccace-2e4f-11e5-9284-b827eb9e62be<commit_after>29eccace-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>#include "ModI2CHost.h"
CModI2CHost::CModI2CHost(void)
{
// nothing
}
void CModI2CHost::Begin(void)
{
CModTemplate::Begin();
ModGUID = 8; // GUID of this sspecific mod
if (Global.HasUSB) // i2c host only activates if this device is plugged into the PC
{
Wire.begin(8);
Wire.onRequest(CModI2CHost::RequestInfo);
Wire.onReceive(CModI2CHost::ReceiveInfo);
}
}
void CModI2CHost::RequestEvent()
{
// first byte is the type of the packet
// second byte always sends the templayer
// third byte forward is the data of the packet
/*
type 0 does nothing
type 1 sends templayer (templayer)
type 2 is a full reflash type that writes sub eeprom from 0 to 1024 (byte0, byte1, byte2, ...)
type 3 signals EOL for other continuous types (length, byte0, byte1, byte2, ...)
type 4 writes sub eeprom starting from 900 (byte0, byte1, byte2, ...)
type 5 EOL for type 4
type 6 changes the LED setting (brightness)
type 7 changes the refresh rate (refresh)
*/
if (SignalType == 1)
{
SetTempLayer();
}
else if (SignalType == 2)
{
SetSubEEPROM();
EEPROMPacketIndex = 0;
EEPROMPacketCounter--;
SignalType == 1;
}
else if (SignalType == 3)
{
SetSubEEPROMEOL();
EEPROMPacketIndex = 255; // resets packet index and counter
EEPROMPacketCounter = 255;
SignalType == 1;
}
else if (SignalType == 4)
{
SetSubBoardSettings();
}
else if (SignalType == 5)
{
SetSubBoardEOL();
}
else if (SignalType == 6)
{
SetSubLEDBrightness();
}
else if (SignalType == 7)
{
SetSubRefreshRate();
}
}
void CModI2CHost::ReceiveEvent(byte numBytes)
{
}
void CModI2CHost::LoadData(void)
{
CModTemplate::LoadData();
if (Global.HasUSB)
{
}
}
void CModI2CHost::Loop(void)
{
CModTemplate::Loop();
if (Animus.GetMillis())
{
if (Global.HasUSB)
{
}
}
}
void CModI2CHost::PrePress(byte val, byte type)
{
CModTemplate::PrePress(val, type);
if (Global.HasUSB)
{
}
}
void CModI2CHost::PressKey(byte val, byte type)
{
CModTemplate::PressKey(val, type);
if (Global.HasUSB)
{
}
}
void CModI2CHost::ReleaseKey(byte val, byte type)
{
CModTemplate::ReleaseKey(val, type);
if (Global.HasUSB)
{
}
}
void CModI2CHost::SerialComms(byte mode) // holy shit this is complicated
{
CModTemplate::SerialComms(mode);
if (mode == 6) // start of package transfer TODO: you could remove the length in non-EOL packets
{
if (Serial.available()>0)
{
if (EEPROMPacketCounter == 255) // if first byte from message is not read
{
EEPROMPacketCounter = Serial.read(); // read first byte as packet counter
}
else if (EEPROMPacketEOLSize == 255) // if second byte from message is not read
{
EEPROMPacketEOLSize = Serial.read();
}
else // length and number of packets is currently known
{
if (SignalType == 1) // if packet buffer is free
{
if (EEPROMPacketCounter-1 > 0) // if there are still pending packets
{
if (EEPROMPacketIndex < 29) // if i2c transfer buffer is not filled then fill buffer
{
EEPROMPacket[EEPROMPacketIndex] = Serial.read();
EEPROMPacketIndex++;
}
else // when buffer is filled, signal package ready to send
{
SignalType = 2;
}
}
else // this is the EOL packet
{
if (EEPROMPacketIndex < EEPROMPacketEOLSize) // if EOL packet reaches designated size
{
EEPROMPacket[EEPROMPacketIndex] = Serial.read();
EEPROMPacketIndex++;
}
else // last package ready, signal package ready to send
{
SignalType = 3;
Comms.mode = 0;// comms is reset
}
}
}
}
}
}
}
void CModI2CHost::RequestInfo(void)
{
ModI2CHost.RequestEvent();
}
void CModI2CHost::ReceiveInfo(byte numBytes)
{
ModI2CHost.ReceiveEvent(numBytes);
}
// first byte is the type of the packet
// second byte always sends the templayer
// third byte forward is the data of the packet
/*
type 0 does nothing
type 1 sends templayer (templayer)
type 2 is a full reflash type that writes sub eeprom from 0 to 1024 (byte0, byte1, byte2, ...)
type 3 signals EOL for other continuous types (length, byte0, byte1, byte2, ...)
type 4 writes sub eeprom starting from 900 (byte0, byte1, byte2, ...)
type 5 EOL for type 4
type 6 changes the LED setting (brightness)
type 7 changes the refresh rate (refresh)
*/
void CModI2CHost::SetTempLayer()
{
Wire.write(1);
Wire.write(Global.TempLayer);
}
void CModI2CHost::SetSubEEPROM(void)
{
Wire.write(2);
Wire.write(Global.TempLayer);
Wire.write(EEPROMPacket, EEPROMPacketIndex);
}
void CModI2CHost::SetSubEEPROMEOL(void)
{
Wire.write(3);
Wire.write(Global.TempLayer);
Wire.write(EEPROMPacketIndex);
Wire.write(EEPROMPacket, EEPROMPacketIndex);
}
void CModI2CHost::SetSubBoardSettings(void)
{
Wire.write(4);
Wire.write(Global.TempLayer);
Wire.write(EEPROMPacket, EEPROMPacketIndex);
}
void CModI2CHost::SetSubBoardEOL(void)
{
Wire.write(5);
Wire.write(Global.TempLayer);
Wire.write(EEPROMPacketIndex);
Wire.write(EEPROMPacket, EEPROMPacketIndex);
}
void CModI2CHost::SetSubLEDBrightness(void)
{
Wire.write(6);
Wire.write(Global.TempLayer);
Wire.write(Global.LEDBrightness);
}
void CModI2CHost::SetSubRefreshRate(void)
{
Wire.write(7);
Wire.write(Global.TempLayer);
Wire.write(Global.RefreshDelay);
}
CModI2CHost ModI2CHost;
<commit_msg>add: i2c host guest-host recognition<commit_after>#include "ModI2CHost.h"
CModI2CHost::CModI2CHost(void)
{
// nothing
}
void CModI2CHost::Begin(void)
{
CModTemplate::Begin();
ModGUID = 8; // GUID of this sspecific mod
if (Global.HasUSB) // i2c host only activates if this device is plugged into the PC
{
Wire.begin(8);
Wire.onRequest(CModI2CHost::RequestInfo);
Wire.onReceive(CModI2CHost::ReceiveInfo);
}
}
void CModI2CHost::RequestEvent()
{
// first byte is the type of the packet
// second byte always sends the templayer
// third byte forward is the data of the packet
/*
type 0 does nothing
type 1 sends templayer (templayer)
type 2 is a full reflash type that writes sub eeprom from 0 to 1024 (byte0, byte1, byte2, ...)
type 3 signals EOL for other continuous types (length, byte0, byte1, byte2, ...)
type 4 writes sub eeprom starting from 900 (byte0, byte1, byte2, ...)
type 5 EOL for type 4
type 6 changes the LED setting (brightness)
type 7 changes the refresh rate (refresh)
*/
if (Global.HasUSB)
{
if (SignalType == 1)
{
SetTempLayer();
}
else if (SignalType == 2)
{
SetSubEEPROM();
EEPROMPacketIndex = 0;
EEPROMPacketCounter--;
SignalType == 1;
}
else if (SignalType == 3)
{
SetSubEEPROMEOL();
EEPROMPacketIndex = 255; // resets packet index and counter
EEPROMPacketCounter = 255;
SignalType == 1;
}
else if (SignalType == 4)
{
SetSubBoardSettings();
}
else if (SignalType == 5)
{
SetSubBoardEOL();
}
else if (SignalType == 6)
{
SetSubLEDBrightness();
}
else if (SignalType == 7)
{
SetSubRefreshRate();
}
}
}
void CModI2CHost::ReceiveEvent(byte numBytes)
{
}
void CModI2CHost::LoadData(void)
{
CModTemplate::LoadData();
if (Global.HasUSB)
{
}
}
void CModI2CHost::Loop(void)
{
CModTemplate::Loop();
if (Animus.GetMillis())
{
if (Global.HasUSB)
{
}
}
}
void CModI2CHost::PrePress(byte val, byte type)
{
CModTemplate::PrePress(val, type);
if (Global.HasUSB)
{
}
}
void CModI2CHost::PressKey(byte val, byte type)
{
CModTemplate::PressKey(val, type);
if (Global.HasUSB)
{
}
}
void CModI2CHost::ReleaseKey(byte val, byte type)
{
CModTemplate::ReleaseKey(val, type);
if (Global.HasUSB)
{
}
}
void CModI2CHost::SerialComms(byte mode) // holy shit this is complicated
{
CModTemplate::SerialComms(mode);
if (Global.HasUSB)
{
if (mode == 6) // start of package transfer TODO: you could remove the length in non-EOL packets
{
if (Serial.available()>0)
{
if (EEPROMPacketCounter == 255) // if first byte from message is not read
{
EEPROMPacketCounter = Serial.read(); // read first byte as packet counter
}
else if (EEPROMPacketEOLSize == 255) // if second byte from message is not read
{
EEPROMPacketEOLSize = Serial.read();
}
else // length and number of packets is currently known
{
if (SignalType == 1) // if packet buffer is free
{
if (EEPROMPacketCounter-1 > 0) // if there are still pending packets
{
if (EEPROMPacketIndex < 29) // if i2c transfer buffer is not filled then fill buffer
{
EEPROMPacket[EEPROMPacketIndex] = Serial.read();
EEPROMPacketIndex++;
}
else // when buffer is filled, signal package ready to send
{
SignalType = 2;
}
}
else // this is the EOL packet
{
if (EEPROMPacketIndex < EEPROMPacketEOLSize) // if EOL packet reaches designated size
{
EEPROMPacket[EEPROMPacketIndex] = Serial.read();
EEPROMPacketIndex++;
}
else // last package ready, signal package ready to send
{
SignalType = 3;
Comms.mode = 0;// comms is reset
}
}
}
}
}
}
}
}
void CModI2CHost::RequestInfo(void)
{
ModI2CHost.RequestEvent();
}
void CModI2CHost::ReceiveInfo(byte numBytes)
{
ModI2CHost.ReceiveEvent(numBytes);
}
// first byte is the type of the packet
// second byte always sends the templayer
// third byte forward is the data of the packet
/*
type 0 does nothing
type 1 sends templayer (templayer)
type 2 is a full reflash type that writes sub eeprom from 0 to 1024 (byte0, byte1, byte2, ...)
type 3 signals EOL for other continuous types (length, byte0, byte1, byte2, ...)
type 4 writes sub eeprom starting from 900 (byte0, byte1, byte2, ...)
type 5 EOL for type 4
type 6 changes the LED setting (brightness)
type 7 changes the refresh rate (refresh)
*/
void CModI2CHost::SetTempLayer()
{
Wire.write(1);
Wire.write(Global.TempLayer);
}
void CModI2CHost::SetSubEEPROM(void)
{
Wire.write(2);
Wire.write(Global.TempLayer);
Wire.write(EEPROMPacket, EEPROMPacketIndex);
}
void CModI2CHost::SetSubEEPROMEOL(void)
{
Wire.write(3);
Wire.write(Global.TempLayer);
Wire.write(EEPROMPacketIndex);
Wire.write(EEPROMPacket, EEPROMPacketIndex);
}
void CModI2CHost::SetSubBoardSettings(void)
{
Wire.write(4);
Wire.write(Global.TempLayer);
Wire.write(EEPROMPacket, EEPROMPacketIndex);
}
void CModI2CHost::SetSubBoardEOL(void)
{
Wire.write(5);
Wire.write(Global.TempLayer);
Wire.write(EEPROMPacketIndex);
Wire.write(EEPROMPacket, EEPROMPacketIndex);
}
void CModI2CHost::SetSubLEDBrightness(void)
{
Wire.write(6);
Wire.write(Global.TempLayer);
Wire.write(Global.LEDBrightness);
}
void CModI2CHost::SetSubRefreshRate(void)
{
Wire.write(7);
Wire.write(Global.TempLayer);
Wire.write(Global.RefreshDelay);
}
CModI2CHost ModI2CHost;
<|endoftext|> |
<commit_before>4d0f8570-2e4d-11e5-9284-b827eb9e62be<commit_msg>4d149952-2e4d-11e5-9284-b827eb9e62be<commit_after>4d149952-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>931c18f2-2e4e-11e5-9284-b827eb9e62be<commit_msg>93211bfe-2e4e-11e5-9284-b827eb9e62be<commit_after>93211bfe-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>#include <list>
#include <algorithm>
#include "rtmidi/RtMidi.h"
#include "core.hpp"
#include "MidiIO.hpp"
using namespace rack;
/**
* MidiIO implements the shared functionality of all midi modules, namely:
* + Channel Selection (including helper for storing json)
* + Interface Selection (including helper for storing json)
* + rtMidi initialisation (input or output)
*/
MidiIO::MidiIO(bool isOut) {
channel = -1;
this->isOut = isOut;
if (isOut) {
fprintf(stderr, "Midi Out is currently not supported (will be added soon)");
}
};
void MidiIO::setChannel(int channel) {
this->channel = channel;
}
std::unordered_map<std::string, MidiInWrapper *> MidiIO::midiInMap = {};
json_t *MidiIO::addBaseJson(json_t *rootJ) {
if (deviceName != "") {
json_object_set_new(rootJ, "interfaceName", json_string(deviceName.c_str()));
json_object_set_new(rootJ, "channel", json_integer(channel));
}
return rootJ;
}
void MidiIO::baseFromJson(json_t *rootJ) {
json_t *portNameJ = json_object_get(rootJ, "interfaceName");
if (portNameJ) {
openDevice(json_string_value(portNameJ));
}
json_t *channelJ = json_object_get(rootJ, "channel");
if (channelJ) {
setChannel(json_integer_value(channelJ));
}
}
std::vector<std::string> MidiIO::getDevices() {
/* Note: we could also use an existing interface if one exists */
RtMidiIn m;
std::vector<std::string> names = {};
for (unsigned int i = 0; i < m.getPortCount(); i++) {
names.push_back(m.getPortName(i));
}
return names;
}
void MidiIO::openDevice(std::string deviceName) {
if (this->id > 0 || this->deviceName != "") {
close();
}
MidiInWrapper *mw = midiInMap[deviceName];
if (!mw) {
try {
mw = new MidiInWrapper();
midiInMap[deviceName] = mw;
for (unsigned int i = 0; i < mw->getPortCount(); i++) {
if (deviceName == mw->getPortName(i)) {
mw->openPort(i);
break;
}
}
}
catch (RtMidiError &error) {
fprintf(stderr, "Failed to create RtMidiIn: %s\n", error.getMessage().c_str());
this->deviceName = "";
this->id = -1;
return;
}
}
this->deviceName = deviceName;
id = midiInMap[deviceName]->add();
onDeviceChange();
}
void MidiIO::setIgnores(bool ignoreSysex, bool ignoreTime, bool ignoreSense) {
bool sy = true, ti = true, se = true;
midiInMap[deviceName]->ignoresMap[id].midiSysex = ignoreSysex;
midiInMap[deviceName]->ignoresMap[id].midiTime = ignoreTime;
midiInMap[deviceName]->ignoresMap[id].midiSense = ignoreSense;
for (auto kv : midiInMap[deviceName]->ignoresMap) {
sy = sy && kv.second.midiSysex;
ti = ti && kv.second.midiTime;
se = se && kv.second.midiSense;
}
midiInMap[deviceName]->ignoreTypes(se, ti, se);
}
std::string MidiIO::getDeviceName() {
return deviceName;
}
double MidiIO::getMessage(std::vector<unsigned char> *msg) {
MidiMessage next_msg = MidiMessage();
MidiInWrapper *mw = midiInMap[deviceName];
if (!mw) {
fprintf(stderr, "Device not opened!: %s\n", deviceName.c_str());
return 0;
}
next_msg.timeStamp = midiInMap[deviceName]->getMessage(&next_msg.bytes);
if (next_msg.bytes.size() > 0) {
for (auto &kv : mw->idMessagesMap) {
kv.second.push_back(next_msg);
}
}
if (mw->idMessagesMap[id].size() > 0) {
next_msg = mw->idMessagesMap[id].front();
mw->idMessagesMap[id].pop_front();
}
*msg = next_msg.bytes;
return next_msg.timeStamp;
}
bool MidiIO::isPortOpen() {
return id > 0;
}
void MidiIO::close() {
MidiInWrapper *mw = midiInMap[deviceName];
if (!mw || id < 0) {
//fprintf(stderr, "Trying to close already closed device!\n");
return;
}
setIgnores(); // reset ignore types for this instance
mw->erase(id);
if (mw->idMessagesMap.size() == 0) {
mw->closePort();
midiInMap.erase(deviceName);
delete (mw);
}
id = -1;
deviceName = "";
}
void MidiItem::onAction() {
midiModule->resetMidi(); // reset Midi values
midiModule->openDevice(text);
}
void MidiChoice::onAction() {
Menu *menu = gScene->createMenu();
menu->box.pos = getAbsoluteOffset(Vec(0, box.size.y)).round();
menu->box.size.x = box.size.x;
{
MidiItem *midiItem = new MidiItem();
midiItem->midiModule = midiModule;
midiItem->text = "";
menu->pushChild(midiItem);
}
std::vector<std::string> deviceNames = midiModule->getDevices();
for (unsigned int i = 0; i < deviceNames.size(); i++) {
MidiItem *midiItem = new MidiItem();
midiItem->midiModule = midiModule;
midiItem->text = deviceNames[i];
menu->pushChild(midiItem);
}
}
void MidiChoice::step() {
if (midiModule->getDeviceName() == "") {
text = "No Device";
return;
}
std::string name = midiModule->getDeviceName();
text = ellipsize(name, 15);
}
void ChannelItem::onAction() {
midiModule->resetMidi(); // reset Midi values
midiModule->setChannel(channel);
}
void ChannelChoice::onAction() {
Menu *menu = gScene->createMenu();
menu->box.pos = getAbsoluteOffset(Vec(0, box.size.y)).round();
menu->box.size.x = box.size.x;
{
ChannelItem *channelItem = new ChannelItem();
channelItem->midiModule = midiModule;
channelItem->channel = -1;
channelItem->text = "All";
menu->pushChild(channelItem);
}
for (int channel = 0; channel < 16; channel++) {
ChannelItem *channelItem = new ChannelItem();
channelItem->midiModule = midiModule;
channelItem->channel = channel;
channelItem->text = stringf("%d", channel + 1);
menu->pushChild(channelItem);
}
}
void ChannelChoice::step() {
text = (midiModule->channel >= 0) ? stringf("%d", midiModule->channel + 1) : "All";
}<commit_msg>Avoid creating an rtMidi instance if not necessary and make sure it's delete after<commit_after>#include <list>
#include <algorithm>
#include "rtmidi/RtMidi.h"
#include "core.hpp"
#include "MidiIO.hpp"
using namespace rack;
/**
* MidiIO implements the shared functionality of all midi modules, namely:
* + Channel Selection (including helper for storing json)
* + Interface Selection (including helper for storing json)
* + rtMidi initialisation (input or output)
*/
MidiIO::MidiIO(bool isOut) {
channel = -1;
this->isOut = isOut;
if (isOut) {
fprintf(stderr, "Midi Out is currently not supported (will be added soon)");
}
};
void MidiIO::setChannel(int channel) {
this->channel = channel;
}
std::unordered_map<std::string, MidiInWrapper *> MidiIO::midiInMap = {};
json_t *MidiIO::addBaseJson(json_t *rootJ) {
if (deviceName != "") {
json_object_set_new(rootJ, "interfaceName", json_string(deviceName.c_str()));
json_object_set_new(rootJ, "channel", json_integer(channel));
}
return rootJ;
}
void MidiIO::baseFromJson(json_t *rootJ) {
json_t *portNameJ = json_object_get(rootJ, "interfaceName");
if (portNameJ) {
openDevice(json_string_value(portNameJ));
}
json_t *channelJ = json_object_get(rootJ, "channel");
if (channelJ) {
setChannel(json_integer_value(channelJ));
}
}
std::vector<std::string> MidiIO::getDevices() {
MidiApi *m = NULL;
std::vector<std::string> names = {};
if (isPortOpen()) {
if (isOut) {
//m = (RtMidiApi *) midiInMap[this->deviceName];
return names;
} else {
m = (MidiApi *) midiInMap[this->deviceName];
}
} else {
try {
m = (MidiApi *) new RtMidiIn();
} catch (RtMidiError &error) {
fprintf(stderr, "Failed to create RtMidiIn: %s\n", error.getMessage().c_str());
return names;
}
}
for (unsigned int i = 0; i < m->getPortCount(); i++) {
names.push_back(m->getPortName(i));
}
if (!isPortOpen())
delete (m);
return names;
}
void MidiIO::openDevice(std::string deviceName) {
if (this->id > 0 || this->deviceName != "") {
close();
}
MidiInWrapper *mw = midiInMap[deviceName];
if (!mw) {
try {
mw = new MidiInWrapper();
midiInMap[deviceName] = mw;
for (unsigned int i = 0; i < mw->getPortCount(); i++) {
if (deviceName == mw->getPortName(i)) {
mw->openPort(i);
break;
}
}
if (!mw->isPortOpen()) {
fprintf(stderr, "Failed to create RtMidiIn: No such device %s\n", deviceName.c_str());
this->deviceName = "";
this->id = -1;
return;
}
}
catch (RtMidiError &error) {
fprintf(stderr, "Failed to create RtMidiIn: %s\n", error.getMessage().c_str());
this->deviceName = "";
this->id = -1;
return;
}
}
this->deviceName = deviceName;
id = midiInMap[deviceName]->add();
onDeviceChange();
}
void MidiIO::setIgnores(bool ignoreSysex, bool ignoreTime, bool ignoreSense) {
bool sy = true, ti = true, se = true;
midiInMap[deviceName]->ignoresMap[id].midiSysex = ignoreSysex;
midiInMap[deviceName]->ignoresMap[id].midiTime = ignoreTime;
midiInMap[deviceName]->ignoresMap[id].midiSense = ignoreSense;
for (auto kv : midiInMap[deviceName]->ignoresMap) {
sy = sy && kv.second.midiSysex;
ti = ti && kv.second.midiTime;
se = se && kv.second.midiSense;
}
midiInMap[deviceName]->ignoreTypes(se, ti, se);
}
std::string MidiIO::getDeviceName() {
return deviceName;
}
double MidiIO::getMessage(std::vector<unsigned char> *msg) {
MidiMessage next_msg = MidiMessage();
MidiInWrapper *mw = midiInMap[deviceName];
if (!mw) {
fprintf(stderr, "Device not opened!: %s\n", deviceName.c_str());
return 0;
}
next_msg.timeStamp = midiInMap[deviceName]->getMessage(&next_msg.bytes);
if (next_msg.bytes.size() > 0) {
for (auto &kv : mw->idMessagesMap) {
kv.second.push_back(next_msg);
}
}
if (mw->idMessagesMap[id].size() > 0) {
next_msg = mw->idMessagesMap[id].front();
mw->idMessagesMap[id].pop_front();
}
*msg = next_msg.bytes;
return next_msg.timeStamp;
}
bool MidiIO::isPortOpen() {
return id > 0;
}
void MidiIO::close() {
MidiInWrapper *mw = midiInMap[deviceName];
if (!mw || id < 0) {
//fprintf(stderr, "Trying to close already closed device!\n");
return;
}
setIgnores(); // reset ignore types for this instance
mw->erase(id);
if (mw->idMessagesMap.size() == 0) {
mw->closePort();
midiInMap.erase(deviceName);
delete (mw);
}
id = -1;
deviceName = "";
}
void MidiItem::onAction() {
midiModule->resetMidi(); // reset Midi values
midiModule->openDevice(text);
}
void MidiChoice::onAction() {
Menu *menu = gScene->createMenu();
menu->box.pos = getAbsoluteOffset(Vec(0, box.size.y)).round();
menu->box.size.x = box.size.x;
{
MidiItem *midiItem = new MidiItem();
midiItem->midiModule = midiModule;
midiItem->text = "";
menu->pushChild(midiItem);
}
std::vector<std::string> deviceNames = midiModule->getDevices();
for (unsigned int i = 0; i < deviceNames.size(); i++) {
MidiItem *midiItem = new MidiItem();
midiItem->midiModule = midiModule;
midiItem->text = deviceNames[i];
menu->pushChild(midiItem);
}
}
void MidiChoice::step() {
if (midiModule->getDeviceName() == "") {
text = "No Device";
return;
}
std::string name = midiModule->getDeviceName();
text = ellipsize(name, 15);
}
void ChannelItem::onAction() {
midiModule->resetMidi(); // reset Midi values
midiModule->setChannel(channel);
}
void ChannelChoice::onAction() {
Menu *menu = gScene->createMenu();
menu->box.pos = getAbsoluteOffset(Vec(0, box.size.y)).round();
menu->box.size.x = box.size.x;
{
ChannelItem *channelItem = new ChannelItem();
channelItem->midiModule = midiModule;
channelItem->channel = -1;
channelItem->text = "All";
menu->pushChild(channelItem);
}
for (int channel = 0; channel < 16; channel++) {
ChannelItem *channelItem = new ChannelItem();
channelItem->midiModule = midiModule;
channelItem->channel = channel;
channelItem->text = stringf("%d", channel + 1);
menu->pushChild(channelItem);
}
}
void ChannelChoice::step() {
text = (midiModule->channel >= 0) ? stringf("%d", midiModule->channel + 1) : "All";
}
<|endoftext|> |
<commit_before>db6834c0-2e4d-11e5-9284-b827eb9e62be<commit_msg>db6d372c-2e4d-11e5-9284-b827eb9e62be<commit_after>db6d372c-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>/*
* Copyright 2007 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkMask.h"
#include "SkMalloc.h"
//#define TRACK_SKMASK_LIFETIME
/** returns the product if it is positive and fits in 31 bits. Otherwise this
returns 0.
*/
static int32_t safeMul32(int32_t a, int32_t b) {
int64_t size = sk_64_mul(a, b);
if (size > 0 && sk_64_isS32(size)) {
return sk_64_asS32(size);
}
return 0;
}
size_t SkMask::computeImageSize() const {
return safeMul32(fBounds.height(), fRowBytes);
}
size_t SkMask::computeTotalImageSize() const {
size_t size = this->computeImageSize();
if (fFormat == SkMask::k3D_Format) {
size = safeMul32(SkToS32(size), 3);
}
return size;
}
#ifdef TRACK_SKMASK_LIFETIME
static int gCounter;
#endif
/** We explicitly use this allocator for SkBimap pixels, so that we can
freely assign memory allocated by one class to the other.
*/
uint8_t* SkMask::AllocImage(size_t size) {
#ifdef TRACK_SKMASK_LIFETIME
SkDebugf("SkMask::AllocImage %d\n", gCounter++);
#endif
return (uint8_t*)sk_malloc_throw(SkAlign4(size));
}
/** We explicitly use this allocator for SkBimap pixels, so that we can
freely assign memory allocated by one class to the other.
*/
void SkMask::FreeImage(void* image) {
#ifdef TRACK_SKMASK_LIFETIME
if (image) {
SkDebugf("SkMask::FreeImage %d\n", --gCounter);
}
#endif
sk_free(image);
}
///////////////////////////////////////////////////////////////////////////////
static const int gMaskFormatToShift[] = {
~0, // BW -- not supported
0, // A8
0, // 3D
2, // ARGB32
1, // LCD16
};
static int maskFormatToShift(SkMask::Format format) {
SkASSERT((unsigned)format < SK_ARRAY_COUNT(gMaskFormatToShift));
SkASSERT(SkMask::kBW_Format != format);
return gMaskFormatToShift[format];
}
void* SkMask::getAddr(int x, int y) const {
SkASSERT(kBW_Format != fFormat);
SkASSERT(fBounds.contains(x, y));
SkASSERT(fImage);
char* addr = (char*)fImage;
addr += (y - fBounds.fTop) * fRowBytes;
addr += (x - fBounds.fLeft) << maskFormatToShift(fFormat);
return addr;
}
<commit_msg>Remove unsafe align4 call<commit_after>/*
* Copyright 2007 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkMask.h"
#include "SkMalloc.h"
//#define TRACK_SKMASK_LIFETIME
/** returns the product if it is positive and fits in 31 bits. Otherwise this
returns 0.
*/
static int32_t safeMul32(int32_t a, int32_t b) {
int64_t size = sk_64_mul(a, b);
if (size > 0 && sk_64_isS32(size)) {
return sk_64_asS32(size);
}
return 0;
}
size_t SkMask::computeImageSize() const {
return safeMul32(fBounds.height(), fRowBytes);
}
size_t SkMask::computeTotalImageSize() const {
size_t size = this->computeImageSize();
if (fFormat == SkMask::k3D_Format) {
size = safeMul32(SkToS32(size), 3);
}
return size;
}
#ifdef TRACK_SKMASK_LIFETIME
static int gCounter;
#endif
/** We explicitly use this allocator for SkBimap pixels, so that we can
freely assign memory allocated by one class to the other.
*/
uint8_t* SkMask::AllocImage(size_t size) {
#ifdef TRACK_SKMASK_LIFETIME
SkDebugf("SkMask::AllocImage %d\n", gCounter++);
#endif
size_t aligned_size = std::numeric_limits<size_t>::max();
// Expand size to next multiple of four.
size_t adjustment = 3;
if (size + adjustment > size) {
aligned_size = (size + adjustment) & ~adjustment;
}
return static_cast<uint8_t*>(sk_malloc_throw(aligned_size));
}
/** We explicitly use this allocator for SkBimap pixels, so that we can
freely assign memory allocated by one class to the other.
*/
void SkMask::FreeImage(void* image) {
#ifdef TRACK_SKMASK_LIFETIME
if (image) {
SkDebugf("SkMask::FreeImage %d\n", --gCounter);
}
#endif
sk_free(image);
}
///////////////////////////////////////////////////////////////////////////////
static const int gMaskFormatToShift[] = {
~0, // BW -- not supported
0, // A8
0, // 3D
2, // ARGB32
1, // LCD16
};
static int maskFormatToShift(SkMask::Format format) {
SkASSERT((unsigned)format < SK_ARRAY_COUNT(gMaskFormatToShift));
SkASSERT(SkMask::kBW_Format != format);
return gMaskFormatToShift[format];
}
void* SkMask::getAddr(int x, int y) const {
SkASSERT(kBW_Format != fFormat);
SkASSERT(fBounds.contains(x, y));
SkASSERT(fImage);
char* addr = (char*)fImage;
addr += (y - fBounds.fTop) * fRowBytes;
addr += (x - fBounds.fLeft) << maskFormatToShift(fFormat);
return addr;
}
<|endoftext|> |
<commit_before>b078675c-2e4e-11e5-9284-b827eb9e62be<commit_msg>b07d6d06-2e4e-11e5-9284-b827eb9e62be<commit_after>b07d6d06-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>db592840-2e4d-11e5-9284-b827eb9e62be<commit_msg>db5e3308-2e4d-11e5-9284-b827eb9e62be<commit_after>db5e3308-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>
/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkRect.h"
void SkIRect::join(int32_t left, int32_t top, int32_t right, int32_t bottom) {
// do nothing if the params are empty
if (left >= right || top >= bottom) {
return;
}
// if we are empty, just assign
if (fLeft >= fRight || fTop >= fBottom) {
this->set(left, top, right, bottom);
} else {
if (left < fLeft) fLeft = left;
if (top < fTop) fTop = top;
if (right > fRight) fRight = right;
if (bottom > fBottom) fBottom = bottom;
}
}
void SkIRect::sort() {
if (fLeft > fRight) {
SkTSwap<int32_t>(fLeft, fRight);
}
if (fTop > fBottom) {
SkTSwap<int32_t>(fTop, fBottom);
}
}
/////////////////////////////////////////////////////////////////////////////
void SkRect::sort() {
if (fLeft > fRight) {
SkTSwap<SkScalar>(fLeft, fRight);
}
if (fTop > fBottom) {
SkTSwap<SkScalar>(fTop, fBottom);
}
}
void SkRect::toQuad(SkPoint quad[4]) const {
SkASSERT(quad);
quad[0].set(fLeft, fTop);
quad[1].set(fRight, fTop);
quad[2].set(fRight, fBottom);
quad[3].set(fLeft, fBottom);
}
#ifdef SK_SCALAR_IS_FLOAT
#define SkFLOATCODE(code) code
#else
#define SkFLOATCODE(code)
#endif
void SkRect::set(const SkPoint pts[], int count) {
SkASSERT((pts && count > 0) || count == 0);
if (count <= 0) {
sk_bzero(this, sizeof(SkRect));
} else {
#ifdef SK_SCALAR_SLOW_COMPARES
int32_t l, t, r, b;
l = r = SkScalarAs2sCompliment(pts[0].fX);
t = b = SkScalarAs2sCompliment(pts[0].fY);
for (int i = 1; i < count; i++) {
int32_t x = SkScalarAs2sCompliment(pts[i].fX);
int32_t y = SkScalarAs2sCompliment(pts[i].fY);
if (x < l) l = x; else if (x > r) r = x;
if (y < t) t = y; else if (y > b) b = y;
}
this->set(Sk2sComplimentAsScalar(l),
Sk2sComplimentAsScalar(t),
Sk2sComplimentAsScalar(r),
Sk2sComplimentAsScalar(b));
#else
SkScalar l, t, r, b;
SkFLOATCODE(int isNaN;)
l = r = pts[0].fX;
t = b = pts[0].fY;
// If all of the points are finite, accum should stay 0. If we encounter
// a NaN or infinity, then accum should become NaN.
SkFLOATCODE(float accum = 0;)
for (int i = 1; i < count; i++) {
SkScalar x = pts[i].fX;
SkScalar y = pts[i].fY;
SkFLOATCODE(accum *= x; accum *= y;)
if (x < l) l = x; else if (x > r) r = x;
if (y < t) t = y; else if (y > b) b = y;
}
#ifdef SK_SCALAR_IS_FLOAT
SkASSERT(!accum || !SkScalarIsFinite(accum));
if (accum) {
l = t = r = b = 0;
}
#endif
this->set(l, t, r, b);
#endif
}
}
bool SkRect::intersect(SkScalar left, SkScalar top, SkScalar right,
SkScalar bottom) {
if (left < right && top < bottom && !this->isEmpty() && // check for empties
fLeft < right && left < fRight && fTop < bottom && top < fBottom)
{
if (fLeft < left) fLeft = left;
if (fTop < top) fTop = top;
if (fRight > right) fRight = right;
if (fBottom > bottom) fBottom = bottom;
return true;
}
return false;
}
bool SkRect::intersect(const SkRect& r) {
SkASSERT(&r);
return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom);
}
bool SkRect::intersect(const SkRect& a, const SkRect& b) {
SkASSERT(&a && &b);
if (!a.isEmpty() && !b.isEmpty() &&
a.fLeft < b.fRight && b.fLeft < a.fRight &&
a.fTop < b.fBottom && b.fTop < a.fBottom) {
fLeft = SkMaxScalar(a.fLeft, b.fLeft);
fTop = SkMaxScalar(a.fTop, b.fTop);
fRight = SkMinScalar(a.fRight, b.fRight);
fBottom = SkMinScalar(a.fBottom, b.fBottom);
return true;
}
return false;
}
void SkRect::join(SkScalar left, SkScalar top, SkScalar right,
SkScalar bottom) {
// do nothing if the params are empty
if (left >= right || top >= bottom) {
return;
}
// if we are empty, just assign
if (fLeft >= fRight || fTop >= fBottom) {
this->set(left, top, right, bottom);
} else {
if (left < fLeft) fLeft = left;
if (top < fTop) fTop = top;
if (right > fRight) fRight = right;
if (bottom > fBottom) fBottom = bottom;
}
}
<commit_msg>for scalar==float, tis faster to always to MIN and MAX, than to put an ELSE betwixt them.<commit_after>
/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkRect.h"
void SkIRect::join(int32_t left, int32_t top, int32_t right, int32_t bottom) {
// do nothing if the params are empty
if (left >= right || top >= bottom) {
return;
}
// if we are empty, just assign
if (fLeft >= fRight || fTop >= fBottom) {
this->set(left, top, right, bottom);
} else {
if (left < fLeft) fLeft = left;
if (top < fTop) fTop = top;
if (right > fRight) fRight = right;
if (bottom > fBottom) fBottom = bottom;
}
}
void SkIRect::sort() {
if (fLeft > fRight) {
SkTSwap<int32_t>(fLeft, fRight);
}
if (fTop > fBottom) {
SkTSwap<int32_t>(fTop, fBottom);
}
}
/////////////////////////////////////////////////////////////////////////////
void SkRect::sort() {
if (fLeft > fRight) {
SkTSwap<SkScalar>(fLeft, fRight);
}
if (fTop > fBottom) {
SkTSwap<SkScalar>(fTop, fBottom);
}
}
void SkRect::toQuad(SkPoint quad[4]) const {
SkASSERT(quad);
quad[0].set(fLeft, fTop);
quad[1].set(fRight, fTop);
quad[2].set(fRight, fBottom);
quad[3].set(fLeft, fBottom);
}
#ifdef SK_SCALAR_IS_FLOAT
#define SkFLOATCODE(code) code
#else
#define SkFLOATCODE(code)
#endif
// For float compares (at least on x86, by removing the else from the min/max
// computation, we get MAXSS and MINSS instructions, and no branches.
// Fixed point has no such opportunity (afaik), so we leave the else in that case
#ifdef SK_SCALAR_IS_FLOAT
#define MINMAX_ELSE
#else
#define MINMAX_ELSE else
#endif
void SkRect::set(const SkPoint pts[], int count) {
SkASSERT((pts && count > 0) || count == 0);
if (count <= 0) {
sk_bzero(this, sizeof(SkRect));
} else {
#ifdef SK_SCALAR_SLOW_COMPARES
int32_t l, t, r, b;
l = r = SkScalarAs2sCompliment(pts[0].fX);
t = b = SkScalarAs2sCompliment(pts[0].fY);
for (int i = 1; i < count; i++) {
int32_t x = SkScalarAs2sCompliment(pts[i].fX);
int32_t y = SkScalarAs2sCompliment(pts[i].fY);
if (x < l) l = x; else if (x > r) r = x;
if (y < t) t = y; else if (y > b) b = y;
}
this->set(Sk2sComplimentAsScalar(l),
Sk2sComplimentAsScalar(t),
Sk2sComplimentAsScalar(r),
Sk2sComplimentAsScalar(b));
#else
SkScalar l, t, r, b;
SkFLOATCODE(int isNaN;)
l = r = pts[0].fX;
t = b = pts[0].fY;
// If all of the points are finite, accum should stay 0. If we encounter
// a NaN or infinity, then accum should become NaN.
SkFLOATCODE(float accum = 0;)
SkFLOATCODE(accum *= l; accum *= t;)
for (int i = 1; i < count; i++) {
SkScalar x = pts[i].fX;
SkScalar y = pts[i].fY;
SkFLOATCODE(accum *= x; accum *= y;)
if (x < l) l = x; MINMAX_ELSE if (x > r) r = x;
if (y < t) t = y; MINMAX_ELSE if (y > b) b = y;
}
#ifdef SK_SCALAR_IS_FLOAT
SkASSERT(!accum || !SkScalarIsFinite(accum));
if (accum) {
l = t = r = b = 0;
}
#endif
this->set(l, t, r, b);
#endif
}
}
bool SkRect::intersect(SkScalar left, SkScalar top, SkScalar right,
SkScalar bottom) {
if (left < right && top < bottom && !this->isEmpty() && // check for empties
fLeft < right && left < fRight && fTop < bottom && top < fBottom)
{
if (fLeft < left) fLeft = left;
if (fTop < top) fTop = top;
if (fRight > right) fRight = right;
if (fBottom > bottom) fBottom = bottom;
return true;
}
return false;
}
bool SkRect::intersect(const SkRect& r) {
SkASSERT(&r);
return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom);
}
bool SkRect::intersect(const SkRect& a, const SkRect& b) {
SkASSERT(&a && &b);
if (!a.isEmpty() && !b.isEmpty() &&
a.fLeft < b.fRight && b.fLeft < a.fRight &&
a.fTop < b.fBottom && b.fTop < a.fBottom) {
fLeft = SkMaxScalar(a.fLeft, b.fLeft);
fTop = SkMaxScalar(a.fTop, b.fTop);
fRight = SkMinScalar(a.fRight, b.fRight);
fBottom = SkMinScalar(a.fBottom, b.fBottom);
return true;
}
return false;
}
void SkRect::join(SkScalar left, SkScalar top, SkScalar right,
SkScalar bottom) {
// do nothing if the params are empty
if (left >= right || top >= bottom) {
return;
}
// if we are empty, just assign
if (fLeft >= fRight || fTop >= fBottom) {
this->set(left, top, right, bottom);
} else {
if (left < fLeft) fLeft = left;
if (top < fTop) fTop = top;
if (right > fRight) fRight = right;
if (bottom > fBottom) fBottom = bottom;
}
}
<|endoftext|> |
<commit_before>0c3cc6aa-2e4f-11e5-9284-b827eb9e62be<commit_msg>0c41b78c-2e4f-11e5-9284-b827eb9e62be<commit_after>0c41b78c-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>22d46cd4-2e4e-11e5-9284-b827eb9e62be<commit_msg>22d97738-2e4e-11e5-9284-b827eb9e62be<commit_after>22d97738-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>1f644dbc-2e4e-11e5-9284-b827eb9e62be<commit_msg>1f6956e0-2e4e-11e5-9284-b827eb9e62be<commit_after>1f6956e0-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>723242a6-2e4e-11e5-9284-b827eb9e62be<commit_msg>723773c0-2e4e-11e5-9284-b827eb9e62be<commit_after>723773c0-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>306da882-2e4f-11e5-9284-b827eb9e62be<commit_msg>3072ae54-2e4f-11e5-9284-b827eb9e62be<commit_after>3072ae54-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>a1650b7c-2e4d-11e5-9284-b827eb9e62be<commit_msg>a16a04a6-2e4d-11e5-9284-b827eb9e62be<commit_after>a16a04a6-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>a84f4a82-2e4e-11e5-9284-b827eb9e62be<commit_msg>a85457f2-2e4e-11e5-9284-b827eb9e62be<commit_after>a85457f2-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>186f6da8-2e4d-11e5-9284-b827eb9e62be<commit_msg>18746ee8-2e4d-11e5-9284-b827eb9e62be<commit_after>18746ee8-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>51b4c176-2e4d-11e5-9284-b827eb9e62be<commit_msg>51b9ced2-2e4d-11e5-9284-b827eb9e62be<commit_after>51b9ced2-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>f952cf86-2e4d-11e5-9284-b827eb9e62be<commit_msg>f957e3d6-2e4d-11e5-9284-b827eb9e62be<commit_after>f957e3d6-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>c9055368-2e4c-11e5-9284-b827eb9e62be<commit_msg>c90a52c8-2e4c-11e5-9284-b827eb9e62be<commit_after>c90a52c8-2e4c-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>String::String(String&& o)
: Object(static_cast<Object&&>(o))
, m_Str(o.m_Str)
{
o.m_Str = 0;
}
String::String(const char* str) : ::java::lang::Object(str ? jni::NewStringUTF(str) : NULL) { __Initialize(); }
String::~String()
{
if (m_Str)
jni::ReleaseStringUTFChars(*this, m_Str);
m_Str = 0;
}
void String::__Initialize()
{
m_Str = 0;
}
String::operator jstring () const
{
return (jstring)(jobject)m_Object;
}
String& String::operator = (const String& other)
{
if (m_Object == other.m_Object)
return *this;
if (m_Str)
jni::ReleaseStringUTFChars(*this, m_Str);
m_Str = 0;
m_Object = other.m_Object;
return *this;
}
String& String::operator = (String&& other)
{
if (m_Str)
jni::ReleaseStringUTFChars(*this, m_Str);
m_Str = other.m_Str;
other.m_Str = 0;
m_Object = static_cast<jni::Ref<jni::GlobalRefAllocator, jobject>&&>(other.m_Object);
return *this;
}
const char* String::c_str()
{
if (m_Object && !m_Str)
m_Str = jni::GetStringUTFChars(*this);
return m_Str;
}
bool String::EmptyOrNull()
{
if (!m_Object)
return true;
const char* str = c_str();
return !str || !str[0];
}
<commit_msg>Guard against self assignment<commit_after>String::String(String&& o)
: Object(static_cast<Object&&>(o))
, m_Str(o.m_Str)
{
o.m_Str = 0;
}
String::String(const char* str) : ::java::lang::Object(str ? jni::NewStringUTF(str) : NULL) { __Initialize(); }
String::~String()
{
if (m_Str)
jni::ReleaseStringUTFChars(*this, m_Str);
m_Str = 0;
}
void String::__Initialize()
{
m_Str = 0;
}
String::operator jstring () const
{
return (jstring)(jobject)m_Object;
}
String& String::operator = (const String& other)
{
if (m_Object == other.m_Object)
return *this;
if (m_Str)
jni::ReleaseStringUTFChars(*this, m_Str);
m_Str = 0;
m_Object = other.m_Object;
return *this;
}
String& String::operator = (String&& other)
{
if (&other == this)
return *this;
if (m_Str)
jni::ReleaseStringUTFChars(*this, m_Str);
m_Str = other.m_Str;
other.m_Str = 0;
m_Object = static_cast<jni::Ref<jni::GlobalRefAllocator, jobject>&&>(other.m_Object);
return *this;
}
const char* String::c_str()
{
if (m_Object && !m_Str)
m_Str = jni::GetStringUTFChars(*this);
return m_Str;
}
bool String::EmptyOrNull()
{
if (!m_Object)
return true;
const char* str = c_str();
return !str || !str[0];
}
<|endoftext|> |
<commit_before>///
/// @file cmdoptions.cpp
/// @brief Parse command-line options for the primesieve console
/// (terminal) application.
///
/// Copyright (C) 2013 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///
#include "cmdoptions.h"
#include "ExpressionParser.h"
#include "../../soe/PrimeSieve.h"
#include <string>
#include <map>
#include <exception>
#include <cstdlib>
#include <cstddef>
#include <stdint.h>
void help();
void version();
bool test_ParallelPrimeSieve();
using namespace std;
namespace {
/// e.g. id = "--threads", value = "4"
struct Option {
string id;
string value;
template <typename T>
T getValue() const
{
ExpressionParser<T> parser;
T result = parser.eval(value);
return result;
}
};
enum OptionValues {
OPTION_COUNT,
OPTION_HELP,
OPTION_NTHPRIME,
OPTION_NUMBER,
OPTION_OFFSET,
OPTION_PRINT,
OPTION_QUIET,
OPTION_SIZE,
OPTION_TEST,
OPTION_THREADS,
OPTION_VERSION
};
/// Command-line options
map<string, OptionValues> optionMap;
void initOptionMap()
{
optionMap["-c"] = OPTION_COUNT;
optionMap["--count"] = OPTION_COUNT;
optionMap["-h"] = OPTION_HELP;
optionMap["--help"] = OPTION_HELP;
optionMap["-n"] = OPTION_NTHPRIME;
optionMap["--nthprime"] = OPTION_NTHPRIME;
optionMap["--number"] = OPTION_NUMBER;
optionMap["-o"] = OPTION_OFFSET;
optionMap["--offset"] = OPTION_OFFSET;
optionMap["-p"] = OPTION_PRINT;
optionMap["--print"] = OPTION_PRINT;
optionMap["-q"] = OPTION_QUIET;
optionMap["--quiet"] = OPTION_QUIET;
optionMap["-s"] = OPTION_SIZE;
optionMap["--size"] = OPTION_SIZE;
optionMap["--test"] = OPTION_TEST;
optionMap["-t"] = OPTION_THREADS;
optionMap["--threads"] = OPTION_THREADS;
optionMap["-v"] = OPTION_VERSION;
optionMap["--version"] = OPTION_VERSION;
}
void test()
{
bool ok = test_ParallelPrimeSieve();
exit(ok ? 0 : 1);
}
int check(int primeType)
{
primeType--;
if (primeType < 0 || primeType > 6)
help();
return primeType;
}
int getCountFlags(int val)
{
int flags = 0;
do {
flags |= PrimeSieve::COUNT_PRIMES << check(val % 10);
val /= 10;
} while (val > 0);
return flags;
}
int getPrintFlags(int val)
{
return PrimeSieve::PRINT_PRIMES << check(val);
}
/// e.g. "--threads=8" -> { id = "--threads", value = "8" }
Option makeOption(const string& str)
{
Option option;
size_t delimiter = str.find_first_of("=0123456789");
if (delimiter == string::npos) {
option.id = str;
} else {
option.id = str.substr(0, delimiter);
option.value = str.substr(delimiter + (str.at(delimiter) == '=' ? 1 : 0));
}
if (option.id.empty() && !option.value.empty())
option.id = "--number";
if (optionMap.count(option.id) == 0)
option.id = "--help";
return option;
}
} // end namespace
PrimeSieveOptions parseOptions(int argc, char** argv)
{
// skip program name in argv[0]
argc--; argv++;
PrimeSieveOptions pso;
initOptionMap();
try {
for (int i = 0; i < argc; i++) {
Option option = makeOption(argv[i]);
switch (optionMap[option.id]) {
case OPTION_COUNT: pso.flags |= getCountFlags(option.getValue<int>()); break;
case OPTION_PRINT: pso.flags |= getPrintFlags(option.getValue<int>()); pso.quiet = true; break;
case OPTION_SIZE: pso.sieveSize = option.getValue<int>(); break;
case OPTION_THREADS: pso.threads = option.getValue<int>(); break;
case OPTION_QUIET: pso.quiet = true; break;
case OPTION_NTHPRIME: pso.nthPrime = true; break;
case OPTION_NUMBER: pso.n.push_back(option.getValue<uint64_t>()); break;
case OPTION_OFFSET: pso.n.push_back(option.getValue<uint64_t>() + pso.n.front()); break;
case OPTION_TEST: test(); break;
case OPTION_VERSION: version(); break;
case OPTION_HELP: help(); break;
}
}
} catch (exception&) {
help();
}
if (pso.n.size() == 1) pso.n.push_front(0);
if (pso.n.size() != 2) help();
return pso;
}
<commit_msg>Refactoring<commit_after>///
/// @file cmdoptions.cpp
/// @brief Parse command-line options for the primesieve console
/// (terminal) application.
///
/// Copyright (C) 2013 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///
#include "cmdoptions.h"
#include "ExpressionParser.h"
#include "../../soe/PrimeSieve.h"
#include <string>
#include <map>
#include <exception>
#include <cstdlib>
#include <cstddef>
#include <stdint.h>
void help();
void version();
bool test_ParallelPrimeSieve();
using namespace std;
namespace {
/// e.g. id = "--threads", value = "4"
struct Option {
string id;
string value;
template <typename T>
T getValue() const
{
ExpressionParser<T> parser;
T result = parser.eval(value);
return result;
}
};
enum OptionValues {
OPTION_COUNT,
OPTION_HELP,
OPTION_NTHPRIME,
OPTION_NUMBER,
OPTION_OFFSET,
OPTION_PRINT,
OPTION_QUIET,
OPTION_SIZE,
OPTION_TEST,
OPTION_THREADS,
OPTION_VERSION
};
/// Command-line options
map<string, OptionValues> optionMap;
void initOptionMap()
{
optionMap["-c"] = OPTION_COUNT;
optionMap["--count"] = OPTION_COUNT;
optionMap["-h"] = OPTION_HELP;
optionMap["--help"] = OPTION_HELP;
optionMap["-n"] = OPTION_NTHPRIME;
optionMap["--nthprime"] = OPTION_NTHPRIME;
optionMap["--number"] = OPTION_NUMBER;
optionMap["-o"] = OPTION_OFFSET;
optionMap["--offset"] = OPTION_OFFSET;
optionMap["-p"] = OPTION_PRINT;
optionMap["--print"] = OPTION_PRINT;
optionMap["-q"] = OPTION_QUIET;
optionMap["--quiet"] = OPTION_QUIET;
optionMap["-s"] = OPTION_SIZE;
optionMap["--size"] = OPTION_SIZE;
optionMap["--test"] = OPTION_TEST;
optionMap["-t"] = OPTION_THREADS;
optionMap["--threads"] = OPTION_THREADS;
optionMap["-v"] = OPTION_VERSION;
optionMap["--version"] = OPTION_VERSION;
}
void test()
{
bool ok = test_ParallelPrimeSieve();
exit(ok ? 0 : 1);
}
int check(int primeType)
{
primeType--;
if (primeType < 0 || primeType > 6)
help();
return primeType;
}
int getCountFlags(int val)
{
int flags = 0;
do {
flags |= PrimeSieve::COUNT_PRIMES << check(val % 10);
val /= 10;
} while (val > 0);
return flags;
}
int getPrintFlags(int val)
{
return PrimeSieve::PRINT_PRIMES << check(val);
}
/// e.g. "--threads=8" -> { id = "--threads", value = "8" }
Option makeOption(const string& str)
{
Option option;
size_t delimiter = str.find_first_of("=0123456789");
if (delimiter == string::npos)
option.id = str;
else
{
option.id = str.substr(0, delimiter);
option.value = str.substr(delimiter + (str.at(delimiter) == '=' ? 1 : 0));
}
if (option.id.empty() && !option.value.empty())
option.id = "--number";
if (optionMap.count(option.id) == 0)
option.id = "--help";
return option;
}
} // end namespace
PrimeSieveOptions parseOptions(int argc, char** argv)
{
PrimeSieveOptions pso;
initOptionMap();
try
{
for (int i = 1; i < argc; i++)
{
Option option = makeOption(argv[i]);
switch (optionMap[option.id])
{
case OPTION_COUNT: pso.flags |= getCountFlags(option.getValue<int>()); break;
case OPTION_PRINT: pso.flags |= getPrintFlags(option.getValue<int>()); pso.quiet = true; break;
case OPTION_SIZE: pso.sieveSize = option.getValue<int>(); break;
case OPTION_THREADS: pso.threads = option.getValue<int>(); break;
case OPTION_QUIET: pso.quiet = true; break;
case OPTION_NTHPRIME: pso.nthPrime = true; break;
case OPTION_NUMBER: pso.n.push_back(option.getValue<uint64_t>()); break;
case OPTION_OFFSET: pso.n.push_back(option.getValue<uint64_t>() + pso.n.front()); break;
case OPTION_TEST: test(); break;
case OPTION_VERSION: version(); break;
case OPTION_HELP: help(); break;
}
}
}
catch (exception&)
{
help();
}
if (pso.n.size() == 1) pso.n.push_front(0);
if (pso.n.size() != 2) help();
return pso;
}
<|endoftext|> |
<commit_before>e67fc724-2e4d-11e5-9284-b827eb9e62be<commit_msg>e688c75c-2e4d-11e5-9284-b827eb9e62be<commit_after>e688c75c-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>2bfba330-2e4f-11e5-9284-b827eb9e62be<commit_msg>2c009520-2e4f-11e5-9284-b827eb9e62be<commit_after>2c009520-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>530f0350-2e4e-11e5-9284-b827eb9e62be<commit_msg>53141be2-2e4e-11e5-9284-b827eb9e62be<commit_after>53141be2-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>ba83b41e-2e4d-11e5-9284-b827eb9e62be<commit_msg>ba88a820-2e4d-11e5-9284-b827eb9e62be<commit_after>ba88a820-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>fac6df82-2e4e-11e5-9284-b827eb9e62be<commit_msg>facbf012-2e4e-11e5-9284-b827eb9e62be<commit_after>facbf012-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>31fc6738-2e4f-11e5-9284-b827eb9e62be<commit_msg>32015cb6-2e4f-11e5-9284-b827eb9e62be<commit_after>32015cb6-2e4f-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>aef2e5f8-2e4c-11e5-9284-b827eb9e62be<commit_msg>aef7d978-2e4c-11e5-9284-b827eb9e62be<commit_after>aef7d978-2e4c-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>41c7bb06-2e4d-11e5-9284-b827eb9e62be<commit_msg>41ccc4ac-2e4d-11e5-9284-b827eb9e62be<commit_after>41ccc4ac-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>eb428c0a-2e4e-11e5-9284-b827eb9e62be<commit_msg>eb47b5a4-2e4e-11e5-9284-b827eb9e62be<commit_after>eb47b5a4-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>9db1ce20-2e4d-11e5-9284-b827eb9e62be<commit_msg>9db719b6-2e4d-11e5-9284-b827eb9e62be<commit_after>9db719b6-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>e476ee58-2e4d-11e5-9284-b827eb9e62be<commit_msg>e47bfb6e-2e4d-11e5-9284-b827eb9e62be<commit_after>e47bfb6e-2e4d-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>642d3b8e-2e4e-11e5-9284-b827eb9e62be<commit_msg>64324a84-2e4e-11e5-9284-b827eb9e62be<commit_after>64324a84-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>ab8a5912-2e4e-11e5-9284-b827eb9e62be<commit_msg>ab8f4cce-2e4e-11e5-9284-b827eb9e62be<commit_after>ab8f4cce-2e4e-11e5-9284-b827eb9e62be<|endoftext|> |
<commit_before>fb31e7e8-2e4c-11e5-9284-b827eb9e62be<commit_msg>fb36ee28-2e4c-11e5-9284-b827eb9e62be<commit_after>fb36ee28-2e4c-11e5-9284-b827eb9e62be<|endoftext|> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.