text:
static void  insert_something( struct mulle_concurrent_hashmap *map)
{
   intptr_t   hash;
   void       *value;
   int        rval;

   hash  = rand() << 1;  // no uneven ids
   value = (void *) (hash * 10);
   rval  = _mulle_concurrent_hashmap_insert( map, hash, value);

   switch( rval)
   {
   default :
      perror( "mulle_concurrent_hashmap_insert");
      abort();

   case 0 :
   case EEXIST :
      return;
   }
}

static void  delete_something( struct mulle_concurrent_hashmap *map)
{
   intptr_t   hash;
   void      *value;
   int       rval;

   hash  = rand() << 1;  // no uneven ids
   value = (void *) (hash * 10);
   rval  = _mulle_concurrent_hashmap_remove( map, hash, value);
   if( rval == ENOMEM)
   {
      perror( "mulle_concurrent_hashmap_remove");
      abort();
   }
}

static void  lookup_something( struct mulle_concurrent_hashmap *map)
{
   intptr_t   hash;
   void      *value;

   hash  = rand();
   value = _mulle_concurrent_hashmap_lookup( map, hash);
   if( ! value)
      return;
   assert( ! (hash & 0x1));
   assert( value == (void *) (hash * 10));
}

static void  enumerate_something( struct mulle_concurrent_hashmap *map)
{
   struct mulle_concurrent_hashmapenumerator   rover;
   intptr_t                                    hash;
   void                                        *value;
   int                                         rval;

retry:
   rover = mulle_concurrent_hashmap_enumerate( map);
   while( (rval = _mulle_concurrent_hashmapenumerator_next( &rover, &hash, &value)) == 1)
   {
      assert( ! (hash & 0x1));
      if( value != (void *) (hash * 10))
      {
         fprintf( stderr, "expected %p, got %p\n", (void *) (hash * 10), value);
         exit( 1);
      }
   }
   mulle_concurrent_hashmapenumerator_done( &rover);

   if( rval == EBUSY)
      goto retry;
}

//
// run until the mask is >= 1 mio entries
//
static void  tester( struct mulle_concurrent_hashmap *map)
{
   int   todo;

   mulle_aba_register();

   while( mulle_concurrent_hashmap_get_size( map) < 1024 * 1024)
   {
      todo = rand() % 1000;
      if( todo == 1)   // 1 % chance of enumerate
      {
         enumerate_something( map);
         continue;
      }

      if( todo < 100)    // 10 % chance of delete
      {
         delete_something( map);
         continue;
      }

      if( todo < 200)    // 20 % chance of insert
      {
         insert_something( map);
         continue;
      }

      lookup_something( map);
   }
   mulle_aba_unregister();
}

static void  multi_threaded_test( unsigned int n_threads)
{
   struct mulle_concurrent_hashmap   map;
   mulle_thread_t                             threads[ 32];
   unsigned int                               i;

   assert( n_threads <= 32);

   mulle_aba_init( &mulle_testallocator);
   mulle_allocator_set_aba( &mulle_testallocator, mulle_aba_get_global(), (void (*)()) _mulle_aba_free);
   mulle_aba_register();

   mulle_concurrent_hashmap_init( &map, 0, &mulle_testallocator);

   {
      for( i = 0; i < n_threads; i++)
      {
         if( mulle_thread_create( (void *) tester, &map, &threads[ i]))
         {
            perror( "mulle_thread_create");
            abort();
         }
      }

      for( i = 0; i < n_threads; i++)
         mulle_thread_join( threads[ i]);
   }

   mulle_concurrent_hashmap_done( &map);

   mulle_aba_unregister();
   mulle_allocator_set_aba( &mulle_testallocator, NULL, NULL);
   mulle_aba_done();
}

static void  single_threaded_test( void)
{
   intptr_t                                     hash;
   struct mulle_concurrent_hashmap             map;
   struct mulle_concurrent_hashmapenumerator   rover;
   unsigned int                                i;
   void                                        *value;

   mulle_aba_init( &mulle_testallocator);
   mulle_allocator_set_aba( &mulle_testallocator, mulle_aba_get_global(), (void (*)()) _mulle_aba_free);
   mulle_aba_register();

   mulle_concurrent_hashmap_init( &map, 0, &mulle_testallocator);
   {
      for( i = 1; i <= 100; i++)
      {
         hash = i;
         value    = (void *) (i * 10);
         if( mulle_concurrent_hashmap_insert( &map, hash, value))
         {
            perror( "mulle_concurrent_hashmap_insert");
            abort();
         }
         assert( mulle_concurrent_hashmap_lookup( &map, i) == (void *) (i * 10));
      }

      for( i = 1; i <= 100; i++)
         assert( mulle_concurrent_hashmap_lookup( &map, i) == (void *) (i * 10));

      assert( ! mulle_concurrent_hashmap_lookup( &map, 101));

      i = 0;
      rover = mulle_concurrent_hashmap_enumerate( &map);
      while( mulle_concurrent_hashmapenumerator_next( &rover, &hash, &value) == 1)
      {
         assert( value == (void *) (hash * 10));
         ++i;
         assert( i <= 100);
      }
      mulle_concurrent_hashmapenumerator_done( &rover);
      assert( i == 100);

      mulle_concurrent_hashmap_remove( &map, 50, (void *) (50 * 10));
      assert( mulle_concurrent_hashmap_lookup( &map, 50) == NULL);

      i = 0;
      rover = mulle_concurrent_hashmap_enumerate( &map);
      while( mulle_concurrent_hashmapenumerator_next( &rover, &hash, &value) == 1)
      {
         assert( value == (void *) (hash * 10));
         ++i;
         assert( i <= 99);
      }
      mulle_concurrent_hashmapenumerator_done( &rover);
      assert( i == 99);
   }
   mulle_concurrent_hashmap_done( &map);

   mulle_aba_unregister();
   mulle_allocator_set_aba( &mulle_testallocator, NULL, NULL);
   mulle_aba_done();
}

int   main(int argc, const char * argv[])
{
   mulle_testallocator_reset();

   do
   {
      single_threaded_test();
      mulle_testallocator_reset();

      multi_threaded_test( 1);
      mulle_testallocator_reset();

      multi_threaded_test( 2);
      mulle_testallocator_reset();

      multi_threaded_test( 32);
      mulle_testallocator_reset();
   }
   while( FOREVER);

   return( 0);
}


text:
/**
 * post model
 * @author adrienjoly, whyd
 **/

var mongodb = require("./mongodb.js");
var ObjectId = mongodb.ObjectId; //ObjectID.createFromHexString;
var db = mongodb.collections;

var snip = require("../snip.js");
var notif = require("../models/notif.js");
var searchModel = require("../models/search.js");
var activityModel = require("../models/activity.js");
var trackModel = require("../models/track.js");
//var recomModel = require("../models/recom.js");
var plTagsModel = require("../models/plTags.js");
var notifModel = require("../models/notif.js");

var config = require("../models/config.js");
var NB_POSTS = config.nbPostsPerNewsfeedPage;

var playlistSort = {
	sort: [['order','asc'], ['_id','desc']]
};

function processPosts(results) {
	for (var i in results)
		results[i].lov = results[i].lov || [];
	return results;
}

// core functions

exports.count = function(q, o, cb) {
	mongodb.collections["post"].count(q, o || {}, cb);
}

exports.forEachPost = function(q, p, handler) {
	mongodb.collections["post"].find(q, p, function(err, cursor){
		cursor.each(function(err, track) {
			if (err) // we're done
				console.log("post.forEachPost error:", err);
			handler(track);
		});
	});
}

function processAdvQuery(query, params, options) {
	query = query || {};
	params = params || {};
	options = options || {};
	params.limit = (options.limit || NB_POSTS) + 1;
	if (options.before != null) {
		if (mongodb.isObjectId(options.before))
			query._id = {$gt: ObjectId(""+options.before)};
		else if (!isNaN(parseInt(options.before))) //if (params.sort == playlistSort.sort)
			query.order = {$lt: parseInt(options.before)};
	}
	if (options.after != null) {
		if (mongodb.isObjectId(options.after))
			query._id = {$lt: ObjectId(""+options.after)};
		else if (!isNaN(parseInt(options.after))) //if (params.sort == playlistSort.sort)
			query.order = {$gt: parseInt(options.after)};
	}
	if (options.until) query._id = {$gt: mongodb.ObjectId(mongodb.dateToHexObjectId(options.until))};
	if (!params.sort) params.sort = [/*['rTm','desc'],*/ ['_id','desc']]; // by default
}

exports.fetchPosts = function (query, params, options, handler) {
	params = params || {};
	processAdvQuery(query, params, options);
	//console.log("status.fetchPosts, query=", query);
	mongodb.collections["post"].find(query, params, function(err, cursor) {
		if (err)
			console.error(err);
		cursor.toArray(function(err, results) {
			if (err)
				console.error("model.fetchPosts ERROR on query", query, "with params", params, ":", err);
			results = results || [];
			processPosts(results);
			console.log("=> fetched " + results.length + " posts");
			handler(results);
		});
	});
};

// more specific functions

exports.fetchAll  = function (handler, after, limit) {
	console.log("post.fetchAll... after=", after);
	exports.fetchPosts({}, {}, {limit:limit, after:after/*, before:before*/}, handler);
};

exports.fetchAllUntil  = function (date, limit, handler) {
	console.log("post.fetchAllUntil date=", date);
	exports.fetchPosts({"_id": {$gt: fakeOid}}, {}, {limit:limit, until:date}, handler);
};

exports.fetchByAuthorsOld  = function (uidList, options, handler) {
	console.log("post.fetchByAuthors...");
	var query = {
		uId: {"$in":uidList},
		"repost.uId": {"$nin":uidList}
	};
	exports.fetchPosts(query, {}, {after:options.after, before:options.before, limit:options.limit}, handler);
};

exports.fetchByAuthors  = function (uidList, options, cb) {
	console.time("post.fetchByAuthors2...");
	var posts = [],
		query = {uId: uidList.length > 1 ? {"$in":uidList} : uidList[0]},
		uidSet = snip.arrayToSet(uidList);
	var params = {}; //{after:options.after, before:options.before, limit:(options.limit || NB_POSTS) + 1};
	processAdvQuery(query, params, {after:options.after, before:options.before, limit:options.limit});
	var limit = params.limit;
	params.limit = undefined; // prevent forEach2 from limiting cursor
	params.q = query;
	mongodb.forEach2("post", params, function(post, next){
		if (post && !uidSet[(post.repost || {}).uId])
			posts.push(post);
		if (!post || !next || posts.length == limit) {
			console.timeEnd("post.fetchByAuthors2...");
			cb(processPosts(posts)); // TODO: close cursor
		}
		else
			next();
	});
};

var MAX_OTHER_AUTHORS = 3;

exports.fetchByOtherAuthors  = function (uidList, options, cb) {
	console.time("post.fetchByOtherAuthors...");
	var posts = [], query = {}, uidSet = snip.arrayToSet(uidList);
	mongodb.forEach2("post", {q:{}, sort:[['_id','desc']]}, function(post, next){
		if (post && !uidSet[post.uId])
			posts.push(post);
		if (!post || !next || posts.length == MAX_OTHER_AUTHORS) {
			console.timeEnd("post.fetchByOtherAuthors...");
			cb(processPosts(posts)); // TODO: close cursor
		}
		else
			next();
	});
};

exports.fetchRepostsFromMe  = function (uid, options, handler) {
	console.log("post.fetchRepostsFromMe...");
	var query = {
		uId: {"$nin": [""+uid]},
		"repost.uId": ""+uid
	};
	exports.fetchPosts(query, {}, {after:options.after, before:options.before, limit:options.limit, until:options.until}, handler);
};

