Пример #1
0
 const std::string& lookup(const Container& container, const pair<iterator>& tag) const {
     auto result = lower_bound(container.cbegin(), container.cend(), tag);
     if (result == container.cend())
         return empty;
     return result->first == tag ? result->second : empty;
 }
Пример #2
0
static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
			    struct dm_btree_value_type *vt,
			    uint64_t key, unsigned *index)
{
	int r, i = *index, top = 1;
	struct btree_node *node;

	for (;;) {
		r = shadow_step(s, root, vt);
		if (r < 0)
			return r;

		node = dm_block_data(shadow_current(s));

		/*
		 * We have to patch up the parent node, ugly, but I don't
		 * see a way to do this automatically as part of the spine
		 * op.
		 */
		if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
			__le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));

			__dm_bless_for_disk(&location);
			memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i),
				    &location, sizeof(__le64));
		}

		node = dm_block_data(shadow_current(s));

		if (node->header.nr_entries == node->header.max_entries) {
			if (top)
				r = btree_split_beneath(s, key);
			else
				r = btree_split_sibling(s, root, i, key);

			if (r < 0)
				return r;
		}

		node = dm_block_data(shadow_current(s));

		i = lower_bound(node, key);

		if (le32_to_cpu(node->header.flags) & LEAF_NODE)
			break;

		if (i < 0) {
			/* change the bounds on the lowest key */
			node->keys[0] = cpu_to_le64(key);
			i = 0;
		}

		root = value64(node, i);
		top = 0;
	}

	if (i < 0 || le64_to_cpu(node->keys[i]) != key)
		i++;

	*index = i;
	return 0;
}
Пример #3
0
	void Resources::loadXML(const string &xml_name, 
		LoadResourcesContext *load_context, 
		bool load_completely, bool use_load_counter, 
		const string &prebuilt_folder_)
	{


		_name = xml_name;
		_loadCounter = load_completely ? 1 : 0;


		FS_LOG("step0");
		file::buffer fb;
		file::read(xml_name.c_str(), fb);

		FS_LOG("step1");
		

        updateName(xml_name);

        char destHead[255];
        char destTail[255];
        path::split(xml_name.c_str(), destHead, destTail);

        string prebuilt_folder = prebuilt_folder_ + "/" + destTail + ".ox/";
		if (prebuilt_folder[0] == '/')
		{
			prebuilt_folder.erase(prebuilt_folder.begin());
		}

		file::buffer fb_meta;
		pugi::xml_document doc_meta;
		string ox = prebuilt_folder + "meta.xml";
		const char *ox_file = ox.c_str();

		
		FS_LOG("step2");

		if (file::exists(ox_file))
		{
			file::read(ox_file, fb_meta, ep_ignore_error);
			if (fb_meta.getSize())
				doc_meta.load_buffer_inplace(&fb_meta.data[0], fb_meta.data.size());
		}

		if (!fb.data.size())
		{
			OX_ASSERT(fb.data.size()  && "can't find xml file");
			return;
		}

		pugi::xml_document *doc = new pugi::xml_document();
		_docs.push_back(doc);

		bool loaded = doc->load_buffer(&fb.data[0], fb.data.size());
		OX_ASSERT(loaded);

		pugi::xml_node resources = doc->first_child();
		pugi::xml_node resources_meta = doc_meta.first_child();


		int i = 0;

		string id;
		//string file;
		string rect_str;

		FS_LOG("loading xml resources");

		XmlWalker walker(destHead, 1.0f, load_completely, resources, resources_meta);

		while(true)
		{
			CreateResourceContext context;
			context.walker = walker.next();
			if (context.walker.empty())
				break;


			const char *type = context.walker.getType();

			registeredResources::iterator i = lower_bound(_registeredResources.begin(), _registeredResources.end(), type);
			if (i == _registeredResources.end() || strcmp(i->id, type))
			{
				log::error("unknown resource. type: '%s' id: '%s'", type, Resource::extractID(context.walker.getNode(), "", "").c_str());
				OX_ASSERT(!"unknown resource type");
				continue;
			}

			registeredResource &r = *i;

			
			context.xml_name = &xml_name;
			context.resources = this;

			string prebuilt_xml_folder = prebuilt_folder + "/" + type + "/";
			context.prebuilt_folder = &prebuilt_xml_folder;


			FS_LOG("resource: %s ", name);
			Resource *res = r.cb(context);
			OX_ASSERT(res);
			res->setUseLoadCounter(use_load_counter);

			if (res)
			{	
				bool load = context.walker.getLoad();

				//res-> = child;
				if (load)
					res->load(load_context);
                res->setParent(this);
				_resources.push_back(res);
				_owned.push_back(res);
			}
		}

		sort(_fastAccessResources.begin(), _fastAccessResources.end(), ObjectBasePredicate());
		FS_LOG("xml loaded");
	}