exports.countUserPosts = function(uid, handler) {
	mongodb.collections["post"].count({"uId":uid, "rTo":null}, function(err, result) {
		handler(result);
	});
};

exports.model = exports;

// used by apiPost (for loves)
exports.fetchPostById = function (pId, handler) {
	mongodb.collections["post"].findOne({_id:ObjectId(""+pId)}, function (err, res) {
		if (err) console.log(err);
		handler(res);
	});
}

exports.isPostLovedByUid = function (pId, uId, handler) {
	exports.fetchPostById(pId, function(post) {
		handler(post ? snip.arrayHas(post.lov, uId) : false, post)
	});
}

function setPostLove (collection, pId, uId, state, handler) {
	var update = state ? {$push:{lov:""+uId}} : {$pull:{lov:""+uId}};
	collection.update({_id:ObjectId(""+pId)}, update, function (err, res) {
		if (err) console.log(err);
		collection.findOne({_id:ObjectId(""+pId)}, function (err, post) {
			if (err) console.log(err);
			console.log("setPostLove -> notif", pId, uId, post ? post.uId : null, post);
			if (post && uId != post.uId)
				notif[state ? "love" : "unlove"](uId, post);
			if (handler) handler(post);
			if (post)
				trackModel.updateByEid(post.eId);
			if (state)
				activityModel.addLikeByPost(post, {id: uId, name: mongodb.getUserNameFromId(uId)});
			else
				activityModel.removeLike(pId, uId);
		});
	})
}

exports.lovePost = function(pId, uId, handler) {
	console.log("lovePost", pId, uId);
	setPostLove(mongodb.collections["post"], pId, uId, true, handler);
}

exports.unlovePost = function(pId, uId, handler) {
	console.log("unLovePost", pId, uId);
	setPostLove(mongodb.collections["post"], pId, uId, false, handler);
}

exports.countLovedPosts = function(uid, callback) {
	db["post"].count({"lov":""+uid}, function(err, count) {
		callback(count);
	});
}

function notifyMentionedUsers(post, cb) {
	var mentionedUsers = snip.extractMentions(post.text),
		comment = {uId: post.uId, uNm: post.uNm};
	if (mentionedUsers.length) {
		console.log("notif mentioned users");
		snip.forEachArrayItem(mentionedUsers, function(mentionedUid, next) {
			notifModel.mention(post, comment, mentionedUid, next);
		}, cb);
	}
}

exports.savePost = function (postObj, handler) {
	//console.log("post.savePost: ", postObj);
	var pId = postObj._id;
	function whenDone(error, result) {
		if (error) console.error("post.savePost() error: ", error);
		//console.log("post.savePost() result: ", result);
		if (result) {
			if (Array.isArray(result))
				result = result[0];
			searchModel.indexTyped("post", result);
			//trackModel.updateAndPopulateMetadata(result.eId); // Notice: Cross platform player metadata extraction was shut down on 7/1/2015
			result.isNew = !pId;
			if (result.isNew)
				notif.post(result);
			/*
			recomModel.matchingEngine.addPost(result, function() {
				//console.log("=> added post to matching engine index");
			});
			*/
			plTagsModel.tagEngine.addPost(result, function() {
				//console.log("=> added post to tag engine index");
			});
			notifyMentionedUsers(result);
		}
		handler(result);
	}
	if (postObj.pl && typeof postObj.pl.id !== "number")
		postObj.pl.id = parseInt(""+postObj.pl.id);
	if (pId) {
		delete postObj._id;
		var update = { $set:postObj };
		if (postObj.pl == null || (isNaN(postObj.pl.id) && !postObj.pl.collabId)) {
			delete update.$set.pl;
			update.$unset = { pl: 1 };
		}
		mongodb.collections["post"].update({_id:ObjectId(""+pId)}, update, function(error, result) {
			if (error)
				console.log("update error", error);
			mongodb.collections["post"].findOne({_id:ObjectId(""+pId)}, whenDone);
		});
	}
	else
		mongodb.collections["post"].insert(postObj, whenDone);
}

var fieldsToCopy = {
	name: true,
	eId: true,
	img: true,
//	nbP: true
}; // => not: _id, uId, uNm, text, pl, order, repost, src, lov, nbR, nbP

exports.rePost = function (pId, repostObj, handler) {
	//console.log("post.rePost: ", pId, repostObj);
	var collection = mongodb.collections["post"];
	exports.fetchPostById(pId, function(postObj) {
		postObj = postObj || {error: "post not found"};
		if (postObj.error) {
			handler(postObj);
			return;
		}
		for (var i in fieldsToCopy)
			if (/*repostObj[i] == null &&*/ postObj[i] != null)
				repostObj[i] = postObj[i];
		repostObj.repost = {pId: pId, uId: postObj.uId, uNm: postObj.uNm};
		repostObj.lov = [];
		repostObj.nbR = 0;
		repostObj.nbP = 0;
		delete repostObj._id;
		if (repostObj.pl && typeof repostObj.pl.id !== "number")
			repostObj.pl.id = parseInt(""+repostObj.pl.id);
		collection.insert(repostObj, function(error, result) {
			//console.log("result", result);
			if (error) console.error("post.rePost() error: ", (error));
			if (repostObj.uId != repostObj.repost.uId) {
				notif.repost(repostObj.uId, postObj);
				notif.post(postObj);
				collection.update({_id:ObjectId(""+pId)}, {$inc:{nbR:1}}, {w:0});
			}
			if (result && result.length) {
				//searchModel.indexPost(result);
				result = result[0];
				searchModel.indexTyped("post", result);
				//trackModel.updateAndPopulateMetadata(result.eId); // Notice: Cross platform player metadata extraction was shut down on 7/1/2015
				/*
				recomModel.matchingEngine.addPost(result, function() {
					console.log("=> added post to matching engine index");
				});
				*/
				plTagsModel.tagEngine.addPost(result, function() {
					console.log("=> added post to tag engine index");
				});
				notifyMentionedUsers(result);
			}
			handler(result);
		});
	})
}

exports.deletePost = function (pId, handler, uId) {
	console.log("post.deletePost: ", pId, uId);
	var collection = mongodb.collections["post"];
	var q = {
		_id: ObjectId(pId)
	};
	if (uId)
		q.uId = uId;
	exports.fetchPostById(pId, function(postObj) {
		if (postObj)
			collection.remove(q, function(error, result) {
				if (error) console.log("post.deletePost() error: ", (error));
				searchModel.deleteDoc("post", pId);
				handler(result);
				if (postObj.repost)
					collection.update({_id:ObjectId(""+postObj.repost.pId)}, {$inc:{nbR:-1}}, {w:0});
				trackModel.updateByEid(postObj.eId);
			});
		else {
			console.log("post.deletePost() error: ", pId);
			handler();
		}
	});
};

exports.incrPlayCounter = function(pId, cb) {
	var _id = ObjectId(""+pId);
	if (!_id)
		cb();
	mongodb.collections["post"].update({_id: _id}, {$inc:{nbP:1}}, function (err, res) {
		if (err) console.log(err);
		//cb && cb(res || err);
		exports.fetchPostById(pId, function(postObj) {
			if (postObj)
				trackModel.updateByEid(postObj.eId);
			cb && cb(postObj || err);
		});
	});
}

// wrappers for playlists

exports.fetchPlaylistPosts = function (uId, plId, options, handler) {
	//console.log("fetchPlaylistPosts(uId, plId, options) + order: ", uId, plId, options, playlistSort);
	var options = options || {};
	options = {
		limit: options.limit,
		after: options.after,
		before: options.before
	};
	//console.time("fetchPlaylistPosts");
	if (uId)
		exports.fetchPosts({uId:uId, "pl.id": parseInt(plId)/*{$in:[parseInt(plId), ""+plId]}*/}, playlistSort, options, handler/*function(){
			console.timeEnd("fetchPlaylistPosts");
			handler.apply(null, arguments);
		}*/);
	else
		exports.fetchPosts({"pl.collabId":{$in:[""+plId, ObjectId(""+plId)]}}, playlistSort, options, handler);
};

exports.countPlaylistPosts = function (uId, plId, handler) {
	function handle(err, result) {
		handler(result);
	}
	if (uId)
		db["post"].count({"uId":uId, "pl.id": parseInt(plId)/*{$in:[parseInt(plId), ""+plId]}*/}, handle);
	else
		db["post"].count({"pl.collabId":{$in:[""+plId, ObjectId(""+plId)]}}, handle);
};

exports.setPlaylist = function (uId, plId, plName, handler) {
	console.log("post.setPlaylist", uId, plId, plName);
	var criteria = {
		uId: uId,
		"pl.id":  parseInt(plId)
	}
	var update = {$set: {pl: {id: parseInt(plId), name: plName}}};
	mongodb.collections["post"].update(criteria, update, {multi:true}, function (err, res) {
		if (err) console.log(err);
		if (handler) handler(res);
	});
}

exports.unsetPlaylist = function (uId, plId, handler) {
	console.log("post.unsetPlaylist", uId, plId);
	var criteria = {
		uId: uId,
		"pl.id":  parseInt(plId)
	}
	var update = {$unset: {pl: 1}};
	mongodb.collections["post"].update(criteria, update, {multi:true}, function (err, res) {
		if (err) console.log(err);
		if (handler) handler(res);
	});
}

exports.setPlaylistOrder = function(uId, plId, order, handler) {
	var order = order || [];
	console.log("post.setPlaylistOrder(uId, plId, order.length): ", uId, plId, order.length);
	var collection = mongodb.collections["post"];
	function next(err) {
		if (err)
			console.log("error", err);
		if (!order.length)
			handler({ok:1});
		else {
			var post = {
				"_id": ObjectId(""+order.pop()),
				"uId": uId,
				"pl.id": parseInt(plId)
			};
			console.log("moving post ", post._id, " to pos ", order.length);
			collection.update(post, {$set:{"order":order.length}}, next);
		}
	}
	next();
}

redpajama_set_name: RedPajamaGithub


text:
"""
BoundConstrainedTrustRegionResult

Note that we re-use problem (1) as defined in the documentation of the
solve_bound_constrained_trust_region function.
"""
mutable struct BoundConstrainedTrustRegionResult
  "The approximate solution to (1)."
  solution::Vector{Float64}
  "The value of objective_vector' * (solution - center_point)."
  value::Float64
end

"""
Finds a solution to the problem:

argmin_x objective_vector' * x
s.t. variable_lower_bounds <= x <= variable_upper_bounds                    (1)
     || x - center_point || <= target_radius

where || . || is weighted by norm_weights.

for a positive value of target_radius, by solving the related problem

argmin_t objective_vector' * x
s.t. x := min(max(center_point - t * objective_vector, variable_lower_bounds),
              variable_upper_bounds)
     || x - center_point || <= target_radius

Note that the definition of x is just applying the lower/upper bound constraints
to center_point - t * objective_vector.

This problem is solved by computing the breakpoint at which each component of
x switches from varying with t to being fixed at its bounds.  The radius of the
median breakpoint is evaluated, eliminating half of the components.  The process
is iterated until the argmin is identified.

# Inputs:
- `center_point::Vector{AbstractFloat64}`. See (1). It is assumed that
  center_point satisfies the bounds.
- `objective_vector::Vector{Float64}`. See (1).
- `variable_lower_bounds::Vector{Float64}`. See (1).
- `variable_upper_bounds::Vector{Float64}`. See (1).
- `norm_weights::AbstractVector{Float64}`. The weights used for the norm
  || . ||.
- `target_radius::Float64`. See (1).
- `solve_approximately::Bool`. If true, solve the approximate version of (1)
  which only applies the bound constraints to variables already at their bound.

# Output:
BoundConstrainedTrustRegionResult struct.
"""
function solve_bound_constrained_trust_region(
  center_point::AbstractVector{Float64},
  objective_vector::Vector{Float64},
  variable_lower_bounds::Vector{Float64},
  variable_upper_bounds::Vector{Float64},
  norm_weights::AbstractVector{Float64},
  target_radius::Float64,
  solve_approximately::Bool,
)
  if solve_approximately
    return approximately_solve_bound_constrained_trust_region(
      center_point,
      objective_vector,
      variable_lower_bounds,
      variable_upper_bounds,
      norm_weights,
      target_radius,
    )
  end

  @assert 0.0 <= target_radius < Inf
  if target_radius == 0.0 || norm(objective_vector, 2) == 0.0
    return BoundConstrainedTrustRegionResult(copy(center_point), 0.0)
  end

  direction = zeros(length(center_point))
  threshold = zeros(length(center_point))
  for idx in 1:length(center_point)
    if center_point[idx] >= variable_upper_bounds[idx] &&
       objective_vector[idx] <= 0
      continue
    end
    if center_point[idx] <= variable_lower_bounds[idx] &&
       objective_vector[idx] >= 0
      continue
    end
    direction[idx] = -objective_vector[idx] / norm_weights[idx]
    if direction[idx] > 0
      threshold[idx] =
        (variable_upper_bounds[idx] - center_point[idx]) / direction[idx]
    elseif direction[idx] < 0
      threshold[idx] =
        (variable_lower_bounds[idx] - center_point[idx]) / direction[idx]
    else
      # Variable doesn't move.  Rather than an infinite threshold, or a NaN if
      # the corresponding bound was infinite as well, treat it as fixed, which
      # is effectively equivalent.
      threshold[idx] = 0.0
    end
  end

  # The weighted radius squared of the indices discarded because they are below
  # the threshold.
  low_radius_sq = 0.0
  # The weighted norm squared of the objective coefficients of the indices
  # discarded because they are above the threshold.
  high_radius_sq = 0.0

  indices = collect(1:length(center_point))
  # Infinite thresholds can combine with zeros to create NaNs.  To avoid this,
  # handle indices with infinite thresholds separately.
  infinite_indices = filter(i -> isinf(threshold[i]), indices)
  high_radius_sq +=
    weighted_norm(direction[infinite_indices], norm_weights[infinite_indices])^2
  filter!(i -> isfinite(threshold[i]), indices)

  while length(indices) > 0
    test_threshold = median(threshold[indices])
    test_point =
      clamp.(
        center_point[indices] + test_threshold * direction[indices],
        variable_lower_bounds[indices],
        variable_upper_bounds[indices],
      )
    test_radius =
      weighted_norm(test_point - center_point[indices], norm_weights[indices])
    if low_radius_sq + test_radius^2 + test_threshold^2 * high_radius_sq >=
       target_radius^2
      # test_threshold is too high.  Discard indices greater than it.
      discard_indices = filter(i -> threshold[i] >= test_threshold, indices)
      high_radius_sq +=
        weighted_norm(
          direction[discard_indices],
          norm_weights[discard_indices],
        )^2
      filter!(i -> threshold[i] < test_threshold, indices)
    else
      # test_threshold is too low.  Discard indices less than it.
      discard_indices = filter(i -> threshold[i] <= test_threshold, indices)
      discard_point =
        clamp.(
          center_point[discard_indices] +
          test_threshold * direction[discard_indices],
          variable_lower_bounds[discard_indices],
          variable_upper_bounds[discard_indices],
        )
      low_radius_sq +=
        weighted_norm(
          discard_point - center_point[discard_indices],
          norm_weights[discard_indices],
        )^2
      filter!(i -> threshold[i] > test_threshold, indices)
    end
  end

  # target_threshold is the solution of
  # low_radius_sq + target_threshold^2 * high_radius_sq = target_radius^2.
  if high_radius_sq <= 0.0
    # Special case: high_radius_sq = 0.0, means all bounds hit before reaching
    # target radius.
    target_threshold = maximum(threshold)
  else
    target_threshold = sqrt((target_radius^2 - low_radius_sq) / high_radius_sq)
  end
  candidate_point =
    clamp.(
      center_point + target_threshold * direction,
      variable_lower_bounds,
      variable_upper_bounds,
    )
  return BoundConstrainedTrustRegionResult(
    candidate_point,
    dot(objective_vector, candidate_point - center_point),
  )
end

function approximately_solve_bound_constrained_trust_region(
  center_point::AbstractVector{Float64},
  objective_vector::Vector{Float64},
  variable_lower_bounds::Vector{Float64},
  variable_upper_bounds::Vector{Float64},
  norm_weights::AbstractVector{Float64},
  target_radius::Float64,
)
  direction = zeros(length(center_point))
  for idx in 1:length(center_point)
    if center_point[idx] >= variable_upper_bounds[idx] &&
       objective_vector[idx] <= 0
      continue
    end
    if center_point[idx] <= variable_lower_bounds[idx] &&
       objective_vector[idx] >= 0
      continue
    end
    direction[idx] = -objective_vector[idx] / norm_weights[idx]
  end

  direction_norm = weighted_norm(direction, norm_weights)
  if direction_norm > 0.0
    direction *= target_radius / direction_norm
  end

  return BoundConstrainedTrustRegionResult(
    center_point + direction,
    dot(objective_vector, direction),
  )
end

struct OptimalObjectiveBoundResult
  lagrangian_value::Float64
  lower_bound_value::Float64
  upper_bound_value::Float64
  "The primal solution that minimizes the localized duality gap."
  primal_solution::AbstractVector{Float64}
  "The dual solution that minimizes the localized duality gap."
  dual_solution::AbstractVector{Float64}
end

function get_gap(result::OptimalObjectiveBoundResult)
  return result.upper_bound_value - result.lower_bound_value
end

"""
The norm that defines a ball in joint primal-dual space (x,y). If
norm = MAX_NORM then max{|x|_2, |y|_2} where |.|_2 is the Euclidean norm or
the Euclidean norm in the joint space if norm = EUCLIDEAN_NORM.
"""
@enum LocalizedDualityGapNorm MAX_NORM EUCLIDEAN_NORM

"""
Given a maximum distance to optimality this function returns a tuple with
a lower bound, lagrangian value, and a upper bound on the optimal objective.

These are computed by solving
min grad_x L(primal_solution, dual_solution)^T (x - primal_solution)
- grad_y L(primal_solution, dual_solution)^T (y - dual_solution)
s.t. (x,y) in ball of radius distance_to_optimality centered
at (primal_solution,dual_solution).

See the definition of LocalizedDualityGapNorm for information about
how the ball norm is defined. Note if norm=MAX_NORM then this optimization can
be split into a seperate problems in the primal and the dual.

The lower bound is computed via
L(primal_solution, dual_solution) +
grad_x L(primal_solution, dual_solution)^T (x - primal_solution)
where x is the solution to the trust region problem.

The upper bound is computed via
L(primal_solution, dual_solution) +
grad_y L(primal_solution, dual_solution)^T (y - dual_solution)
where y is the solution to the trust region problem.
"""
function bound_optimal_objective(
  problem::QuadraticProgrammingProblem,
  primal_solution::AbstractVector{Float64},
  dual_solution::AbstractVector{Float64},
  primal_norm_weights::AbstractVector{Float64},
  dual_norm_weights::AbstractVector{Float64},
  distance_to_optimality::Float64,
  norm::LocalizedDualityGapNorm;
  solve_approximately::Bool = false,
)
  # TODO: These are also unnecessarily recomputed.
  primal_gradient =
    compute_primal_gradient(problem, primal_solution, dual_solution)

  lagrangian_value =
    compute_lagrangian_value(problem, primal_solution, dual_solution)

  dual_variable_lower_bounds = -Inf * ones(length(dual_solution))
  dual_variable_upper_bounds = Inf * ones(length(dual_solution))
  dual_variable_lower_bounds[inequality_range(problem)] .= 0.0
  dual_gradient = compute_dual_gradient(problem, primal_solution)

  if norm == MAX_NORM
    # We can split the max norm into two cases.
    # Compute the primal part
    primal_result = solve_bound_constrained_trust_region(
      primal_solution,
      primal_gradient,
      problem.variable_lower_bound,
      problem.variable_upper_bound,
      primal_norm_weights,
      distance_to_optimality,
      solve_approximately,
    )

    # Compute the dual part
    dual_result = solve_bound_constrained_trust_region(
      dual_solution,
      -dual_gradient,
      dual_variable_lower_bounds,
      dual_variable_upper_bounds,
      dual_norm_weights,
      distance_to_optimality,
      solve_approximately,
    )
    OptimalObjectiveBoundResult(
      lagrangian_value,
      # lower bound
      lagrangian_value + primal_result.value,
      # upper bound
      lagrangian_value - dual_result.value,
      # primal solution
      primal_result.solution,
      # dual solution
      dual_result.solution,
    )
  elseif norm == EUCLIDEAN_NORM
    z = [primal_solution; dual_solution]
    z_gradient = [primal_gradient; -dual_gradient]
    z_lower_bound = [problem.variable_lower_bound; dual_variable_lower_bounds]
    z_upper_bound = [problem.variable_upper_bound; dual_variable_upper_bounds]
    norm_weights = [primal_norm_weights; dual_norm_weights]

    # Compute the lower bound
    result = solve_bound_constrained_trust_region(
      z,
      z_gradient,
      z_lower_bound,
      z_upper_bound,
      norm_weights,
      distance_to_optimality,
      solve_approximately,
    )
    primal_tr_solution = result.solution[1:length(primal_solution)]
    dual_tr_solution = result.solution[(length(primal_solution)+1):end]

    return OptimalObjectiveBoundResult(
      lagrangian_value,
      # lower bound
      lagrangian_value +
      dot(primal_tr_solution - primal_solution, primal_gradient),
      # upper bound
      lagrangian_value + dot(dual_tr_solution - dual_solution, dual_gradient),
      primal_tr_solution,
      dual_tr_solution,
    )
  else
    error("unknown norm = $norm, value unknown")
  end
end


text:
namespace ccm {

    MulticastCommunication::MulticastCommunication(const std::string &address, uint16_t port) :
            socketDesc(-1),
            ipv6Address(),
            socketAddress(),
            port(port) {

        inet_pton(AF_INET6, address.c_str(), &ipv6Address);
        socketAddress = {AF_INET6, htons(port), 0, ipv6Address, 0};
    }

    MulticastCommunication::~MulticastCommunication() {
        disconnect();
    }

    bool MulticastCommunication::connect() {
        if (socketDesc != -1) {
            std::cerr << "Already connected" << std::endl;
            return false;
        }

        socketDesc = socket(PF_INET6, SOCK_DGRAM, 0);

        if (socketDesc == -1) {
            std::cerr << "Could not create socket" << std::endl;
            return false;
        }

        int addr = 1;
        if (setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &addr, sizeof(addr)) < 0) {
            std::cerr << "Error while setting SO_REUSEADDR" << std::endl;
            return false;
        }

        int loop = 1;
        if (setsockopt(socketDesc, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) {
            std::cerr << "Error while setting IPV6_MULTICAST_LOOP" << std::endl;
            return false;
        }

        ipv6_mreq group = { ipv6Address, 0 };
        if (setsockopt(socketDesc, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &group, sizeof(group)) < 0) {
            std::cerr << "Could not join group" << std::endl;
            return false;
        }

        sockaddr_in6 bindAddress = { AF_INET6, htons(port), 0, in6addr_any, 0 };
        if (bind(socketDesc, (struct sockaddr *) &bindAddress, sizeof(bindAddress)) < 0) {
            std::cerr << "Could not bind socket" << std::endl;
            return false;
        }

        return true;
    }

    bool MulticastCommunication::disconnect() {
        if (socketDesc == -1) {
            return false;
        }

        if (!close(socketDesc)) {
            return false;
        }

        socketDesc = -1;

        return true;
    }