Пример #4
0
	cell find(const index_type &index) {
	    index_type base;
	    size_type size;
	    lower_bound(index, base, size);
	    return cell(origin_ + project(base), base, size);
	}
Пример #5
0
 ll query(ll x) { // Laufzeit: O(log(n)) 
     auto l = *lower_bound((Line) {x, LLONG_MIN});
     return l.m * x + l.b;
 }
Пример #6
0
void TestFunctors (void)
{
    vector<int> v;
    v.resize (20);
    fill (v, 2);
    foreach (vector<int>::iterator, i, v)
        *i -= distance(v.begin(), i) & 1;
    vector<int> v1 (v);

    cout << "start:\t\t\t";
    PrintVector (v);

    v = v1;
    cout << "plus:\t\t\t";
    transform (v, v.begin(), v.begin(), plus<int>());
    PrintVector (v);

    v = v1;
    cout << "minus:\t\t\t";
    transform (v, v.begin(), v.begin(), minus<int>());
    PrintVector (v);

    v = v1;
    cout << "divides:\t\t";
    transform (v, v.begin(), v.begin(), divides<int>());
    PrintVector (v);

    v = v1;
    cout << "multiplies:\t\t";
    transform (v, v.begin(), v.begin(), multiplies<int>());
    PrintVector (v);

    v = v1;
    cout << "modulus:\t\t";
    transform (v, v.begin(), v.begin(), modulus<int>());
    PrintVector (v);

    v = v1;
    cout << "logical_and:\t\t";
    transform (v, v.begin(), v.begin(), logical_and<int>());
    PrintVector (v);

    v = v1;
    cout << "logical_or:\t\t";
    transform (v, v.begin(), v.begin(), logical_or<int>());
    PrintVector (v);

    v = v1;
    cout << "equal_to:\t\t";
    transform (v, v.begin(), v.begin(), equal_to<int>());
    PrintVector (v);

    v = v1;
    cout << "not_equal_to:\t\t";
    transform (v, v.begin(), v.begin(), not_equal_to<int>());
    PrintVector (v);

    v = v1;
    cout << "greater:\t\t";
    transform (v, v.begin(), v.begin(), greater<int>());
    PrintVector (v);

    v = v1;
    cout << "less:\t\t\t";
    transform (v, v.begin(), v.begin(), less<int>());
    PrintVector (v);

    v = v1;
    cout << "greater_equal:\t\t";
    transform (v, v.begin(), v.begin(), greater_equal<int>());
    PrintVector (v);

    v = v1;
    cout << "less_equal:\t\t";
    transform (v, v.begin(), v.begin(), less_equal<int>());
    PrintVector (v);

    v = v1;
    cout << "compare:\t\t";
    transform (v, v.begin(), v.begin(), compare<int>());
    PrintVector (v);

    v = v1;
    cout << "negate:\t\t\t";
    transform (v, negate<int>());
    PrintVector (v);

    v = v1;
    cout << "logical_not:\t\t";
    transform (v, logical_not<int>());
    PrintVector (v);

    v = v1;
    cout << "unary_neg(negate):\t";
    transform (v, unary_negator(negate<int>()));
    PrintVector (v);

    v = v1;
    cout << "binder1st(plus,5):\t";
    transform (v, bind1st(plus<int>(), 5));
    PrintVector (v);

    v = v1;
    cout << "binder2nd(minus,1):\t";
    transform (v, bind2nd(minus<int>(), 1));
    PrintVector (v);

    v = v1;
    cout << "compose1(-,+5):\t\t";
    transform (v, compose1 (negate<int>(), bind2nd(plus<int>(), 5)));
    PrintVector (v);

    v = v1;
    cout << "compose1(-,-4):\t\t";
    transform (v, compose1 (negate<int>(), bind2nd(minus<int>(), 4)));
    PrintVector (v);

    v = v1;
    cout << "compose2(/,+6,-4):\t";
    transform (v, compose2 (divides<int>(), bind2nd(plus<int>(), 6), bind2nd(minus<int>(), 4)));
    PrintVector (v);

    cout << "mem_var(plus,6):\t";
    vector<A> av;
    for (uoff_t i = 0; i < 20; ++ i)
        av.push_back (A(i));
    transform (av, mem_var1(&A::m_v, bind2nd(plus<int>(), 6)));
    PrintVector (av);

    vector<A>::iterator found = find_if (av, mem_var_equal_to(&A::m_v, 14));
    cout << "14 found at position " << found - av.begin() << endl;
    found = lower_bound (av.begin(), av.end(), 18, mem_var_less(&A::m_v));
    cout << "18 found at position " << found - av.begin() << endl;

    cout << "add next:\t\t";
    transform (av.begin(), av.end() - 1, av.begin() + 1, av.begin(), mem_var2(&A::m_v, plus<int>()));
    PrintVector (av);
}
Пример #7
0
int main(){
  scanf("%lld %d", &n, &q);
  for (int i = 0; i < q; i++)
    scanf("%d %lld", &seq[i].second, &seq[i].first);
  qsort(seq, q, sizeof(pair), cmpP);
  int fir = 0;
  while (seq[fir].second == 0)
    fir++;
  int lst = q - 1;
  while (seq[lst].second == 0)
    lst--;
  ll cur = 0;
  ll obt = -1;
  for (int i = 0; i < q; i++){
    if(seq[i].second == 0)
      mem[i] = obt == -1? cur: gcd(cur, seq[i].first - obt);
    else{
      if (obt != -1)
        cur = gcd(cur, seq[i].first - obt);
      obt = seq[i].first;
    }
  }
  for(ll i = 1; i * i <= cur; i++){
    if (cur % i == 0){
      dvs.ptr = pushback(dvs.ptr, &dvs.sz, i);
      if(i*i < cur)
        dvs.ptr = pushback(dvs.ptr, &dvs.sz, cur/i);
    }
  }
  qsort(dvs.ptr, dvs.sz, sizeof(ll), cmpLL);
  for(int i = 0; i < dvs.sz; i++){
    mn[i] = 1;
    mx[i] = n;
  }
  cur = 0;
  obt = -1;
  for(int i = q - 1; i >= 0; i--){
    if(seq[i].second == 0){
      ll newmem = obt == -1? cur: gcd(cur, obt - seq[i].first);
      ll nd = gcd(mem[i], newmem);
      int ind = lower_bound(dvs.ptr, dvs.sz, nd);
      if (ind < dvs.sz && dvs.ptr[ind] == nd){
        if (i < fir)
          mn[ind] = max(mn[ind], seq[i].first + 1);
        else if (i < lst) { 
          mn[ind] = n + 1, 
          mx[ind] = 0;
        }
        else 
          mx[ind] = min(mx[ind], seq[i].first - 1);
      }
    }
    else {
      if (obt != -1)
        cur = gcd(cur, obt - seq[i].first);
      obt = seq[i].first;
    }
  }
  for (int i = dvs.sz - 1; i >= 0; i--){
    for (int j = i + 1; j < dvs.sz; j++){
      if(dvs.ptr[j] % dvs.ptr[i] == 0){
        mn[i] = max(mn[i], mn[j]);
        mx[i] = min(mx[i], mx[j]);
      }
    }
    ll a = mn[i] <= seq[fir].first? 1 + (seq[fir].first - mn[i]) / dvs.ptr[i]: 0;
    ll b = seq[lst].first <= mx[i]? 1 + (mx[i] - seq[lst].first) / dvs.ptr[i]: 0;
    a %= mod;
    b %= mod;
    res = (res + a * b) % mod;
  }
  printf("%d\n", res);
  return 0;
}