    bool MulticastCommunication::sendMessage(const Message *message) {
        auto messageSize = sendto(socketDesc, message->message().data(), message->messageSize(), 0, (struct sockaddr *) &socketAddress, sizeof(socketAddress));
        return messageSize != -1;
    }

    bool MulticastCommunication::receiveMessage(Message *message) {
        auto messageSize = recvfrom(socketDesc, message->message().data(), message->maxMessageSize(), 0, nullptr, nullptr);
        return messageSize != -1;
    }

} // ccm


text:
package com.amazonaws.services.guardduty.model;

import java.io.Serializable;
import javax.annotation.Generated;

import com.amazonaws.AmazonWebServiceRequest;

/**
 *
 * @see AWS API
 *      Documentation
 */
@Generated("com.amazonaws:aws-java-sdk-code-generator")
public class ListInvitationsRequest extends com.amazonaws.AmazonWebServiceRequest implements Serializable, Cloneable {

    /**
     *

     * You can use this parameter to indicate the maximum number of items that you want in the response. The default
     * value is 50. The maximum value is 50.
     *

     */
    private Integer maxResults;
    /**
     *

     * You can use this parameter when paginating results. Set the value of this parameter to null on your first call to
     * the list action. For subsequent calls to the action, fill nextToken in the request with the value of NextToken
     * from the previous response to continue listing data.
     *

     */
    private String nextToken;

    /**
     *

     * You can use this parameter to indicate the maximum number of items that you want in the response. The default
     * value is 50. The maximum value is 50.
     *

     *
     * @param maxResults
     *        You can use this parameter to indicate the maximum number of items that you want in the response. The
     *        default value is 50. The maximum value is 50.
     */

    public void setMaxResults(Integer maxResults) {
        this.maxResults = maxResults;
    }

    /**
     *

     * You can use this parameter to indicate the maximum number of items that you want in the response. The default
     * value is 50. The maximum value is 50.
     *

     *
     * @return You can use this parameter to indicate the maximum number of items that you want in the response. The
     *         default value is 50. The maximum value is 50.
     */

    public Integer getMaxResults() {
        return this.maxResults;
    }

    /**
     *

     * You can use this parameter to indicate the maximum number of items that you want in the response. The default
     * value is 50. The maximum value is 50.
     *

     *
     * @param maxResults
     *        You can use this parameter to indicate the maximum number of items that you want in the response. The
     *        default value is 50. The maximum value is 50.
     * @return Returns a reference to this object so that method calls can be chained together.
     */

    public ListInvitationsRequest withMaxResults(Integer maxResults) {
        setMaxResults(maxResults);
        return this;
    }

    /**
     *

     * You can use this parameter when paginating results. Set the value of this parameter to null on your first call to
     * the list action. For subsequent calls to the action, fill nextToken in the request with the value of NextToken
     * from the previous response to continue listing data.
     *

     *
     * @param nextToken
     *        You can use this parameter when paginating results. Set the value of this parameter to null on your first
     *        call to the list action. For subsequent calls to the action, fill nextToken in the request with the value
     *        of NextToken from the previous response to continue listing data.
     */

    public void setNextToken(String nextToken) {
        this.nextToken = nextToken;
    }

    /**
     *

     * You can use this parameter when paginating results. Set the value of this parameter to null on your first call to
     * the list action. For subsequent calls to the action, fill nextToken in the request with the value of NextToken
     * from the previous response to continue listing data.
     *

     *
     * @return You can use this parameter when paginating results. Set the value of this parameter to null on your first
     *         call to the list action. For subsequent calls to the action, fill nextToken in the request with the value
     *         of NextToken from the previous response to continue listing data.
     */

    public String getNextToken() {
        return this.nextToken;
    }

    /**
     *

     * You can use this parameter when paginating results. Set the value of this parameter to null on your first call to
     * the list action. For subsequent calls to the action, fill nextToken in the request with the value of NextToken
     * from the previous response to continue listing data.
     *

     *
     * @param nextToken
     *        You can use this parameter when paginating results. Set the value of this parameter to null on your first
     *        call to the list action. For subsequent calls to the action, fill nextToken in the request with the value
     *        of NextToken from the previous response to continue listing data.
     * @return Returns a reference to this object so that method calls can be chained together.
     */

    public ListInvitationsRequest withNextToken(String nextToken) {
        setNextToken(nextToken);
        return this;
    }

    /**
     * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be
     * redacted from this string using a placeholder value.
     *
     * @return A string representation of this object.
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        if (getMaxResults() != null)
            sb.append("MaxResults: ").append(getMaxResults()).append(",");
        if (getNextToken() != null)
            sb.append("NextToken: ").append(getNextToken());
        sb.append("}");
        return sb.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;

        if (obj instanceof ListInvitationsRequest == false)
            return false;
        ListInvitationsRequest other = (ListInvitationsRequest) obj;
        if (other.getMaxResults() == null ^ this.getMaxResults() == null)
            return false;
        if (other.getMaxResults() != null && other.getMaxResults().equals(this.getMaxResults()) == false)
            return false;
        if (other.getNextToken() == null ^ this.getNextToken() == null)
            return false;
        if (other.getNextToken() != null && other.getNextToken().equals(this.getNextToken()) == false)
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int hashCode = 1;

        hashCode = prime * hashCode + ((getMaxResults() == null) ? 0 : getMaxResults().hashCode());
        hashCode = prime * hashCode + ((getNextToken() == null) ? 0 : getNextToken().hashCode());
        return hashCode;
    }

    @Override
    public ListInvitationsRequest clone() {
        return (ListInvitationsRequest) super.clone();
    }

}


text:
#ifndef QCONNMANSERVICE_H
#define QCONNMANSERVICE_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtDBus/QtDBus>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusError>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusMessage>
#include <QtDBus/QDBusReply>
#include <QtDBus/QDBusArgument>

#include <QtDBus/QDBusPendingCallWatcher>
#include <QtDBus/QDBusObjectPath>
#include <QtDBus/QDBusContext>
#include <QMap>

#ifndef QT_NO_BEARERMANAGEMENT
#ifndef QT_NO_DBUS

#ifndef __CONNMAN_DBUS_H

#define CONNMAN_SERVICE     "net.connman"
#define CONNMAN_PATH        "/net/connman"
#define CONNMAN_MANAGER_INTERFACE   CONNMAN_SERVICE ".Manager"
#define CONNMAN_MANAGER_PATH        "/"
#define CONNMAN_SERVICE_INTERFACE   CONNMAN_SERVICE ".Service"
#define CONNMAN_TECHNOLOGY_INTERFACE    CONNMAN_SERVICE ".Technology"
#endif

QT_BEGIN_NAMESPACE

struct ConnmanMap {
    QDBusObjectPath objectPath;
    QVariantMap propertyMap;
};

typedef QList< ConnmanMap > ConnmanMapList;

QT_END_NAMESPACE

Q_DECLARE_METATYPE(QT_PREPEND_NAMESPACE(ConnmanMap))
Q_DECLARE_METATYPE(QT_PREPEND_NAMESPACE(ConnmanMapList))

QT_BEGIN_NAMESPACE

QDBusArgument &operator<<(QDBusArgument &argument, const ConnmanMap &obj);
const QDBusArgument &operator>>(const QDBusArgument &argument, ConnmanMap &obj);

class QConnmanTechnologyInterface;
class QConnmanServiceInterface;

class QConnmanManagerInterface : public  QDBusAbstractInterface
{
    Q_OBJECT

public:

    QConnmanManagerInterface( QObject *parent = 0);
    ~QConnmanManagerInterface();

    QDBusObjectPath path() const;
    QVariantMap getProperties();

    QString getState();
    bool getOfflineMode();
    QStringList getTechnologies();
    QStringList getServices();
    void requestScan(const QString &type);

    QHash<QString, QConnmanTechnologyInterface *> technologiesMap;

Q_SIGNALS:
    void propertyChanged(const QString &, const QDBusVariant &value);
    void stateChanged(const QString &);
    void propertyChangedContext(const QString &,const QString &,const QDBusVariant &);
    void servicesChanged(const ConnmanMapList&, const QList<QDBusObjectPath> &);

    void servicesReady(const QStringList &);
    void scanFinished();

protected:
    void connectNotify(const QMetaMethod &signal);
    QVariant getProperty(const QString &);

private:
     QVariantMap propertiesCacheMap;
     QStringList servicesList;
     QStringList technologiesList;

private slots:
    void onServicesChanged(const ConnmanMapList&, const QList<QDBusObjectPath> &);
    void changedProperty(const QString &, const QDBusVariant &value);

    void propertiesReply(QDBusPendingCallWatcher *call);
    void servicesReply(QDBusPendingCallWatcher *call);

    void technologyAdded(const QDBusObjectPath &technology, const QVariantMap &properties);
    void technologyRemoved(const QDBusObjectPath &technology);

};

class QConnmanServiceInterface : public QDBusAbstractInterface
{
    Q_OBJECT

public:

    explicit QConnmanServiceInterface(const QString &dbusPathName,QObject *parent = 0);
    ~QConnmanServiceInterface();

    QVariantMap getProperties();
      // clearProperty
    void connect();
    void disconnect();
    void remove();

// properties
    QString state();
    QString lastError();
    QString name();
    QString type();
    QString security();
    bool favorite();
    bool autoConnect();
    bool roaming();
    QVariantMap ethernet();
    QString serviceInterface();

    bool isOfflineMode();
    QStringList services();

Q_SIGNALS:
    void propertyChanged(const QString &, const QDBusVariant &value);
    void propertyChangedContext(const QString &,const QString &,const QDBusVariant &);
    void propertiesReady();
    void stateChanged(const QString &state);

protected:
    void connectNotify(const QMetaMethod &signal);
    QVariant getProperty(const QString &);
private:
    QVariantMap propertiesCacheMap;
private slots:
    void propertiesReply(QDBusPendingCallWatcher *call);
    void changedProperty(const QString &, const QDBusVariant &value);

};

class QConnmanTechnologyInterface : public QDBusAbstractInterface
{
    Q_OBJECT

public:

    explicit QConnmanTechnologyInterface(const QString &dbusPathName,QObject *parent = 0);
    ~QConnmanTechnologyInterface();

    QString type();

    void scan();
Q_SIGNALS:
    void propertyChanged(const QString &, const QDBusVariant &value);
    void propertyChangedContext(const QString &,const QString &,const QDBusVariant &);
    void scanFinished();
protected:
    void connectNotify(const QMetaMethod &signal);
    QVariant getProperty(const QString &);
private:
    QVariantMap properties();
    QVariantMap propertiesMap;
    void scanReply(QDBusPendingCallWatcher *call);

};

QT_END_NAMESPACE

#endif // QT_NO_DBUS
#endif // QT_NO_BEARERMANAGEMENT

#endif //QCONNMANSERVICE_H


text:
module MonadTexcersises  where

import           Control.Applicative
import           Control.Arrow
-- use arrow like - join (***) (+1) (1,2)
import           Control.Monad
import           Control.Monad.IO.Class
import           Control.Monad.Trans.Class
import           Control.Monad.Trans.Except
import           Control.Monad.Trans.Maybe
import           Control.Monad.Trans.Reader
import           Control.Monad.Trans.State
import           Data.Monoid
import           System.Random

-----------------------------   get   (return, put)
--     StateT  s  m   a   --    s ->  (ma,      s)
foo :: StateT Int IO ()
foo = do
    liftIO $ putStr "Hello enter a value: "   -- IO a -> StateT [Integer] IO a
    ent <- lift getLine                       -- m a -> t m a
    nt <- get
    -- put $ nt + read ent
    modify (+ read ent)-- same as put
    return ()
{-- In Terminal
runStateT foo 3
Hello enter a value: 10
((),13)
--}

pop :: StateT [Integer] IO Integer
pop = do
    liftIO $ putStr "Hello enter a value: "
    s1 <- lift getLine
    (x:xs) <- get
    put xs
    return $ x + read s1
{--
runStateT pop [2]
Hello enter a value: 23
(25,[])
--}

main = do answer <- getStdRandom (randomR (1,100)) -- think of a number
          putStrLn "I'm thinking of a number between 1 and 100, can you guess it?"
          guesses <- execStateT (guessSession answer) 0
          putStrLn $ "Success in " ++ show guesses ++ " tries."

guessSession :: Int -> StateT Int IO ()
guessSession answer =
    do gs <- lift getLine    -- get guess from user
       let g = read gs       -- convert to number
       modify (+1)           -- increment number of guesses
       case compare g answer of
              LT -> do lift $ putStrLn "Too low"
                       guessSession answer
              GT -> do lift $ putStrLn "Too high"
                       guessSession answer
              EQ -> lift $ putStrLn ("Got it!," ++ show answer)

dbError :: MaybeT (ExceptT String (ReaderT () IO) ) Int
dbError = do
    liftIO $ putStr "o"
    return 2
-- runMaybeT ::  ExceptT String (ReaderT () IO) (Maybe Int)
-- runExceptT :: ReaderT () IO (Either String (Maybe Int))
-- runReaderT :: () -> IO (Either String (Maybe Int))
-- Right (Just 2)

{--
StateT s (ExceptT e m) a
has the "expanded" form: s -> m (Either e (s, a))
ExceptT e (StateT s m)
"expands" to: s -> m (s, Either e a).

former allows you to roll-back changes to the state when an error occurs in catchError,

latter commits faulty states making them harder to rollback
--}

rollBack :: StateT String (ExceptT String IO) Int
rollBack = do
    a <- get
    if length a > 5 then lift $ throwE a
                    else return (length a)
-- runStateT :: ::  String -> ExceptT String IO (Int, String)
-- runExceptT (runStateT rollBack "OP") :: :: IO (Either String (Int, String))
-- Just (Right (1,"OP"))

data LengthError = EmptyString
          | StringTooLong Int
          | OtherError String

instance Show LengthError where
    show EmptyString = "The string was empty!"
    show (StringTooLong len) =
        "The length of the string (" ++ show len ++ ") is too long"
    show (OtherError msg) = msg

checkLength :: String -> Either LengthError String
checkLength xs
        | ln == 0 = Left EmptyString
        | ln <= 5 = Left $ OtherError "Not much to deduce"
        | ln > 20 = Left $ StringTooLong ln
        | otherwise = Right xs
        where
            ln = length xs

calculateLength :: ExceptT LengthError IO String
calculateLength = do
     liftIO $ putStrLn "Enter a sentence"
     sen <- lift getLine
     case checkLength sen of
         Left a  -> throwE a
         Right a -> return a

calculateLength' :: ReaderT String (ExceptT LengthError IO) String
calculateLength' = do
    sen <- ask
    case checkLength sen of
        Left a  -> lift $ throwE a
        Right a -> return a
-- runExceptT (runReaderT calculateLength' "I am Alok") --- Right "I am Alok"
-- runExceptT (runReaderT calculateLength' "I am") --- Left Not much to deduce

calculateLength'' :: StateT String (ReaderT String (ExceptT LengthError IO)) String
calculateLength'' = return "kk"

-- runStateT calculateLength'' :: String -> ReaderT String (ExceptT LengthError IO) (String, String)
-- runReaderT (runStateT calculateLength'' "p") :: String -> ExceptT LengthError IO (String, String)
-- runExceptT $ runReaderT (runStateT calculateLength'' "p") "a" :: IO (Either LengthError (String, String)) ---------- Right ("kk","p")

type Env = String
calculateLength''' :: ExceptT LengthError (StateT Int (ReaderT Env IO)) String
calculateLength''' = do
    -- sen <- lift get --- get from StateT
    lift $ modify (+1) -- added as second thougth hence below comments may change
    sen2 <- lift . lift $ ask -- ask ReaderT
    case checkLength sen2 of
        Left a  -> throwE a
        Right a -> return a
-- (runExceptT calculateLength''') :: StateT String (ReaderT Env IO) (Either LengthError String)
-- runStateT (runExceptT calculateLength''') :: String -> ReaderT Env IO (Either LengthError String, String)
--  runReaderT (runStateT (runExceptT calculateLength''')"state") :: Env -> IO (Either LengthError String, String)
-- runReaderT (runStateT (runExceptT calculateLength''')"state") "reader" :: IO (Either LengthError String, String) ---------  (Right "kk","state")

gt :: StateT String IO String
gt = do
   name <- get
   liftIO $ putStrLn "HI"
   put "T"
   return ("hh..." ++ name ++ "...!")


text:
<?php
App::uses('AppController', 'Controller');
/**
 * ClientTypes Controller
 *
 * @property ClientType $ClientType
 */
class ClientTypesController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->ClientType->id = $id;
		if (!$this->ClientType->exists()) {
			throw new NotFoundException(__('Invalid client type'));
		}
		$this->set('clientType', $this->ClientType->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->ClientType->create();
			if ($this->ClientType->save($this->request->data)) {
				$this->Session->setFlash(__('The client type has been saved'), 'flash_success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The client type could not be saved. Please, try again.'), 'flash_fail');
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->ClientType->id = $id;
		if (!$this->ClientType->exists()) {
			throw new NotFoundException(__('Invalid client type'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->ClientType->save($this->request->data)) {
				$this->Session->setFlash(__('The client type has been saved'), 'flash_success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The client type could not be saved. Please, try again.'), 'flash_fail');
			}
		} else {
			$this->request->data = $this->ClientType->read(null, $id);
		}
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		//if (!$this->request->is('post')) {
		//	throw new MethodNotAllowedException();
		//}
		$this->ClientType->id = $id;
		if (!$this->ClientType->exists()) {
			throw new NotFoundException(__('Invalid client type'));
		}
		if ($this->ClientType->delete()) {
			$this->Session->setFlash(__('Client type deleted'), 'flash_success');
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Client type was not deleted'), 'flash_fail');
		$this->redirect(array('action' => 'index'));
	}

	public function jsondata(){

		$fields = array('ClientType.id','ClientType.name','ClientType.description');
		$limit = 10;
		$offset = 0;
		$orderby = array();
		$and = array();
		$conditions = array();

		if ( isset( $this->request->query['iDisplayStart'] ) && $this->request->query['iDisplayLength'] != '-1' )
		{
			$limit = $this->request->query['iDisplayLength'];
			$offset = $this->request->query['iDisplayStart'];
		}

		if ( isset( $this->request->query['iSortCol_0'] ) )
		{
			for ( $i=0 ; $i<intval( $this->request->query['iSortingCols'] ) ; $i++ )
			{
				if ( $_GET[ 'bSortable_'.intval($this->request->query['iSortCol_'.$i]) ] == "true" )
				{
					$orderby[] = $fields[ intval( $this->request->query['iSortCol_'.$i] ) ]." ".$this->request->query['sSortDir_'.$i];
				}
			}
		}

		$and[] = array('1'=>1);
		//

		if(count($and)>1){
			$conditions = array('AND' => $and);
		}else{
			$conditions = $and;
		}

		$result = $this->ClientType->find('all',
									array(
										'conditions' => $conditions,
									    'recursive' => 0,
									    'fields' => $fields,
									    'order' => $orderby,
									    'limit' => $limit,
									    'offset' => $offset,
									)
									);

		/* Total data set length */
		$iTotal = $this->ClientType->find('count',array($conditions));

		$output = array(
		"sEcho" => intval($this->request->query['sEcho']),
		"iTotalRecords" => $iTotal,
		"iTotalDisplayRecords" => $iTotal,
		"aaData" => array()
		);

		for($j=0;$j<count($result);$j++) {
			{
				$row = array();
				for ( $i=0 ; $i<count($fields) ; $i++ )
				{
					if ( $fields[$i] != ' ' )
					{
						$fld = explode( '.', $fields[$i] );
						$row[] = $result[$j][$fld[0]][$fld[1]];
					}
				}
				$row[] = "";
				$output['aaData'][] = $row;
			}
		}
		return new CakeResponse(array('body' => json_encode($output)));
	}
}


text:
$:.unshift 'data/plugins'

require 'java'
require 'fixins'
require 'plugin'
require 'hook'

class TestPlugin < Plugin::Base
        def load
                bind :button, :id => 0, :action => 'test'
        end

        def test(plr)
                p '#4'
        end
end


text:
var fs = require('fs');
var path = require('path');
var express = require('express');
var router = express.Router();
var jsonfile = require("jsonfile");
var _ = require('lodash');
var mongoose = require('mongoose');
var Abhanga = require('../models/abhanga');

/* GET Abhanga listing. */
router.get('/', function(req, res, next) {
  //res.send('respond with a resource');
  // And insert something like this instead:
  res.render('index', { title: 'All Abhangas' });

});

router.get('/:id', function(req, res, next) {

	var id = Number(req.params.id);
		Abhanga.find({ srNo: id }, function (err, records) {
	  if (err){
			res.json(err);
	  }
		res.json(records);
	});

});

router.get('/page/:page', function(req, res, next) {

	var pageNumber = Number(req.params.page);
	var nPerPage = 100;

	var offset = pageNumber > 0 ? ((pageNumber-1) * nPerPage) : 1;

	// get all records
	var query = Abhanga.find().skip(offset).limit(nPerPage).sort({ srNo: 1 });

	// execute the query at a later time
	query.exec(function (err, records) {
	  if (err){
			res.json(err);
	  }
		res.json(records);
	})

});

module.exports = router;


text:
@class TBVAsset;
@interface TBVAssetsConfiguration : NSObject
+ (instancetype)defaultConfiguration;
@property (nonatomic, strong) NSArray <TBVAsset *> *selectedAssets;
@property (assign, nonatomic) NSInteger maxSelectedCount;
@property (assign, nonatomic) BOOL showsEmptyAlbums;
@property (assign, nonatomic) TBVAssetsMediaType mediaType;
@property (assign, nonatomic) BOOL shouldScrollToBottom;
@end


text:
PROG_NOINST = Popup
SRCS = MainWindow.m AppDelegate.m

include ../../buildsys.mk

CPPFLAGS += -I../../src
LIBS := -L../../src -lgtkkit ${LIBS}
LD = ${OBJC}


text:
package com.winston.structures.binaryTree;

public class Node {
    int key;
    String value;

    Node rightChild;
    Node leftChild;

    public Node(int key, String value){
        this.key = key;
        this.value = value;
    }

}


text:
<?php

DEFINE('LOGLABELS_PLAIN',0);
DEFINE('LOGLABELS_MAGNITUDE',1);

//===================================================
// CLASS LogScale
// Description: Logarithmic scale between world and screen
//===================================================
class LogScale extends LinearScale {
//---------------
// CONSTRUCTOR

    // Log scale is specified using the log of min and max
    function LogScale($min,$max,$type="y") {
	$this->LinearScale($min,$max,$type);
	$this->ticks = new LogTicks();
	$this->name = 'log';
    }

//----------------
// PUBLIC METHODS

    // Translate between world and screen
    function Translate($a) {
	if( !is_numeric($a) ) {
	    if( $a != '' && $a != '-' && $a != 'x' )
		JpGraphError::RaiseL(11001);
//('Your data contains non-numeric values.');
	    return 1;
	}
	if( $a < 0 ) {
	    JpGraphError::RaiseL(11002);
//("Negative data values can not be used in a log scale.");
	    exit(1);
	}
	if( $a==0 ) $a=1;
	$a=log10($a);
	return ceil($this->off + ($a*1.0 - $this->scale[0]) * $this->scale_factor);
    }

    // Relative translate (don't include offset) usefull when we just want
    // to know the relative position (in pixels) on the axis
    function RelTranslate($a) {
	if( !is_numeric($a) ) {
	    if( $a != '' && $a != '-' && $a != 'x' )
		JpGraphError::RaiseL(11001);
//('Your data contains non-numeric values.');
	    return 1;
	}
	if( $a==0 ) $a=1;
	$a=log10($a);
	return round(($a*1.0 - $this->scale[0]) * $this->scale_factor);
    }

    // Use bcpow() for increased precision
    function GetMinVal() {
	if( function_exists("bcpow") )
	    return round(bcpow(10,$this->scale[0],15),14);
	else
	    return round(pow(10,$this->scale[0]),14);
    }

    function GetMaxVal() {
	if( function_exists("bcpow") )
	    return round(bcpow(10,$this->scale[1],15),14);
	else
	    return round(pow(10,$this->scale[1]),14);
    }

    // Logarithmic autoscaling is much simplier since we just
    // set the min and max to logs of the min and max values.
    // Note that for log autoscale the "maxstep" the fourth argument
    // isn't used. This is just included to give the method the same
    // signature as the linear counterpart.
    function AutoScale($img,$min,$max,$maxsteps,$majend=true) {
	if( $min==0 ) $min=1;

	if( $max <= 0 ) {
	    JpGraphError::RaiseL(11004);
//('Scale error for logarithmic scale. You have a problem with your data values. The max value must be greater than 0. It is mathematically impossible to have 0 in a logarithmic scale.');
	}
	if( is_numeric($this->autoscale_min) ) {
	    $smin = round($this->autoscale_min);
	    $smax = ceil(log10($max));
	    if( $min >= $max ) {
		JpGraphError::RaiseL(25071);//('You have specified a min value with SetAutoMin() which is larger than the maximum value used for the scale. This is not possible.');
	    }
	}
	else {
	    $smin = floor(log10($min));
	    if( is_numeric($this->autoscale_max) ) {
		$smax = round($this->autoscale_max);
		if( $smin >= $smax ) {
		    JpGraphError::RaiseL(25072);//('You have specified a max value with SetAutoMax() which is smaller than the miminum value used for the scale. This is not possible.');
		}
	    }
	    else
		$smax = ceil(log10($max));
	}

	$this->Update($img,$smin,$smax);
    }
//---------------
// PRIVATE METHODS
} // Class

//===================================================
// CLASS LogTicks
// Description:
//===================================================
class LogTicks extends Ticks{
    private $label_logtype=LOGLABELS_MAGNITUDE;
    private $ticklabels_pos = array();
//---------------
// CONSTRUCTOR
    function LogTicks() {
    }
//---------------
// PUBLIC METHODS
    function IsSpecified() {
	return true;
    }

    function SetLabelLogType($aType) {
	$this->label_logtype = $aType;
    }

    // For log scale it's meaningless to speak about a major step
    // We just return -1 to make the framework happy (specifically
    // StrokeLabels() )
    function GetMajor() {
	return -1;
    }

    function SetTextLabelStart($aStart) {
	JpGraphError::RaiseL(11005);
//('Specifying tick interval for a logarithmic scale is undefined. Remove any calls to SetTextLabelStart() or SetTextTickInterval() on the logarithmic scale.');
    }

    function SetXLabelOffset($dummy) {
	// For log scales we dont care about XLabel offset
    }

    // Draw ticks on image "img" using scale "scale". The axis absolute
    // position in the image is specified in pos, i.e. for an x-axis
    // it specifies the absolute y-coord and for Y-ticks it specified the
    // absolute x-position.
    function Stroke($img,$scale,$pos) {
	$start = $scale->GetMinVal();
	$limit = $scale->GetMaxVal();
	$nextMajor = 10*$start;
	$step = $nextMajor / 10.0;

	$img->SetLineWeight($this->weight);

	if( $scale->type == "y" ) {
	    // member direction specified if the ticks should be on
	    // left or right side.
	    $a=$pos + $this->direction*$this->GetMinTickAbsSize();
	    $a2=$pos + $this->direction*$this->GetMajTickAbsSize();

	    $count=1;
	    $this->maj_ticks_pos[0]=$scale->Translate($start);
	    $this->maj_ticklabels_pos[0]=$scale->Translate($start);
	    if( $this->supress_first )
		$this->maj_ticks_label[0]="";
	    else {
		if( $this->label_formfunc != '' ) {
		    $f = $this->label_formfunc;
		    $this->maj_ticks_label[0]=call_user_func($f,$start);
		}
		elseif( $this->label_logtype == LOGLABELS_PLAIN )
		    $this->maj_ticks_label[0]=$start;
		else
		    $this->maj_ticks_label[0]='10^'.round(log10($start));
	    }
	    $i=1;
	    for($y=$start; $y<=$limit; $y+=$step,++$count  ) {
		$ys=$scale->Translate($y);
		$this->ticks_pos[]=$ys;
		$this->ticklabels_pos[]=$ys;
		if( $count % 10 == 0 ) {
		    if( !$this->supress_tickmarks ) {
			if( $this->majcolor!="" ) {
			    $img->PushColor($this->majcolor);
			    $img->Line($pos,$ys,$a2,$ys);
			    $img->PopColor();
			}
			else
			    $img->Line($pos,$ys,$a2,$ys);
		    }

		    $this->maj_ticks_pos[$i]=$ys;
		    $this->maj_ticklabels_pos[$i]=$ys;

		    if( $this->label_formfunc != '' ) {
			$f = $this->label_formfunc;
			$this->maj_ticks_label[$i]=call_user_func($f,$nextMajor);
		    }
		    elseif( $this->label_logtype == 0 )
			$this->maj_ticks_label[$i]=$nextMajor;
		    else
			$this->maj_ticks_label[$i]='10^'.round(log10($nextMajor));
		    ++$i;
		    $nextMajor *= 10;
		    $step *= 10;
		    $count=1;
		}
		else {
		    if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks) {
			if( $this->mincolor!="" ) $img->PushColor($this->mincolor);
			$img->Line($pos,$ys,$a,$ys);
			if( $this->mincolor!="" ) $img->PopColor();
		    }
		}
	    }
	}
	else {
	    $a=$pos - $this->direction*$this->GetMinTickAbsSize();
	    $a2=$pos - $this->direction*$this->GetMajTickAbsSize();
	    $count=1;
	    $this->maj_ticks_pos[0]=$scale->Translate($start);
	    $this->maj_ticklabels_pos[0]=$scale->Translate($start);
	    if( $this->supress_first )
		$this->maj_ticks_label[0]="";
	    else {
		if( $this->label_formfunc != '' ) {
		    $f = $this->label_formfunc;
		    $this->maj_ticks_label[0]=call_user_func($f,$start);
		}
		elseif( $this->label_logtype == 0 )
		    $this->maj_ticks_label[0]=$start;
		else
		    $this->maj_ticks_label[0]='10^'.round(log10($start));
	    }
	    $i=1;
	    for($x=$start; $x<=$limit; $x+=$step,++$count  ) {
		$xs=$scale->Translate($x);
		$this->ticks_pos[]=$xs;
		$this->ticklabels_pos[]=$xs;
		if( $count % 10 == 0 ) {
		    if( !$this->supress_tickmarks ) {
			$img->Line($xs,$pos,$xs,$a2);
		    }
		    $this->maj_ticks_pos[$i]=$xs;
		    $this->maj_ticklabels_pos[$i]=$xs;

		    if( $this->label_formfunc != '' ) {
			$f = $this->label_formfunc;
			$this->maj_ticks_label[$i]=call_user_func($f,$nextMajor);
		    }
		    elseif( $this->label_logtype == 0 )
			$this->maj_ticks_label[$i]=$nextMajor;
		    else
			$this->maj_ticks_label[$i]='10^'.round(log10($nextMajor));
		    ++$i;
		    $nextMajor *= 10;
		    $step *= 10;
		    $count=1;
		}
		else {
		    if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks) {
			$img->Line($xs,$pos,$xs,$a);
		    }
		}
	    }
	}
	return true;
    }
} // Class
/* EOF */
?>


text:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS Lists: parsing counter()/counters() values for the 'content' property</title>
<link rel="help" href="https://drafts.csswg.org/css-lists-3/#counter-functions">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/css/support/parsing-testcommon.js"></script>
</head>
<body>
<script>
test_invalid_value('content', 'counter(foo, none)');
test_invalid_value('content', 'counters(foo, "", none)');
test_invalid_value('content', 'counter(foo, unset)');
test_invalid_value('content', 'counters(foo, "", unset)');
test_invalid_value('content', 'counter(foo, initial)');
test_invalid_value('content', 'counters(foo, "", initial)');
test_invalid_value('content', 'counter(foo, inherit)');
test_invalid_value('content', 'counters(foo, "", inherit)');

test_invalid_value('content', '"z" counter(foo, none)');
test_invalid_value('content', '"z" counters(foo, "", none)');
test_invalid_value('content', '"z" counter(foo, unset)');
test_invalid_value('content', '"z" counters(foo, "", unset)');
test_invalid_value('content', '"z" counter(foo, initial)');
test_invalid_value('content', '"z" counters(foo, "", initial)');
test_invalid_value('content', '"z" counter(foo, inherit)');
test_invalid_value('content', '"z" counters(foo, "", inherit)');

test_invalid_value('content', 'counter(foo, none) "z"');
test_invalid_value('content', 'counters(foo, "", none) "z"');
test_invalid_value('content', 'counter(foo, unset) "z"');
test_invalid_value('content', 'counters(foo, "", unset) "z"');
test_invalid_value('content', 'counter(foo, initial) "z"');
test_invalid_value('content', 'counters(foo, "", initial) "z"');
test_invalid_value('content', 'counter(foo, inherit) "z"');
test_invalid_value('content', 'counters(foo, "", inherit) "z"');
</script>
</body>
</html>


text:
import regex

from collections import namedtuple

Attr = namedtuple('Attr', 'lft op rgt')
Pseudo = namedtuple('Pseudo', 'name value')

class Selector(object):
    DESCENDANT = ' '
    CHILD = '>'
    SIBLING = '~'
    ADJACENT = '+'
    NOT_SET = None

    class RE(object):
        id = r'_?[A-Za-z0-9_]+|_'
        ws = r'[\x20\t\r\n\f]*'
        comma = '^{ws},{ws}'.format(ws=ws)
        combinator = r'^{ws}([>+~ ]){ws}'.format(ws=ws)

        type_selector = '({id})'.format(id=id)
        id_selector = '#({id})'.format(id=id)
        class_selector = r'\.(' + id + ')'
        pseudo_selector = r'(:({id})\(([^()]+|(?1)?)\))'.format(id=id)
        attr_selector = r'\[{ws}({id}){ws}([*^$|!~]?=)(.*?)\]'.format(
            id=id, ws=ws)

        selector = '(?:(?:{typ})?({id}|{cls}|{pseudo}|{attr})+|{typ})'.format(
            typ=id, id=id_selector, cls=class_selector, pseudo=pseudo_selector,
            attr=attr_selector)

    def __init__(self, name, combinator=None):
        self.name = name
        self.combinator = combinator
        self.next_selector = None

        selector_patterns = {
            'types': self.RE.type_selector,
            'ids': self.RE.id_selector,
            'classes': self.RE.class_selector,
            'pseudos': self.RE.pseudo_selector,
            'attrs': self.RE.attr_selector,
        }

        matches = {}

        while True:
            pattern_matched = False
            for key, pattern in selector_patterns.items():
                match = regex.search(r'^{}'.format(pattern), name)
                if match:
                    i, pos = match.span()
                    if key not in matches:
                        matches[key] = []
                    matches[key].append(match.groups())
                    name = name[pos:]
                    pattern_matched = True
            if not pattern_matched:
                break

        self.typ = None
        for types in matches.pop('types', []):
            self.typ = types[0]

        self.id_ = None
        for ids in matches.pop('ids', []):
            self.id_ = ids[0]

        self.classes = [a[0] for a in matches.pop('classes', [])]

        self.attrs = [
            Attr(l, o, r.strip())
            for l, o, r in matches.pop('attrs', [])
        ]
        self.pseudos = [
            Pseudo(*a[1:])
            for a in matches.pop('pseudos', [])
        ]

    def __repr__(self):
        return 'Selector <{}>'.format(self.name)

    @classmethod
    def parse(cls, string):
        selectors = []

        combinator = None
        prev_selector = None

        while True:
            match = regex.search(cls.RE.comma, string)
            if match:
                # skip comma
                _, pos = match.span()
                string = string[pos:]
                continue

            match = regex.search(cls.RE.combinator, string)
            if match:
                _, pos = match.span()
                combinator = string[:pos].strip()
                string = string[pos:]
            else:
                combinator = None

            match = regex.search(cls.RE.selector, string)
            if match:
                _, pos = match.span()
                seltext = string[:pos]
                string = string[pos:]
                selector = cls(seltext, combinator=combinator)
                if combinator is not None and prev_selector:
                    prev_selector.next_selector = prev_selector = selector
                else:
                    prev_selector = selector
                    selectors.append(selector)
                continue

            break

        return selectors


text:
/* @flow */
export * from './platform';


text:
function f(a,b,c) {
  var args = Array.prototype.slice.call(arguments, 3);
  assert (typeof args.splice === "function");
}
f();


text:
#include <stdio.h>
#include <string.h>
#include "ds3.h"
#include "ds3_net.h"
#include "test.h"
#include <boost/test/unit_test.hpp>
#include "checksum.h"
#include <glib.h>

static const unsigned char MAX_UNIT_TEST_FILEPATH_LENGTH = 64;

static const char FOLDER_PREFIX[] = "resources/";
static const unsigned char LENGTH_OF_FOLDER_PREFIX = sizeof(FOLDER_PREFIX);
static const char TEMP_PREFIX[] = "temp-";
static const unsigned char LENGTH_OF_TEMP_PREFIX = sizeof(TEMP_PREFIX);

struct checksum_result {
    char original_name[MAX_UNIT_TEST_FILEPATH_LENGTH];
    char tmp_name[MAX_UNIT_TEST_FILEPATH_LENGTH];
    int num_chunks;
    bool passed;
};

bool check_all_passed(uint32_t num_files, checksum_result* results) {
    bool result = true;
    for (uint32_t i = 0; i < num_files; i++) {
        result = result && results[i].passed;
    }
    return result;
}

uint32_t get_number_of_chunks(uint32_t num_files, checksum_result* results, const char* filename) {
    for (uint32_t i = 0; i < num_files; i++) {
        if (g_strcmp0(results[i].original_name, filename) == 0) {
            return results[i].num_chunks;
        }
    }
    return 0;
}

uint32_t get_sum_of_chunks(uint32_t num_files, checksum_result* results) {
    uint32_t result = 0;
    for (uint32_t i = 0; i < num_files; i++) {
        result += results[i].num_chunks;
    }
    return result;
}

uint64_t getFileIndexForChunk(uint64_t* max_file_index, ds3_str* current_obj_name, checksum_result* results) {
    int64_t file_index = -1;
    for (uint64_t current_file_index = 0; current_file_index < *max_file_index; current_file_index++) {
        if (g_strcmp0(current_obj_name->value, results[current_file_index].original_name) == 0) {
            file_index = current_file_index;
            break;
        }
    }

    if (file_index == -1) {

        file_index = *max_file_index;
        (*max_file_index)++;

        memcpy(results[file_index].original_name, current_obj_name->value, current_obj_name->size);

        memcpy(results[file_index].tmp_name, FOLDER_PREFIX, LENGTH_OF_FOLDER_PREFIX); // "resources/"
        memcpy(results[file_index].tmp_name+LENGTH_OF_FOLDER_PREFIX-1, TEMP_PREFIX, LENGTH_OF_TEMP_PREFIX); // "resources/temp-"
        memcpy(results[file_index].tmp_name+LENGTH_OF_FOLDER_PREFIX+LENGTH_OF_TEMP_PREFIX-2, current_obj_name->value+LENGTH_OF_FOLDER_PREFIX-1, current_obj_name->size-LENGTH_OF_FOLDER_PREFIX+1);
    }
    results[file_index].num_chunks++;
    return (uint64_t)file_index;
}

void checkChunkResponse(ds3_client* client, uint32_t num_files, ds3_master_object_list_response* chunk_response, checksum_result* results) {
    uint64_t max_file_index = 0;
    int64_t file_index = 0;
    ds3_request* request = NULL;
    ds3_error* error = NULL;

    for (uint64_t chunk_index = 0; chunk_index < chunk_response->num_objects; chunk_index++) {
        ds3_objects_response* chunk_object_list = chunk_response->objects[chunk_index];
        for (uint64_t chunk_object_index = 0; chunk_object_index < chunk_object_list->num_objects; chunk_object_index++) {
            FILE* w_file;
            ds3_bulk_object_response* current_obj = chunk_object_list->objects[chunk_object_index];
            file_index = getFileIndexForChunk(&max_file_index, current_obj->name, results);

            const uint64_t length = current_obj->length;
            const uint64_t offset = current_obj->offset;

            request = ds3_init_get_object_request(chunk_response->bucket_name->value, current_obj->name->value, length);
            ds3_request_set_job(request, chunk_response->job_id->value);
            ds3_request_set_offset(request, offset);

            w_file = fopen(results[file_index].tmp_name, "a+");
            fseek(w_file, (long)current_obj->offset, SEEK_SET);

            error = ds3_get_object_request(client, request, w_file, ds3_write_to_file);
            ds3_request_free(request);
            fclose(w_file);
            handle_error(error);
        }
    }

    for (uint64_t current_file_index = 0; current_file_index < max_file_index; current_file_index++) {
        printf("------Performing Data Integrity Test-------\n");
        results[current_file_index].passed = compare_hash(results[current_file_index].original_name, results[current_file_index].tmp_name);
        unlink(results[current_file_index].tmp_name);
    }
}

void checkChunkResponsePartials(ds3_client* client, uint32_t num_files, ds3_master_object_list_response* chunk_response, checksum_result* results, uint32_t segment_size) {
    uint64_t max_file_index = 0;
    int64_t file_index = 0;
    ds3_request* request = NULL;
    ds3_error* error = NULL;

    for (uint64_t chunk_index = 0; chunk_index < chunk_response->num_objects; chunk_index++) {
        ds3_objects_response* chunk_object_list = chunk_response->objects[chunk_index];
        for (uint64_t chunk_object_index = 0; chunk_object_index < chunk_object_list->num_objects; chunk_object_index++) {
            FILE* w_file;
            ds3_bulk_object_response* current_obj = chunk_object_list->objects[chunk_object_index];
            file_index = getFileIndexForChunk(&max_file_index, current_obj->name, results);

            const uint64_t length = current_obj->length;
            const uint64_t offset = current_obj->offset;
            request = ds3_init_get_object_request(chunk_response->bucket_name->value, current_obj->name->value, length);
            ds3_request_set_job(request, chunk_response->job_id->value);
            ds3_request_set_offset(request, offset);

            ds3_request_set_byte_range(request, segment_size, segment_size*2-1);
            ds3_request_set_byte_range(request, segment_size*3, segment_size*4-1);

            w_file = fopen(results[file_index].tmp_name, "a+");
            fseek(w_file, (long)current_obj->offset, SEEK_SET);

            error = ds3_get_object_request(client, request, w_file, ds3_write_to_file);
            ds3_request_free(request);
            fclose(w_file);
            handle_error(error);
        }
    }

    for (uint64_t current_file_index = 0; current_file_index < max_file_index; current_file_index++) {
        printf("------Performing Data Integrity Test-------\n");
        bool check1 = compare_hash_extended(results[current_file_index].original_name, results[current_file_index].tmp_name, segment_size, segment_size, 0);
        bool check2 = compare_hash_extended(results[current_file_index].original_name, results[current_file_index].tmp_name, segment_size, segment_size*3, segment_size);
        results[current_file_index].passed = check1 && check2;
        unlink(results[current_file_index].tmp_name);
    }
}

BOOST_AUTO_TEST_CASE( bulk_get ) {
    ds3_request* request = NULL;
    ds3_error* error = NULL;
    ds3_master_object_list_response* bulk_response = NULL;
    const uint32_t num_files = 5;
    ds3_bulk_object_list_response* object_list = default_object_list();
    ds3_master_object_list_response* chunk_response = NULL;

    ds3_client* client = get_client();
    const char* bucket_name = "test_bulk_get_bucket";

    printf("-----Testing Bulk GET-------\n");

    populate_with_objects(client, bucket_name);

    request = ds3_init_get_bulk_job_spectra_s3_request(bucket_name, object_list);
    ds3_job_chunk_client_processing_order_guarantee chunk_order = DS3_JOB_CHUNK_CLIENT_PROCESSING_ORDER_GUARANTEE_NONE;
    ds3_request_set_chunk_client_processing_order_guarantee_ds3_job_chunk_client_processing_order_guarantee(request, chunk_order);

    error = ds3_get_bulk_job_spectra_s3_request(client, request, &bulk_response);
    ds3_request_free(request);
    ds3_bulk_object_list_response_free(object_list);
    handle_error(error);

    chunk_response = ensure_available_chunks(client, bulk_response->job_id);
    handle_error(error);

    checksum_result* checksum_results = (checksum_result*) calloc(num_files, sizeof(checksum_result));
    checkChunkResponse(client, num_files, chunk_response, checksum_results);
    BOOST_CHECK(check_all_passed(num_files, checksum_results) == true);
    BOOST_CHECK(get_sum_of_chunks(num_files, checksum_results) == num_files);
    free(checksum_results);

    // check to make sure that the 'job' has completed
    /* works sporadically in 1.2, shjould be fixed in 3.0
    ds3_bulk_response* completed_job = NULL;
    request = ds3_init_get_job(bulk_response->job_id->value);
    error = ds3_get_job(client, request, &completed_job);
    handle_error(error);

    BOOST_CHECK(completed_job != NULL);
    BOOST_CHECK(completed_job->status == COMPLETED);

    ds3_free_request(request);
    ds3_free_bulk_response(completed_job);*/

    ds3_master_object_list_response_free(chunk_response);
    ds3_master_object_list_response_free(bulk_response);

    clear_bucket(client, bucket_name);
    free_client(client);
}

BOOST_AUTO_TEST_CASE( max_upload_size ) {
    ds3_request* request = NULL;
    ds3_error* error = NULL;
    ds3_master_object_list_response* bulk_response = NULL;
    ds3_bulk_object_list_response* object_list = NULL;
    ds3_master_object_list_response* chunk_response = NULL;

    ds3_client* client = get_client();
    const char* bucket_name = "test_max_upload_size_bucket";

    printf("-----Testing Bulk GET with max_upload_size-------\n");

    const uint32_t num_files = 6;
    const char* books[num_files] = {"resources/beowulf.txt", "resources/sherlock_holmes.txt", "resources/tale_of_two_cities.txt", "resources/ulysses.txt", "resources/ulysses_large.txt", "resources/ulysses_46mb.txt"};
    object_list = ds3_convert_file_list(books, num_files);

    request = populate_bulk_return_request(client, bucket_name, object_list);
    ds3_request_set_max_upload_size(request, 10485760);
    bulk_response = populate_bulk_return_response(client, request);
    populate_with_objects_from_bulk(client, bucket_name, bulk_response);
    ds3_master_object_list_response_free(bulk_response);

    request = ds3_init_get_bulk_job_spectra_s3_request(bucket_name, object_list);
    ds3_job_chunk_client_processing_order_guarantee chunk_order = DS3_JOB_CHUNK_CLIENT_PROCESSING_ORDER_GUARANTEE_NONE;
    ds3_request_set_chunk_client_processing_order_guarantee_ds3_job_chunk_client_processing_order_guarantee(request, chunk_order);

    error = ds3_get_bulk_job_spectra_s3_request(client, request, &bulk_response);
    ds3_request_free(request);
    ds3_bulk_object_list_response_free(object_list);
    handle_error(error);

    chunk_response = ensure_available_chunks(client, bulk_response->job_id);

    checksum_result* checksum_results = (checksum_result*) calloc(num_files, sizeof(checksum_result));
    checkChunkResponse(client, num_files, chunk_response, checksum_results);

    BOOST_CHECK(check_all_passed(num_files, checksum_results) == true);
    BOOST_CHECK(get_number_of_chunks(num_files, checksum_results, "resources/ulysses_large.txt") == 2);
    BOOST_CHECK(get_number_of_chunks(num_files, checksum_results, "resources/ulysses_46mb.txt") == 5);
    BOOST_CHECK(get_sum_of_chunks(num_files, checksum_results) == 11);

    free(checksum_results);

    // check to make sure that the 'job' has completed
    /* works sporadically in 1.2, shjould be fixed in 3.0
    ds3_bulk_response* completed_job = NULL;
    request = ds3_init_get_job(bulk_response->job_id->value);
    error = ds3_get_job(client, request, &completed_job);

    handle_error(error);

    BOOST_CHECK(completed_job != NULL);
    BOOST_CHECK(completed_job->status == COMPLETED);

    ds3_free_request(request);
    ds3_free_bulk_response(completed_job);*/

    ds3_master_object_list_response_free(chunk_response);
    ds3_master_object_list_response_free(bulk_response);

    clear_bucket(client, bucket_name);
    free_client(client);
}

BOOST_AUTO_TEST_CASE( chunk_preference ) {
    ds3_request* request = NULL;
    ds3_error* error = NULL;
    ds3_master_object_list_response* bulk_response = NULL;
    const uint32_t num_files = 5;
    ds3_bulk_object_list_response* object_list = default_object_list();
    ds3_master_object_list_response* chunk_response = NULL;
    bool retry_get;

    ds3_client* client = get_client();
    const char* bucket_name = "test_chunk_preference_bucket";

    printf("-----Testing Bulk GET with chunk_preference-------\n");

    populate_with_objects(client, bucket_name);

    request = ds3_init_get_bulk_job_spectra_s3_request(bucket_name, object_list);
    ds3_job_chunk_client_processing_order_guarantee chunk_order = DS3_JOB_CHUNK_CLIENT_PROCESSING_ORDER_GUARANTEE_NONE;
    ds3_request_set_chunk_client_processing_order_guarantee_ds3_job_chunk_client_processing_order_guarantee(request, chunk_order);

    error = ds3_get_bulk_job_spectra_s3_request(client, request, &bulk_response);
    ds3_request_free(request);
    ds3_bulk_object_list_response_free(object_list);
    handle_error(error);

    do {
        retry_get = false;
        request =  ds3_init_get_job_chunks_ready_for_client_processing_spectra_s3_request(bulk_response->job_id->value);
        error = ds3_get_job_chunks_ready_for_client_processing_spectra_s3_request(client, request, &chunk_response);
        ds3_request_free(request);
        handle_error(error);
        BOOST_CHECK(chunk_response != NULL);

        if (chunk_response->num_objects == 0) {
            // if this happens we need to try the request
            BOOST_TEST_MESSAGE( "Hit retry, sleeping for 30 seconds..."); //<< chunk_response->retry_after);
            retry_get = true;

            //TODO parse metadata retry_after
            //sleep(chunk_response->retry_after);
            g_usleep(30);
            ds3_master_object_list_response_free(chunk_response);
        }
    } while(retry_get);
    handle_error(error);

    checksum_result* checksum_results = (checksum_result*) calloc(num_files, sizeof(checksum_result));
    checkChunkResponse(client, num_files, chunk_response, checksum_results);

    BOOST_CHECK(check_all_passed(num_files, checksum_results) == true);
    BOOST_CHECK(get_sum_of_chunks(num_files, checksum_results) == num_files);

    free(checksum_results);

    // check to make sure that the 'job' has completed
    /* works sporadically in 1.2, shjould be fixed in 3.0
    ds3_bulk_response* completed_job = NULL;
    request = ds3_init_get_job(bulk_response->job_id->value);
    error = ds3_get_job(client, request, &completed_job);

    handle_error(error);

    BOOST_CHECK(completed_job != NULL);
    BOOST_CHECK(completed_job->status == COMPLETED);

    ds3_free_request(request);
    ds3_free_bulk_response(completed_job);*/

    ds3_master_object_list_response_free(chunk_response);
    ds3_master_object_list_response_free(bulk_response);

    clear_bucket(client, bucket_name);
    free_client(client);
}

BOOST_AUTO_TEST_CASE( partial_get ) {
    ds3_request* request = NULL;
    ds3_error* error = NULL;
    ds3_master_object_list_response* bulk_response = NULL;
    const uint32_t num_files = 5;
    ds3_bulk_object_list_response* object_list = default_object_list();
    ds3_master_object_list_response* chunk_response = NULL;

    ds3_client* client = get_client();
    const char* bucket_name = "unit_test_bucket";

    printf("-----Testing Bulk GET with partial range-------\n");

    populate_with_objects(client, bucket_name);

    request = ds3_init_get_bulk_job_spectra_s3_request(bucket_name, object_list);
    ds3_job_chunk_client_processing_order_guarantee chunk_order = DS3_JOB_CHUNK_CLIENT_PROCESSING_ORDER_GUARANTEE_NONE;
    ds3_request_set_chunk_client_processing_order_guarantee_ds3_job_chunk_client_processing_order_guarantee(request, chunk_order);

    error = ds3_get_bulk_job_spectra_s3_request(client, request, &bulk_response);
    ds3_request_free(request);
    ds3_bulk_object_list_response_free(object_list);
    handle_error(error);

    chunk_response = ensure_available_chunks(client, bulk_response->job_id);
    handle_error(error);

    checksum_result* checksum_results = (checksum_result*) calloc(num_files, sizeof(checksum_result));
    checkChunkResponsePartials(client, num_files, chunk_response, checksum_results, 3200);

    BOOST_CHECK(check_all_passed(num_files, checksum_results) == true);
    BOOST_CHECK(get_sum_of_chunks(num_files, checksum_results) == num_files);

    free(checksum_results);

    // check to make sure that the 'job' has completed
    /* works sporadically in 1.2, shjould be fixed in 3.0
    ds3_bulk_response* completed_job = NULL;
    request = ds3_init_get_job(bulk_response->job_id->value);
    error = ds3_get_job(client, request, &completed_job);

    handle_error(error);

    BOOST_CHECK(completed_job != NULL);
    BOOST_CHECK(completed_job->status == COMPLETED);

    ds3_free_request(request);
    ds3_free_bulk_response(completed_job);*/

    ds3_master_object_list_response_free(chunk_response);
    ds3_master_object_list_response_free(bulk_response);

    clear_bucket(client, bucket_name);
    free_client(client);
}

BOOST_AUTO_TEST_CASE( escape_urls ) {
    const char *delimiters[4] = {"or", "/", "@", "="};
    const char *strings_to_test[5] = {"some normal text", "/an/object/name", "bytes=0-255,300-400,550-800", "orqwerty/qwerty@qwerty=", "`1234567890-=~!@#$%^&*()_+[]\\{}|;:,./<>?"};
    const char *object_name_results[5] = {"some%20normal%20text", "/an/object/name", "bytes%3D0-255%2C300-400%2C550-800", "orqwerty/qwerty%40qwerty%3D",
                                        "%601234567890-%3D~%21%40%23%24%25%5E%26%2A%28%29_%2B%5B%5D%5C%7B%7D%7C%3B%3A%2C./%3C%3E%3F"};
    const char *range_header_results[5] = {"some%20normal%20text", "%2Fan%2Fobject%2Fname", "bytes=0-255,300-400,550-800", "orqwerty%2Fqwerty%40qwerty=",
                                         "%601234567890-=~%21%40%23%24%25%5E%26%2A%28%29_%2B%5B%5D%5C%7B%7D%7C%3B%3A,.%2F%3C%3E%3F"};
    const char *general_delimiter_results[5] = {"some%20normal%20text", "/an/object/name", "bytes=0-255%2C300-400%2C550-800", "orqwerty/qwerty@qwerty=",
                                              "%601234567890-=~%21@%23%24%25%5E%26%2A%28%29_%2B%5B%5D%5C%7B%7D%7C%3B%3A%2C./%3C%3E%3F"};

    printf("-----Testing escape url helpers-------\n");

    for (int i = 0; i < 5; i++) {
        char* escaped_url = escape_url_object_name(strings_to_test[i]);
	if(strcmp(escaped_url, object_name_results[i]) != 0){
	  printf("%s != %s\n", escaped_url, object_name_results[i]);
	}
        BOOST_CHECK(strcmp(escaped_url, object_name_results[i]) == 0);
        free(escaped_url);
    }
    for (int i = 0; i < 5; i++) {
        char* escaped_url = escape_url_range_header(strings_to_test[i]);
	if(strcmp(escaped_url, range_header_results[i]) != 0){
	  printf("%s != %s\n", escaped_url, range_header_results[i]);
	}
        BOOST_CHECK(strcmp(escaped_url, range_header_results[i]) == 0);
        free(escaped_url);
    }
    for (int i = 0; i < 5; i++) {
        char* escaped_url = escape_url_extended(strings_to_test[i], delimiters, 4);
	if(strcmp(escaped_url, general_delimiter_results[i]) != 0){
	  printf("%s != %s\n", escaped_url, general_delimiter_results[i]);
	}
        BOOST_CHECK(strcmp(escaped_url, general_delimiter_results[i]) == 0);
        free(escaped_url);
    }
}

BOOST_AUTO_TEST_CASE( convert_list_helper ) {
    const char* books[4] = {"beowulf.txt", "sherlock_holmes.txt", "tale_of_two_cities.txt", "ulysses.txt"};
    ds3_bulk_object_list_response* obj_list;

    printf("-----Testing convert_list helper-------\n");

    obj_list = ds3_convert_file_list_with_basepath(books, 4, "resources/");

    BOOST_CHECK(strcmp(obj_list->objects[0]->name->value, "beowulf.txt") == 0);
    BOOST_CHECK(obj_list->objects[0]->length == 294059);

    ds3_bulk_object_list_response_free(obj_list);
}

BOOST_AUTO_TEST_CASE( directory_size ) {
    const char* books[1] = {"resources"};
    ds3_bulk_object_list_response* obj_list;

    printf("-----Testing directory size on convert list helper-------\n");

    obj_list = ds3_convert_file_list_with_basepath(books, 1, NULL);

    BOOST_CHECK(strcmp(obj_list->objects[0]->name->value, "resources") == 0);
    BOOST_CHECK(obj_list->objects[0]->length == 0);

    ds3_bulk_object_list_response_free(obj_list);
}


text:
namespace :deploy do
  desc 'Deploy Tenon'
  task :tenon, roles: :db do
    run "cd #{current_release}; bundle exec rake tenon:deploy RAILS_ENV=production PASSWORD=#{ENV['PASSWORD']}"
  end
end